Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | |
| 17 | #include "scoped_arena_allocator.h" |
| 18 | |
Andreas Gampe | 121f148 | 2017-05-12 10:28:35 -0700 | [diff] [blame] | 19 | #include "arena_allocator-inl.h" |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 20 | #include "base/memory_tool.h" |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 21 | |
| 22 | namespace art { |
| 23 | |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 24 | static constexpr size_t kMemoryToolRedZoneBytes = 8; |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 25 | |
| 26 | ArenaStack::ArenaStack(ArenaPool* arena_pool) |
| 27 | : DebugStackRefCounter(), |
| 28 | stats_and_pool_(arena_pool), |
| 29 | bottom_arena_(nullptr), |
| 30 | top_arena_(nullptr), |
| 31 | top_ptr_(nullptr), |
Vladimir Marko | 2a408a3 | 2015-09-18 14:11:00 +0100 | [diff] [blame] | 32 | top_end_(nullptr) { |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | ArenaStack::~ArenaStack() { |
Vladimir Marko | 53b6afc | 2014-03-21 14:21:20 +0000 | [diff] [blame] | 36 | DebugStackRefCounter::CheckNoRefs(); |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 37 | stats_and_pool_.pool->FreeArenaChain(bottom_arena_); |
| 38 | } |
| 39 | |
Vladimir Marko | 53b6afc | 2014-03-21 14:21:20 +0000 | [diff] [blame] | 40 | void ArenaStack::Reset() { |
| 41 | DebugStackRefCounter::CheckNoRefs(); |
| 42 | stats_and_pool_.pool->FreeArenaChain(bottom_arena_); |
| 43 | bottom_arena_ = nullptr; |
| 44 | top_arena_ = nullptr; |
| 45 | top_ptr_ = nullptr; |
| 46 | top_end_ = nullptr; |
| 47 | } |
| 48 | |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 49 | MemStats ArenaStack::GetPeakStats() const { |
| 50 | DebugStackRefCounter::CheckNoRefs(); |
| 51 | return MemStats("ArenaStack peak", static_cast<const TaggedStats<Peak>*>(&stats_and_pool_), |
| 52 | bottom_arena_); |
| 53 | } |
| 54 | |
| 55 | uint8_t* ArenaStack::AllocateFromNextArena(size_t rounded_bytes) { |
| 56 | UpdateBytesAllocated(); |
Andreas Gampe | 121f148 | 2017-05-12 10:28:35 -0700 | [diff] [blame] | 57 | size_t allocation_size = std::max(arena_allocator::kArenaDefaultSize, rounded_bytes); |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 58 | if (UNLIKELY(top_arena_ == nullptr)) { |
| 59 | top_arena_ = bottom_arena_ = stats_and_pool_.pool->AllocArena(allocation_size); |
| 60 | top_arena_->next_ = nullptr; |
| 61 | } else if (top_arena_->next_ != nullptr && top_arena_->next_->Size() >= allocation_size) { |
| 62 | top_arena_ = top_arena_->next_; |
| 63 | } else { |
| 64 | Arena* tail = top_arena_->next_; |
| 65 | top_arena_->next_ = stats_and_pool_.pool->AllocArena(allocation_size); |
| 66 | top_arena_ = top_arena_->next_; |
| 67 | top_arena_->next_ = tail; |
| 68 | } |
| 69 | top_end_ = top_arena_->End(); |
| 70 | // top_ptr_ shall be updated by ScopedArenaAllocator. |
| 71 | return top_arena_->Begin(); |
| 72 | } |
| 73 | |
| 74 | void ArenaStack::UpdatePeakStatsAndRestore(const ArenaAllocatorStats& restore_stats) { |
| 75 | if (PeakStats()->BytesAllocated() < CurrentStats()->BytesAllocated()) { |
| 76 | PeakStats()->Copy(*CurrentStats()); |
| 77 | } |
| 78 | CurrentStats()->Copy(restore_stats); |
| 79 | } |
| 80 | |
| 81 | void ArenaStack::UpdateBytesAllocated() { |
| 82 | if (top_arena_ != nullptr) { |
| 83 | // Update how many bytes we have allocated into the arena so that the arena pool knows how |
| 84 | // much memory to zero out. Though ScopedArenaAllocator doesn't guarantee the memory is |
| 85 | // zero-initialized, the Arena may be reused by ArenaAllocator which does guarantee this. |
| 86 | size_t allocated = static_cast<size_t>(top_ptr_ - top_arena_->Begin()); |
| 87 | if (top_arena_->bytes_allocated_ < allocated) { |
| 88 | top_arena_->bytes_allocated_ = allocated; |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
Vladimir Marko | 2a408a3 | 2015-09-18 14:11:00 +0100 | [diff] [blame] | 93 | void* ArenaStack::AllocWithMemoryTool(size_t bytes, ArenaAllocKind kind) { |
Vladimir Marko | 7500193 | 2015-11-10 20:54:22 +0000 | [diff] [blame] | 94 | // We mark all memory for a newly retrieved arena as inaccessible and then |
| 95 | // mark only the actually allocated memory as defined. That leaves red zones |
| 96 | // and padding between allocations marked as inaccessible. |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 97 | size_t rounded_bytes = RoundUp(bytes + kMemoryToolRedZoneBytes, 8); |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 98 | uint8_t* ptr = top_ptr_; |
| 99 | if (UNLIKELY(static_cast<size_t>(top_end_ - ptr) < rounded_bytes)) { |
| 100 | ptr = AllocateFromNextArena(rounded_bytes); |
Vladimir | f41b92c | 2014-10-13 13:41:38 +0700 | [diff] [blame] | 101 | CHECK(ptr != nullptr) << "Failed to allocate memory"; |
Vladimir Marko | 4fb3a42 | 2016-02-15 10:13:11 +0000 | [diff] [blame] | 102 | MEMORY_TOOL_MAKE_NOACCESS(ptr, top_end_ - ptr); |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 103 | } |
| 104 | CurrentStats()->RecordAlloc(bytes, kind); |
| 105 | top_ptr_ = ptr + rounded_bytes; |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 106 | MEMORY_TOOL_MAKE_UNDEFINED(ptr, bytes); |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 107 | return ptr; |
| 108 | } |
| 109 | |
| 110 | ScopedArenaAllocator::ScopedArenaAllocator(ArenaStack* arena_stack) |
| 111 | : DebugStackReference(arena_stack), |
| 112 | DebugStackRefCounter(), |
| 113 | ArenaAllocatorStats(*arena_stack->CurrentStats()), |
| 114 | arena_stack_(arena_stack), |
| 115 | mark_arena_(arena_stack->top_arena_), |
| 116 | mark_ptr_(arena_stack->top_ptr_), |
| 117 | mark_end_(arena_stack->top_end_) { |
| 118 | } |
| 119 | |
| 120 | ScopedArenaAllocator::~ScopedArenaAllocator() { |
Vladimir Marko | 3d2ec35 | 2014-10-10 15:39:11 +0100 | [diff] [blame] | 121 | DoReset(); |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void ScopedArenaAllocator::Reset() { |
Vladimir Marko | 3d2ec35 | 2014-10-10 15:39:11 +0100 | [diff] [blame] | 125 | DoReset(); |
| 126 | // If this allocator was Create()d, we need to move the arena_stack_->top_ptr_ past *this. |
| 127 | if (mark_ptr_ == reinterpret_cast<uint8_t*>(this)) { |
| 128 | arena_stack_->top_ptr_ = mark_ptr_ + RoundUp(sizeof(ScopedArenaAllocator), 8); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | void ScopedArenaAllocator::DoReset() { |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 133 | DebugStackReference::CheckTop(); |
| 134 | DebugStackRefCounter::CheckNoRefs(); |
| 135 | arena_stack_->UpdatePeakStatsAndRestore(*this); |
| 136 | arena_stack_->UpdateBytesAllocated(); |
| 137 | if (LIKELY(mark_arena_ != nullptr)) { |
| 138 | arena_stack_->top_arena_ = mark_arena_; |
| 139 | arena_stack_->top_ptr_ = mark_ptr_; |
| 140 | arena_stack_->top_end_ = mark_end_; |
| 141 | } else if (arena_stack_->bottom_arena_ != nullptr) { |
| 142 | mark_arena_ = arena_stack_->top_arena_ = arena_stack_->bottom_arena_; |
| 143 | mark_ptr_ = arena_stack_->top_ptr_ = mark_arena_->Begin(); |
| 144 | mark_end_ = arena_stack_->top_end_ = mark_arena_->End(); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | } // namespace art |