blob: 2263797d4a53325cf75ce5cfd7bdae4fa91e2f5d [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
Vladimir Marko80afd022015-05-19 18:08:00 +010020#include "base/bit_utils.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070021#include "bump_pointer_space.h"
22
23namespace art {
24namespace gc {
25namespace space {
26
Ian Rogers6fac4472014-02-25 17:01:10 -080027inline mirror::Object* BumpPointerSpace::Alloc(Thread*, size_t num_bytes, size_t* bytes_allocated,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070028 size_t* usable_size,
29 size_t* bytes_tl_bulk_allocated) {
Ian Rogers6fac4472014-02-25 17:01:10 -080030 num_bytes = RoundUp(num_bytes, kAlignment);
31 mirror::Object* ret = AllocNonvirtual(num_bytes);
32 if (LIKELY(ret != nullptr)) {
33 *bytes_allocated = num_bytes;
34 if (usable_size != nullptr) {
35 *usable_size = num_bytes;
36 }
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070037 *bytes_tl_bulk_allocated = num_bytes;
Ian Rogers6fac4472014-02-25 17:01:10 -080038 }
39 return ret;
40}
41
Mathieu Chartier0651d412014-04-29 14:37:57 -070042inline mirror::Object* BumpPointerSpace::AllocThreadUnsafe(Thread* self, size_t num_bytes,
43 size_t* bytes_allocated,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070044 size_t* usable_size,
45 size_t* bytes_tl_bulk_allocated) {
Mathieu Chartier0651d412014-04-29 14:37:57 -070046 Locks::mutator_lock_->AssertExclusiveHeld(self);
47 num_bytes = RoundUp(num_bytes, kAlignment);
Ian Rogers13735952014-10-08 12:43:28 -070048 uint8_t* end = end_.LoadRelaxed();
Ian Rogersbe2a1df2014-07-10 00:56:36 -070049 if (end + num_bytes > growth_end_) {
Mathieu Chartier0651d412014-04-29 14:37:57 -070050 return nullptr;
51 }
Ian Rogersbe2a1df2014-07-10 00:56:36 -070052 mirror::Object* obj = reinterpret_cast<mirror::Object*>(end);
53 end_.StoreRelaxed(end + num_bytes);
Mathieu Chartier0651d412014-04-29 14:37:57 -070054 *bytes_allocated = num_bytes;
55 // Use the CAS free versions as an optimization.
Ian Rogers3e5cf302014-05-20 16:40:37 -070056 objects_allocated_.StoreRelaxed(objects_allocated_.LoadRelaxed() + 1);
57 bytes_allocated_.StoreRelaxed(bytes_allocated_.LoadRelaxed() + num_bytes);
Mathieu Chartier0651d412014-04-29 14:37:57 -070058 if (UNLIKELY(usable_size != nullptr)) {
59 *usable_size = num_bytes;
60 }
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070061 *bytes_tl_bulk_allocated = num_bytes;
Mathieu Chartier0651d412014-04-29 14:37:57 -070062 return obj;
63}
64
Mathieu Chartier692fafd2013-11-29 17:24:40 -080065inline mirror::Object* BumpPointerSpace::AllocNonvirtualWithoutAccounting(size_t num_bytes) {
Roland Levillain14d90572015-07-16 10:52:26 +010066 DCHECK_ALIGNED(num_bytes, kAlignment);
Ian Rogers13735952014-10-08 12:43:28 -070067 uint8_t* old_end;
68 uint8_t* new_end;
Mathieu Chartier590fee92013-09-13 13:46:47 -070069 do {
Ian Rogersbe2a1df2014-07-10 00:56:36 -070070 old_end = end_.LoadRelaxed();
Mathieu Chartier590fee92013-09-13 13:46:47 -070071 new_end = old_end + num_bytes;
72 // If there is no more room in the region, we are out of memory.
73 if (UNLIKELY(new_end > growth_end_)) {
74 return nullptr;
75 }
Ian Rogersbe2a1df2014-07-10 00:56:36 -070076 } while (!end_.CompareExchangeWeakSequentiallyConsistent(old_end, new_end));
Mathieu Chartier590fee92013-09-13 13:46:47 -070077 return reinterpret_cast<mirror::Object*>(old_end);
78}
79
Mathieu Chartier692fafd2013-11-29 17:24:40 -080080inline mirror::Object* BumpPointerSpace::AllocNonvirtual(size_t num_bytes) {
81 mirror::Object* ret = AllocNonvirtualWithoutAccounting(num_bytes);
82 if (ret != nullptr) {
Ian Rogers3e5cf302014-05-20 16:40:37 -070083 objects_allocated_.FetchAndAddSequentiallyConsistent(1);
84 bytes_allocated_.FetchAndAddSequentiallyConsistent(num_bytes);
Mathieu Chartier692fafd2013-11-29 17:24:40 -080085 }
86 return ret;
87}
88
Ian Rogers6fac4472014-02-25 17:01:10 -080089inline size_t BumpPointerSpace::AllocationSizeNonvirtual(mirror::Object* obj, size_t* usable_size)
Mathieu Chartier90443472015-07-16 20:32:27 -070090 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers6fac4472014-02-25 17:01:10 -080091 size_t num_bytes = obj->SizeOf();
92 if (usable_size != nullptr) {
93 *usable_size = RoundUp(num_bytes, kAlignment);
94 }
95 return num_bytes;
96}
97
Mathieu Chartier590fee92013-09-13 13:46:47 -070098} // namespace space
99} // namespace gc
100} // namespace art
101
102#endif // ART_RUNTIME_GC_SPACE_BUMP_POINTER_SPACE_INL_H_