blob: 61e0aabbaf6a424e6741382e0f79bc36cdde9474 [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
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000017#include <algorithm>
Ian Rogers6f3dbba2014-10-14 17:41:57 -070018#include <iomanip>
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000019#include <numeric>
20
buzbee862a7602013-04-05 10:58:54 -070021#include "arena_allocator.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080022#include "logging.h"
Vladimir Marko3481ba22015-04-13 12:22:36 +010023#include "mem_map.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080024#include "mutex.h"
Ian Rogers02ed4c02013-09-06 13:10:04 -070025#include "thread-inl.h"
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -080026#include "systrace.h"
buzbee862a7602013-04-05 10:58:54 -070027
28namespace art {
29
Evgenii Stepanov1e133742015-05-20 12:30:59 -070030static constexpr size_t kMemoryToolRedZoneBytes = 8;
Mark Mendell45c11652013-12-11 12:27:35 -080031constexpr size_t Arena::kDefaultSize;
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -070032
Vladimir Markobd9e9db2014-03-07 19:41:05 +000033template <bool kCount>
Vladimir Marko8dea81c2014-06-06 14:50:36 +010034const char* const ArenaAllocatorStatsImpl<kCount>::kAllocNames[] = {
Vladimir Markof9f64412015-09-02 14:05:49 +010035 "Misc ",
Vladimir Markof9f64412015-09-02 14:05:49 +010036 "SwitchTbl ",
Vladimir Markof9f64412015-09-02 14:05:49 +010037 "SlowPaths ",
Vladimir Markof9f64412015-09-02 14:05:49 +010038 "GrowBitMap ",
Vladimir Markof9f64412015-09-02 14:05:49 +010039 "STL ",
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010040 "GraphBuilder ",
Vladimir Markof9f64412015-09-02 14:05:49 +010041 "Graph ",
42 "BasicBlock ",
Vladimir Markofa6b93c2015-09-15 10:15:55 +010043 "BlockList ",
44 "RevPostOrder ",
45 "LinearOrder ",
46 "ConstantsMap ",
Vladimir Marko60584552015-09-03 13:35:12 +000047 "Predecessors ",
48 "Successors ",
49 "Dominated ",
Vladimir Markof9f64412015-09-02 14:05:49 +010050 "Instruction ",
Vladimir Markofa6b93c2015-09-15 10:15:55 +010051 "InvokeInputs ",
52 "PhiInputs ",
Vladimir Markof9f64412015-09-02 14:05:49 +010053 "LoopInfo ",
Vladimir Markofa6b93c2015-09-15 10:15:55 +010054 "LIBackEdges ",
Vladimir Markof9f64412015-09-02 14:05:49 +010055 "TryCatchInf ",
56 "UseListNode ",
57 "Environment ",
Vladimir Markofa6b93c2015-09-15 10:15:55 +010058 "EnvVRegs ",
59 "EnvLocations ",
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010060 "LocSummary ",
Vladimir Marko71bf8092015-09-15 15:33:14 +010061 "SsaBuilder ",
Vladimir Markof9f64412015-09-02 14:05:49 +010062 "MoveOperands ",
63 "CodeBuffer ",
64 "StackMaps ",
Vladimir Markof9f64412015-09-02 14:05:49 +010065 "Optimization ",
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010066 "GVN ",
Vladimir Marko5233f932015-09-29 19:01:15 +010067 "InductionVar ",
68 "BCE ",
Vladimir Markof6a35de2016-03-21 12:01:50 +000069 "DCE ",
70 "LSE ",
71 "LICM ",
Aart Bik96202302016-10-04 17:33:56 -070072 "LoopOpt ",
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010073 "SsaLiveness ",
74 "SsaPhiElim ",
75 "RefTypeProp ",
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010076 "SideEffects ",
77 "RegAllocator ",
Vladimir Markof6a35de2016-03-21 12:01:50 +000078 "RegAllocVldt ",
Vladimir Marko225b6462015-09-28 12:17:40 +010079 "StackMapStm ",
80 "CodeGen ",
Vladimir Marko93205e32016-04-13 11:59:46 +010081 "Assembler ",
Vladimir Marko225b6462015-09-28 12:17:40 +010082 "ParallelMove ",
Vladimir Marko655e5852015-10-12 10:38:28 +010083 "GraphChecker ",
Mathieu Chartierde40d472015-10-15 17:47:48 -070084 "Verifier ",
Vladimir Marko93205e32016-04-13 11:59:46 +010085 "CallingConv ",
Vladimir Marko4e335d02016-12-19 16:04:33 +000086 "CHA ",
buzbee862a7602013-04-05 10:58:54 -070087};
88
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000089template <bool kCount>
90ArenaAllocatorStatsImpl<kCount>::ArenaAllocatorStatsImpl()
Vladimir Marko4e335d02016-12-19 16:04:33 +000091 : num_allocations_(0u),
92 alloc_stats_(kNumArenaAllocKinds, 0u) {
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000093}
94
95template <bool kCount>
96void ArenaAllocatorStatsImpl<kCount>::Copy(const ArenaAllocatorStatsImpl& other) {
97 num_allocations_ = other.num_allocations_;
Vladimir Marko4e335d02016-12-19 16:04:33 +000098 std::copy_n(other.alloc_stats_.begin(), kNumArenaAllocKinds, alloc_stats_.begin());
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000099}
100
101template <bool kCount>
102void ArenaAllocatorStatsImpl<kCount>::RecordAlloc(size_t bytes, ArenaAllocKind kind) {
103 alloc_stats_[kind] += bytes;
104 ++num_allocations_;
105}
106
107template <bool kCount>
108size_t ArenaAllocatorStatsImpl<kCount>::NumAllocations() const {
109 return num_allocations_;
110}
111
112template <bool kCount>
113size_t ArenaAllocatorStatsImpl<kCount>::BytesAllocated() const {
114 const size_t init = 0u; // Initial value of the correct type.
Vladimir Marko4e335d02016-12-19 16:04:33 +0000115 return std::accumulate(alloc_stats_.begin(), alloc_stats_.end(), init);
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000116}
117
118template <bool kCount>
119void ArenaAllocatorStatsImpl<kCount>::Dump(std::ostream& os, const Arena* first,
120 ssize_t lost_bytes_adjustment) const {
121 size_t malloc_bytes = 0u;
122 size_t lost_bytes = 0u;
123 size_t num_arenas = 0u;
124 for (const Arena* arena = first; arena != nullptr; arena = arena->next_) {
125 malloc_bytes += arena->Size();
126 lost_bytes += arena->RemainingSpace();
127 ++num_arenas;
128 }
129 // The lost_bytes_adjustment is used to make up for the fact that the current arena
130 // may not have the bytes_allocated_ updated correctly.
131 lost_bytes += lost_bytes_adjustment;
132 const size_t bytes_allocated = BytesAllocated();
133 os << " MEM: used: " << bytes_allocated << ", allocated: " << malloc_bytes
134 << ", lost: " << lost_bytes << "\n";
Vladimir Markobd9e9db2014-03-07 19:41:05 +0000135 size_t num_allocations = NumAllocations();
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000136 if (num_allocations != 0) {
137 os << "Number of arenas allocated: " << num_arenas << ", Number of allocations: "
138 << num_allocations << ", avg size: " << bytes_allocated / num_allocations << "\n";
139 }
140 os << "===== Allocation by kind\n";
Andreas Gampe785d2f22014-11-03 22:57:30 -0800141 static_assert(arraysize(kAllocNames) == kNumArenaAllocKinds, "arraysize of kAllocNames");
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000142 for (int i = 0; i < kNumArenaAllocKinds; i++) {
Vladimir Markobd9e9db2014-03-07 19:41:05 +0000143 os << kAllocNames[i] << std::setw(10) << alloc_stats_[i] << "\n";
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000144 }
145}
146
147// Explicitly instantiate the used implementation.
148template class ArenaAllocatorStatsImpl<kArenaAllocatorCountAllocations>;
149
Vladimir Marko7bda3b62015-10-07 12:44:31 +0000150void ArenaAllocatorMemoryTool::DoMakeDefined(void* ptr, size_t size) {
151 MEMORY_TOOL_MAKE_DEFINED(ptr, size);
152}
153
154void ArenaAllocatorMemoryTool::DoMakeUndefined(void* ptr, size_t size) {
155 MEMORY_TOOL_MAKE_UNDEFINED(ptr, size);
156}
157
158void ArenaAllocatorMemoryTool::DoMakeInaccessible(void* ptr, size_t size) {
159 MEMORY_TOOL_MAKE_NOACCESS(ptr, size);
160}
161
Mathieu Chartierc6201fa2015-03-12 10:06:33 -0700162Arena::Arena() : bytes_allocated_(0), next_(nullptr) {
Ian Rogerse7a5b7d2013-04-18 20:09:02 -0700163}
164
Mathieu Chartierc6201fa2015-03-12 10:06:33 -0700165MallocArena::MallocArena(size_t size) {
166 memory_ = reinterpret_cast<uint8_t*>(calloc(1, size));
Vladimir Marko3e0e7172016-04-22 18:07:13 +0100167 CHECK(memory_ != nullptr); // Abort on OOM.
Andreas Gampef6dd8292016-08-19 20:22:19 -0700168 DCHECK_ALIGNED(memory_, ArenaAllocator::kAlignment);
Mathieu Chartierc6201fa2015-03-12 10:06:33 -0700169 size_ = size;
buzbee862a7602013-04-05 10:58:54 -0700170}
171
Mathieu Chartierc6201fa2015-03-12 10:06:33 -0700172MallocArena::~MallocArena() {
173 free(reinterpret_cast<void*>(memory_));
174}
175
Nicolas Geoffray25e04562016-03-01 13:17:58 +0000176MemMapArena::MemMapArena(size_t size, bool low_4gb, const char* name) {
Mathieu Chartierc6201fa2015-03-12 10:06:33 -0700177 std::string error_msg;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700178 map_.reset(MemMap::MapAnonymous(
Nicolas Geoffray25e04562016-03-01 13:17:58 +0000179 name, nullptr, size, PROT_READ | PROT_WRITE, low_4gb, false, &error_msg));
Mathieu Chartierc6201fa2015-03-12 10:06:33 -0700180 CHECK(map_.get() != nullptr) << error_msg;
181 memory_ = map_->Begin();
182 size_ = map_->Size();
183}
184
Vladimir Marko3481ba22015-04-13 12:22:36 +0100185MemMapArena::~MemMapArena() {
186 // Destroys MemMap via std::unique_ptr<>.
187}
188
Mathieu Chartierc6201fa2015-03-12 10:06:33 -0700189void MemMapArena::Release() {
190 if (bytes_allocated_ > 0) {
Mathieu Chartier9b34b242015-03-09 11:30:17 -0700191 map_->MadviseDontNeedAndZero();
192 bytes_allocated_ = 0;
193 }
194}
195
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700196void Arena::Reset() {
Mathieu Chartier9b34b242015-03-09 11:30:17 -0700197 if (bytes_allocated_ > 0) {
Mathieu Chartierc6201fa2015-03-12 10:06:33 -0700198 memset(Begin(), 0, bytes_allocated_);
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700199 bytes_allocated_ = 0;
buzbee862a7602013-04-05 10:58:54 -0700200 }
buzbee862a7602013-04-05 10:58:54 -0700201}
202
Nicolas Geoffray25e04562016-03-01 13:17:58 +0000203ArenaPool::ArenaPool(bool use_malloc, bool low_4gb, const char* name)
204 : use_malloc_(use_malloc),
205 lock_("Arena pool lock", kArenaPoolLock),
206 free_arenas_(nullptr),
207 low_4gb_(low_4gb),
208 name_(name) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700209 if (low_4gb) {
210 CHECK(!use_malloc) << "low4gb must use map implementation";
211 }
Mathieu Chartierc6201fa2015-03-12 10:06:33 -0700212 if (!use_malloc) {
Mathieu Chartier9b34b242015-03-09 11:30:17 -0700213 MemMap::Init();
214 }
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700215}
216
217ArenaPool::~ArenaPool() {
Jean-Philippe Halimica76a1a2016-02-02 19:48:52 +0100218 ReclaimMemory();
219}
220
221void ArenaPool::ReclaimMemory() {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700222 while (free_arenas_ != nullptr) {
223 auto* arena = free_arenas_;
224 free_arenas_ = free_arenas_->next_;
225 delete arena;
226 }
227}
228
Jean-Philippe Halimica76a1a2016-02-02 19:48:52 +0100229void ArenaPool::LockReclaimMemory() {
230 MutexLock lock(Thread::Current(), lock_);
231 ReclaimMemory();
232}
233
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700234Arena* ArenaPool::AllocArena(size_t size) {
235 Thread* self = Thread::Current();
236 Arena* ret = nullptr;
237 {
238 MutexLock lock(self, lock_);
239 if (free_arenas_ != nullptr && LIKELY(free_arenas_->Size() >= size)) {
240 ret = free_arenas_;
241 free_arenas_ = free_arenas_->next_;
242 }
243 }
244 if (ret == nullptr) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700245 ret = use_malloc_ ? static_cast<Arena*>(new MallocArena(size)) :
Nicolas Geoffray25e04562016-03-01 13:17:58 +0000246 new MemMapArena(size, low_4gb_, name_);
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700247 }
248 ret->Reset();
249 return ret;
250}
251
Mathieu Chartier9b34b242015-03-09 11:30:17 -0700252void ArenaPool::TrimMaps() {
Mathieu Chartierc6201fa2015-03-12 10:06:33 -0700253 if (!use_malloc_) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800254 ScopedTrace trace(__PRETTY_FUNCTION__);
Mathieu Chartierc6201fa2015-03-12 10:06:33 -0700255 // Doesn't work for malloc.
256 MutexLock lock(Thread::Current(), lock_);
257 for (auto* arena = free_arenas_; arena != nullptr; arena = arena->next_) {
258 arena->Release();
259 }
Mathieu Chartier9b34b242015-03-09 11:30:17 -0700260 }
261}
262
Mathieu Chartier49285c52014-12-02 15:43:48 -0800263size_t ArenaPool::GetBytesAllocated() const {
264 size_t total = 0;
265 MutexLock lock(Thread::Current(), lock_);
266 for (Arena* arena = free_arenas_; arena != nullptr; arena = arena->next_) {
267 total += arena->GetBytesAllocated();
268 }
269 return total;
270}
271
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000272void ArenaPool::FreeArenaChain(Arena* first) {
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700273 if (UNLIKELY(RUNNING_ON_MEMORY_TOOL > 0)) {
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000274 for (Arena* arena = first; arena != nullptr; arena = arena->next_) {
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700275 MEMORY_TOOL_MAKE_UNDEFINED(arena->memory_, arena->bytes_allocated_);
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000276 }
Mathieu Chartier75165d02013-09-12 14:00:31 -0700277 }
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000278 if (first != nullptr) {
279 Arena* last = first;
280 while (last->next_ != nullptr) {
281 last = last->next_;
282 }
283 Thread* self = Thread::Current();
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700284 MutexLock lock(self, lock_);
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000285 last->next_ = free_arenas_;
286 free_arenas_ = first;
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700287 }
288}
289
290size_t ArenaAllocator::BytesAllocated() const {
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000291 return ArenaAllocatorStats::BytesAllocated();
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700292}
293
Mathieu Chartierc7853442015-03-27 14:35:38 -0700294size_t ArenaAllocator::BytesUsed() const {
295 size_t total = ptr_ - begin_;
296 if (arena_head_ != nullptr) {
297 for (Arena* cur_arena = arena_head_->next_; cur_arena != nullptr;
298 cur_arena = cur_arena->next_) {
299 total += cur_arena->GetBytesAllocated();
300 }
301 }
302 return total;
303}
304
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700305ArenaAllocator::ArenaAllocator(ArenaPool* pool)
306 : pool_(pool),
307 begin_(nullptr),
308 end_(nullptr),
309 ptr_(nullptr),
Vladimir Marko2a408a32015-09-18 14:11:00 +0100310 arena_head_(nullptr) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700311}
312
313void ArenaAllocator::UpdateBytesAllocated() {
314 if (arena_head_ != nullptr) {
315 // Update how many bytes we have allocated into the arena so that the arena pool knows how
316 // much memory to zero out.
317 arena_head_->bytes_allocated_ = ptr_ - begin_;
318 }
319}
320
Vladimir Marko2a408a32015-09-18 14:11:00 +0100321void* ArenaAllocator::AllocWithMemoryTool(size_t bytes, ArenaAllocKind kind) {
Vladimir Marko75001932015-11-10 20:54:22 +0000322 // We mark all memory for a newly retrieved arena as inaccessible and then
323 // mark only the actually allocated memory as defined. That leaves red zones
324 // and padding between allocations marked as inaccessible.
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700325 size_t rounded_bytes = RoundUp(bytes + kMemoryToolRedZoneBytes, 8);
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000326 ArenaAllocatorStats::RecordAlloc(rounded_bytes, kind);
Vladimir Marko3f84f2c2016-04-25 19:40:34 +0100327 uint8_t* ret;
Vladimir Marko3e0e7172016-04-22 18:07:13 +0100328 if (UNLIKELY(rounded_bytes > static_cast<size_t>(end_ - ptr_))) {
Vladimir Marko3f84f2c2016-04-25 19:40:34 +0100329 ret = AllocFromNewArena(rounded_bytes);
330 uint8_t* noaccess_begin = ret + bytes;
331 uint8_t* noaccess_end;
Vladimir Marko3e0e7172016-04-22 18:07:13 +0100332 if (ret == arena_head_->Begin()) {
333 DCHECK(ptr_ - rounded_bytes == ret);
Vladimir Marko3f84f2c2016-04-25 19:40:34 +0100334 noaccess_end = end_;
Vladimir Marko3e0e7172016-04-22 18:07:13 +0100335 } else {
336 // We're still using the old arena but `ret` comes from a new one just after it.
337 DCHECK(arena_head_->next_ != nullptr);
338 DCHECK(ret == arena_head_->next_->Begin());
339 DCHECK_EQ(rounded_bytes, arena_head_->next_->GetBytesAllocated());
Vladimir Marko3f84f2c2016-04-25 19:40:34 +0100340 noaccess_end = arena_head_->next_->End();
Vladimir Marko3e0e7172016-04-22 18:07:13 +0100341 }
Vladimir Marko3f84f2c2016-04-25 19:40:34 +0100342 MEMORY_TOOL_MAKE_NOACCESS(noaccess_begin, noaccess_end - noaccess_begin);
343 } else {
344 ret = ptr_;
345 ptr_ += rounded_bytes;
Vladimir Marko3e0e7172016-04-22 18:07:13 +0100346 }
Vladimir Marko2a408a32015-09-18 14:11:00 +0100347 MEMORY_TOOL_MAKE_DEFINED(ret, bytes);
Vladimir Marko75001932015-11-10 20:54:22 +0000348 // Check that the memory is already zeroed out.
349 DCHECK(std::all_of(ret, ret + bytes, [](uint8_t val) { return val == 0u; }));
Mathieu Chartier75165d02013-09-12 14:00:31 -0700350 return ret;
351}
352
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700353ArenaAllocator::~ArenaAllocator() {
354 // Reclaim all the arenas by giving them back to the thread pool.
355 UpdateBytesAllocated();
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000356 pool_->FreeArenaChain(arena_head_);
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700357}
358
Vladimir Marko3f84f2c2016-04-25 19:40:34 +0100359uint8_t* ArenaAllocator::AllocFromNewArena(size_t bytes) {
Vladimir Marko3e0e7172016-04-22 18:07:13 +0100360 Arena* new_arena = pool_->AllocArena(std::max(Arena::kDefaultSize, bytes));
361 DCHECK(new_arena != nullptr);
362 DCHECK_LE(bytes, new_arena->Size());
363 if (static_cast<size_t>(end_ - ptr_) > new_arena->Size() - bytes) {
364 // The old arena has more space remaining than the new one, so keep using it.
365 // This can happen when the requested size is over half of the default size.
366 DCHECK(arena_head_ != nullptr);
367 new_arena->bytes_allocated_ = bytes; // UpdateBytesAllocated() on the new_arena.
368 new_arena->next_ = arena_head_->next_;
369 arena_head_->next_ = new_arena;
370 } else {
371 UpdateBytesAllocated();
372 new_arena->next_ = arena_head_;
373 arena_head_ = new_arena;
374 // Update our internal data structures.
375 begin_ = new_arena->Begin();
Andreas Gampef6dd8292016-08-19 20:22:19 -0700376 DCHECK_ALIGNED(begin_, kAlignment);
Vladimir Marko3e0e7172016-04-22 18:07:13 +0100377 ptr_ = begin_ + bytes;
378 end_ = new_arena->End();
379 }
380 return new_arena->Begin();
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700381}
382
Mathieu Chartiere401d142015-04-22 13:56:20 -0700383bool ArenaAllocator::Contains(const void* ptr) const {
384 if (ptr >= begin_ && ptr < end_) {
385 return true;
386 }
387 for (const Arena* cur_arena = arena_head_; cur_arena != nullptr; cur_arena = cur_arena->next_) {
388 if (cur_arena->Contains(ptr)) {
389 return true;
390 }
391 }
392 return false;
393}
394
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000395MemStats::MemStats(const char* name, const ArenaAllocatorStats* stats, const Arena* first_arena,
396 ssize_t lost_bytes_adjustment)
397 : name_(name),
398 stats_(stats),
399 first_arena_(first_arena),
400 lost_bytes_adjustment_(lost_bytes_adjustment) {
401}
402
403void MemStats::Dump(std::ostream& os) const {
404 os << name_ << " stats:\n";
405 stats_->Dump(os, first_arena_, lost_bytes_adjustment_);
406}
407
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700408// Dump memory usage stats.
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000409MemStats ArenaAllocator::GetMemStats() const {
410 ssize_t lost_bytes_adjustment =
411 (arena_head_ == nullptr) ? 0 : (end_ - ptr_) - arena_head_->RemainingSpace();
412 return MemStats("ArenaAllocator", this, arena_head_, lost_bytes_adjustment);
buzbee862a7602013-04-05 10:58:54 -0700413}
414
415} // namespace art