blob: 54b40f28cf16e61fac0fabab0bf9f3e18aefa238 [file] [log] [blame]
buzbee862a7602013-04-05 10:58:54 -07001/*
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 Gamped4901292017-05-30 18:41:34 -070017#include "arena_allocator-inl.h"
18
19#include <sys/mman.h>
20
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000021#include <algorithm>
Vladimir Markof44d36c2017-03-14 14:18:46 +000022#include <cstddef>
Ian Rogers6f3dbba2014-10-14 17:41:57 -070023#include <iomanip>
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000024#include <numeric>
25
Mathieu Chartierb666f482015-02-18 14:33:14 -080026#include "logging.h"
Vladimir Marko3481ba22015-04-13 12:22:36 +010027#include "mem_map.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080028#include "mutex.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070029#include "thread-current-inl.h"
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -080030#include "systrace.h"
buzbee862a7602013-04-05 10:58:54 -070031
32namespace art {
33
Vladimir Markof44d36c2017-03-14 14:18:46 +000034constexpr size_t kMemoryToolRedZoneBytes = 8;
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -070035
Vladimir Markobd9e9db2014-03-07 19:41:05 +000036template <bool kCount>
Vladimir Marko8dea81c2014-06-06 14:50:36 +010037const char* const ArenaAllocatorStatsImpl<kCount>::kAllocNames[] = {
Igor Murashkind01745e2017-04-05 16:40:31 -070038 // Every name should have the same width and end with a space. Abbreviate if necessary:
Vladimir Markof9f64412015-09-02 14:05:49 +010039 "Misc ",
Vladimir Markof9f64412015-09-02 14:05:49 +010040 "SwitchTbl ",
Vladimir Markof9f64412015-09-02 14:05:49 +010041 "SlowPaths ",
Vladimir Markof9f64412015-09-02 14:05:49 +010042 "GrowBitMap ",
Vladimir Markof9f64412015-09-02 14:05:49 +010043 "STL ",
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010044 "GraphBuilder ",
Vladimir Markof9f64412015-09-02 14:05:49 +010045 "Graph ",
46 "BasicBlock ",
Vladimir Markofa6b93c2015-09-15 10:15:55 +010047 "BlockList ",
48 "RevPostOrder ",
49 "LinearOrder ",
50 "ConstantsMap ",
Vladimir Marko60584552015-09-03 13:35:12 +000051 "Predecessors ",
52 "Successors ",
53 "Dominated ",
Vladimir Markof9f64412015-09-02 14:05:49 +010054 "Instruction ",
Igor Murashkind01745e2017-04-05 16:40:31 -070055 "CtorFenceIns ",
Vladimir Markofa6b93c2015-09-15 10:15:55 +010056 "InvokeInputs ",
57 "PhiInputs ",
Vladimir Markof9f64412015-09-02 14:05:49 +010058 "LoopInfo ",
Vladimir Markofa6b93c2015-09-15 10:15:55 +010059 "LIBackEdges ",
Vladimir Markof9f64412015-09-02 14:05:49 +010060 "TryCatchInf ",
61 "UseListNode ",
62 "Environment ",
Vladimir Markofa6b93c2015-09-15 10:15:55 +010063 "EnvVRegs ",
64 "EnvLocations ",
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010065 "LocSummary ",
Vladimir Marko71bf8092015-09-15 15:33:14 +010066 "SsaBuilder ",
Vladimir Markof9f64412015-09-02 14:05:49 +010067 "MoveOperands ",
68 "CodeBuffer ",
69 "StackMaps ",
Vladimir Markof9f64412015-09-02 14:05:49 +010070 "Optimization ",
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010071 "GVN ",
Vladimir Marko5233f932015-09-29 19:01:15 +010072 "InductionVar ",
73 "BCE ",
Vladimir Markof6a35de2016-03-21 12:01:50 +000074 "DCE ",
75 "LSE ",
76 "LICM ",
Aart Bik96202302016-10-04 17:33:56 -070077 "LoopOpt ",
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010078 "SsaLiveness ",
79 "SsaPhiElim ",
80 "RefTypeProp ",
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010081 "SideEffects ",
82 "RegAllocator ",
Vladimir Markof6a35de2016-03-21 12:01:50 +000083 "RegAllocVldt ",
Vladimir Marko225b6462015-09-28 12:17:40 +010084 "StackMapStm ",
Vladimir Markod38ba0a2017-04-13 13:43:43 +010085 "VectorNode ",
Vladimir Marko225b6462015-09-28 12:17:40 +010086 "CodeGen ",
Vladimir Marko93205e32016-04-13 11:59:46 +010087 "Assembler ",
Vladimir Marko225b6462015-09-28 12:17:40 +010088 "ParallelMove ",
Vladimir Marko655e5852015-10-12 10:38:28 +010089 "GraphChecker ",
Mathieu Chartierde40d472015-10-15 17:47:48 -070090 "Verifier ",
Vladimir Marko93205e32016-04-13 11:59:46 +010091 "CallingConv ",
Vladimir Marko4e335d02016-12-19 16:04:33 +000092 "CHA ",
Alexandre Rames22aa54b2016-10-18 09:32:29 +010093 "Scheduler ",
Calin Juravlecc3171a2017-05-19 16:47:53 -070094 "Profile ",
buzbee862a7602013-04-05 10:58:54 -070095};
96
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000097template <bool kCount>
98ArenaAllocatorStatsImpl<kCount>::ArenaAllocatorStatsImpl()
Vladimir Marko4e335d02016-12-19 16:04:33 +000099 : num_allocations_(0u),
100 alloc_stats_(kNumArenaAllocKinds, 0u) {
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000101}
102
103template <bool kCount>
104void ArenaAllocatorStatsImpl<kCount>::Copy(const ArenaAllocatorStatsImpl& other) {
105 num_allocations_ = other.num_allocations_;
Vladimir Marko4e335d02016-12-19 16:04:33 +0000106 std::copy_n(other.alloc_stats_.begin(), kNumArenaAllocKinds, alloc_stats_.begin());
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000107}
108
109template <bool kCount>
110void ArenaAllocatorStatsImpl<kCount>::RecordAlloc(size_t bytes, ArenaAllocKind kind) {
111 alloc_stats_[kind] += bytes;
112 ++num_allocations_;
113}
114
115template <bool kCount>
116size_t ArenaAllocatorStatsImpl<kCount>::NumAllocations() const {
117 return num_allocations_;
118}
119
120template <bool kCount>
121size_t ArenaAllocatorStatsImpl<kCount>::BytesAllocated() const {
122 const size_t init = 0u; // Initial value of the correct type.
Vladimir Marko4e335d02016-12-19 16:04:33 +0000123 return std::accumulate(alloc_stats_.begin(), alloc_stats_.end(), init);
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000124}
125
126template <bool kCount>
127void ArenaAllocatorStatsImpl<kCount>::Dump(std::ostream& os, const Arena* first,
128 ssize_t lost_bytes_adjustment) const {
129 size_t malloc_bytes = 0u;
130 size_t lost_bytes = 0u;
131 size_t num_arenas = 0u;
132 for (const Arena* arena = first; arena != nullptr; arena = arena->next_) {
133 malloc_bytes += arena->Size();
134 lost_bytes += arena->RemainingSpace();
135 ++num_arenas;
136 }
137 // The lost_bytes_adjustment is used to make up for the fact that the current arena
138 // may not have the bytes_allocated_ updated correctly.
139 lost_bytes += lost_bytes_adjustment;
140 const size_t bytes_allocated = BytesAllocated();
141 os << " MEM: used: " << bytes_allocated << ", allocated: " << malloc_bytes
142 << ", lost: " << lost_bytes << "\n";
Vladimir Markobd9e9db2014-03-07 19:41:05 +0000143 size_t num_allocations = NumAllocations();
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000144 if (num_allocations != 0) {
145 os << "Number of arenas allocated: " << num_arenas << ", Number of allocations: "
146 << num_allocations << ", avg size: " << bytes_allocated / num_allocations << "\n";
147 }
148 os << "===== Allocation by kind\n";
Andreas Gampe785d2f22014-11-03 22:57:30 -0800149 static_assert(arraysize(kAllocNames) == kNumArenaAllocKinds, "arraysize of kAllocNames");
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000150 for (int i = 0; i < kNumArenaAllocKinds; i++) {
Vladimir Markobd9e9db2014-03-07 19:41:05 +0000151 os << kAllocNames[i] << std::setw(10) << alloc_stats_[i] << "\n";
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000152 }
153}
154
Stephen Hines47b22472017-01-26 00:50:31 -0800155#pragma GCC diagnostic push
Jayant Chowdhary5f9da552017-02-02 12:48:10 -0800156#if __clang_major__ >= 4
Stephen Hines47b22472017-01-26 00:50:31 -0800157#pragma GCC diagnostic ignored "-Winstantiation-after-specialization"
Jayant Chowdhary5f9da552017-02-02 12:48:10 -0800158#endif
Vladimir Markod38ba0a2017-04-13 13:43:43 +0100159// We're going to use ArenaAllocatorStatsImpl<kArenaAllocatorCountAllocations> which needs
160// to be explicitly instantiated if kArenaAllocatorCountAllocations is true. Explicit
161// instantiation of the specialization ArenaAllocatorStatsImpl<false> does not do anything
162// but requires the warning "-Winstantiation-after-specialization" to be turned off.
163//
164// To avoid bit-rot of the ArenaAllocatorStatsImpl<true>, instantiate it also in debug builds
165// (but keep the unnecessary code out of release builds) as we do not usually compile with
166// kArenaAllocatorCountAllocations set to true.
167template class ArenaAllocatorStatsImpl<kArenaAllocatorCountAllocations || kIsDebugBuild>;
Stephen Hines47b22472017-01-26 00:50:31 -0800168#pragma GCC diagnostic pop
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000169
Vladimir Marko7bda3b62015-10-07 12:44:31 +0000170void ArenaAllocatorMemoryTool::DoMakeDefined(void* ptr, size_t size) {
171 MEMORY_TOOL_MAKE_DEFINED(ptr, size);
172}
173
174void ArenaAllocatorMemoryTool::DoMakeUndefined(void* ptr, size_t size) {
175 MEMORY_TOOL_MAKE_UNDEFINED(ptr, size);
176}
177
178void ArenaAllocatorMemoryTool::DoMakeInaccessible(void* ptr, size_t size) {
179 MEMORY_TOOL_MAKE_NOACCESS(ptr, size);
180}
181
Andreas Gamped9911ee2017-03-27 13:27:24 -0700182Arena::Arena() : bytes_allocated_(0), memory_(nullptr), size_(0), next_(nullptr) {
Ian Rogerse7a5b7d2013-04-18 20:09:02 -0700183}
184
Vladimir Markof44d36c2017-03-14 14:18:46 +0000185class MallocArena FINAL : public Arena {
186 public:
Andreas Gampe121f1482017-05-12 10:28:35 -0700187 explicit MallocArena(size_t size = arena_allocator::kArenaDefaultSize);
Vladimir Markof44d36c2017-03-14 14:18:46 +0000188 virtual ~MallocArena();
189 private:
190 static constexpr size_t RequiredOverallocation() {
191 return (alignof(std::max_align_t) < ArenaAllocator::kArenaAlignment)
192 ? ArenaAllocator::kArenaAlignment - alignof(std::max_align_t)
193 : 0u;
194 }
195
196 uint8_t* unaligned_memory_;
197};
198
Mathieu Chartierc6201fa2015-03-12 10:06:33 -0700199MallocArena::MallocArena(size_t size) {
Vladimir Markof44d36c2017-03-14 14:18:46 +0000200 // We need to guarantee kArenaAlignment aligned allocation for the new arena.
201 // TODO: Use std::aligned_alloc() when it becomes available with C++17.
202 constexpr size_t overallocation = RequiredOverallocation();
203 unaligned_memory_ = reinterpret_cast<uint8_t*>(calloc(1, size + overallocation));
204 CHECK(unaligned_memory_ != nullptr); // Abort on OOM.
205 DCHECK_ALIGNED(unaligned_memory_, alignof(std::max_align_t));
206 if (overallocation == 0u) {
207 memory_ = unaligned_memory_;
208 } else {
209 memory_ = AlignUp(unaligned_memory_, ArenaAllocator::kArenaAlignment);
210 if (UNLIKELY(RUNNING_ON_MEMORY_TOOL > 0)) {
211 size_t head = memory_ - unaligned_memory_;
212 size_t tail = overallocation - head;
213 MEMORY_TOOL_MAKE_NOACCESS(unaligned_memory_, head);
214 MEMORY_TOOL_MAKE_NOACCESS(memory_ + size, tail);
215 }
216 }
217 DCHECK_ALIGNED(memory_, ArenaAllocator::kArenaAlignment);
Mathieu Chartierc6201fa2015-03-12 10:06:33 -0700218 size_ = size;
buzbee862a7602013-04-05 10:58:54 -0700219}
220
Mathieu Chartierc6201fa2015-03-12 10:06:33 -0700221MallocArena::~MallocArena() {
Vladimir Markof44d36c2017-03-14 14:18:46 +0000222 constexpr size_t overallocation = RequiredOverallocation();
223 if (overallocation != 0u && UNLIKELY(RUNNING_ON_MEMORY_TOOL > 0)) {
224 size_t head = memory_ - unaligned_memory_;
225 size_t tail = overallocation - head;
226 MEMORY_TOOL_MAKE_UNDEFINED(unaligned_memory_, head);
227 MEMORY_TOOL_MAKE_UNDEFINED(memory_ + size_, tail);
228 }
229 free(reinterpret_cast<void*>(unaligned_memory_));
Mathieu Chartierc6201fa2015-03-12 10:06:33 -0700230}
231
Vladimir Markof44d36c2017-03-14 14:18:46 +0000232class MemMapArena FINAL : public Arena {
233 public:
234 MemMapArena(size_t size, bool low_4gb, const char* name);
235 virtual ~MemMapArena();
236 void Release() OVERRIDE;
237
238 private:
239 std::unique_ptr<MemMap> map_;
240};
241
Nicolas Geoffray25e04562016-03-01 13:17:58 +0000242MemMapArena::MemMapArena(size_t size, bool low_4gb, const char* name) {
Vladimir Markof44d36c2017-03-14 14:18:46 +0000243 // Round up to a full page as that's the smallest unit of allocation for mmap()
244 // and we want to be able to use all memory that we actually allocate.
245 size = RoundUp(size, kPageSize);
Mathieu Chartierc6201fa2015-03-12 10:06:33 -0700246 std::string error_msg;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700247 map_.reset(MemMap::MapAnonymous(
Nicolas Geoffray25e04562016-03-01 13:17:58 +0000248 name, nullptr, size, PROT_READ | PROT_WRITE, low_4gb, false, &error_msg));
Mathieu Chartierc6201fa2015-03-12 10:06:33 -0700249 CHECK(map_.get() != nullptr) << error_msg;
250 memory_ = map_->Begin();
Vladimir Markof44d36c2017-03-14 14:18:46 +0000251 static_assert(ArenaAllocator::kArenaAlignment <= kPageSize,
252 "Arena should not need stronger alignment than kPageSize.");
253 DCHECK_ALIGNED(memory_, ArenaAllocator::kArenaAlignment);
Mathieu Chartierc6201fa2015-03-12 10:06:33 -0700254 size_ = map_->Size();
255}
256
Vladimir Marko3481ba22015-04-13 12:22:36 +0100257MemMapArena::~MemMapArena() {
258 // Destroys MemMap via std::unique_ptr<>.
259}
260
Mathieu Chartierc6201fa2015-03-12 10:06:33 -0700261void MemMapArena::Release() {
262 if (bytes_allocated_ > 0) {
Mathieu Chartier9b34b242015-03-09 11:30:17 -0700263 map_->MadviseDontNeedAndZero();
264 bytes_allocated_ = 0;
265 }
266}
267
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700268void Arena::Reset() {
Mathieu Chartier9b34b242015-03-09 11:30:17 -0700269 if (bytes_allocated_ > 0) {
Mathieu Chartierc6201fa2015-03-12 10:06:33 -0700270 memset(Begin(), 0, bytes_allocated_);
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700271 bytes_allocated_ = 0;
buzbee862a7602013-04-05 10:58:54 -0700272 }
buzbee862a7602013-04-05 10:58:54 -0700273}
274
Nicolas Geoffray25e04562016-03-01 13:17:58 +0000275ArenaPool::ArenaPool(bool use_malloc, bool low_4gb, const char* name)
276 : use_malloc_(use_malloc),
277 lock_("Arena pool lock", kArenaPoolLock),
278 free_arenas_(nullptr),
279 low_4gb_(low_4gb),
280 name_(name) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700281 if (low_4gb) {
282 CHECK(!use_malloc) << "low4gb must use map implementation";
283 }
Mathieu Chartierc6201fa2015-03-12 10:06:33 -0700284 if (!use_malloc) {
Mathieu Chartier9b34b242015-03-09 11:30:17 -0700285 MemMap::Init();
286 }
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700287}
288
289ArenaPool::~ArenaPool() {
Jean-Philippe Halimica76a1a2016-02-02 19:48:52 +0100290 ReclaimMemory();
291}
292
293void ArenaPool::ReclaimMemory() {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700294 while (free_arenas_ != nullptr) {
295 auto* arena = free_arenas_;
296 free_arenas_ = free_arenas_->next_;
297 delete arena;
298 }
299}
300
Jean-Philippe Halimica76a1a2016-02-02 19:48:52 +0100301void ArenaPool::LockReclaimMemory() {
302 MutexLock lock(Thread::Current(), lock_);
303 ReclaimMemory();
304}
305
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700306Arena* ArenaPool::AllocArena(size_t size) {
307 Thread* self = Thread::Current();
308 Arena* ret = nullptr;
309 {
310 MutexLock lock(self, lock_);
311 if (free_arenas_ != nullptr && LIKELY(free_arenas_->Size() >= size)) {
312 ret = free_arenas_;
313 free_arenas_ = free_arenas_->next_;
314 }
315 }
316 if (ret == nullptr) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700317 ret = use_malloc_ ? static_cast<Arena*>(new MallocArena(size)) :
Nicolas Geoffray25e04562016-03-01 13:17:58 +0000318 new MemMapArena(size, low_4gb_, name_);
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700319 }
320 ret->Reset();
321 return ret;
322}
323
Mathieu Chartier9b34b242015-03-09 11:30:17 -0700324void ArenaPool::TrimMaps() {
Mathieu Chartierc6201fa2015-03-12 10:06:33 -0700325 if (!use_malloc_) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800326 ScopedTrace trace(__PRETTY_FUNCTION__);
Mathieu Chartierc6201fa2015-03-12 10:06:33 -0700327 // Doesn't work for malloc.
328 MutexLock lock(Thread::Current(), lock_);
329 for (auto* arena = free_arenas_; arena != nullptr; arena = arena->next_) {
330 arena->Release();
331 }
Mathieu Chartier9b34b242015-03-09 11:30:17 -0700332 }
333}
334
Mathieu Chartier49285c52014-12-02 15:43:48 -0800335size_t ArenaPool::GetBytesAllocated() const {
336 size_t total = 0;
337 MutexLock lock(Thread::Current(), lock_);
338 for (Arena* arena = free_arenas_; arena != nullptr; arena = arena->next_) {
339 total += arena->GetBytesAllocated();
340 }
341 return total;
342}
343
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000344void ArenaPool::FreeArenaChain(Arena* first) {
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700345 if (UNLIKELY(RUNNING_ON_MEMORY_TOOL > 0)) {
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000346 for (Arena* arena = first; arena != nullptr; arena = arena->next_) {
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700347 MEMORY_TOOL_MAKE_UNDEFINED(arena->memory_, arena->bytes_allocated_);
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000348 }
Mathieu Chartier75165d02013-09-12 14:00:31 -0700349 }
Andreas Gampe121f1482017-05-12 10:28:35 -0700350
351 if (arena_allocator::kArenaAllocatorPreciseTracking) {
352 // Do not reuse arenas when tracking.
353 while (first != nullptr) {
354 Arena* next = first->next_;
355 delete first;
356 first = next;
357 }
358 return;
359 }
360
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000361 if (first != nullptr) {
362 Arena* last = first;
363 while (last->next_ != nullptr) {
364 last = last->next_;
365 }
366 Thread* self = Thread::Current();
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700367 MutexLock lock(self, lock_);
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000368 last->next_ = free_arenas_;
369 free_arenas_ = first;
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700370 }
371}
372
373size_t ArenaAllocator::BytesAllocated() const {
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000374 return ArenaAllocatorStats::BytesAllocated();
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700375}
376
Mathieu Chartierc7853442015-03-27 14:35:38 -0700377size_t ArenaAllocator::BytesUsed() const {
378 size_t total = ptr_ - begin_;
379 if (arena_head_ != nullptr) {
380 for (Arena* cur_arena = arena_head_->next_; cur_arena != nullptr;
381 cur_arena = cur_arena->next_) {
382 total += cur_arena->GetBytesAllocated();
383 }
384 }
385 return total;
386}
387
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700388ArenaAllocator::ArenaAllocator(ArenaPool* pool)
389 : pool_(pool),
390 begin_(nullptr),
391 end_(nullptr),
392 ptr_(nullptr),
Vladimir Marko2a408a32015-09-18 14:11:00 +0100393 arena_head_(nullptr) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700394}
395
396void ArenaAllocator::UpdateBytesAllocated() {
397 if (arena_head_ != nullptr) {
398 // Update how many bytes we have allocated into the arena so that the arena pool knows how
399 // much memory to zero out.
400 arena_head_->bytes_allocated_ = ptr_ - begin_;
401 }
402}
403
Vladimir Marko2a408a32015-09-18 14:11:00 +0100404void* ArenaAllocator::AllocWithMemoryTool(size_t bytes, ArenaAllocKind kind) {
Vladimir Marko75001932015-11-10 20:54:22 +0000405 // We mark all memory for a newly retrieved arena as inaccessible and then
406 // mark only the actually allocated memory as defined. That leaves red zones
407 // and padding between allocations marked as inaccessible.
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700408 size_t rounded_bytes = RoundUp(bytes + kMemoryToolRedZoneBytes, 8);
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000409 ArenaAllocatorStats::RecordAlloc(rounded_bytes, kind);
Vladimir Marko3f84f2c2016-04-25 19:40:34 +0100410 uint8_t* ret;
Vladimir Marko3e0e7172016-04-22 18:07:13 +0100411 if (UNLIKELY(rounded_bytes > static_cast<size_t>(end_ - ptr_))) {
Vladimir Markof44d36c2017-03-14 14:18:46 +0000412 ret = AllocFromNewArenaWithMemoryTool(rounded_bytes);
Vladimir Marko3f84f2c2016-04-25 19:40:34 +0100413 } else {
414 ret = ptr_;
415 ptr_ += rounded_bytes;
Vladimir Marko3e0e7172016-04-22 18:07:13 +0100416 }
Vladimir Marko2a408a32015-09-18 14:11:00 +0100417 MEMORY_TOOL_MAKE_DEFINED(ret, bytes);
Vladimir Marko75001932015-11-10 20:54:22 +0000418 // Check that the memory is already zeroed out.
419 DCHECK(std::all_of(ret, ret + bytes, [](uint8_t val) { return val == 0u; }));
Mathieu Chartier75165d02013-09-12 14:00:31 -0700420 return ret;
421}
422
Vladimir Markof44d36c2017-03-14 14:18:46 +0000423void* ArenaAllocator::AllocWithMemoryToolAlign16(size_t bytes, ArenaAllocKind kind) {
424 // We mark all memory for a newly retrieved arena as inaccessible and then
425 // mark only the actually allocated memory as defined. That leaves red zones
426 // and padding between allocations marked as inaccessible.
427 size_t rounded_bytes = bytes + kMemoryToolRedZoneBytes;
428 DCHECK_ALIGNED(rounded_bytes, 8); // `bytes` is 16-byte aligned, red zone is 8-byte aligned.
429 uintptr_t padding =
430 ((reinterpret_cast<uintptr_t>(ptr_) + 15u) & 15u) - reinterpret_cast<uintptr_t>(ptr_);
431 ArenaAllocatorStats::RecordAlloc(rounded_bytes, kind);
432 uint8_t* ret;
433 if (UNLIKELY(padding + rounded_bytes > static_cast<size_t>(end_ - ptr_))) {
434 static_assert(kArenaAlignment >= 16, "Expecting sufficient alignment for new Arena.");
435 ret = AllocFromNewArenaWithMemoryTool(rounded_bytes);
436 } else {
437 ptr_ += padding; // Leave padding inaccessible.
438 ret = ptr_;
439 ptr_ += rounded_bytes;
440 }
441 MEMORY_TOOL_MAKE_DEFINED(ret, bytes);
442 // Check that the memory is already zeroed out.
443 DCHECK(std::all_of(ret, ret + bytes, [](uint8_t val) { return val == 0u; }));
444 return ret;
445}
446
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700447ArenaAllocator::~ArenaAllocator() {
448 // Reclaim all the arenas by giving them back to the thread pool.
449 UpdateBytesAllocated();
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000450 pool_->FreeArenaChain(arena_head_);
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700451}
452
Vladimir Marko3f84f2c2016-04-25 19:40:34 +0100453uint8_t* ArenaAllocator::AllocFromNewArena(size_t bytes) {
Andreas Gampe121f1482017-05-12 10:28:35 -0700454 Arena* new_arena = pool_->AllocArena(std::max(arena_allocator::kArenaDefaultSize, bytes));
Vladimir Marko3e0e7172016-04-22 18:07:13 +0100455 DCHECK(new_arena != nullptr);
456 DCHECK_LE(bytes, new_arena->Size());
457 if (static_cast<size_t>(end_ - ptr_) > new_arena->Size() - bytes) {
458 // The old arena has more space remaining than the new one, so keep using it.
459 // This can happen when the requested size is over half of the default size.
460 DCHECK(arena_head_ != nullptr);
461 new_arena->bytes_allocated_ = bytes; // UpdateBytesAllocated() on the new_arena.
462 new_arena->next_ = arena_head_->next_;
463 arena_head_->next_ = new_arena;
464 } else {
465 UpdateBytesAllocated();
466 new_arena->next_ = arena_head_;
467 arena_head_ = new_arena;
468 // Update our internal data structures.
469 begin_ = new_arena->Begin();
Andreas Gampef6dd8292016-08-19 20:22:19 -0700470 DCHECK_ALIGNED(begin_, kAlignment);
Vladimir Marko3e0e7172016-04-22 18:07:13 +0100471 ptr_ = begin_ + bytes;
472 end_ = new_arena->End();
473 }
474 return new_arena->Begin();
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700475}
476
Vladimir Markof44d36c2017-03-14 14:18:46 +0000477uint8_t* ArenaAllocator::AllocFromNewArenaWithMemoryTool(size_t bytes) {
478 uint8_t* ret = AllocFromNewArena(bytes);
479 uint8_t* noaccess_begin = ret + bytes;
480 uint8_t* noaccess_end;
481 if (ret == arena_head_->Begin()) {
482 DCHECK(ptr_ - bytes == ret);
483 noaccess_end = end_;
484 } else {
485 // We're still using the old arena but `ret` comes from a new one just after it.
486 DCHECK(arena_head_->next_ != nullptr);
487 DCHECK(ret == arena_head_->next_->Begin());
488 DCHECK_EQ(bytes, arena_head_->next_->GetBytesAllocated());
489 noaccess_end = arena_head_->next_->End();
490 }
491 MEMORY_TOOL_MAKE_NOACCESS(noaccess_begin, noaccess_end - noaccess_begin);
492 return ret;
493}
494
Mathieu Chartiere401d142015-04-22 13:56:20 -0700495bool ArenaAllocator::Contains(const void* ptr) const {
496 if (ptr >= begin_ && ptr < end_) {
497 return true;
498 }
499 for (const Arena* cur_arena = arena_head_; cur_arena != nullptr; cur_arena = cur_arena->next_) {
500 if (cur_arena->Contains(ptr)) {
501 return true;
502 }
503 }
504 return false;
505}
506
Vladimir Markof44d36c2017-03-14 14:18:46 +0000507MemStats::MemStats(const char* name,
508 const ArenaAllocatorStats* stats,
509 const Arena* first_arena,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000510 ssize_t lost_bytes_adjustment)
511 : name_(name),
512 stats_(stats),
513 first_arena_(first_arena),
514 lost_bytes_adjustment_(lost_bytes_adjustment) {
515}
516
517void MemStats::Dump(std::ostream& os) const {
518 os << name_ << " stats:\n";
519 stats_->Dump(os, first_arena_, lost_bytes_adjustment_);
520}
521
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700522// Dump memory usage stats.
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000523MemStats ArenaAllocator::GetMemStats() const {
524 ssize_t lost_bytes_adjustment =
525 (arena_head_ == nullptr) ? 0 : (end_ - ptr_) - arena_head_->RemainingSpace();
526 return MemStats("ArenaAllocator", this, arena_head_, lost_bytes_adjustment);
buzbee862a7602013-04-05 10:58:54 -0700527}
528
529} // namespace art