xref: /aosp_15_r20/art/libartbase/base/scoped_arena_allocator.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2014 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #include "scoped_arena_allocator.h"
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker #include "arena_allocator-inl.h"
20*795d594fSAndroid Build Coastguard Worker #include "memory_tool.h"
21*795d594fSAndroid Build Coastguard Worker 
22*795d594fSAndroid Build Coastguard Worker namespace art {
23*795d594fSAndroid Build Coastguard Worker 
24*795d594fSAndroid Build Coastguard Worker static constexpr size_t kMemoryToolRedZoneBytes = 8;
25*795d594fSAndroid Build Coastguard Worker 
ArenaStack(ArenaPool * arena_pool)26*795d594fSAndroid Build Coastguard Worker ArenaStack::ArenaStack(ArenaPool* arena_pool)
27*795d594fSAndroid Build Coastguard Worker   : DebugStackRefCounter(),
28*795d594fSAndroid Build Coastguard Worker     stats_and_pool_(arena_pool),
29*795d594fSAndroid Build Coastguard Worker     bottom_arena_(nullptr),
30*795d594fSAndroid Build Coastguard Worker     top_arena_(nullptr),
31*795d594fSAndroid Build Coastguard Worker     top_ptr_(nullptr),
32*795d594fSAndroid Build Coastguard Worker     top_end_(nullptr) {
33*795d594fSAndroid Build Coastguard Worker }
34*795d594fSAndroid Build Coastguard Worker 
~ArenaStack()35*795d594fSAndroid Build Coastguard Worker ArenaStack::~ArenaStack() {
36*795d594fSAndroid Build Coastguard Worker   DebugStackRefCounter::CheckNoRefs();
37*795d594fSAndroid Build Coastguard Worker   stats_and_pool_.pool->FreeArenaChain(bottom_arena_);
38*795d594fSAndroid Build Coastguard Worker }
39*795d594fSAndroid Build Coastguard Worker 
Reset()40*795d594fSAndroid Build Coastguard Worker void ArenaStack::Reset() {
41*795d594fSAndroid Build Coastguard Worker   DebugStackRefCounter::CheckNoRefs();
42*795d594fSAndroid Build Coastguard Worker   stats_and_pool_.pool->FreeArenaChain(bottom_arena_);
43*795d594fSAndroid Build Coastguard Worker   bottom_arena_ = nullptr;
44*795d594fSAndroid Build Coastguard Worker   top_arena_  = nullptr;
45*795d594fSAndroid Build Coastguard Worker   top_ptr_ = nullptr;
46*795d594fSAndroid Build Coastguard Worker   top_end_ = nullptr;
47*795d594fSAndroid Build Coastguard Worker }
48*795d594fSAndroid Build Coastguard Worker 
GetPeakStats() const49*795d594fSAndroid Build Coastguard Worker MemStats ArenaStack::GetPeakStats() const {
50*795d594fSAndroid Build Coastguard Worker   DebugStackRefCounter::CheckNoRefs();
51*795d594fSAndroid Build Coastguard Worker   return MemStats("ArenaStack peak", PeakStats(), bottom_arena_);
52*795d594fSAndroid Build Coastguard Worker }
53*795d594fSAndroid Build Coastguard Worker 
AllocateFromNextArena(size_t rounded_bytes)54*795d594fSAndroid Build Coastguard Worker uint8_t* ArenaStack::AllocateFromNextArena(size_t rounded_bytes) {
55*795d594fSAndroid Build Coastguard Worker   UpdateBytesAllocated();
56*795d594fSAndroid Build Coastguard Worker   size_t allocation_size = std::max(arena_allocator::kArenaDefaultSize, rounded_bytes);
57*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(top_arena_ == nullptr)) {
58*795d594fSAndroid Build Coastguard Worker     top_arena_ = bottom_arena_ = stats_and_pool_.pool->AllocArena(allocation_size);
59*795d594fSAndroid Build Coastguard Worker     top_arena_->next_ = nullptr;
60*795d594fSAndroid Build Coastguard Worker   } else if (top_arena_->next_ != nullptr && top_arena_->next_->Size() >= allocation_size) {
61*795d594fSAndroid Build Coastguard Worker     top_arena_ = top_arena_->next_;
62*795d594fSAndroid Build Coastguard Worker   } else {
63*795d594fSAndroid Build Coastguard Worker     Arena* tail = top_arena_->next_;
64*795d594fSAndroid Build Coastguard Worker     top_arena_->next_ = stats_and_pool_.pool->AllocArena(allocation_size);
65*795d594fSAndroid Build Coastguard Worker     top_arena_ = top_arena_->next_;
66*795d594fSAndroid Build Coastguard Worker     top_arena_->next_ = tail;
67*795d594fSAndroid Build Coastguard Worker   }
68*795d594fSAndroid Build Coastguard Worker   top_end_ = top_arena_->End();
69*795d594fSAndroid Build Coastguard Worker   // top_ptr_ shall be updated by ScopedArenaAllocator.
70*795d594fSAndroid Build Coastguard Worker   return top_arena_->Begin();
71*795d594fSAndroid Build Coastguard Worker }
72*795d594fSAndroid Build Coastguard Worker 
UpdatePeakStatsAndRestore(const ArenaAllocatorStats & restore_stats)73*795d594fSAndroid Build Coastguard Worker void ArenaStack::UpdatePeakStatsAndRestore(const ArenaAllocatorStats& restore_stats) {
74*795d594fSAndroid Build Coastguard Worker   if (PeakStats()->BytesAllocated() < CurrentStats()->BytesAllocated()) {
75*795d594fSAndroid Build Coastguard Worker     PeakStats()->Copy(*CurrentStats());
76*795d594fSAndroid Build Coastguard Worker   }
77*795d594fSAndroid Build Coastguard Worker   CurrentStats()->Copy(restore_stats);
78*795d594fSAndroid Build Coastguard Worker }
79*795d594fSAndroid Build Coastguard Worker 
UpdateBytesAllocated()80*795d594fSAndroid Build Coastguard Worker void ArenaStack::UpdateBytesAllocated() {
81*795d594fSAndroid Build Coastguard Worker   if (top_arena_ != nullptr) {
82*795d594fSAndroid Build Coastguard Worker     // Update how many bytes we have allocated into the arena so that the arena pool knows how
83*795d594fSAndroid Build Coastguard Worker     // much memory to zero out. Though ScopedArenaAllocator doesn't guarantee the memory is
84*795d594fSAndroid Build Coastguard Worker     // zero-initialized, the Arena may be reused by ArenaAllocator which does guarantee this.
85*795d594fSAndroid Build Coastguard Worker     size_t allocated = static_cast<size_t>(top_ptr_ - top_arena_->Begin());
86*795d594fSAndroid Build Coastguard Worker     if (top_arena_->bytes_allocated_ < allocated) {
87*795d594fSAndroid Build Coastguard Worker       top_arena_->bytes_allocated_ = allocated;
88*795d594fSAndroid Build Coastguard Worker     }
89*795d594fSAndroid Build Coastguard Worker   }
90*795d594fSAndroid Build Coastguard Worker }
91*795d594fSAndroid Build Coastguard Worker 
AllocWithMemoryTool(size_t bytes,ArenaAllocKind kind)92*795d594fSAndroid Build Coastguard Worker void* ArenaStack::AllocWithMemoryTool(size_t bytes, ArenaAllocKind kind) {
93*795d594fSAndroid Build Coastguard Worker   // We mark all memory for a newly retrieved arena as inaccessible and then
94*795d594fSAndroid Build Coastguard Worker   // mark only the actually allocated memory as defined. That leaves red zones
95*795d594fSAndroid Build Coastguard Worker   // and padding between allocations marked as inaccessible.
96*795d594fSAndroid Build Coastguard Worker   size_t rounded_bytes = RoundUp(bytes + kMemoryToolRedZoneBytes, 8);
97*795d594fSAndroid Build Coastguard Worker   uint8_t* ptr = top_ptr_;
98*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(static_cast<size_t>(top_end_ - ptr) < rounded_bytes)) {
99*795d594fSAndroid Build Coastguard Worker     ptr = AllocateFromNextArena(rounded_bytes);
100*795d594fSAndroid Build Coastguard Worker     CHECK(ptr != nullptr) << "Failed to allocate memory";
101*795d594fSAndroid Build Coastguard Worker     MEMORY_TOOL_MAKE_NOACCESS(ptr, top_end_ - ptr);
102*795d594fSAndroid Build Coastguard Worker   }
103*795d594fSAndroid Build Coastguard Worker   CurrentStats()->RecordAlloc(bytes, kind);
104*795d594fSAndroid Build Coastguard Worker   top_ptr_ = ptr + rounded_bytes;
105*795d594fSAndroid Build Coastguard Worker   MEMORY_TOOL_MAKE_UNDEFINED(ptr, bytes);
106*795d594fSAndroid Build Coastguard Worker   return ptr;
107*795d594fSAndroid Build Coastguard Worker }
108*795d594fSAndroid Build Coastguard Worker 
ApproximatePeakBytes()109*795d594fSAndroid Build Coastguard Worker size_t ArenaStack::ApproximatePeakBytes() {
110*795d594fSAndroid Build Coastguard Worker   UpdateBytesAllocated();
111*795d594fSAndroid Build Coastguard Worker   size_t sum = 0;
112*795d594fSAndroid Build Coastguard Worker   for (Arena* arena = bottom_arena_; arena != nullptr; arena = arena->next_) {
113*795d594fSAndroid Build Coastguard Worker     sum += arena->bytes_allocated_;
114*795d594fSAndroid Build Coastguard Worker   }
115*795d594fSAndroid Build Coastguard Worker   return sum;
116*795d594fSAndroid Build Coastguard Worker }
117*795d594fSAndroid Build Coastguard Worker 
ScopedArenaAllocator(ScopedArenaAllocator && other)118*795d594fSAndroid Build Coastguard Worker ScopedArenaAllocator::ScopedArenaAllocator(ScopedArenaAllocator&& other) noexcept
119*795d594fSAndroid Build Coastguard Worker     : DebugStackReference(std::move(other)),
120*795d594fSAndroid Build Coastguard Worker       DebugStackRefCounter(),
121*795d594fSAndroid Build Coastguard Worker       // NOLINTBEGIN(bugprone-use-after-move) - the accessed fields are still valid after the move
122*795d594fSAndroid Build Coastguard Worker       ArenaAllocatorStats(other),
123*795d594fSAndroid Build Coastguard Worker       arena_stack_(other.arena_stack_),
124*795d594fSAndroid Build Coastguard Worker       mark_arena_(other.mark_arena_),
125*795d594fSAndroid Build Coastguard Worker       mark_ptr_(other.mark_ptr_),
126*795d594fSAndroid Build Coastguard Worker       mark_end_(other.mark_end_) {
127*795d594fSAndroid Build Coastguard Worker   other.DebugStackRefCounter::CheckNoRefs();
128*795d594fSAndroid Build Coastguard Worker   other.arena_stack_ = nullptr;
129*795d594fSAndroid Build Coastguard Worker   // NOLINTEND(bugprone-use-after-move)
130*795d594fSAndroid Build Coastguard Worker }
131*795d594fSAndroid Build Coastguard Worker 
ScopedArenaAllocator(ArenaStack * arena_stack)132*795d594fSAndroid Build Coastguard Worker ScopedArenaAllocator::ScopedArenaAllocator(ArenaStack* arena_stack)
133*795d594fSAndroid Build Coastguard Worker     : DebugStackReference(arena_stack),
134*795d594fSAndroid Build Coastguard Worker       DebugStackRefCounter(),
135*795d594fSAndroid Build Coastguard Worker       ArenaAllocatorStats(*arena_stack->CurrentStats()),
136*795d594fSAndroid Build Coastguard Worker       arena_stack_(arena_stack),
137*795d594fSAndroid Build Coastguard Worker       mark_arena_(arena_stack->top_arena_),
138*795d594fSAndroid Build Coastguard Worker       mark_ptr_(arena_stack->top_ptr_),
139*795d594fSAndroid Build Coastguard Worker       mark_end_(arena_stack->top_end_) {
140*795d594fSAndroid Build Coastguard Worker }
141*795d594fSAndroid Build Coastguard Worker 
~ScopedArenaAllocator()142*795d594fSAndroid Build Coastguard Worker ScopedArenaAllocator::~ScopedArenaAllocator() {
143*795d594fSAndroid Build Coastguard Worker   if (arena_stack_ != nullptr) {
144*795d594fSAndroid Build Coastguard Worker     DoReset();
145*795d594fSAndroid Build Coastguard Worker   }
146*795d594fSAndroid Build Coastguard Worker }
147*795d594fSAndroid Build Coastguard Worker 
Reset()148*795d594fSAndroid Build Coastguard Worker void ScopedArenaAllocator::Reset() {
149*795d594fSAndroid Build Coastguard Worker   DoReset();
150*795d594fSAndroid Build Coastguard Worker   // If this allocator was Create()d, we need to move the arena_stack_->top_ptr_ past *this.
151*795d594fSAndroid Build Coastguard Worker   if (mark_ptr_ == reinterpret_cast<uint8_t*>(this)) {
152*795d594fSAndroid Build Coastguard Worker     arena_stack_->top_ptr_ = mark_ptr_ + RoundUp(sizeof(ScopedArenaAllocator), 8);
153*795d594fSAndroid Build Coastguard Worker   }
154*795d594fSAndroid Build Coastguard Worker }
155*795d594fSAndroid Build Coastguard Worker 
DoReset()156*795d594fSAndroid Build Coastguard Worker void ScopedArenaAllocator::DoReset() {
157*795d594fSAndroid Build Coastguard Worker   DebugStackReference::CheckTop();
158*795d594fSAndroid Build Coastguard Worker   DebugStackRefCounter::CheckNoRefs();
159*795d594fSAndroid Build Coastguard Worker   arena_stack_->UpdatePeakStatsAndRestore(*this);
160*795d594fSAndroid Build Coastguard Worker   arena_stack_->UpdateBytesAllocated();
161*795d594fSAndroid Build Coastguard Worker   if (LIKELY(mark_arena_ != nullptr)) {
162*795d594fSAndroid Build Coastguard Worker     arena_stack_->top_arena_ = mark_arena_;
163*795d594fSAndroid Build Coastguard Worker     arena_stack_->top_ptr_ = mark_ptr_;
164*795d594fSAndroid Build Coastguard Worker     arena_stack_->top_end_ = mark_end_;
165*795d594fSAndroid Build Coastguard Worker   } else if (arena_stack_->bottom_arena_ != nullptr) {
166*795d594fSAndroid Build Coastguard Worker     mark_arena_ = arena_stack_->top_arena_ = arena_stack_->bottom_arena_;
167*795d594fSAndroid Build Coastguard Worker     mark_ptr_ = arena_stack_->top_ptr_ = mark_arena_->Begin();
168*795d594fSAndroid Build Coastguard Worker     mark_end_ = arena_stack_->top_end_ = mark_arena_->End();
169*795d594fSAndroid Build Coastguard Worker   }
170*795d594fSAndroid Build Coastguard Worker }
171*795d594fSAndroid Build Coastguard Worker 
ApproximatePeakBytes()172*795d594fSAndroid Build Coastguard Worker size_t ScopedArenaAllocator::ApproximatePeakBytes() {
173*795d594fSAndroid Build Coastguard Worker   size_t subtract;
174*795d594fSAndroid Build Coastguard Worker   Arena* start;
175*795d594fSAndroid Build Coastguard Worker   if (LIKELY(mark_arena_ != nullptr)) {
176*795d594fSAndroid Build Coastguard Worker     start = mark_arena_;
177*795d594fSAndroid Build Coastguard Worker     size_t mark_free = static_cast<size_t>(mark_end_ - mark_ptr_);
178*795d594fSAndroid Build Coastguard Worker     DCHECK_GE(mark_arena_->bytes_allocated_, mark_arena_->size_ - mark_free);
179*795d594fSAndroid Build Coastguard Worker     subtract = mark_arena_->bytes_allocated_ - (mark_arena_->size_ - mark_free);
180*795d594fSAndroid Build Coastguard Worker   } else {
181*795d594fSAndroid Build Coastguard Worker     start = arena_stack_->bottom_arena_;
182*795d594fSAndroid Build Coastguard Worker     subtract = 0;
183*795d594fSAndroid Build Coastguard Worker   }
184*795d594fSAndroid Build Coastguard Worker 
185*795d594fSAndroid Build Coastguard Worker   size_t sum = 0;
186*795d594fSAndroid Build Coastguard Worker   for (Arena* arena = start; arena != nullptr; arena = arena->next_) {
187*795d594fSAndroid Build Coastguard Worker     if (arena == arena_stack_->top_arena_) {
188*795d594fSAndroid Build Coastguard Worker       sum += static_cast<size_t>(arena_stack_->top_ptr_ - arena->Begin());
189*795d594fSAndroid Build Coastguard Worker       break;
190*795d594fSAndroid Build Coastguard Worker     } else {
191*795d594fSAndroid Build Coastguard Worker       sum += arena->bytes_allocated_;
192*795d594fSAndroid Build Coastguard Worker     }
193*795d594fSAndroid Build Coastguard Worker   }
194*795d594fSAndroid Build Coastguard Worker   return sum - subtract;
195*795d594fSAndroid Build Coastguard Worker }
196*795d594fSAndroid Build Coastguard Worker 
197*795d594fSAndroid Build Coastguard Worker }  // namespace art
198