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