1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2011 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_DLMALLOC_SPACE_INL_H_
18*795d594fSAndroid Build Coastguard Worker #define ART_RUNTIME_GC_SPACE_DLMALLOC_SPACE_INL_H_
19*795d594fSAndroid Build Coastguard Worker
20*795d594fSAndroid Build Coastguard Worker #include "dlmalloc_space.h"
21*795d594fSAndroid Build Coastguard Worker #include "gc/allocator/art-dlmalloc.h"
22*795d594fSAndroid Build Coastguard Worker #include "thread.h"
23*795d594fSAndroid Build Coastguard Worker
24*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
25*795d594fSAndroid Build Coastguard Worker namespace gc {
26*795d594fSAndroid Build Coastguard Worker namespace space {
27*795d594fSAndroid Build Coastguard Worker
AllocNonvirtual(Thread * self,size_t num_bytes,size_t * bytes_allocated,size_t * usable_size,size_t * bytes_tl_bulk_allocated)28*795d594fSAndroid Build Coastguard Worker inline mirror::Object* DlMallocSpace::AllocNonvirtual(Thread* self, size_t num_bytes,
29*795d594fSAndroid Build Coastguard Worker 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 mirror::Object* obj;
33*795d594fSAndroid Build Coastguard Worker {
34*795d594fSAndroid Build Coastguard Worker MutexLock mu(self, lock_);
35*795d594fSAndroid Build Coastguard Worker obj = AllocWithoutGrowthLocked(self, num_bytes, bytes_allocated, usable_size,
36*795d594fSAndroid Build Coastguard Worker bytes_tl_bulk_allocated);
37*795d594fSAndroid Build Coastguard Worker }
38*795d594fSAndroid Build Coastguard Worker if (LIKELY(obj != nullptr)) {
39*795d594fSAndroid Build Coastguard Worker // Zero freshly allocated memory, done while not holding the space's lock.
40*795d594fSAndroid Build Coastguard Worker memset(obj, 0, num_bytes);
41*795d594fSAndroid Build Coastguard Worker }
42*795d594fSAndroid Build Coastguard Worker return obj;
43*795d594fSAndroid Build Coastguard Worker }
44*795d594fSAndroid Build Coastguard Worker
AllocationSizeNonvirtual(mirror::Object * obj,size_t * usable_size)45*795d594fSAndroid Build Coastguard Worker inline size_t DlMallocSpace::AllocationSizeNonvirtual(mirror::Object* obj, size_t* usable_size) {
46*795d594fSAndroid Build Coastguard Worker void* obj_ptr = const_cast<void*>(reinterpret_cast<const void*>(obj));
47*795d594fSAndroid Build Coastguard Worker size_t size = mspace_usable_size(obj_ptr);
48*795d594fSAndroid Build Coastguard Worker if (usable_size != nullptr) {
49*795d594fSAndroid Build Coastguard Worker *usable_size = size;
50*795d594fSAndroid Build Coastguard Worker }
51*795d594fSAndroid Build Coastguard Worker return size + kChunkOverhead;
52*795d594fSAndroid Build Coastguard Worker }
53*795d594fSAndroid Build Coastguard Worker
AllocWithoutGrowthLocked(Thread *,size_t num_bytes,size_t * bytes_allocated,size_t * usable_size,size_t * bytes_tl_bulk_allocated)54*795d594fSAndroid Build Coastguard Worker inline mirror::Object* DlMallocSpace::AllocWithoutGrowthLocked(
55*795d594fSAndroid Build Coastguard Worker Thread* /*self*/, size_t num_bytes,
56*795d594fSAndroid Build Coastguard Worker size_t* bytes_allocated,
57*795d594fSAndroid Build Coastguard Worker size_t* usable_size,
58*795d594fSAndroid Build Coastguard Worker size_t* bytes_tl_bulk_allocated) {
59*795d594fSAndroid Build Coastguard Worker mirror::Object* result = reinterpret_cast<mirror::Object*>(mspace_malloc(mspace_, num_bytes));
60*795d594fSAndroid Build Coastguard Worker if (LIKELY(result != nullptr)) {
61*795d594fSAndroid Build Coastguard Worker if (kDebugSpaces) {
62*795d594fSAndroid Build Coastguard Worker CHECK(Contains(result)) << "Allocation (" << reinterpret_cast<void*>(result)
63*795d594fSAndroid Build Coastguard Worker << ") not in bounds of allocation space " << *this;
64*795d594fSAndroid Build Coastguard Worker }
65*795d594fSAndroid Build Coastguard Worker size_t allocation_size = AllocationSizeNonvirtual(result, usable_size);
66*795d594fSAndroid Build Coastguard Worker DCHECK(bytes_allocated != nullptr);
67*795d594fSAndroid Build Coastguard Worker *bytes_allocated = allocation_size;
68*795d594fSAndroid Build Coastguard Worker *bytes_tl_bulk_allocated = allocation_size;
69*795d594fSAndroid Build Coastguard Worker }
70*795d594fSAndroid Build Coastguard Worker return result;
71*795d594fSAndroid Build Coastguard Worker }
72*795d594fSAndroid Build Coastguard Worker
73*795d594fSAndroid Build Coastguard Worker } // namespace space
74*795d594fSAndroid Build Coastguard Worker } // namespace gc
75*795d594fSAndroid Build Coastguard Worker } // namespace art
76*795d594fSAndroid Build Coastguard Worker
77*795d594fSAndroid Build Coastguard Worker #endif // ART_RUNTIME_GC_SPACE_DLMALLOC_SPACE_INL_H_
78