| 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 | |
| Andreas Gampe | d490129 | 2017-05-30 18:41:34 -0700 | [diff] [blame] | 17 | #include "arena_allocator-inl.h" |
| 18 | |
| Andreas Gampe | d490129 | 2017-05-30 18:41:34 -0700 | [diff] [blame] | 19 | |
| Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 20 | #include <algorithm> |
| Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 21 | #include <cstddef> |
| Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 22 | #include <iomanip> |
| Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 23 | #include <numeric> |
| 24 | |
| Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 25 | #include <android-base/logging.h> |
| 26 | |
| David Sehr | 10db8fe | 2018-07-18 11:01:20 -0700 | [diff] [blame] | 27 | #include "mman.h" |
| 28 | |
| buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 29 | namespace art { |
| 30 | |
| Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 31 | constexpr size_t kMemoryToolRedZoneBytes = 8; |
| Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 32 | |
| Vladimir Marko | bd9e9db | 2014-03-07 19:41:05 +0000 | [diff] [blame] | 33 | template <bool kCount> |
| Vladimir Marko | 8dea81c | 2014-06-06 14:50:36 +0100 | [diff] [blame] | 34 | const char* const ArenaAllocatorStatsImpl<kCount>::kAllocNames[] = { |
| Igor Murashkin | d01745e | 2017-04-05 16:40:31 -0700 | [diff] [blame] | 35 | // Every name should have the same width and end with a space. Abbreviate if necessary: |
| Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 36 | "Misc ", |
| Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 37 | "SwitchTbl ", |
| Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 38 | "SlowPaths ", |
| Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 39 | "GrowBitMap ", |
| Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 40 | "STL ", |
| Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 41 | "GraphBuilder ", |
| Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 42 | "Graph ", |
| 43 | "BasicBlock ", |
| Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 44 | "BlockList ", |
| 45 | "RevPostOrder ", |
| 46 | "LinearOrder ", |
| 47 | "ConstantsMap ", |
| Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 48 | "Predecessors ", |
| 49 | "Successors ", |
| 50 | "Dominated ", |
| Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 51 | "Instruction ", |
| Igor Murashkin | d01745e | 2017-04-05 16:40:31 -0700 | [diff] [blame] | 52 | "CtorFenceIns ", |
| Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 53 | "InvokeInputs ", |
| 54 | "PhiInputs ", |
| Vladimir Marko | 175e786 | 2018-03-27 09:03:13 +0000 | [diff] [blame] | 55 | "TypeCheckIns ", |
| Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 56 | "LoopInfo ", |
| Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 57 | "LIBackEdges ", |
| Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 58 | "TryCatchInf ", |
| 59 | "UseListNode ", |
| 60 | "Environment ", |
| Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 61 | "EnvVRegs ", |
| 62 | "EnvLocations ", |
| Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 63 | "LocSummary ", |
| Vladimir Marko | 71bf809 | 2015-09-15 15:33:14 +0100 | [diff] [blame] | 64 | "SsaBuilder ", |
| Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 65 | "MoveOperands ", |
| 66 | "CodeBuffer ", |
| 67 | "StackMaps ", |
| Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 68 | "Optimization ", |
| Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 69 | "GVN ", |
| Vladimir Marko | 5233f93 | 2015-09-29 19:01:15 +0100 | [diff] [blame] | 70 | "InductionVar ", |
| 71 | "BCE ", |
| Vladimir Marko | f6a35de | 2016-03-21 12:01:50 +0000 | [diff] [blame] | 72 | "DCE ", |
| Vladimir Marko | 009d166 | 2017-10-10 13:21:15 +0100 | [diff] [blame] | 73 | "LSA ", |
| Vladimir Marko | f6a35de | 2016-03-21 12:01:50 +0000 | [diff] [blame] | 74 | "LSE ", |
| Igor Murashkin | dd018df | 2017-08-09 10:38:31 -0700 | [diff] [blame] | 75 | "CFRE ", |
| Vladimir Marko | f6a35de | 2016-03-21 12:01:50 +0000 | [diff] [blame] | 76 | "LICM ", |
| Aart Bik | 9620230 | 2016-10-04 17:33:56 -0700 | [diff] [blame] | 77 | "LoopOpt ", |
| Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 78 | "SsaLiveness ", |
| 79 | "SsaPhiElim ", |
| 80 | "RefTypeProp ", |
| Aart Bik | 6d05700 | 2018-04-09 15:39:58 -0700 | [diff] [blame] | 81 | "SelectGen ", |
| Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 82 | "SideEffects ", |
| 83 | "RegAllocator ", |
| Vladimir Marko | f6a35de | 2016-03-21 12:01:50 +0000 | [diff] [blame] | 84 | "RegAllocVldt ", |
| Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 85 | "StackMapStm ", |
| David Srbecky | dd966bc | 2018-05-24 13:55:52 +0100 | [diff] [blame] | 86 | "BitTableBld ", |
| Vladimir Marko | d38ba0a | 2017-04-13 13:43:43 +0100 | [diff] [blame] | 87 | "VectorNode ", |
| Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 88 | "CodeGen ", |
| Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 89 | "Assembler ", |
| Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 90 | "ParallelMove ", |
| Vladimir Marko | 655e585 | 2015-10-12 10:38:28 +0100 | [diff] [blame] | 91 | "GraphChecker ", |
| Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 92 | "Verifier ", |
| Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 93 | "CallingConv ", |
| Vladimir Marko | 4e335d0 | 2016-12-19 16:04:33 +0000 | [diff] [blame] | 94 | "CHA ", |
| Alexandre Rames | 22aa54b | 2016-10-18 09:32:29 +0100 | [diff] [blame] | 95 | "Scheduler ", |
| Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 96 | "Profile ", |
| Artem Serov | 7f4aff6 | 2017-06-21 17:02:18 +0100 | [diff] [blame] | 97 | "SBCloner ", |
| buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 98 | }; |
| 99 | |
| Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 100 | template <bool kCount> |
| 101 | ArenaAllocatorStatsImpl<kCount>::ArenaAllocatorStatsImpl() |
| Vladimir Marko | 4e335d0 | 2016-12-19 16:04:33 +0000 | [diff] [blame] | 102 | : num_allocations_(0u), |
| 103 | alloc_stats_(kNumArenaAllocKinds, 0u) { |
| Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | template <bool kCount> |
| 107 | void ArenaAllocatorStatsImpl<kCount>::Copy(const ArenaAllocatorStatsImpl& other) { |
| 108 | num_allocations_ = other.num_allocations_; |
| Vladimir Marko | 4e335d0 | 2016-12-19 16:04:33 +0000 | [diff] [blame] | 109 | std::copy_n(other.alloc_stats_.begin(), kNumArenaAllocKinds, alloc_stats_.begin()); |
| Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | template <bool kCount> |
| 113 | void ArenaAllocatorStatsImpl<kCount>::RecordAlloc(size_t bytes, ArenaAllocKind kind) { |
| 114 | alloc_stats_[kind] += bytes; |
| 115 | ++num_allocations_; |
| 116 | } |
| 117 | |
| 118 | template <bool kCount> |
| 119 | size_t ArenaAllocatorStatsImpl<kCount>::NumAllocations() const { |
| 120 | return num_allocations_; |
| 121 | } |
| 122 | |
| 123 | template <bool kCount> |
| 124 | size_t ArenaAllocatorStatsImpl<kCount>::BytesAllocated() const { |
| 125 | const size_t init = 0u; // Initial value of the correct type. |
| Vladimir Marko | 4e335d0 | 2016-12-19 16:04:33 +0000 | [diff] [blame] | 126 | return std::accumulate(alloc_stats_.begin(), alloc_stats_.end(), init); |
| Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | template <bool kCount> |
| 130 | void ArenaAllocatorStatsImpl<kCount>::Dump(std::ostream& os, const Arena* first, |
| 131 | ssize_t lost_bytes_adjustment) const { |
| 132 | size_t malloc_bytes = 0u; |
| 133 | size_t lost_bytes = 0u; |
| 134 | size_t num_arenas = 0u; |
| 135 | for (const Arena* arena = first; arena != nullptr; arena = arena->next_) { |
| 136 | malloc_bytes += arena->Size(); |
| 137 | lost_bytes += arena->RemainingSpace(); |
| 138 | ++num_arenas; |
| 139 | } |
| 140 | // The lost_bytes_adjustment is used to make up for the fact that the current arena |
| 141 | // may not have the bytes_allocated_ updated correctly. |
| 142 | lost_bytes += lost_bytes_adjustment; |
| 143 | const size_t bytes_allocated = BytesAllocated(); |
| 144 | os << " MEM: used: " << bytes_allocated << ", allocated: " << malloc_bytes |
| 145 | << ", lost: " << lost_bytes << "\n"; |
| Vladimir Marko | bd9e9db | 2014-03-07 19:41:05 +0000 | [diff] [blame] | 146 | size_t num_allocations = NumAllocations(); |
| Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 147 | if (num_allocations != 0) { |
| 148 | os << "Number of arenas allocated: " << num_arenas << ", Number of allocations: " |
| 149 | << num_allocations << ", avg size: " << bytes_allocated / num_allocations << "\n"; |
| 150 | } |
| 151 | os << "===== Allocation by kind\n"; |
| Andreas Gampe | 785d2f2 | 2014-11-03 22:57:30 -0800 | [diff] [blame] | 152 | static_assert(arraysize(kAllocNames) == kNumArenaAllocKinds, "arraysize of kAllocNames"); |
| Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 153 | for (int i = 0; i < kNumArenaAllocKinds; i++) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 154 | // Reduce output by listing only allocation kinds that actually have allocations. |
| 155 | if (alloc_stats_[i] != 0u) { |
| Vladimir Marko | bd9e9db | 2014-03-07 19:41:05 +0000 | [diff] [blame] | 156 | os << kAllocNames[i] << std::setw(10) << alloc_stats_[i] << "\n"; |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 157 | } |
| Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
| Stephen Hines | 47b2247 | 2017-01-26 00:50:31 -0800 | [diff] [blame] | 161 | #pragma GCC diagnostic push |
| Jayant Chowdhary | 5f9da55 | 2017-02-02 12:48:10 -0800 | [diff] [blame] | 162 | #if __clang_major__ >= 4 |
| Stephen Hines | 47b2247 | 2017-01-26 00:50:31 -0800 | [diff] [blame] | 163 | #pragma GCC diagnostic ignored "-Winstantiation-after-specialization" |
| Jayant Chowdhary | 5f9da55 | 2017-02-02 12:48:10 -0800 | [diff] [blame] | 164 | #endif |
| Vladimir Marko | d38ba0a | 2017-04-13 13:43:43 +0100 | [diff] [blame] | 165 | // We're going to use ArenaAllocatorStatsImpl<kArenaAllocatorCountAllocations> which needs |
| 166 | // to be explicitly instantiated if kArenaAllocatorCountAllocations is true. Explicit |
| 167 | // instantiation of the specialization ArenaAllocatorStatsImpl<false> does not do anything |
| 168 | // but requires the warning "-Winstantiation-after-specialization" to be turned off. |
| 169 | // |
| 170 | // To avoid bit-rot of the ArenaAllocatorStatsImpl<true>, instantiate it also in debug builds |
| 171 | // (but keep the unnecessary code out of release builds) as we do not usually compile with |
| 172 | // kArenaAllocatorCountAllocations set to true. |
| 173 | template class ArenaAllocatorStatsImpl<kArenaAllocatorCountAllocations || kIsDebugBuild>; |
| Stephen Hines | 47b2247 | 2017-01-26 00:50:31 -0800 | [diff] [blame] | 174 | #pragma GCC diagnostic pop |
| Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 175 | |
| Vladimir Marko | 7bda3b6 | 2015-10-07 12:44:31 +0000 | [diff] [blame] | 176 | void ArenaAllocatorMemoryTool::DoMakeDefined(void* ptr, size_t size) { |
| 177 | MEMORY_TOOL_MAKE_DEFINED(ptr, size); |
| 178 | } |
| 179 | |
| 180 | void ArenaAllocatorMemoryTool::DoMakeUndefined(void* ptr, size_t size) { |
| 181 | MEMORY_TOOL_MAKE_UNDEFINED(ptr, size); |
| 182 | } |
| 183 | |
| 184 | void ArenaAllocatorMemoryTool::DoMakeInaccessible(void* ptr, size_t size) { |
| 185 | MEMORY_TOOL_MAKE_NOACCESS(ptr, size); |
| 186 | } |
| 187 | |
| Andreas Gampe | d9911ee | 2017-03-27 13:27:24 -0700 | [diff] [blame] | 188 | Arena::Arena() : bytes_allocated_(0), memory_(nullptr), size_(0), next_(nullptr) { |
| Ian Rogers | e7a5b7d | 2013-04-18 20:09:02 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 191 | size_t ArenaAllocator::BytesAllocated() const { |
| Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 192 | return ArenaAllocatorStats::BytesAllocated(); |
| Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 195 | size_t ArenaAllocator::BytesUsed() const { |
| 196 | size_t total = ptr_ - begin_; |
| 197 | if (arena_head_ != nullptr) { |
| 198 | for (Arena* cur_arena = arena_head_->next_; cur_arena != nullptr; |
| 199 | cur_arena = cur_arena->next_) { |
| 200 | total += cur_arena->GetBytesAllocated(); |
| 201 | } |
| 202 | } |
| 203 | return total; |
| 204 | } |
| 205 | |
| Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 206 | ArenaAllocator::ArenaAllocator(ArenaPool* pool) |
| 207 | : pool_(pool), |
| 208 | begin_(nullptr), |
| 209 | end_(nullptr), |
| 210 | ptr_(nullptr), |
| Vladimir Marko | 2a408a3 | 2015-09-18 14:11:00 +0100 | [diff] [blame] | 211 | arena_head_(nullptr) { |
| Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | void ArenaAllocator::UpdateBytesAllocated() { |
| 215 | if (arena_head_ != nullptr) { |
| 216 | // Update how many bytes we have allocated into the arena so that the arena pool knows how |
| 217 | // much memory to zero out. |
| 218 | arena_head_->bytes_allocated_ = ptr_ - begin_; |
| 219 | } |
| 220 | } |
| 221 | |
| Vladimir Marko | 2a408a3 | 2015-09-18 14:11:00 +0100 | [diff] [blame] | 222 | void* ArenaAllocator::AllocWithMemoryTool(size_t bytes, ArenaAllocKind kind) { |
| Vladimir Marko | 7500193 | 2015-11-10 20:54:22 +0000 | [diff] [blame] | 223 | // We mark all memory for a newly retrieved arena as inaccessible and then |
| 224 | // mark only the actually allocated memory as defined. That leaves red zones |
| 225 | // and padding between allocations marked as inaccessible. |
| Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 226 | size_t rounded_bytes = RoundUp(bytes + kMemoryToolRedZoneBytes, 8); |
| Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 227 | ArenaAllocatorStats::RecordAlloc(rounded_bytes, kind); |
| Vladimir Marko | 3f84f2c | 2016-04-25 19:40:34 +0100 | [diff] [blame] | 228 | uint8_t* ret; |
| Vladimir Marko | 3e0e717 | 2016-04-22 18:07:13 +0100 | [diff] [blame] | 229 | if (UNLIKELY(rounded_bytes > static_cast<size_t>(end_ - ptr_))) { |
| Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 230 | ret = AllocFromNewArenaWithMemoryTool(rounded_bytes); |
| Vladimir Marko | 3f84f2c | 2016-04-25 19:40:34 +0100 | [diff] [blame] | 231 | } else { |
| 232 | ret = ptr_; |
| 233 | ptr_ += rounded_bytes; |
| Vladimir Marko | 3e0e717 | 2016-04-22 18:07:13 +0100 | [diff] [blame] | 234 | } |
| Vladimir Marko | 2a408a3 | 2015-09-18 14:11:00 +0100 | [diff] [blame] | 235 | MEMORY_TOOL_MAKE_DEFINED(ret, bytes); |
| Vladimir Marko | 7500193 | 2015-11-10 20:54:22 +0000 | [diff] [blame] | 236 | // Check that the memory is already zeroed out. |
| 237 | DCHECK(std::all_of(ret, ret + bytes, [](uint8_t val) { return val == 0u; })); |
| Mathieu Chartier | 75165d0 | 2013-09-12 14:00:31 -0700 | [diff] [blame] | 238 | return ret; |
| 239 | } |
| 240 | |
| Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 241 | void* ArenaAllocator::AllocWithMemoryToolAlign16(size_t bytes, ArenaAllocKind kind) { |
| 242 | // We mark all memory for a newly retrieved arena as inaccessible and then |
| 243 | // mark only the actually allocated memory as defined. That leaves red zones |
| 244 | // and padding between allocations marked as inaccessible. |
| 245 | size_t rounded_bytes = bytes + kMemoryToolRedZoneBytes; |
| 246 | DCHECK_ALIGNED(rounded_bytes, 8); // `bytes` is 16-byte aligned, red zone is 8-byte aligned. |
| 247 | uintptr_t padding = |
| 248 | ((reinterpret_cast<uintptr_t>(ptr_) + 15u) & 15u) - reinterpret_cast<uintptr_t>(ptr_); |
| 249 | ArenaAllocatorStats::RecordAlloc(rounded_bytes, kind); |
| 250 | uint8_t* ret; |
| 251 | if (UNLIKELY(padding + rounded_bytes > static_cast<size_t>(end_ - ptr_))) { |
| 252 | static_assert(kArenaAlignment >= 16, "Expecting sufficient alignment for new Arena."); |
| 253 | ret = AllocFromNewArenaWithMemoryTool(rounded_bytes); |
| 254 | } else { |
| 255 | ptr_ += padding; // Leave padding inaccessible. |
| 256 | ret = ptr_; |
| 257 | ptr_ += rounded_bytes; |
| 258 | } |
| 259 | MEMORY_TOOL_MAKE_DEFINED(ret, bytes); |
| 260 | // Check that the memory is already zeroed out. |
| 261 | DCHECK(std::all_of(ret, ret + bytes, [](uint8_t val) { return val == 0u; })); |
| 262 | return ret; |
| 263 | } |
| 264 | |
| Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 265 | ArenaAllocator::~ArenaAllocator() { |
| 266 | // Reclaim all the arenas by giving them back to the thread pool. |
| 267 | UpdateBytesAllocated(); |
| Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 268 | pool_->FreeArenaChain(arena_head_); |
| Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| Vladimir Marko | 3f84f2c | 2016-04-25 19:40:34 +0100 | [diff] [blame] | 271 | uint8_t* ArenaAllocator::AllocFromNewArena(size_t bytes) { |
| Andreas Gampe | 121f148 | 2017-05-12 10:28:35 -0700 | [diff] [blame] | 272 | Arena* new_arena = pool_->AllocArena(std::max(arena_allocator::kArenaDefaultSize, bytes)); |
| Vladimir Marko | 3e0e717 | 2016-04-22 18:07:13 +0100 | [diff] [blame] | 273 | DCHECK(new_arena != nullptr); |
| 274 | DCHECK_LE(bytes, new_arena->Size()); |
| 275 | if (static_cast<size_t>(end_ - ptr_) > new_arena->Size() - bytes) { |
| 276 | // The old arena has more space remaining than the new one, so keep using it. |
| 277 | // This can happen when the requested size is over half of the default size. |
| 278 | DCHECK(arena_head_ != nullptr); |
| 279 | new_arena->bytes_allocated_ = bytes; // UpdateBytesAllocated() on the new_arena. |
| 280 | new_arena->next_ = arena_head_->next_; |
| 281 | arena_head_->next_ = new_arena; |
| 282 | } else { |
| 283 | UpdateBytesAllocated(); |
| 284 | new_arena->next_ = arena_head_; |
| 285 | arena_head_ = new_arena; |
| 286 | // Update our internal data structures. |
| 287 | begin_ = new_arena->Begin(); |
| Andreas Gampe | f6dd829 | 2016-08-19 20:22:19 -0700 | [diff] [blame] | 288 | DCHECK_ALIGNED(begin_, kAlignment); |
| Vladimir Marko | 3e0e717 | 2016-04-22 18:07:13 +0100 | [diff] [blame] | 289 | ptr_ = begin_ + bytes; |
| 290 | end_ = new_arena->End(); |
| 291 | } |
| 292 | return new_arena->Begin(); |
| Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 295 | uint8_t* ArenaAllocator::AllocFromNewArenaWithMemoryTool(size_t bytes) { |
| 296 | uint8_t* ret = AllocFromNewArena(bytes); |
| 297 | uint8_t* noaccess_begin = ret + bytes; |
| 298 | uint8_t* noaccess_end; |
| 299 | if (ret == arena_head_->Begin()) { |
| 300 | DCHECK(ptr_ - bytes == ret); |
| 301 | noaccess_end = end_; |
| 302 | } else { |
| 303 | // We're still using the old arena but `ret` comes from a new one just after it. |
| 304 | DCHECK(arena_head_->next_ != nullptr); |
| 305 | DCHECK(ret == arena_head_->next_->Begin()); |
| 306 | DCHECK_EQ(bytes, arena_head_->next_->GetBytesAllocated()); |
| 307 | noaccess_end = arena_head_->next_->End(); |
| 308 | } |
| 309 | MEMORY_TOOL_MAKE_NOACCESS(noaccess_begin, noaccess_end - noaccess_begin); |
| 310 | return ret; |
| 311 | } |
| 312 | |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 313 | bool ArenaAllocator::Contains(const void* ptr) const { |
| 314 | if (ptr >= begin_ && ptr < end_) { |
| 315 | return true; |
| 316 | } |
| 317 | for (const Arena* cur_arena = arena_head_; cur_arena != nullptr; cur_arena = cur_arena->next_) { |
| 318 | if (cur_arena->Contains(ptr)) { |
| 319 | return true; |
| 320 | } |
| 321 | } |
| 322 | return false; |
| 323 | } |
| 324 | |
| Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 325 | MemStats::MemStats(const char* name, |
| 326 | const ArenaAllocatorStats* stats, |
| 327 | const Arena* first_arena, |
| Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 328 | ssize_t lost_bytes_adjustment) |
| 329 | : name_(name), |
| 330 | stats_(stats), |
| 331 | first_arena_(first_arena), |
| 332 | lost_bytes_adjustment_(lost_bytes_adjustment) { |
| 333 | } |
| 334 | |
| 335 | void MemStats::Dump(std::ostream& os) const { |
| 336 | os << name_ << " stats:\n"; |
| 337 | stats_->Dump(os, first_arena_, lost_bytes_adjustment_); |
| 338 | } |
| 339 | |
| Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 340 | // Dump memory usage stats. |
| Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 341 | MemStats ArenaAllocator::GetMemStats() const { |
| 342 | ssize_t lost_bytes_adjustment = |
| 343 | (arena_head_ == nullptr) ? 0 : (end_ - ptr_) - arena_head_->RemainingSpace(); |
| 344 | return MemStats("ArenaAllocator", this, arena_head_, lost_bytes_adjustment); |
| buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | } // namespace art |