Hiroshi Yamauchi | 50b2928 | 2013-07-30 13:58:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | #ifndef ART_RUNTIME_GC_SPACE_DLMALLOC_SPACE_INL_H_ |
| 18 | #define ART_RUNTIME_GC_SPACE_DLMALLOC_SPACE_INL_H_ |
| 19 | |
| 20 | #include "dlmalloc_space.h" |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 21 | #include "gc/allocator/dlmalloc.h" |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 22 | #include "thread.h" |
Hiroshi Yamauchi | 50b2928 | 2013-07-30 13:58:37 -0700 | [diff] [blame] | 23 | |
| 24 | namespace art { |
| 25 | namespace gc { |
| 26 | namespace space { |
| 27 | |
| 28 | inline mirror::Object* DlMallocSpace::AllocNonvirtual(Thread* self, size_t num_bytes, |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 29 | size_t* bytes_allocated, |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 30 | size_t* usable_size, |
| 31 | size_t* bytes_tl_bulk_allocated) { |
Hiroshi Yamauchi | 50b2928 | 2013-07-30 13:58:37 -0700 | [diff] [blame] | 32 | mirror::Object* obj; |
| 33 | { |
| 34 | MutexLock mu(self, lock_); |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 35 | obj = AllocWithoutGrowthLocked(self, num_bytes, bytes_allocated, usable_size, |
| 36 | bytes_tl_bulk_allocated); |
Hiroshi Yamauchi | 50b2928 | 2013-07-30 13:58:37 -0700 | [diff] [blame] | 37 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 38 | if (LIKELY(obj != nullptr)) { |
Hiroshi Yamauchi | 50b2928 | 2013-07-30 13:58:37 -0700 | [diff] [blame] | 39 | // Zero freshly allocated memory, done while not holding the space's lock. |
| 40 | memset(obj, 0, num_bytes); |
| 41 | } |
| 42 | return obj; |
| 43 | } |
| 44 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 45 | inline size_t DlMallocSpace::AllocationSizeNonvirtual(mirror::Object* obj, size_t* usable_size) { |
| 46 | void* obj_ptr = const_cast<void*>(reinterpret_cast<const void*>(obj)); |
| 47 | size_t size = mspace_usable_size(obj_ptr); |
| 48 | if (usable_size != nullptr) { |
| 49 | *usable_size = size; |
| 50 | } |
| 51 | return size + kChunkOverhead; |
| 52 | } |
| 53 | |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 54 | inline mirror::Object* DlMallocSpace::AllocWithoutGrowthLocked( |
| 55 | Thread* /*self*/, size_t num_bytes, |
| 56 | size_t* bytes_allocated, |
| 57 | size_t* usable_size, |
| 58 | size_t* bytes_tl_bulk_allocated) { |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 59 | mirror::Object* result = reinterpret_cast<mirror::Object*>(mspace_malloc(mspace_, num_bytes)); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 60 | if (LIKELY(result != nullptr)) { |
Hiroshi Yamauchi | 50b2928 | 2013-07-30 13:58:37 -0700 | [diff] [blame] | 61 | if (kDebugSpaces) { |
| 62 | CHECK(Contains(result)) << "Allocation (" << reinterpret_cast<void*>(result) |
| 63 | << ") not in bounds of allocation space " << *this; |
| 64 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 65 | size_t allocation_size = AllocationSizeNonvirtual(result, usable_size); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 66 | DCHECK(bytes_allocated != nullptr); |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 67 | *bytes_allocated = allocation_size; |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 68 | *bytes_tl_bulk_allocated = allocation_size; |
Hiroshi Yamauchi | 50b2928 | 2013-07-30 13:58:37 -0700 | [diff] [blame] | 69 | } |
| 70 | return result; |
| 71 | } |
| 72 | |
| 73 | } // namespace space |
| 74 | } // namespace gc |
| 75 | } // namespace art |
| 76 | |
| 77 | #endif // ART_RUNTIME_GC_SPACE_DLMALLOC_SPACE_INL_H_ |