buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Mathieu Chartier | b666f48 | 2015-02-18 14:33:14 -0800 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_BASE_ARENA_ALLOCATOR_H_ |
| 18 | #define ART_RUNTIME_BASE_ARENA_ALLOCATOR_H_ |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 19 | |
| 20 | #include <stdint.h> |
| 21 | #include <stddef.h> |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 22 | |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 23 | #include "base/bit_utils.h" |
Mathieu Chartier | b666f48 | 2015-02-18 14:33:14 -0800 | [diff] [blame] | 24 | #include "debug_stack.h" |
| 25 | #include "macros.h" |
Mathieu Chartier | b666f48 | 2015-02-18 14:33:14 -0800 | [diff] [blame] | 26 | #include "mutex.h" |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 27 | |
| 28 | namespace art { |
| 29 | |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 30 | class Arena; |
| 31 | class ArenaPool; |
| 32 | class ArenaAllocator; |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 33 | class ArenaStack; |
| 34 | class ScopedArenaAllocator; |
Vladimir Marko | 3481ba2 | 2015-04-13 12:22:36 +0100 | [diff] [blame] | 35 | class MemMap; |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 36 | class MemStats; |
| 37 | |
Vladimir Marko | 8081d2b | 2014-07-31 15:33:43 +0100 | [diff] [blame] | 38 | template <typename T> |
| 39 | class ArenaAllocatorAdapter; |
| 40 | |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 41 | static constexpr bool kArenaAllocatorCountAllocations = false; |
| 42 | |
| 43 | // Type of allocation for memory tuning. |
| 44 | enum ArenaAllocKind { |
| 45 | kArenaAllocMisc, |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 46 | kArenaAllocBBList, |
| 47 | kArenaAllocBBPredecessors, |
| 48 | kArenaAllocDfsPreOrder, |
| 49 | kArenaAllocDfsPostOrder, |
| 50 | kArenaAllocDomPostOrder, |
| 51 | kArenaAllocTopologicalSortOrder, |
| 52 | kArenaAllocLoweringInfo, |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 53 | kArenaAllocLIR, |
Vladimir Marko | 8dea81c | 2014-06-06 14:50:36 +0100 | [diff] [blame] | 54 | kArenaAllocLIRResourceMask, |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 55 | kArenaAllocSwitchTable, |
| 56 | kArenaAllocFillArrayData, |
| 57 | kArenaAllocSlowPaths, |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 58 | kArenaAllocMIR, |
| 59 | kArenaAllocDFInfo, |
| 60 | kArenaAllocGrowableArray, |
| 61 | kArenaAllocGrowableBitMap, |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 62 | kArenaAllocSSAToDalvikMap, |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 63 | kArenaAllocDalvikToSSAMap, |
| 64 | kArenaAllocDebugInfo, |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 65 | kArenaAllocRegAlloc, |
| 66 | kArenaAllocData, |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 67 | kArenaAllocSTL, |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 68 | kArenaAllocGraphBuilder, |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 69 | kArenaAllocGraph, |
| 70 | kArenaAllocBasicBlock, |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 71 | kArenaAllocBlockList, |
| 72 | kArenaAllocReversePostOrder, |
| 73 | kArenaAllocLinearOrder, |
| 74 | kArenaAllocConstantsMap, |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 75 | kArenaAllocPredecessors, |
| 76 | kArenaAllocSuccessors, |
| 77 | kArenaAllocDominated, |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 78 | kArenaAllocInstruction, |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 79 | kArenaAllocInvokeInputs, |
| 80 | kArenaAllocPhiInputs, |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 81 | kArenaAllocLoopInfo, |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 82 | kArenaAllocLoopInfoBackEdges, |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 83 | kArenaAllocTryCatchInfo, |
| 84 | kArenaAllocUseListNode, |
| 85 | kArenaAllocEnvironment, |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 86 | kArenaAllocEnvironmentVRegs, |
| 87 | kArenaAllocEnvironmentLocations, |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 88 | kArenaAllocLocationSummary, |
Vladimir Marko | 71bf809 | 2015-09-15 15:33:14 +0100 | [diff] [blame] | 89 | kArenaAllocSsaBuilder, |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 90 | kArenaAllocMoveOperands, |
| 91 | kArenaAllocCodeBuffer, |
| 92 | kArenaAllocStackMaps, |
| 93 | kArenaAllocBaselineMaps, |
| 94 | kArenaAllocOptimization, |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 95 | kArenaAllocGvn, |
Vladimir Marko | 5233f93 | 2015-09-29 19:01:15 +0100 | [diff] [blame^] | 96 | kArenaAllocInductionVarAnalysis, |
| 97 | kArenaAllocBoundsCheckElimination, |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 98 | kArenaAllocSsaLiveness, |
| 99 | kArenaAllocSsaPhiElimination, |
| 100 | kArenaAllocReferenceTypePropagation, |
| 101 | kArenaAllocPrimitiveTypePropagation, |
| 102 | kArenaAllocSideEffectsAnalysis, |
| 103 | kArenaAllocRegisterAllocator, |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 104 | kArenaAllocStackMapStream, |
| 105 | kArenaAllocCodeGenerator, |
| 106 | kArenaAllocParallelMoveResolver, |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 107 | kNumArenaAllocKinds |
| 108 | }; |
| 109 | |
| 110 | template <bool kCount> |
| 111 | class ArenaAllocatorStatsImpl; |
| 112 | |
| 113 | template <> |
| 114 | class ArenaAllocatorStatsImpl<false> { |
| 115 | public: |
| 116 | ArenaAllocatorStatsImpl() = default; |
| 117 | ArenaAllocatorStatsImpl(const ArenaAllocatorStatsImpl& other) = default; |
| 118 | ArenaAllocatorStatsImpl& operator = (const ArenaAllocatorStatsImpl& other) = delete; |
| 119 | |
| 120 | void Copy(const ArenaAllocatorStatsImpl& other) { UNUSED(other); } |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 121 | void RecordAlloc(size_t bytes, ArenaAllocKind kind) { UNUSED(bytes, kind); } |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 122 | size_t NumAllocations() const { return 0u; } |
| 123 | size_t BytesAllocated() const { return 0u; } |
| 124 | void Dump(std::ostream& os, const Arena* first, ssize_t lost_bytes_adjustment) const { |
| 125 | UNUSED(os); UNUSED(first); UNUSED(lost_bytes_adjustment); |
| 126 | } |
| 127 | }; |
| 128 | |
| 129 | template <bool kCount> |
| 130 | class ArenaAllocatorStatsImpl { |
| 131 | public: |
| 132 | ArenaAllocatorStatsImpl(); |
| 133 | ArenaAllocatorStatsImpl(const ArenaAllocatorStatsImpl& other) = default; |
| 134 | ArenaAllocatorStatsImpl& operator = (const ArenaAllocatorStatsImpl& other) = delete; |
| 135 | |
| 136 | void Copy(const ArenaAllocatorStatsImpl& other); |
| 137 | void RecordAlloc(size_t bytes, ArenaAllocKind kind); |
| 138 | size_t NumAllocations() const; |
| 139 | size_t BytesAllocated() const; |
| 140 | void Dump(std::ostream& os, const Arena* first, ssize_t lost_bytes_adjustment) const; |
| 141 | |
| 142 | private: |
| 143 | size_t num_allocations_; |
| 144 | // TODO: Use std::array<size_t, kNumArenaAllocKinds> from C++11 when we upgrade the STL. |
| 145 | size_t alloc_stats_[kNumArenaAllocKinds]; // Bytes used by various allocation kinds. |
Vladimir Marko | bd9e9db | 2014-03-07 19:41:05 +0000 | [diff] [blame] | 146 | |
Vladimir Marko | 8dea81c | 2014-06-06 14:50:36 +0100 | [diff] [blame] | 147 | static const char* const kAllocNames[]; |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | typedef ArenaAllocatorStatsImpl<kArenaAllocatorCountAllocations> ArenaAllocatorStats; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 151 | |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 152 | class Arena { |
| 153 | public: |
| 154 | static constexpr size_t kDefaultSize = 128 * KB; |
Mathieu Chartier | c6201fa | 2015-03-12 10:06:33 -0700 | [diff] [blame] | 155 | Arena(); |
| 156 | virtual ~Arena() { } |
Mathieu Chartier | 9b34b24 | 2015-03-09 11:30:17 -0700 | [diff] [blame] | 157 | // Reset is for pre-use and uses memset for performance. |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 158 | void Reset(); |
Mathieu Chartier | 9b34b24 | 2015-03-09 11:30:17 -0700 | [diff] [blame] | 159 | // Release is used inbetween uses and uses madvise for memory usage. |
Mathieu Chartier | c6201fa | 2015-03-12 10:06:33 -0700 | [diff] [blame] | 160 | virtual void Release() { } |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 161 | uint8_t* Begin() { |
| 162 | return memory_; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 165 | uint8_t* End() { |
| 166 | return memory_ + size_; |
| 167 | } |
| 168 | |
| 169 | size_t Size() const { |
| 170 | return size_; |
| 171 | } |
| 172 | |
| 173 | size_t RemainingSpace() const { |
| 174 | return Size() - bytes_allocated_; |
| 175 | } |
| 176 | |
Mathieu Chartier | 49285c5 | 2014-12-02 15:43:48 -0800 | [diff] [blame] | 177 | size_t GetBytesAllocated() const { |
| 178 | return bytes_allocated_; |
| 179 | } |
| 180 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 181 | // Return true if ptr is contained in the arena. |
| 182 | bool Contains(const void* ptr) const { |
| 183 | return memory_ <= ptr && ptr < memory_ + bytes_allocated_; |
| 184 | } |
| 185 | |
Mathieu Chartier | c6201fa | 2015-03-12 10:06:33 -0700 | [diff] [blame] | 186 | protected: |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 187 | size_t bytes_allocated_; |
| 188 | uint8_t* memory_; |
| 189 | size_t size_; |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 190 | Arena* next_; |
| 191 | friend class ArenaPool; |
| 192 | friend class ArenaAllocator; |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 193 | friend class ArenaStack; |
| 194 | friend class ScopedArenaAllocator; |
| 195 | template <bool kCount> friend class ArenaAllocatorStatsImpl; |
Mathieu Chartier | c6201fa | 2015-03-12 10:06:33 -0700 | [diff] [blame] | 196 | |
| 197 | private: |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 198 | DISALLOW_COPY_AND_ASSIGN(Arena); |
| 199 | }; |
| 200 | |
Mathieu Chartier | c6201fa | 2015-03-12 10:06:33 -0700 | [diff] [blame] | 201 | class MallocArena FINAL : public Arena { |
| 202 | public: |
| 203 | explicit MallocArena(size_t size = Arena::kDefaultSize); |
| 204 | virtual ~MallocArena(); |
| 205 | }; |
| 206 | |
| 207 | class MemMapArena FINAL : public Arena { |
| 208 | public: |
Roland Levillain | 3887c46 | 2015-08-12 18:15:42 +0100 | [diff] [blame] | 209 | MemMapArena(size_t size, bool low_4gb); |
Vladimir Marko | 3481ba2 | 2015-04-13 12:22:36 +0100 | [diff] [blame] | 210 | virtual ~MemMapArena(); |
Mathieu Chartier | c6201fa | 2015-03-12 10:06:33 -0700 | [diff] [blame] | 211 | void Release() OVERRIDE; |
| 212 | |
| 213 | private: |
| 214 | std::unique_ptr<MemMap> map_; |
| 215 | }; |
| 216 | |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 217 | class ArenaPool { |
| 218 | public: |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 219 | explicit ArenaPool(bool use_malloc = true, bool low_4gb = false); |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 220 | ~ArenaPool(); |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 221 | Arena* AllocArena(size_t size) REQUIRES(!lock_); |
| 222 | void FreeArenaChain(Arena* first) REQUIRES(!lock_); |
| 223 | size_t GetBytesAllocated() const REQUIRES(!lock_); |
Mathieu Chartier | c6201fa | 2015-03-12 10:06:33 -0700 | [diff] [blame] | 224 | // Trim the maps in arenas by madvising, used by JIT to reduce memory usage. This only works |
| 225 | // use_malloc is false. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 226 | void TrimMaps() REQUIRES(!lock_); |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 227 | |
| 228 | private: |
Mathieu Chartier | c6201fa | 2015-03-12 10:06:33 -0700 | [diff] [blame] | 229 | const bool use_malloc_; |
Mathieu Chartier | 49285c5 | 2014-12-02 15:43:48 -0800 | [diff] [blame] | 230 | mutable Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 231 | Arena* free_arenas_ GUARDED_BY(lock_); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 232 | const bool low_4gb_; |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 233 | DISALLOW_COPY_AND_ASSIGN(ArenaPool); |
| 234 | }; |
| 235 | |
Vladimir Marko | 8081d2b | 2014-07-31 15:33:43 +0100 | [diff] [blame] | 236 | class ArenaAllocator : private DebugStackRefCounter, private ArenaAllocatorStats { |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 237 | public: |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 238 | explicit ArenaAllocator(ArenaPool* pool); |
| 239 | ~ArenaAllocator(); |
| 240 | |
Vladimir Marko | 8081d2b | 2014-07-31 15:33:43 +0100 | [diff] [blame] | 241 | // Get adapter for use in STL containers. See arena_containers.h . |
| 242 | ArenaAllocatorAdapter<void> Adapter(ArenaAllocKind kind = kArenaAllocSTL); |
| 243 | |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 244 | // Returns zeroed memory. |
Vladimir Marko | e4fcc5b | 2015-02-13 10:28:29 +0000 | [diff] [blame] | 245 | void* Alloc(size_t bytes, ArenaAllocKind kind = kArenaAllocMisc) ALWAYS_INLINE { |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 246 | if (UNLIKELY(is_running_on_memory_tool_)) { |
Mathieu Chartier | 75165d0 | 2013-09-12 14:00:31 -0700 | [diff] [blame] | 247 | return AllocValgrind(bytes, kind); |
| 248 | } |
Mathieu Chartier | b666f48 | 2015-02-18 14:33:14 -0800 | [diff] [blame] | 249 | bytes = RoundUp(bytes, kAlignment); |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 250 | if (UNLIKELY(ptr_ + bytes > end_)) { |
| 251 | // Obtain a new block. |
| 252 | ObtainNewArenaForAllocation(bytes); |
| 253 | if (UNLIKELY(ptr_ == nullptr)) { |
| 254 | return nullptr; |
| 255 | } |
| 256 | } |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 257 | ArenaAllocatorStats::RecordAlloc(bytes, kind); |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 258 | uint8_t* ret = ptr_; |
| 259 | ptr_ += bytes; |
| 260 | return ret; |
| 261 | } |
| 262 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 263 | // Realloc never frees the input pointer, it is the caller's job to do this if necessary. |
| 264 | void* Realloc(void* ptr, size_t ptr_size, size_t new_size, |
| 265 | ArenaAllocKind kind = kArenaAllocMisc) ALWAYS_INLINE { |
| 266 | DCHECK_GE(new_size, ptr_size); |
| 267 | DCHECK_EQ(ptr == nullptr, ptr_size == 0u); |
| 268 | auto* end = reinterpret_cast<uint8_t*>(ptr) + ptr_size; |
| 269 | // If we haven't allocated anything else, we can safely extend. |
| 270 | if (end == ptr_) { |
| 271 | const size_t size_delta = new_size - ptr_size; |
| 272 | // Check remain space. |
| 273 | const size_t remain = end_ - ptr_; |
| 274 | if (remain >= size_delta) { |
| 275 | ptr_ += size_delta; |
| 276 | ArenaAllocatorStats::RecordAlloc(size_delta, kind); |
| 277 | return ptr; |
| 278 | } |
| 279 | } |
| 280 | auto* new_ptr = Alloc(new_size, kind); |
| 281 | memcpy(new_ptr, ptr, ptr_size); |
| 282 | // TODO: Call free on ptr if linear alloc supports free. |
| 283 | return new_ptr; |
| 284 | } |
| 285 | |
Vladimir Marko | e4fcc5b | 2015-02-13 10:28:29 +0000 | [diff] [blame] | 286 | template <typename T> |
| 287 | T* AllocArray(size_t length, ArenaAllocKind kind = kArenaAllocMisc) { |
| 288 | return static_cast<T*>(Alloc(length * sizeof(T), kind)); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 289 | } |
| 290 | |
Mathieu Chartier | 75165d0 | 2013-09-12 14:00:31 -0700 | [diff] [blame] | 291 | void* AllocValgrind(size_t bytes, ArenaAllocKind kind); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 292 | |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 293 | void ObtainNewArenaForAllocation(size_t allocation_size); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 294 | |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 295 | size_t BytesAllocated() const; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 296 | |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 297 | MemStats GetMemStats() const; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 298 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 299 | // The BytesUsed method sums up bytes allocated from arenas in arena_head_ and nodes. |
| 300 | // TODO: Change BytesAllocated to this behavior? |
| 301 | size_t BytesUsed() const; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 302 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 303 | ArenaPool* GetArenaPool() const { |
| 304 | return pool_; |
| 305 | } |
| 306 | |
| 307 | bool Contains(const void* ptr) const; |
| 308 | |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 309 | private: |
Mathieu Chartier | b666f48 | 2015-02-18 14:33:14 -0800 | [diff] [blame] | 310 | static constexpr size_t kAlignment = 8; |
| 311 | |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 312 | void UpdateBytesAllocated(); |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 313 | |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 314 | ArenaPool* pool_; |
| 315 | uint8_t* begin_; |
| 316 | uint8_t* end_; |
| 317 | uint8_t* ptr_; |
| 318 | Arena* arena_head_; |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 319 | bool is_running_on_memory_tool_; |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 320 | |
Vladimir Marko | 8081d2b | 2014-07-31 15:33:43 +0100 | [diff] [blame] | 321 | template <typename U> |
| 322 | friend class ArenaAllocatorAdapter; |
| 323 | |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 324 | DISALLOW_COPY_AND_ASSIGN(ArenaAllocator); |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 325 | }; // ArenaAllocator |
| 326 | |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 327 | class MemStats { |
| 328 | public: |
| 329 | MemStats(const char* name, const ArenaAllocatorStats* stats, const Arena* first_arena, |
| 330 | ssize_t lost_bytes_adjustment = 0); |
| 331 | void Dump(std::ostream& os) const; |
| 332 | |
| 333 | private: |
| 334 | const char* const name_; |
| 335 | const ArenaAllocatorStats* const stats_; |
| 336 | const Arena* const first_arena_; |
| 337 | const ssize_t lost_bytes_adjustment_; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 338 | }; // MemStats |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 339 | |
| 340 | } // namespace art |
| 341 | |
Mathieu Chartier | b666f48 | 2015-02-18 14:33:14 -0800 | [diff] [blame] | 342 | #endif // ART_RUNTIME_BASE_ARENA_ALLOCATOR_H_ |