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