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" |
Vladimir Marko | 2a408a3 | 2015-09-18 14:11:00 +0100 | [diff] [blame] | 24 | #include "base/memory_tool.h" |
Mathieu Chartier | b666f48 | 2015-02-18 14:33:14 -0800 | [diff] [blame] | 25 | #include "debug_stack.h" |
| 26 | #include "macros.h" |
Mathieu Chartier | b666f48 | 2015-02-18 14:33:14 -0800 | [diff] [blame] | 27 | #include "mutex.h" |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 28 | |
| 29 | namespace art { |
| 30 | |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 31 | class Arena; |
| 32 | class ArenaPool; |
| 33 | class ArenaAllocator; |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 34 | class ArenaStack; |
| 35 | class ScopedArenaAllocator; |
Vladimir Marko | 3481ba2 | 2015-04-13 12:22:36 +0100 | [diff] [blame] | 36 | class MemMap; |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 37 | class MemStats; |
| 38 | |
Vladimir Marko | 8081d2b | 2014-07-31 15:33:43 +0100 | [diff] [blame] | 39 | template <typename T> |
| 40 | class ArenaAllocatorAdapter; |
| 41 | |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 42 | static constexpr bool kArenaAllocatorCountAllocations = false; |
| 43 | |
| 44 | // Type of allocation for memory tuning. |
| 45 | enum ArenaAllocKind { |
| 46 | kArenaAllocMisc, |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 47 | kArenaAllocSwitchTable, |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 48 | kArenaAllocSlowPaths, |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 49 | kArenaAllocGrowableBitMap, |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 50 | kArenaAllocSTL, |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 51 | kArenaAllocGraphBuilder, |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 52 | kArenaAllocGraph, |
| 53 | kArenaAllocBasicBlock, |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 54 | kArenaAllocBlockList, |
| 55 | kArenaAllocReversePostOrder, |
| 56 | kArenaAllocLinearOrder, |
| 57 | kArenaAllocConstantsMap, |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 58 | kArenaAllocPredecessors, |
| 59 | kArenaAllocSuccessors, |
| 60 | kArenaAllocDominated, |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 61 | kArenaAllocInstruction, |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 62 | kArenaAllocInvokeInputs, |
| 63 | kArenaAllocPhiInputs, |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 64 | kArenaAllocLoopInfo, |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 65 | kArenaAllocLoopInfoBackEdges, |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 66 | kArenaAllocTryCatchInfo, |
| 67 | kArenaAllocUseListNode, |
| 68 | kArenaAllocEnvironment, |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 69 | kArenaAllocEnvironmentVRegs, |
| 70 | kArenaAllocEnvironmentLocations, |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 71 | kArenaAllocLocationSummary, |
Vladimir Marko | 71bf809 | 2015-09-15 15:33:14 +0100 | [diff] [blame] | 72 | kArenaAllocSsaBuilder, |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 73 | kArenaAllocMoveOperands, |
| 74 | kArenaAllocCodeBuffer, |
| 75 | kArenaAllocStackMaps, |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 76 | kArenaAllocOptimization, |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 77 | kArenaAllocGvn, |
Vladimir Marko | 5233f93 | 2015-09-29 19:01:15 +0100 | [diff] [blame] | 78 | kArenaAllocInductionVarAnalysis, |
| 79 | kArenaAllocBoundsCheckElimination, |
Vladimir Marko | f6a35de | 2016-03-21 12:01:50 +0000 | [diff] [blame] | 80 | kArenaAllocDCE, |
| 81 | kArenaAllocLSE, |
| 82 | kArenaAllocLICM, |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 83 | kArenaAllocSsaLiveness, |
| 84 | kArenaAllocSsaPhiElimination, |
| 85 | kArenaAllocReferenceTypePropagation, |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 86 | kArenaAllocSideEffectsAnalysis, |
| 87 | kArenaAllocRegisterAllocator, |
Vladimir Marko | f6a35de | 2016-03-21 12:01:50 +0000 | [diff] [blame] | 88 | kArenaAllocRegisterAllocatorValidate, |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 89 | kArenaAllocStackMapStream, |
| 90 | kArenaAllocCodeGenerator, |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 91 | kArenaAllocAssembler, |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 92 | kArenaAllocParallelMoveResolver, |
Vladimir Marko | 655e585 | 2015-10-12 10:38:28 +0100 | [diff] [blame] | 93 | kArenaAllocGraphChecker, |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 94 | kArenaAllocVerifier, |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 95 | kArenaAllocCallingConvention, |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 96 | kNumArenaAllocKinds |
| 97 | }; |
| 98 | |
| 99 | template <bool kCount> |
| 100 | class ArenaAllocatorStatsImpl; |
| 101 | |
| 102 | template <> |
| 103 | class ArenaAllocatorStatsImpl<false> { |
| 104 | public: |
| 105 | ArenaAllocatorStatsImpl() = default; |
| 106 | ArenaAllocatorStatsImpl(const ArenaAllocatorStatsImpl& other) = default; |
| 107 | ArenaAllocatorStatsImpl& operator = (const ArenaAllocatorStatsImpl& other) = delete; |
| 108 | |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 109 | void Copy(const ArenaAllocatorStatsImpl& other ATTRIBUTE_UNUSED) {} |
| 110 | void RecordAlloc(size_t bytes ATTRIBUTE_UNUSED, ArenaAllocKind kind ATTRIBUTE_UNUSED) {} |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 111 | size_t NumAllocations() const { return 0u; } |
| 112 | size_t BytesAllocated() const { return 0u; } |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 113 | void Dump(std::ostream& os ATTRIBUTE_UNUSED, |
| 114 | const Arena* first ATTRIBUTE_UNUSED, |
| 115 | ssize_t lost_bytes_adjustment ATTRIBUTE_UNUSED) const {} |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | template <bool kCount> |
| 119 | class ArenaAllocatorStatsImpl { |
| 120 | public: |
| 121 | ArenaAllocatorStatsImpl(); |
| 122 | ArenaAllocatorStatsImpl(const ArenaAllocatorStatsImpl& other) = default; |
| 123 | ArenaAllocatorStatsImpl& operator = (const ArenaAllocatorStatsImpl& other) = delete; |
| 124 | |
| 125 | void Copy(const ArenaAllocatorStatsImpl& other); |
| 126 | void RecordAlloc(size_t bytes, ArenaAllocKind kind); |
| 127 | size_t NumAllocations() const; |
| 128 | size_t BytesAllocated() const; |
| 129 | void Dump(std::ostream& os, const Arena* first, ssize_t lost_bytes_adjustment) const; |
| 130 | |
| 131 | private: |
| 132 | size_t num_allocations_; |
| 133 | // TODO: Use std::array<size_t, kNumArenaAllocKinds> from C++11 when we upgrade the STL. |
| 134 | size_t alloc_stats_[kNumArenaAllocKinds]; // Bytes used by various allocation kinds. |
Vladimir Marko | bd9e9db | 2014-03-07 19:41:05 +0000 | [diff] [blame] | 135 | |
Vladimir Marko | 8dea81c | 2014-06-06 14:50:36 +0100 | [diff] [blame] | 136 | static const char* const kAllocNames[]; |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | typedef ArenaAllocatorStatsImpl<kArenaAllocatorCountAllocations> ArenaAllocatorStats; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 140 | |
Vladimir Marko | 2a408a3 | 2015-09-18 14:11:00 +0100 | [diff] [blame] | 141 | template <bool kAvailable, bool kValgrind> |
| 142 | class ArenaAllocatorMemoryToolCheckImpl { |
| 143 | // This is the generic template but since there is a partial specialization |
| 144 | // for kValgrind == false, this can be instantiated only for kValgrind == true. |
| 145 | static_assert(kValgrind, "This template can be instantiated only for Valgrind."); |
| 146 | static_assert(kAvailable, "Valgrind implies memory tool availability."); |
| 147 | |
| 148 | public: |
| 149 | ArenaAllocatorMemoryToolCheckImpl() : is_running_on_valgrind_(RUNNING_ON_MEMORY_TOOL) { } |
| 150 | bool IsRunningOnMemoryTool() { return is_running_on_valgrind_; } |
| 151 | |
| 152 | private: |
| 153 | const bool is_running_on_valgrind_; |
| 154 | }; |
| 155 | |
| 156 | template <bool kAvailable> |
| 157 | class ArenaAllocatorMemoryToolCheckImpl<kAvailable, false> { |
| 158 | public: |
| 159 | ArenaAllocatorMemoryToolCheckImpl() { } |
| 160 | bool IsRunningOnMemoryTool() { return kAvailable; } |
| 161 | }; |
| 162 | |
| 163 | typedef ArenaAllocatorMemoryToolCheckImpl<kMemoryToolIsAvailable, kMemoryToolIsValgrind> |
| 164 | ArenaAllocatorMemoryToolCheck; |
| 165 | |
| 166 | class ArenaAllocatorMemoryTool : private ArenaAllocatorMemoryToolCheck { |
| 167 | public: |
| 168 | using ArenaAllocatorMemoryToolCheck::IsRunningOnMemoryTool; |
| 169 | |
| 170 | void MakeDefined(void* ptr, size_t size) { |
Vladimir Marko | 7bda3b6 | 2015-10-07 12:44:31 +0000 | [diff] [blame] | 171 | if (UNLIKELY(IsRunningOnMemoryTool())) { |
| 172 | DoMakeDefined(ptr, size); |
Vladimir Marko | 2a408a3 | 2015-09-18 14:11:00 +0100 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | void MakeUndefined(void* ptr, size_t size) { |
Vladimir Marko | 7bda3b6 | 2015-10-07 12:44:31 +0000 | [diff] [blame] | 176 | if (UNLIKELY(IsRunningOnMemoryTool())) { |
| 177 | DoMakeUndefined(ptr, size); |
Vladimir Marko | 2a408a3 | 2015-09-18 14:11:00 +0100 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | void MakeInaccessible(void* ptr, size_t size) { |
Vladimir Marko | 7bda3b6 | 2015-10-07 12:44:31 +0000 | [diff] [blame] | 181 | if (UNLIKELY(IsRunningOnMemoryTool())) { |
| 182 | DoMakeInaccessible(ptr, size); |
Vladimir Marko | 2a408a3 | 2015-09-18 14:11:00 +0100 | [diff] [blame] | 183 | } |
| 184 | } |
Vladimir Marko | 7bda3b6 | 2015-10-07 12:44:31 +0000 | [diff] [blame] | 185 | |
| 186 | private: |
| 187 | void DoMakeDefined(void* ptr, size_t size); |
| 188 | void DoMakeUndefined(void* ptr, size_t size); |
| 189 | void DoMakeInaccessible(void* ptr, size_t size); |
Vladimir Marko | 2a408a3 | 2015-09-18 14:11:00 +0100 | [diff] [blame] | 190 | }; |
| 191 | |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 192 | class Arena { |
| 193 | public: |
| 194 | static constexpr size_t kDefaultSize = 128 * KB; |
Mathieu Chartier | c6201fa | 2015-03-12 10:06:33 -0700 | [diff] [blame] | 195 | Arena(); |
| 196 | virtual ~Arena() { } |
Mathieu Chartier | 9b34b24 | 2015-03-09 11:30:17 -0700 | [diff] [blame] | 197 | // Reset is for pre-use and uses memset for performance. |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 198 | void Reset(); |
Mathieu Chartier | 9b34b24 | 2015-03-09 11:30:17 -0700 | [diff] [blame] | 199 | // Release is used inbetween uses and uses madvise for memory usage. |
Mathieu Chartier | c6201fa | 2015-03-12 10:06:33 -0700 | [diff] [blame] | 200 | virtual void Release() { } |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 201 | uint8_t* Begin() { |
| 202 | return memory_; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 205 | uint8_t* End() { |
| 206 | return memory_ + size_; |
| 207 | } |
| 208 | |
| 209 | size_t Size() const { |
| 210 | return size_; |
| 211 | } |
| 212 | |
| 213 | size_t RemainingSpace() const { |
| 214 | return Size() - bytes_allocated_; |
| 215 | } |
| 216 | |
Mathieu Chartier | 49285c5 | 2014-12-02 15:43:48 -0800 | [diff] [blame] | 217 | size_t GetBytesAllocated() const { |
| 218 | return bytes_allocated_; |
| 219 | } |
| 220 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 221 | // Return true if ptr is contained in the arena. |
| 222 | bool Contains(const void* ptr) const { |
| 223 | return memory_ <= ptr && ptr < memory_ + bytes_allocated_; |
| 224 | } |
| 225 | |
Mathieu Chartier | c6201fa | 2015-03-12 10:06:33 -0700 | [diff] [blame] | 226 | protected: |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 227 | size_t bytes_allocated_; |
| 228 | uint8_t* memory_; |
| 229 | size_t size_; |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 230 | Arena* next_; |
| 231 | friend class ArenaPool; |
| 232 | friend class ArenaAllocator; |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 233 | friend class ArenaStack; |
| 234 | friend class ScopedArenaAllocator; |
| 235 | template <bool kCount> friend class ArenaAllocatorStatsImpl; |
Mathieu Chartier | c6201fa | 2015-03-12 10:06:33 -0700 | [diff] [blame] | 236 | |
Vladimir Marko | 3e0e717 | 2016-04-22 18:07:13 +0100 | [diff] [blame] | 237 | friend class ArenaAllocatorTest; |
| 238 | |
Mathieu Chartier | c6201fa | 2015-03-12 10:06:33 -0700 | [diff] [blame] | 239 | private: |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 240 | DISALLOW_COPY_AND_ASSIGN(Arena); |
| 241 | }; |
| 242 | |
Mathieu Chartier | c6201fa | 2015-03-12 10:06:33 -0700 | [diff] [blame] | 243 | class MallocArena FINAL : public Arena { |
| 244 | public: |
| 245 | explicit MallocArena(size_t size = Arena::kDefaultSize); |
| 246 | virtual ~MallocArena(); |
| 247 | }; |
| 248 | |
| 249 | class MemMapArena FINAL : public Arena { |
| 250 | public: |
Nicolas Geoffray | 25e0456 | 2016-03-01 13:17:58 +0000 | [diff] [blame] | 251 | MemMapArena(size_t size, bool low_4gb, const char* name); |
Vladimir Marko | 3481ba2 | 2015-04-13 12:22:36 +0100 | [diff] [blame] | 252 | virtual ~MemMapArena(); |
Mathieu Chartier | c6201fa | 2015-03-12 10:06:33 -0700 | [diff] [blame] | 253 | void Release() OVERRIDE; |
| 254 | |
| 255 | private: |
| 256 | std::unique_ptr<MemMap> map_; |
| 257 | }; |
| 258 | |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 259 | class ArenaPool { |
| 260 | public: |
Nicolas Geoffray | 25e0456 | 2016-03-01 13:17:58 +0000 | [diff] [blame] | 261 | ArenaPool(bool use_malloc = true, |
| 262 | bool low_4gb = false, |
| 263 | const char* name = "LinearAlloc"); |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 264 | ~ArenaPool(); |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 265 | Arena* AllocArena(size_t size) REQUIRES(!lock_); |
| 266 | void FreeArenaChain(Arena* first) REQUIRES(!lock_); |
| 267 | size_t GetBytesAllocated() const REQUIRES(!lock_); |
Jean-Philippe Halimi | ca76a1a | 2016-02-02 19:48:52 +0100 | [diff] [blame] | 268 | void ReclaimMemory() NO_THREAD_SAFETY_ANALYSIS; |
| 269 | void LockReclaimMemory() REQUIRES(!lock_); |
Mathieu Chartier | c6201fa | 2015-03-12 10:06:33 -0700 | [diff] [blame] | 270 | // Trim the maps in arenas by madvising, used by JIT to reduce memory usage. This only works |
| 271 | // use_malloc is false. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 272 | void TrimMaps() REQUIRES(!lock_); |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 273 | |
| 274 | private: |
Mathieu Chartier | c6201fa | 2015-03-12 10:06:33 -0700 | [diff] [blame] | 275 | const bool use_malloc_; |
Mathieu Chartier | 49285c5 | 2014-12-02 15:43:48 -0800 | [diff] [blame] | 276 | mutable Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 277 | Arena* free_arenas_ GUARDED_BY(lock_); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 278 | const bool low_4gb_; |
Nicolas Geoffray | 25e0456 | 2016-03-01 13:17:58 +0000 | [diff] [blame] | 279 | const char* name_; |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 280 | DISALLOW_COPY_AND_ASSIGN(ArenaPool); |
| 281 | }; |
| 282 | |
Vladimir Marko | fda0432 | 2015-11-11 18:45:50 +0000 | [diff] [blame] | 283 | // Fast single-threaded allocator for zero-initialized memory chunks. |
| 284 | // |
| 285 | // Memory is allocated from ArenaPool in large chunks and then rationed through |
| 286 | // the ArenaAllocator. It's returned to the ArenaPool only when the ArenaAllocator |
| 287 | // is destroyed. |
Vladimir Marko | 2a408a3 | 2015-09-18 14:11:00 +0100 | [diff] [blame] | 288 | class ArenaAllocator |
| 289 | : private DebugStackRefCounter, private ArenaAllocatorStats, private ArenaAllocatorMemoryTool { |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 290 | public: |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 291 | explicit ArenaAllocator(ArenaPool* pool); |
| 292 | ~ArenaAllocator(); |
| 293 | |
Vladimir Marko | 2a408a3 | 2015-09-18 14:11:00 +0100 | [diff] [blame] | 294 | using ArenaAllocatorMemoryTool::IsRunningOnMemoryTool; |
| 295 | using ArenaAllocatorMemoryTool::MakeDefined; |
| 296 | using ArenaAllocatorMemoryTool::MakeUndefined; |
| 297 | using ArenaAllocatorMemoryTool::MakeInaccessible; |
| 298 | |
Vladimir Marko | 8081d2b | 2014-07-31 15:33:43 +0100 | [diff] [blame] | 299 | // Get adapter for use in STL containers. See arena_containers.h . |
| 300 | ArenaAllocatorAdapter<void> Adapter(ArenaAllocKind kind = kArenaAllocSTL); |
| 301 | |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 302 | // Returns zeroed memory. |
Vladimir Marko | e4fcc5b | 2015-02-13 10:28:29 +0000 | [diff] [blame] | 303 | void* Alloc(size_t bytes, ArenaAllocKind kind = kArenaAllocMisc) ALWAYS_INLINE { |
Vladimir Marko | 2a408a3 | 2015-09-18 14:11:00 +0100 | [diff] [blame] | 304 | if (UNLIKELY(IsRunningOnMemoryTool())) { |
| 305 | return AllocWithMemoryTool(bytes, kind); |
Mathieu Chartier | 75165d0 | 2013-09-12 14:00:31 -0700 | [diff] [blame] | 306 | } |
Mathieu Chartier | b666f48 | 2015-02-18 14:33:14 -0800 | [diff] [blame] | 307 | bytes = RoundUp(bytes, kAlignment); |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 308 | ArenaAllocatorStats::RecordAlloc(bytes, kind); |
Vladimir Marko | 3e0e717 | 2016-04-22 18:07:13 +0100 | [diff] [blame] | 309 | if (UNLIKELY(bytes > static_cast<size_t>(end_ - ptr_))) { |
| 310 | return AllocFromNewArena(bytes); |
| 311 | } |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 312 | uint8_t* ret = ptr_; |
| 313 | ptr_ += bytes; |
| 314 | return ret; |
| 315 | } |
| 316 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 317 | // Realloc never frees the input pointer, it is the caller's job to do this if necessary. |
| 318 | void* Realloc(void* ptr, size_t ptr_size, size_t new_size, |
| 319 | ArenaAllocKind kind = kArenaAllocMisc) ALWAYS_INLINE { |
| 320 | DCHECK_GE(new_size, ptr_size); |
| 321 | DCHECK_EQ(ptr == nullptr, ptr_size == 0u); |
| 322 | auto* end = reinterpret_cast<uint8_t*>(ptr) + ptr_size; |
| 323 | // If we haven't allocated anything else, we can safely extend. |
| 324 | if (end == ptr_) { |
Vladimir Marko | 2a408a3 | 2015-09-18 14:11:00 +0100 | [diff] [blame] | 325 | DCHECK(!IsRunningOnMemoryTool()); // Red zone prevents end == ptr_. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 326 | const size_t size_delta = new_size - ptr_size; |
| 327 | // Check remain space. |
| 328 | const size_t remain = end_ - ptr_; |
| 329 | if (remain >= size_delta) { |
| 330 | ptr_ += size_delta; |
| 331 | ArenaAllocatorStats::RecordAlloc(size_delta, kind); |
| 332 | return ptr; |
| 333 | } |
| 334 | } |
| 335 | auto* new_ptr = Alloc(new_size, kind); |
| 336 | memcpy(new_ptr, ptr, ptr_size); |
| 337 | // TODO: Call free on ptr if linear alloc supports free. |
| 338 | return new_ptr; |
| 339 | } |
| 340 | |
Vladimir Marko | e4fcc5b | 2015-02-13 10:28:29 +0000 | [diff] [blame] | 341 | template <typename T> |
Vladimir Marko | f6a35de | 2016-03-21 12:01:50 +0000 | [diff] [blame] | 342 | T* Alloc(ArenaAllocKind kind = kArenaAllocMisc) { |
| 343 | return AllocArray<T>(1, kind); |
| 344 | } |
| 345 | |
| 346 | template <typename T> |
Vladimir Marko | e4fcc5b | 2015-02-13 10:28:29 +0000 | [diff] [blame] | 347 | T* AllocArray(size_t length, ArenaAllocKind kind = kArenaAllocMisc) { |
| 348 | return static_cast<T*>(Alloc(length * sizeof(T), kind)); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 349 | } |
| 350 | |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 351 | size_t BytesAllocated() const; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 352 | |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 353 | MemStats GetMemStats() const; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 354 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 355 | // The BytesUsed method sums up bytes allocated from arenas in arena_head_ and nodes. |
| 356 | // TODO: Change BytesAllocated to this behavior? |
| 357 | size_t BytesUsed() const; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 358 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 359 | ArenaPool* GetArenaPool() const { |
| 360 | return pool_; |
| 361 | } |
| 362 | |
| 363 | bool Contains(const void* ptr) const; |
| 364 | |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 365 | private: |
Vladimir Marko | 3e0e717 | 2016-04-22 18:07:13 +0100 | [diff] [blame] | 366 | void* AllocWithMemoryTool(size_t bytes, ArenaAllocKind kind); |
Vladimir Marko | 3f84f2c | 2016-04-25 19:40:34 +0100 | [diff] [blame^] | 367 | uint8_t* AllocFromNewArena(size_t bytes); |
Vladimir Marko | 3e0e717 | 2016-04-22 18:07:13 +0100 | [diff] [blame] | 368 | |
Mathieu Chartier | b666f48 | 2015-02-18 14:33:14 -0800 | [diff] [blame] | 369 | static constexpr size_t kAlignment = 8; |
| 370 | |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 371 | void UpdateBytesAllocated(); |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 372 | |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 373 | ArenaPool* pool_; |
| 374 | uint8_t* begin_; |
| 375 | uint8_t* end_; |
| 376 | uint8_t* ptr_; |
| 377 | Arena* arena_head_; |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 378 | |
Vladimir Marko | 8081d2b | 2014-07-31 15:33:43 +0100 | [diff] [blame] | 379 | template <typename U> |
| 380 | friend class ArenaAllocatorAdapter; |
| 381 | |
Vladimir Marko | 3e0e717 | 2016-04-22 18:07:13 +0100 | [diff] [blame] | 382 | friend class ArenaAllocatorTest; |
| 383 | |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 384 | DISALLOW_COPY_AND_ASSIGN(ArenaAllocator); |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 385 | }; // ArenaAllocator |
| 386 | |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 387 | class MemStats { |
| 388 | public: |
| 389 | MemStats(const char* name, const ArenaAllocatorStats* stats, const Arena* first_arena, |
| 390 | ssize_t lost_bytes_adjustment = 0); |
| 391 | void Dump(std::ostream& os) const; |
| 392 | |
| 393 | private: |
| 394 | const char* const name_; |
| 395 | const ArenaAllocatorStats* const stats_; |
| 396 | const Arena* const first_arena_; |
| 397 | const ssize_t lost_bytes_adjustment_; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 398 | }; // MemStats |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 399 | |
| 400 | } // namespace art |
| 401 | |
Mathieu Chartier | b666f48 | 2015-02-18 14:33:14 -0800 | [diff] [blame] | 402 | #endif // ART_RUNTIME_BASE_ARENA_ALLOCATOR_H_ |