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 | |
David Sehr | 1ce2b3b | 2018-04-05 11:02:03 -0700 | [diff] [blame] | 17 | #ifndef ART_LIBARTBASE_BASE_ARENA_ALLOCATOR_H_ |
| 18 | #define ART_LIBARTBASE_BASE_ARENA_ALLOCATOR_H_ |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 19 | |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 20 | #include <stddef.h> |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 21 | #include <stdint.h> |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 22 | |
David Sehr | 1979c64 | 2018-04-26 14:41:18 -0700 | [diff] [blame] | 23 | #include "bit_utils.h" |
| 24 | #include "debug_stack.h" |
| 25 | #include "dchecked_vector.h" |
| 26 | #include "macros.h" |
| 27 | #include "memory_tool.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; |
| 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 | kArenaAllocSwitchTable, |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 47 | kArenaAllocSlowPaths, |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 48 | kArenaAllocGrowableBitMap, |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 49 | kArenaAllocSTL, |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 50 | kArenaAllocGraphBuilder, |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 51 | kArenaAllocGraph, |
| 52 | kArenaAllocBasicBlock, |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 53 | kArenaAllocBlockList, |
| 54 | kArenaAllocReversePostOrder, |
| 55 | kArenaAllocLinearOrder, |
| 56 | kArenaAllocConstantsMap, |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 57 | kArenaAllocPredecessors, |
| 58 | kArenaAllocSuccessors, |
| 59 | kArenaAllocDominated, |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 60 | kArenaAllocInstruction, |
Igor Murashkin | d01745e | 2017-04-05 16:40:31 -0700 | [diff] [blame] | 61 | kArenaAllocConstructorFenceInputs, |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 62 | kArenaAllocInvokeInputs, |
| 63 | kArenaAllocPhiInputs, |
Vladimir Marko | 175e786 | 2018-03-27 09:03:13 +0000 | [diff] [blame] | 64 | kArenaAllocTypeCheckInputs, |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 65 | kArenaAllocLoopInfo, |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 66 | kArenaAllocLoopInfoBackEdges, |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 67 | kArenaAllocTryCatchInfo, |
| 68 | kArenaAllocUseListNode, |
| 69 | kArenaAllocEnvironment, |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 70 | kArenaAllocEnvironmentVRegs, |
| 71 | kArenaAllocEnvironmentLocations, |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 72 | kArenaAllocLocationSummary, |
Vladimir Marko | 71bf809 | 2015-09-15 15:33:14 +0100 | [diff] [blame] | 73 | kArenaAllocSsaBuilder, |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 74 | kArenaAllocMoveOperands, |
| 75 | kArenaAllocCodeBuffer, |
| 76 | kArenaAllocStackMaps, |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 77 | kArenaAllocOptimization, |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 78 | kArenaAllocGvn, |
Vladimir Marko | 5233f93 | 2015-09-29 19:01:15 +0100 | [diff] [blame] | 79 | kArenaAllocInductionVarAnalysis, |
| 80 | kArenaAllocBoundsCheckElimination, |
Vladimir Marko | f6a35de | 2016-03-21 12:01:50 +0000 | [diff] [blame] | 81 | kArenaAllocDCE, |
Vladimir Marko | 009d166 | 2017-10-10 13:21:15 +0100 | [diff] [blame] | 82 | kArenaAllocLSA, |
Vladimir Marko | f6a35de | 2016-03-21 12:01:50 +0000 | [diff] [blame] | 83 | kArenaAllocLSE, |
Igor Murashkin | dd018df | 2017-08-09 10:38:31 -0700 | [diff] [blame] | 84 | kArenaAllocCFRE, |
Vladimir Marko | f6a35de | 2016-03-21 12:01:50 +0000 | [diff] [blame] | 85 | kArenaAllocLICM, |
Aart Bik | 9620230 | 2016-10-04 17:33:56 -0700 | [diff] [blame] | 86 | kArenaAllocLoopOptimization, |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 87 | kArenaAllocSsaLiveness, |
| 88 | kArenaAllocSsaPhiElimination, |
| 89 | kArenaAllocReferenceTypePropagation, |
Aart Bik | 6d05700 | 2018-04-09 15:39:58 -0700 | [diff] [blame] | 90 | kArenaAllocSelectGenerator, |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 91 | kArenaAllocSideEffectsAnalysis, |
| 92 | kArenaAllocRegisterAllocator, |
Vladimir Marko | f6a35de | 2016-03-21 12:01:50 +0000 | [diff] [blame] | 93 | kArenaAllocRegisterAllocatorValidate, |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 94 | kArenaAllocStackMapStream, |
David Srbecky | dd966bc | 2018-05-24 13:55:52 +0100 | [diff] [blame] | 95 | kArenaAllocBitTableBuilder, |
Aart Bik | 92685a8 | 2017-03-06 11:13:43 -0800 | [diff] [blame] | 96 | kArenaAllocVectorNode, |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 97 | kArenaAllocCodeGenerator, |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 98 | kArenaAllocAssembler, |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 99 | kArenaAllocParallelMoveResolver, |
Vladimir Marko | 655e585 | 2015-10-12 10:38:28 +0100 | [diff] [blame] | 100 | kArenaAllocGraphChecker, |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 101 | kArenaAllocVerifier, |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 102 | kArenaAllocCallingConvention, |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 103 | kArenaAllocCHA, |
Alexandre Rames | 22aa54b | 2016-10-18 09:32:29 +0100 | [diff] [blame] | 104 | kArenaAllocScheduler, |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 105 | kArenaAllocProfile, |
Artem Serov | 7f4aff6 | 2017-06-21 17:02:18 +0100 | [diff] [blame] | 106 | kArenaAllocSuperblockCloner, |
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 | |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 120 | void Copy(const ArenaAllocatorStatsImpl& other ATTRIBUTE_UNUSED) {} |
| 121 | void RecordAlloc(size_t bytes ATTRIBUTE_UNUSED, ArenaAllocKind kind ATTRIBUTE_UNUSED) {} |
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; } |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 124 | void Dump(std::ostream& os ATTRIBUTE_UNUSED, |
| 125 | const Arena* first ATTRIBUTE_UNUSED, |
| 126 | ssize_t lost_bytes_adjustment ATTRIBUTE_UNUSED) const {} |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 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_; |
Vladimir Marko | 4e335d0 | 2016-12-19 16:04:33 +0000 | [diff] [blame] | 144 | dchecked_vector<size_t> alloc_stats_; // Bytes used by various allocation kinds. |
Vladimir Marko | bd9e9db | 2014-03-07 19:41:05 +0000 | [diff] [blame] | 145 | |
Vladimir Marko | 8dea81c | 2014-06-06 14:50:36 +0100 | [diff] [blame] | 146 | static const char* const kAllocNames[]; |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | typedef ArenaAllocatorStatsImpl<kArenaAllocatorCountAllocations> ArenaAllocatorStats; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 150 | |
Roland Levillain | 05e34f4 | 2018-05-24 13:19:05 +0000 | [diff] [blame] | 151 | class ArenaAllocatorMemoryTool { |
Vladimir Marko | 2a408a3 | 2015-09-18 14:11:00 +0100 | [diff] [blame] | 152 | public: |
Roland Levillain | 05e34f4 | 2018-05-24 13:19:05 +0000 | [diff] [blame] | 153 | bool IsRunningOnMemoryTool() { return kMemoryToolIsAvailable; } |
Vladimir Marko | 2a408a3 | 2015-09-18 14:11:00 +0100 | [diff] [blame] | 154 | |
| 155 | void MakeDefined(void* ptr, size_t size) { |
Vladimir Marko | 7bda3b6 | 2015-10-07 12:44:31 +0000 | [diff] [blame] | 156 | if (UNLIKELY(IsRunningOnMemoryTool())) { |
| 157 | DoMakeDefined(ptr, size); |
Vladimir Marko | 2a408a3 | 2015-09-18 14:11:00 +0100 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | void MakeUndefined(void* ptr, size_t size) { |
Vladimir Marko | 7bda3b6 | 2015-10-07 12:44:31 +0000 | [diff] [blame] | 161 | if (UNLIKELY(IsRunningOnMemoryTool())) { |
| 162 | DoMakeUndefined(ptr, size); |
Vladimir Marko | 2a408a3 | 2015-09-18 14:11:00 +0100 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | void MakeInaccessible(void* ptr, size_t size) { |
Vladimir Marko | 7bda3b6 | 2015-10-07 12:44:31 +0000 | [diff] [blame] | 166 | if (UNLIKELY(IsRunningOnMemoryTool())) { |
| 167 | DoMakeInaccessible(ptr, size); |
Vladimir Marko | 2a408a3 | 2015-09-18 14:11:00 +0100 | [diff] [blame] | 168 | } |
| 169 | } |
Vladimir Marko | 7bda3b6 | 2015-10-07 12:44:31 +0000 | [diff] [blame] | 170 | |
| 171 | private: |
| 172 | void DoMakeDefined(void* ptr, size_t size); |
| 173 | void DoMakeUndefined(void* ptr, size_t size); |
| 174 | void DoMakeInaccessible(void* ptr, size_t size); |
Vladimir Marko | 2a408a3 | 2015-09-18 14:11:00 +0100 | [diff] [blame] | 175 | }; |
| 176 | |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 177 | class Arena { |
| 178 | public: |
Mathieu Chartier | c6201fa | 2015-03-12 10:06:33 -0700 | [diff] [blame] | 179 | Arena(); |
| 180 | virtual ~Arena() { } |
Mathieu Chartier | 9b34b24 | 2015-03-09 11:30:17 -0700 | [diff] [blame] | 181 | // Reset is for pre-use and uses memset for performance. |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 182 | void Reset(); |
Mathieu Chartier | 9b34b24 | 2015-03-09 11:30:17 -0700 | [diff] [blame] | 183 | // Release is used inbetween uses and uses madvise for memory usage. |
Mathieu Chartier | c6201fa | 2015-03-12 10:06:33 -0700 | [diff] [blame] | 184 | virtual void Release() { } |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 185 | uint8_t* Begin() { |
| 186 | return memory_; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 189 | uint8_t* End() { |
| 190 | return memory_ + size_; |
| 191 | } |
| 192 | |
| 193 | size_t Size() const { |
| 194 | return size_; |
| 195 | } |
| 196 | |
| 197 | size_t RemainingSpace() const { |
| 198 | return Size() - bytes_allocated_; |
| 199 | } |
| 200 | |
Mathieu Chartier | 49285c5 | 2014-12-02 15:43:48 -0800 | [diff] [blame] | 201 | size_t GetBytesAllocated() const { |
| 202 | return bytes_allocated_; |
| 203 | } |
| 204 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 205 | // Return true if ptr is contained in the arena. |
| 206 | bool Contains(const void* ptr) const { |
| 207 | return memory_ <= ptr && ptr < memory_ + bytes_allocated_; |
| 208 | } |
| 209 | |
Mathieu Chartier | c6201fa | 2015-03-12 10:06:33 -0700 | [diff] [blame] | 210 | protected: |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 211 | size_t bytes_allocated_; |
| 212 | uint8_t* memory_; |
| 213 | size_t size_; |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 214 | Arena* next_; |
David Sehr | 3215fff | 2018-04-03 17:10:12 -0700 | [diff] [blame] | 215 | friend class MallocArenaPool; |
| 216 | friend class MemMapArenaPool; |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 217 | friend class ArenaAllocator; |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 218 | friend class ArenaStack; |
| 219 | friend class ScopedArenaAllocator; |
| 220 | template <bool kCount> friend class ArenaAllocatorStatsImpl; |
Mathieu Chartier | c6201fa | 2015-03-12 10:06:33 -0700 | [diff] [blame] | 221 | |
Vladimir Marko | 3e0e717 | 2016-04-22 18:07:13 +0100 | [diff] [blame] | 222 | friend class ArenaAllocatorTest; |
| 223 | |
Mathieu Chartier | c6201fa | 2015-03-12 10:06:33 -0700 | [diff] [blame] | 224 | private: |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 225 | DISALLOW_COPY_AND_ASSIGN(Arena); |
| 226 | }; |
| 227 | |
| 228 | class ArenaPool { |
| 229 | public: |
David Sehr | 3215fff | 2018-04-03 17:10:12 -0700 | [diff] [blame] | 230 | virtual ~ArenaPool() = default; |
| 231 | |
| 232 | virtual Arena* AllocArena(size_t size) = 0; |
| 233 | virtual void FreeArenaChain(Arena* first) = 0; |
| 234 | virtual size_t GetBytesAllocated() const = 0; |
| 235 | virtual void ReclaimMemory() = 0; |
| 236 | virtual void LockReclaimMemory() = 0; |
| 237 | // Trim the maps in arenas by madvising, used by JIT to reduce memory usage. |
| 238 | virtual void TrimMaps() = 0; |
| 239 | |
| 240 | protected: |
| 241 | ArenaPool() = default; |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 242 | |
| 243 | private: |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 244 | DISALLOW_COPY_AND_ASSIGN(ArenaPool); |
| 245 | }; |
| 246 | |
Vladimir Marko | fda0432 | 2015-11-11 18:45:50 +0000 | [diff] [blame] | 247 | // Fast single-threaded allocator for zero-initialized memory chunks. |
| 248 | // |
| 249 | // Memory is allocated from ArenaPool in large chunks and then rationed through |
| 250 | // the ArenaAllocator. It's returned to the ArenaPool only when the ArenaAllocator |
| 251 | // is destroyed. |
Vladimir Marko | 2a408a3 | 2015-09-18 14:11:00 +0100 | [diff] [blame] | 252 | class ArenaAllocator |
| 253 | : private DebugStackRefCounter, private ArenaAllocatorStats, private ArenaAllocatorMemoryTool { |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 254 | public: |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 255 | explicit ArenaAllocator(ArenaPool* pool); |
| 256 | ~ArenaAllocator(); |
| 257 | |
Vladimir Marko | 2a408a3 | 2015-09-18 14:11:00 +0100 | [diff] [blame] | 258 | using ArenaAllocatorMemoryTool::IsRunningOnMemoryTool; |
| 259 | using ArenaAllocatorMemoryTool::MakeDefined; |
| 260 | using ArenaAllocatorMemoryTool::MakeUndefined; |
| 261 | using ArenaAllocatorMemoryTool::MakeInaccessible; |
| 262 | |
Vladimir Marko | 8081d2b | 2014-07-31 15:33:43 +0100 | [diff] [blame] | 263 | // Get adapter for use in STL containers. See arena_containers.h . |
| 264 | ArenaAllocatorAdapter<void> Adapter(ArenaAllocKind kind = kArenaAllocSTL); |
| 265 | |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 266 | // Returns zeroed memory. |
Vladimir Marko | e4fcc5b | 2015-02-13 10:28:29 +0000 | [diff] [blame] | 267 | void* Alloc(size_t bytes, ArenaAllocKind kind = kArenaAllocMisc) ALWAYS_INLINE { |
Vladimir Marko | 2a408a3 | 2015-09-18 14:11:00 +0100 | [diff] [blame] | 268 | if (UNLIKELY(IsRunningOnMemoryTool())) { |
| 269 | return AllocWithMemoryTool(bytes, kind); |
Mathieu Chartier | 75165d0 | 2013-09-12 14:00:31 -0700 | [diff] [blame] | 270 | } |
Mathieu Chartier | b666f48 | 2015-02-18 14:33:14 -0800 | [diff] [blame] | 271 | bytes = RoundUp(bytes, kAlignment); |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 272 | ArenaAllocatorStats::RecordAlloc(bytes, kind); |
Vladimir Marko | 3e0e717 | 2016-04-22 18:07:13 +0100 | [diff] [blame] | 273 | if (UNLIKELY(bytes > static_cast<size_t>(end_ - ptr_))) { |
| 274 | return AllocFromNewArena(bytes); |
| 275 | } |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 276 | uint8_t* ret = ptr_; |
Andreas Gampe | f6dd829 | 2016-08-19 20:22:19 -0700 | [diff] [blame] | 277 | DCHECK_ALIGNED(ret, kAlignment); |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 278 | ptr_ += bytes; |
| 279 | return ret; |
| 280 | } |
| 281 | |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 282 | // Returns zeroed memory. |
| 283 | void* AllocAlign16(size_t bytes, ArenaAllocKind kind = kArenaAllocMisc) ALWAYS_INLINE { |
| 284 | // It is an error to request 16-byte aligned allocation of unaligned size. |
| 285 | DCHECK_ALIGNED(bytes, 16); |
| 286 | if (UNLIKELY(IsRunningOnMemoryTool())) { |
| 287 | return AllocWithMemoryToolAlign16(bytes, kind); |
| 288 | } |
| 289 | uintptr_t padding = |
| 290 | ((reinterpret_cast<uintptr_t>(ptr_) + 15u) & 15u) - reinterpret_cast<uintptr_t>(ptr_); |
| 291 | ArenaAllocatorStats::RecordAlloc(bytes, kind); |
| 292 | if (UNLIKELY(padding + bytes > static_cast<size_t>(end_ - ptr_))) { |
| 293 | static_assert(kArenaAlignment >= 16, "Expecting sufficient alignment for new Arena."); |
| 294 | return AllocFromNewArena(bytes); |
| 295 | } |
| 296 | ptr_ += padding; |
| 297 | uint8_t* ret = ptr_; |
| 298 | DCHECK_ALIGNED(ret, 16); |
| 299 | ptr_ += bytes; |
| 300 | return ret; |
| 301 | } |
| 302 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 303 | // Realloc never frees the input pointer, it is the caller's job to do this if necessary. |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 304 | void* Realloc(void* ptr, |
| 305 | size_t ptr_size, |
| 306 | size_t new_size, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 307 | ArenaAllocKind kind = kArenaAllocMisc) ALWAYS_INLINE { |
| 308 | DCHECK_GE(new_size, ptr_size); |
| 309 | DCHECK_EQ(ptr == nullptr, ptr_size == 0u); |
Andreas Gampe | f6dd829 | 2016-08-19 20:22:19 -0700 | [diff] [blame] | 310 | // We always allocate aligned. |
| 311 | const size_t aligned_ptr_size = RoundUp(ptr_size, kAlignment); |
| 312 | auto* end = reinterpret_cast<uint8_t*>(ptr) + aligned_ptr_size; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 313 | // If we haven't allocated anything else, we can safely extend. |
| 314 | if (end == ptr_) { |
Andreas Gampe | 421dbc2 | 2017-07-11 08:32:29 -0700 | [diff] [blame] | 315 | // Red zone prevents end == ptr_ (unless input = allocator state = null). |
| 316 | DCHECK(!IsRunningOnMemoryTool() || ptr_ == nullptr); |
Andreas Gampe | f6dd829 | 2016-08-19 20:22:19 -0700 | [diff] [blame] | 317 | const size_t aligned_new_size = RoundUp(new_size, kAlignment); |
| 318 | const size_t size_delta = aligned_new_size - aligned_ptr_size; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 319 | // Check remain space. |
| 320 | const size_t remain = end_ - ptr_; |
| 321 | if (remain >= size_delta) { |
| 322 | ptr_ += size_delta; |
| 323 | ArenaAllocatorStats::RecordAlloc(size_delta, kind); |
Andreas Gampe | f6dd829 | 2016-08-19 20:22:19 -0700 | [diff] [blame] | 324 | DCHECK_ALIGNED(ptr_, kAlignment); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 325 | return ptr; |
| 326 | } |
| 327 | } |
Andreas Gampe | f6dd829 | 2016-08-19 20:22:19 -0700 | [diff] [blame] | 328 | 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] | 329 | memcpy(new_ptr, ptr, ptr_size); |
| 330 | // TODO: Call free on ptr if linear alloc supports free. |
| 331 | return new_ptr; |
| 332 | } |
| 333 | |
Vladimir Marko | e4fcc5b | 2015-02-13 10:28:29 +0000 | [diff] [blame] | 334 | template <typename T> |
Vladimir Marko | f6a35de | 2016-03-21 12:01:50 +0000 | [diff] [blame] | 335 | T* Alloc(ArenaAllocKind kind = kArenaAllocMisc) { |
| 336 | return AllocArray<T>(1, kind); |
| 337 | } |
| 338 | |
| 339 | template <typename T> |
Vladimir Marko | e4fcc5b | 2015-02-13 10:28:29 +0000 | [diff] [blame] | 340 | T* AllocArray(size_t length, ArenaAllocKind kind = kArenaAllocMisc) { |
| 341 | return static_cast<T*>(Alloc(length * sizeof(T), kind)); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 342 | } |
| 343 | |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 344 | size_t BytesAllocated() const; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 345 | |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 346 | MemStats GetMemStats() const; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 347 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 348 | // The BytesUsed method sums up bytes allocated from arenas in arena_head_ and nodes. |
| 349 | // TODO: Change BytesAllocated to this behavior? |
| 350 | size_t BytesUsed() const; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 351 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 352 | ArenaPool* GetArenaPool() const { |
| 353 | return pool_; |
| 354 | } |
| 355 | |
| 356 | bool Contains(const void* ptr) const; |
| 357 | |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 358 | // The alignment guaranteed for individual allocations. |
| 359 | static constexpr size_t kAlignment = 8u; |
| 360 | |
| 361 | // The alignment required for the whole Arena rather than individual allocations. |
| 362 | static constexpr size_t kArenaAlignment = 16u; |
Andreas Gampe | f6dd829 | 2016-08-19 20:22:19 -0700 | [diff] [blame] | 363 | |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 364 | private: |
Vladimir Marko | 3e0e717 | 2016-04-22 18:07:13 +0100 | [diff] [blame] | 365 | void* AllocWithMemoryTool(size_t bytes, ArenaAllocKind kind); |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 366 | void* AllocWithMemoryToolAlign16(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 | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 368 | uint8_t* AllocFromNewArenaWithMemoryTool(size_t bytes); |
Mathieu Chartier | b666f48 | 2015-02-18 14:33:14 -0800 | [diff] [blame] | 369 | |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 370 | void UpdateBytesAllocated(); |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 371 | |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 372 | ArenaPool* pool_; |
| 373 | uint8_t* begin_; |
| 374 | uint8_t* end_; |
| 375 | uint8_t* ptr_; |
| 376 | Arena* arena_head_; |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 377 | |
Vladimir Marko | 8081d2b | 2014-07-31 15:33:43 +0100 | [diff] [blame] | 378 | template <typename U> |
| 379 | friend class ArenaAllocatorAdapter; |
| 380 | |
Vladimir Marko | 3e0e717 | 2016-04-22 18:07:13 +0100 | [diff] [blame] | 381 | friend class ArenaAllocatorTest; |
| 382 | |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 383 | DISALLOW_COPY_AND_ASSIGN(ArenaAllocator); |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 384 | }; // ArenaAllocator |
| 385 | |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 386 | class MemStats { |
| 387 | public: |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 388 | MemStats(const char* name, |
| 389 | const ArenaAllocatorStats* stats, |
| 390 | const Arena* first_arena, |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 391 | ssize_t lost_bytes_adjustment = 0); |
| 392 | void Dump(std::ostream& os) const; |
| 393 | |
| 394 | private: |
| 395 | const char* const name_; |
| 396 | const ArenaAllocatorStats* const stats_; |
| 397 | const Arena* const first_arena_; |
| 398 | const ssize_t lost_bytes_adjustment_; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 399 | }; // MemStats |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 400 | |
| 401 | } // namespace art |
| 402 | |
David Sehr | 1ce2b3b | 2018-04-05 11:02:03 -0700 | [diff] [blame] | 403 | #endif // ART_LIBARTBASE_BASE_ARENA_ALLOCATOR_H_ |