xref: /aosp_15_r20/art/runtime/gc/space/bump_pointer_space-inl.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2013 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 #ifndef ART_RUNTIME_GC_SPACE_BUMP_POINTER_SPACE_INL_H_
18*795d594fSAndroid Build Coastguard Worker #define ART_RUNTIME_GC_SPACE_BUMP_POINTER_SPACE_INL_H_
19*795d594fSAndroid Build Coastguard Worker 
20*795d594fSAndroid Build Coastguard Worker #include "bump_pointer_space.h"
21*795d594fSAndroid Build Coastguard Worker 
22*795d594fSAndroid Build Coastguard Worker #include "base/bit_utils.h"
23*795d594fSAndroid Build Coastguard Worker #include "mirror/object-inl.h"
24*795d594fSAndroid Build Coastguard Worker 
25*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
26*795d594fSAndroid Build Coastguard Worker namespace gc {
27*795d594fSAndroid Build Coastguard Worker namespace space {
28*795d594fSAndroid Build Coastguard Worker 
Alloc(Thread *,size_t num_bytes,size_t * bytes_allocated,size_t * usable_size,size_t * bytes_tl_bulk_allocated)29*795d594fSAndroid Build Coastguard Worker inline mirror::Object* BumpPointerSpace::Alloc(Thread*, size_t num_bytes, size_t* bytes_allocated,
30*795d594fSAndroid Build Coastguard Worker                                                size_t* usable_size,
31*795d594fSAndroid Build Coastguard Worker                                                size_t* bytes_tl_bulk_allocated) {
32*795d594fSAndroid Build Coastguard Worker   num_bytes = RoundUp(num_bytes, kAlignment);
33*795d594fSAndroid Build Coastguard Worker   mirror::Object* ret = AllocNonvirtual(num_bytes);
34*795d594fSAndroid Build Coastguard Worker   if (LIKELY(ret != nullptr)) {
35*795d594fSAndroid Build Coastguard Worker     *bytes_allocated = num_bytes;
36*795d594fSAndroid Build Coastguard Worker     if (usable_size != nullptr) {
37*795d594fSAndroid Build Coastguard Worker       *usable_size = num_bytes;
38*795d594fSAndroid Build Coastguard Worker     }
39*795d594fSAndroid Build Coastguard Worker     *bytes_tl_bulk_allocated = num_bytes;
40*795d594fSAndroid Build Coastguard Worker   }
41*795d594fSAndroid Build Coastguard Worker   return ret;
42*795d594fSAndroid Build Coastguard Worker }
43*795d594fSAndroid Build Coastguard Worker 
AllocThreadUnsafe(Thread * self,size_t num_bytes,size_t * bytes_allocated,size_t * usable_size,size_t * bytes_tl_bulk_allocated)44*795d594fSAndroid Build Coastguard Worker inline mirror::Object* BumpPointerSpace::AllocThreadUnsafe(Thread* self, size_t num_bytes,
45*795d594fSAndroid Build Coastguard Worker                                                            size_t* bytes_allocated,
46*795d594fSAndroid Build Coastguard Worker                                                            size_t* usable_size,
47*795d594fSAndroid Build Coastguard Worker                                                            size_t* bytes_tl_bulk_allocated) {
48*795d594fSAndroid Build Coastguard Worker   Locks::mutator_lock_->AssertExclusiveHeld(self);
49*795d594fSAndroid Build Coastguard Worker   num_bytes = RoundUp(num_bytes, kAlignment);
50*795d594fSAndroid Build Coastguard Worker   uint8_t* end = end_.load(std::memory_order_relaxed);
51*795d594fSAndroid Build Coastguard Worker   if (end + num_bytes > growth_end_) {
52*795d594fSAndroid Build Coastguard Worker     return nullptr;
53*795d594fSAndroid Build Coastguard Worker   }
54*795d594fSAndroid Build Coastguard Worker   mirror::Object* obj = reinterpret_cast<mirror::Object*>(end);
55*795d594fSAndroid Build Coastguard Worker   end_.store(end + num_bytes, std::memory_order_relaxed);
56*795d594fSAndroid Build Coastguard Worker   *bytes_allocated = num_bytes;
57*795d594fSAndroid Build Coastguard Worker   // Use the CAS free versions as an optimization.
58*795d594fSAndroid Build Coastguard Worker   objects_allocated_.store(objects_allocated_.load(std::memory_order_relaxed) + 1,
59*795d594fSAndroid Build Coastguard Worker                            std::memory_order_relaxed);
60*795d594fSAndroid Build Coastguard Worker   bytes_allocated_.store(bytes_allocated_.load(std::memory_order_relaxed) + num_bytes,
61*795d594fSAndroid Build Coastguard Worker                          std::memory_order_relaxed);
62*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(usable_size != nullptr)) {
63*795d594fSAndroid Build Coastguard Worker     *usable_size = num_bytes;
64*795d594fSAndroid Build Coastguard Worker   }
65*795d594fSAndroid Build Coastguard Worker   *bytes_tl_bulk_allocated = num_bytes;
66*795d594fSAndroid Build Coastguard Worker   return obj;
67*795d594fSAndroid Build Coastguard Worker }
68*795d594fSAndroid Build Coastguard Worker 
AllocNonvirtualWithoutAccounting(size_t num_bytes)69*795d594fSAndroid Build Coastguard Worker inline mirror::Object* BumpPointerSpace::AllocNonvirtualWithoutAccounting(size_t num_bytes) {
70*795d594fSAndroid Build Coastguard Worker   DCHECK_ALIGNED(num_bytes, kAlignment);
71*795d594fSAndroid Build Coastguard Worker   uint8_t* old_end;
72*795d594fSAndroid Build Coastguard Worker   uint8_t* new_end;
73*795d594fSAndroid Build Coastguard Worker   do {
74*795d594fSAndroid Build Coastguard Worker     old_end = end_.load(std::memory_order_relaxed);
75*795d594fSAndroid Build Coastguard Worker     new_end = old_end + num_bytes;
76*795d594fSAndroid Build Coastguard Worker     // If there is no more room in the region, we are out of memory.
77*795d594fSAndroid Build Coastguard Worker     if (UNLIKELY(new_end > growth_end_)) {
78*795d594fSAndroid Build Coastguard Worker       return nullptr;
79*795d594fSAndroid Build Coastguard Worker     }
80*795d594fSAndroid Build Coastguard Worker   } while (!end_.CompareAndSetWeakSequentiallyConsistent(old_end, new_end));
81*795d594fSAndroid Build Coastguard Worker   return reinterpret_cast<mirror::Object*>(old_end);
82*795d594fSAndroid Build Coastguard Worker }
83*795d594fSAndroid Build Coastguard Worker 
AllocNonvirtual(size_t num_bytes)84*795d594fSAndroid Build Coastguard Worker inline mirror::Object* BumpPointerSpace::AllocNonvirtual(size_t num_bytes) {
85*795d594fSAndroid Build Coastguard Worker   mirror::Object* ret = AllocNonvirtualWithoutAccounting(num_bytes);
86*795d594fSAndroid Build Coastguard Worker   if (ret != nullptr) {
87*795d594fSAndroid Build Coastguard Worker     objects_allocated_.fetch_add(1, std::memory_order_relaxed);
88*795d594fSAndroid Build Coastguard Worker     bytes_allocated_.fetch_add(num_bytes, std::memory_order_relaxed);
89*795d594fSAndroid Build Coastguard Worker   }
90*795d594fSAndroid Build Coastguard Worker   return ret;
91*795d594fSAndroid Build Coastguard Worker }
92*795d594fSAndroid Build Coastguard Worker 
GetNextObject(mirror::Object * obj)93*795d594fSAndroid Build Coastguard Worker inline mirror::Object* BumpPointerSpace::GetNextObject(mirror::Object* obj) {
94*795d594fSAndroid Build Coastguard Worker   const uintptr_t position = reinterpret_cast<uintptr_t>(obj) + obj->SizeOf();
95*795d594fSAndroid Build Coastguard Worker   return reinterpret_cast<mirror::Object*>(RoundUp(position, kAlignment));
96*795d594fSAndroid Build Coastguard Worker }
97*795d594fSAndroid Build Coastguard Worker 
98*795d594fSAndroid Build Coastguard Worker }  // namespace space
99*795d594fSAndroid Build Coastguard Worker }  // namespace gc
100*795d594fSAndroid Build Coastguard Worker }  // namespace art
101*795d594fSAndroid Build Coastguard Worker 
102*795d594fSAndroid Build Coastguard Worker #endif  // ART_RUNTIME_GC_SPACE_BUMP_POINTER_SPACE_INL_H_
103