blob: ee3c979b9a3673553bc1b461074e8e07d565a6f1 [file] [log] [blame]
Mathieu Chartier590fee92013-09-13 13:46:47 -07001/*
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
22namespace art {
23namespace gc {
24namespace space {
25
Ian Rogers6fac4472014-02-25 17:01:10 -080026inline mirror::Object* BumpPointerSpace::Alloc(Thread*, size_t num_bytes, size_t* bytes_allocated,
27 size_t* usable_size) {
28 num_bytes = RoundUp(num_bytes, kAlignment);
29 mirror::Object* ret = AllocNonvirtual(num_bytes);
30 if (LIKELY(ret != nullptr)) {
31 *bytes_allocated = num_bytes;
32 if (usable_size != nullptr) {
33 *usable_size = num_bytes;
34 }
35 }
36 return ret;
37}
38
Mathieu Chartier0651d412014-04-29 14:37:57 -070039inline mirror::Object* BumpPointerSpace::AllocThreadUnsafe(Thread* self, size_t num_bytes,
40 size_t* bytes_allocated,
41 size_t* usable_size) {
42 Locks::mutator_lock_->AssertExclusiveHeld(self);
43 num_bytes = RoundUp(num_bytes, kAlignment);
Ian Rogersbe2a1df2014-07-10 00:56:36 -070044 byte* end = end_.LoadRelaxed();
45 if (end + num_bytes > growth_end_) {
Mathieu Chartier0651d412014-04-29 14:37:57 -070046 return nullptr;
47 }
Ian Rogersbe2a1df2014-07-10 00:56:36 -070048 mirror::Object* obj = reinterpret_cast<mirror::Object*>(end);
49 end_.StoreRelaxed(end + num_bytes);
Mathieu Chartier0651d412014-04-29 14:37:57 -070050 *bytes_allocated = num_bytes;
51 // Use the CAS free versions as an optimization.
Ian Rogers3e5cf302014-05-20 16:40:37 -070052 objects_allocated_.StoreRelaxed(objects_allocated_.LoadRelaxed() + 1);
53 bytes_allocated_.StoreRelaxed(bytes_allocated_.LoadRelaxed() + num_bytes);
Mathieu Chartier0651d412014-04-29 14:37:57 -070054 if (UNLIKELY(usable_size != nullptr)) {
55 *usable_size = num_bytes;
56 }
57 return obj;
58}
59
Mathieu Chartier692fafd2013-11-29 17:24:40 -080060inline mirror::Object* BumpPointerSpace::AllocNonvirtualWithoutAccounting(size_t num_bytes) {
61 DCHECK(IsAligned<kAlignment>(num_bytes));
Mathieu Chartier590fee92013-09-13 13:46:47 -070062 byte* old_end;
63 byte* new_end;
64 do {
Ian Rogersbe2a1df2014-07-10 00:56:36 -070065 old_end = end_.LoadRelaxed();
Mathieu Chartier590fee92013-09-13 13:46:47 -070066 new_end = old_end + num_bytes;
67 // If there is no more room in the region, we are out of memory.
68 if (UNLIKELY(new_end > growth_end_)) {
69 return nullptr;
70 }
Ian Rogersbe2a1df2014-07-10 00:56:36 -070071 } while (!end_.CompareExchangeWeakSequentiallyConsistent(old_end, new_end));
Mathieu Chartier590fee92013-09-13 13:46:47 -070072 return reinterpret_cast<mirror::Object*>(old_end);
73}
74
Mathieu Chartier692fafd2013-11-29 17:24:40 -080075inline mirror::Object* BumpPointerSpace::AllocNonvirtual(size_t num_bytes) {
76 mirror::Object* ret = AllocNonvirtualWithoutAccounting(num_bytes);
77 if (ret != nullptr) {
Ian Rogers3e5cf302014-05-20 16:40:37 -070078 objects_allocated_.FetchAndAddSequentiallyConsistent(1);
79 bytes_allocated_.FetchAndAddSequentiallyConsistent(num_bytes);
Mathieu Chartier692fafd2013-11-29 17:24:40 -080080 }
81 return ret;
82}
83
Ian Rogers6fac4472014-02-25 17:01:10 -080084inline size_t BumpPointerSpace::AllocationSizeNonvirtual(mirror::Object* obj, size_t* usable_size)
85 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
86 size_t num_bytes = obj->SizeOf();
87 if (usable_size != nullptr) {
88 *usable_size = RoundUp(num_bytes, kAlignment);
89 }
90 return num_bytes;
91}
92
Mathieu Chartier590fee92013-09-13 13:46:47 -070093} // namespace space
94} // namespace gc
95} // namespace art
96
97#endif // ART_RUNTIME_GC_SPACE_BUMP_POINTER_SPACE_INL_H_