xref: /aosp_15_r20/art/runtime/gc/space/rosalloc_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_ROSALLOC_SPACE_INL_H_
18*795d594fSAndroid Build Coastguard Worker #define ART_RUNTIME_GC_SPACE_ROSALLOC_SPACE_INL_H_
19*795d594fSAndroid Build Coastguard Worker 
20*795d594fSAndroid Build Coastguard Worker #include "rosalloc_space.h"
21*795d594fSAndroid Build Coastguard Worker 
22*795d594fSAndroid Build Coastguard Worker #include "base/memory_tool.h"
23*795d594fSAndroid Build Coastguard Worker #include "gc/allocator/rosalloc-inl.h"
24*795d594fSAndroid Build Coastguard Worker #include "gc/space/memory_tool_settings.h"
25*795d594fSAndroid Build Coastguard Worker #include "thread.h"
26*795d594fSAndroid Build Coastguard Worker 
27*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
28*795d594fSAndroid Build Coastguard Worker namespace gc {
29*795d594fSAndroid Build Coastguard Worker namespace space {
30*795d594fSAndroid Build Coastguard Worker 
31*795d594fSAndroid Build Coastguard Worker template<bool kThreadSafe>
AllocCommon(Thread * self,size_t num_bytes,size_t * bytes_allocated,size_t * usable_size,size_t * bytes_tl_bulk_allocated)32*795d594fSAndroid Build Coastguard Worker inline mirror::Object* RosAllocSpace::AllocCommon(Thread* self, size_t num_bytes,
33*795d594fSAndroid Build Coastguard Worker                                                   size_t* bytes_allocated, size_t* usable_size,
34*795d594fSAndroid Build Coastguard Worker                                                   size_t* bytes_tl_bulk_allocated) {
35*795d594fSAndroid Build Coastguard Worker   size_t rosalloc_bytes_allocated = 0;
36*795d594fSAndroid Build Coastguard Worker   size_t rosalloc_usable_size = 0;
37*795d594fSAndroid Build Coastguard Worker   size_t rosalloc_bytes_tl_bulk_allocated = 0;
38*795d594fSAndroid Build Coastguard Worker   if (!kThreadSafe) {
39*795d594fSAndroid Build Coastguard Worker     Locks::mutator_lock_->AssertExclusiveHeld(self);
40*795d594fSAndroid Build Coastguard Worker   }
41*795d594fSAndroid Build Coastguard Worker   mirror::Object* result = reinterpret_cast<mirror::Object*>(
42*795d594fSAndroid Build Coastguard Worker       rosalloc_->Alloc<kThreadSafe>(self, num_bytes, &rosalloc_bytes_allocated,
43*795d594fSAndroid Build Coastguard Worker                                     &rosalloc_usable_size,
44*795d594fSAndroid Build Coastguard Worker                                     &rosalloc_bytes_tl_bulk_allocated));
45*795d594fSAndroid Build Coastguard Worker   if (LIKELY(result != nullptr)) {
46*795d594fSAndroid Build Coastguard Worker     if (kDebugSpaces) {
47*795d594fSAndroid Build Coastguard Worker       CHECK(Contains(result)) << "Allocation (" << reinterpret_cast<void*>(result)
48*795d594fSAndroid Build Coastguard Worker             << ") not in bounds of allocation space " << *this;
49*795d594fSAndroid Build Coastguard Worker     }
50*795d594fSAndroid Build Coastguard Worker     DCHECK(bytes_allocated != nullptr);
51*795d594fSAndroid Build Coastguard Worker     *bytes_allocated = rosalloc_bytes_allocated;
52*795d594fSAndroid Build Coastguard Worker     DCHECK_EQ(rosalloc_usable_size, rosalloc_->UsableSize(result));
53*795d594fSAndroid Build Coastguard Worker     if (usable_size != nullptr) {
54*795d594fSAndroid Build Coastguard Worker       *usable_size = rosalloc_usable_size;
55*795d594fSAndroid Build Coastguard Worker     }
56*795d594fSAndroid Build Coastguard Worker     DCHECK(bytes_tl_bulk_allocated != nullptr);
57*795d594fSAndroid Build Coastguard Worker     *bytes_tl_bulk_allocated = rosalloc_bytes_tl_bulk_allocated;
58*795d594fSAndroid Build Coastguard Worker   }
59*795d594fSAndroid Build Coastguard Worker   return result;
60*795d594fSAndroid Build Coastguard Worker }
61*795d594fSAndroid Build Coastguard Worker 
CanAllocThreadLocal(Thread * self,size_t num_bytes)62*795d594fSAndroid Build Coastguard Worker inline bool RosAllocSpace::CanAllocThreadLocal(Thread* self, size_t num_bytes) {
63*795d594fSAndroid Build Coastguard Worker   return rosalloc_->CanAllocFromThreadLocalRun(self, num_bytes);
64*795d594fSAndroid Build Coastguard Worker }
65*795d594fSAndroid Build Coastguard Worker 
AllocThreadLocal(Thread * self,size_t num_bytes,size_t * bytes_allocated)66*795d594fSAndroid Build Coastguard Worker inline mirror::Object* RosAllocSpace::AllocThreadLocal(Thread* self, size_t num_bytes,
67*795d594fSAndroid Build Coastguard Worker                                                        size_t* bytes_allocated) {
68*795d594fSAndroid Build Coastguard Worker   DCHECK(bytes_allocated != nullptr);
69*795d594fSAndroid Build Coastguard Worker   return reinterpret_cast<mirror::Object*>(
70*795d594fSAndroid Build Coastguard Worker       rosalloc_->AllocFromThreadLocalRun(self, num_bytes, bytes_allocated));
71*795d594fSAndroid Build Coastguard Worker }
72*795d594fSAndroid Build Coastguard Worker 
MaxBytesBulkAllocatedForNonvirtual(size_t num_bytes)73*795d594fSAndroid Build Coastguard Worker inline size_t RosAllocSpace::MaxBytesBulkAllocatedForNonvirtual(size_t num_bytes) {
74*795d594fSAndroid Build Coastguard Worker   return rosalloc_->MaxBytesBulkAllocatedFor(num_bytes);
75*795d594fSAndroid Build Coastguard Worker }
76*795d594fSAndroid Build Coastguard Worker 
77*795d594fSAndroid Build Coastguard Worker }  // namespace space
78*795d594fSAndroid Build Coastguard Worker }  // namespace gc
79*795d594fSAndroid Build Coastguard Worker }  // namespace art
80*795d594fSAndroid Build Coastguard Worker 
81*795d594fSAndroid Build Coastguard Worker #endif  // ART_RUNTIME_GC_SPACE_ROSALLOC_SPACE_INL_H_
82