Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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_BUMP_POINTER_SPACE_INL_H_ |
| 18 | #define ART_RUNTIME_GC_SPACE_BUMP_POINTER_SPACE_INL_H_ |
| 19 | |
| 20 | #include "bump_pointer_space.h" |
| 21 | |
| 22 | namespace art { |
| 23 | namespace gc { |
| 24 | namespace space { |
| 25 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 26 | inline mirror::Object* BumpPointerSpace::Alloc(Thread*, size_t num_bytes, size_t* bytes_allocated, |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 27 | size_t* usable_size, |
| 28 | size_t* bytes_tl_bulk_allocated) { |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 29 | num_bytes = RoundUp(num_bytes, kAlignment); |
| 30 | mirror::Object* ret = AllocNonvirtual(num_bytes); |
| 31 | if (LIKELY(ret != nullptr)) { |
| 32 | *bytes_allocated = num_bytes; |
| 33 | if (usable_size != nullptr) { |
| 34 | *usable_size = num_bytes; |
| 35 | } |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 36 | *bytes_tl_bulk_allocated = num_bytes; |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 37 | } |
| 38 | return ret; |
| 39 | } |
| 40 | |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 41 | inline mirror::Object* BumpPointerSpace::AllocThreadUnsafe(Thread* self, size_t num_bytes, |
| 42 | size_t* bytes_allocated, |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 43 | size_t* usable_size, |
| 44 | size_t* bytes_tl_bulk_allocated) { |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 45 | Locks::mutator_lock_->AssertExclusiveHeld(self); |
| 46 | num_bytes = RoundUp(num_bytes, kAlignment); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 47 | uint8_t* end = end_.LoadRelaxed(); |
Ian Rogers | be2a1df | 2014-07-10 00:56:36 -0700 | [diff] [blame] | 48 | if (end + num_bytes > growth_end_) { |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 49 | return nullptr; |
| 50 | } |
Ian Rogers | be2a1df | 2014-07-10 00:56:36 -0700 | [diff] [blame] | 51 | mirror::Object* obj = reinterpret_cast<mirror::Object*>(end); |
| 52 | end_.StoreRelaxed(end + num_bytes); |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 53 | *bytes_allocated = num_bytes; |
| 54 | // Use the CAS free versions as an optimization. |
Ian Rogers | 3e5cf30 | 2014-05-20 16:40:37 -0700 | [diff] [blame] | 55 | objects_allocated_.StoreRelaxed(objects_allocated_.LoadRelaxed() + 1); |
| 56 | bytes_allocated_.StoreRelaxed(bytes_allocated_.LoadRelaxed() + num_bytes); |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 57 | if (UNLIKELY(usable_size != nullptr)) { |
| 58 | *usable_size = num_bytes; |
| 59 | } |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 60 | *bytes_tl_bulk_allocated = num_bytes; |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 61 | return obj; |
| 62 | } |
| 63 | |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 64 | inline mirror::Object* BumpPointerSpace::AllocNonvirtualWithoutAccounting(size_t num_bytes) { |
| 65 | DCHECK(IsAligned<kAlignment>(num_bytes)); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 66 | uint8_t* old_end; |
| 67 | uint8_t* new_end; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 68 | do { |
Ian Rogers | be2a1df | 2014-07-10 00:56:36 -0700 | [diff] [blame] | 69 | old_end = end_.LoadRelaxed(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 70 | new_end = old_end + num_bytes; |
| 71 | // If there is no more room in the region, we are out of memory. |
| 72 | if (UNLIKELY(new_end > growth_end_)) { |
| 73 | return nullptr; |
| 74 | } |
Ian Rogers | be2a1df | 2014-07-10 00:56:36 -0700 | [diff] [blame] | 75 | } while (!end_.CompareExchangeWeakSequentiallyConsistent(old_end, new_end)); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 76 | return reinterpret_cast<mirror::Object*>(old_end); |
| 77 | } |
| 78 | |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 79 | inline mirror::Object* BumpPointerSpace::AllocNonvirtual(size_t num_bytes) { |
| 80 | mirror::Object* ret = AllocNonvirtualWithoutAccounting(num_bytes); |
| 81 | if (ret != nullptr) { |
Ian Rogers | 3e5cf30 | 2014-05-20 16:40:37 -0700 | [diff] [blame] | 82 | objects_allocated_.FetchAndAddSequentiallyConsistent(1); |
| 83 | bytes_allocated_.FetchAndAddSequentiallyConsistent(num_bytes); |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 84 | } |
| 85 | return ret; |
| 86 | } |
| 87 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 88 | inline size_t BumpPointerSpace::AllocationSizeNonvirtual(mirror::Object* obj, size_t* usable_size) |
| 89 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 90 | size_t num_bytes = obj->SizeOf(); |
| 91 | if (usable_size != nullptr) { |
| 92 | *usable_size = RoundUp(num_bytes, kAlignment); |
| 93 | } |
| 94 | return num_bytes; |
| 95 | } |
| 96 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 97 | } // namespace space |
| 98 | } // namespace gc |
| 99 | } // namespace art |
| 100 | |
| 101 | #endif // ART_RUNTIME_GC_SPACE_BUMP_POINTER_SPACE_INL_H_ |