blob: 1c09b5c9bf13915a9a9fc5d08f6f8ac73685812a [file] [log] [blame]
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -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_HEAP_INL_H_
18#define ART_RUNTIME_GC_HEAP_INL_H_
19
20#include "heap.h"
21
Andreas Gampe27fa96c2016-10-07 15:05:24 -070022#include "allocation_listener.h"
David Sehrc431b9d2018-03-02 12:01:51 -080023#include "base/quasi_atomic.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010024#include "base/time_utils.h"
Andreas Gamped4901292017-05-30 18:41:34 -070025#include "gc/accounting/atomic_stack.h"
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -080026#include "gc/accounting/card_table-inl.h"
Man Cao8c2ff642015-05-27 17:25:30 -070027#include "gc/allocation_record.h"
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -080028#include "gc/collector/semi_space.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070029#include "gc/space/bump_pointer_space-inl.h"
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070030#include "gc/space/dlmalloc_space-inl.h"
31#include "gc/space/large_object_space.h"
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080032#include "gc/space/region_space-inl.h"
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070033#include "gc/space/rosalloc_space-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070034#include "handle_scope-inl.h"
Mathieu Chartiera058fdf2016-10-06 15:13:58 -070035#include "obj_ptr-inl.h"
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070036#include "runtime.h"
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070037#include "thread-inl.h"
Andreas Gampe90b936d2017-01-31 08:58:55 -080038#include "verify_object.h"
Mathieu Chartier88ea61e2018-06-20 17:45:41 -070039#include "write_barrier-inl.h"
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070040
41namespace art {
42namespace gc {
43
Mathieu Chartier692fafd2013-11-29 17:24:40 -080044template <bool kInstrumented, bool kCheckLargeObject, typename PreFenceVisitor>
Mathieu Chartiera4f6af92015-08-11 17:35:25 -070045inline mirror::Object* Heap::AllocObjectWithAllocator(Thread* self,
Mathieu Chartier9d156d52016-10-06 17:44:26 -070046 ObjPtr<mirror::Class> klass,
Mathieu Chartiera4f6af92015-08-11 17:35:25 -070047 size_t byte_count,
48 AllocatorType allocator,
Mathieu Chartier1febddf2013-11-20 12:33:14 -080049 const PreFenceVisitor& pre_fence_visitor) {
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080050 if (kIsDebugBuild) {
51 CheckPreconditionsForAllocObject(klass, byte_count);
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070052 // Since allocation can cause a GC which will need to SuspendAll, make sure all allocations are
53 // done in the runnable state where suspension is expected.
54 CHECK_EQ(self->GetState(), kRunnable);
55 self->AssertThreadSuspensionIsAllowable();
Mathieu Chartier8502f722016-06-08 15:09:08 -070056 self->AssertNoPendingException();
Mathieu Chartier9d156d52016-10-06 17:44:26 -070057 // Make sure to preserve klass.
58 StackHandleScope<1> hs(self);
59 HandleWrapperObjPtr<mirror::Class> h = hs.NewHandleWrapper(&klass);
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070060 self->PoisonObjectPointers();
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080061 }
Roland Levillainb81e9e92017-04-20 17:35:32 +010062 // Need to check that we aren't the large object allocator since the large object allocation code
63 // path includes this function. If we didn't check we would have an infinite loop.
Mathieu Chartier9d156d52016-10-06 17:44:26 -070064 ObjPtr<mirror::Object> obj;
Mathieu Chartier446f9ee2014-12-01 15:00:27 -080065 if (kCheckLargeObject && UNLIKELY(ShouldAllocLargeObject(klass, byte_count))) {
66 obj = AllocLargeObject<kInstrumented, PreFenceVisitor>(self, &klass, byte_count,
67 pre_fence_visitor);
68 if (obj != nullptr) {
Mathieu Chartier9d156d52016-10-06 17:44:26 -070069 return obj.Ptr();
Mathieu Chartier446f9ee2014-12-01 15:00:27 -080070 } else {
71 // There should be an OOM exception, since we are retrying, clear it.
72 self->ClearException();
73 }
74 // If the large object allocation failed, try to use the normal spaces (main space,
75 // non moving space). This can happen if there is significant virtual address space
76 // fragmentation.
77 }
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070078 // bytes allocated for the (individual) object.
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070079 size_t bytes_allocated;
80 size_t usable_size;
81 size_t new_num_bytes_allocated = 0;
Mathieu Chartier6bc77742017-04-18 17:46:23 -070082 if (IsTLABAllocator(allocator)) {
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070083 byte_count = RoundUp(byte_count, space::BumpPointerSpace::kAlignment);
84 }
85 // If we have a thread local allocation we don't need to update bytes allocated.
Mathieu Chartier6bc77742017-04-18 17:46:23 -070086 if (IsTLABAllocator(allocator) && byte_count <= self->TlabSize()) {
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070087 obj = self->AllocTlab(byte_count);
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -070088 DCHECK(obj != nullptr) << "AllocTlab can't fail";
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070089 obj->SetClass(klass);
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -070090 if (kUseBakerReadBarrier) {
91 obj->AssertReadBarrierState();
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080092 }
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070093 bytes_allocated = byte_count;
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -070094 usable_size = bytes_allocated;
95 pre_fence_visitor(obj, usable_size);
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070096 QuasiAtomic::ThreadFenceForConstructor();
Mathieu Chartier9d156d52016-10-06 17:44:26 -070097 } else if (
98 !kInstrumented && allocator == kAllocatorTypeRosAlloc &&
99 (obj = rosalloc_space_->AllocThreadLocal(self, byte_count, &bytes_allocated)) != nullptr &&
100 LIKELY(obj != nullptr)) {
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700101 DCHECK(!is_running_on_memory_tool_);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700102 obj->SetClass(klass);
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -0700103 if (kUseBakerReadBarrier) {
104 obj->AssertReadBarrierState();
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700105 }
106 usable_size = bytes_allocated;
107 pre_fence_visitor(obj, usable_size);
108 QuasiAtomic::ThreadFenceForConstructor();
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700109 } else {
Hans Boehmfb8b4e22018-09-05 16:45:42 -0700110 // Bytes allocated that includes bulk thread-local buffer allocations in addition to direct
111 // non-TLAB object allocations.
Roland Levillain9b869ea2018-01-31 13:46:11 +0000112 size_t bytes_tl_bulk_allocated = 0u;
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700113 obj = TryToAllocate<kInstrumented, false>(self, allocator, byte_count, &bytes_allocated,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700114 &usable_size, &bytes_tl_bulk_allocated);
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700115 if (UNLIKELY(obj == nullptr)) {
Mathieu Chartiereebc3af2016-02-29 18:13:38 -0800116 // AllocateInternalWithGc can cause thread suspension, if someone instruments the entrypoints
117 // or changes the allocator in a suspend point here, we need to retry the allocation.
118 obj = AllocateInternalWithGc(self,
119 allocator,
120 kInstrumented,
121 byte_count,
122 &bytes_allocated,
123 &usable_size,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700124 &bytes_tl_bulk_allocated, &klass);
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700125 if (obj == nullptr) {
Mathieu Chartiereebc3af2016-02-29 18:13:38 -0800126 // The only way that we can get a null return if there is no pending exception is if the
127 // allocator or instrumentation changed.
128 if (!self->IsExceptionPending()) {
129 // AllocObject will pick up the new allocator type, and instrumented as true is the safe
130 // default.
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700131 return AllocObject</*kInstrumented=*/true>(self,
132 klass,
133 byte_count,
134 pre_fence_visitor);
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700135 }
136 return nullptr;
137 }
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -0700138 }
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700139 DCHECK_GT(bytes_allocated, 0u);
140 DCHECK_GT(usable_size, 0u);
141 obj->SetClass(klass);
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -0700142 if (kUseBakerReadBarrier) {
143 obj->AssertReadBarrierState();
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700144 }
145 if (collector::SemiSpace::kUseRememberedSet && UNLIKELY(allocator == kAllocatorTypeNonMoving)) {
146 // (Note this if statement will be constant folded away for the
147 // fast-path quick entry points.) Because SetClass() has no write
148 // barrier, if a non-moving space allocation, we need a write
149 // barrier as the class pointer may point to the bump pointer
150 // space (where the class pointer is an "old-to-young" reference,
151 // though rare) under the GSS collector with the remembered set
152 // enabled. We don't need this for kAllocatorTypeRosAlloc/DlMalloc
153 // cases because we don't directly allocate into the main alloc
154 // space (besides promotions) under the SS/GSS collector.
Mathieu Chartier88ea61e2018-06-20 17:45:41 -0700155 WriteBarrier::ForFieldWrite(obj, mirror::Object::ClassOffset(), klass);
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700156 }
157 pre_fence_visitor(obj, usable_size);
Hans Boehmb0171b92016-01-28 17:19:15 -0800158 QuasiAtomic::ThreadFenceForConstructor();
Mathieu Chartier34afcde2017-06-30 15:31:11 -0700159 if (bytes_tl_bulk_allocated > 0) {
Hans Boehmfb8b4e22018-09-05 16:45:42 -0700160 size_t num_bytes_allocated_before =
161 num_bytes_allocated_.fetch_add(bytes_tl_bulk_allocated, std::memory_order_relaxed);
162 new_num_bytes_allocated = num_bytes_allocated_before + bytes_tl_bulk_allocated;
Mathieu Chartier34afcde2017-06-30 15:31:11 -0700163 // Only trace when we get an increase in the number of bytes allocated. This happens when
164 // obtaining a new TLAB and isn't often enough to hurt performance according to golem.
Roland Levillain9b869ea2018-01-31 13:46:11 +0000165 TraceHeapSize(new_num_bytes_allocated);
Mathieu Chartier34afcde2017-06-30 15:31:11 -0700166 }
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800167 }
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -0700168 if (kIsDebugBuild && Runtime::Current()->IsStarted()) {
169 CHECK_LE(obj->SizeOf(), usable_size);
170 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800171 // TODO: Deprecate.
172 if (kInstrumented) {
173 if (Runtime::Current()->HasStatsEnabled()) {
174 RuntimeStats* thread_stats = self->GetStats();
175 ++thread_stats->allocated_objects;
176 thread_stats->allocated_bytes += bytes_allocated;
177 RuntimeStats* global_stats = Runtime::Current()->GetStats();
178 ++global_stats->allocated_objects;
179 global_stats->allocated_bytes += bytes_allocated;
180 }
181 } else {
182 DCHECK(!Runtime::Current()->HasStatsEnabled());
183 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800184 if (kInstrumented) {
Man Cao8c2ff642015-05-27 17:25:30 -0700185 if (IsAllocTrackingEnabled()) {
Mathieu Chartier458b1052016-03-29 14:02:55 -0700186 // allocation_records_ is not null since it never becomes null after allocation tracking is
187 // enabled.
188 DCHECK(allocation_records_ != nullptr);
189 allocation_records_->RecordAllocation(self, &obj, bytes_allocated);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800190 }
Orion Hodson88591fe2018-03-06 13:35:43 +0000191 AllocationListener* l = alloc_listener_.load(std::memory_order_seq_cst);
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700192 if (l != nullptr) {
193 // Same as above. We assume that a listener that was once stored will never be deleted.
194 // Otherwise we'd have to perform this under a lock.
195 l->ObjectAllocated(self, &obj, bytes_allocated);
196 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800197 } else {
Man Cao8c2ff642015-05-27 17:25:30 -0700198 DCHECK(!IsAllocTrackingEnabled());
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800199 }
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800200 if (AllocatorHasAllocationStack(allocator)) {
201 PushOnAllocationStack(self, &obj);
202 }
Mathieu Chartier31000802015-06-14 14:14:37 -0700203 if (kInstrumented) {
204 if (gc_stress_mode_) {
205 CheckGcStressMode(self, &obj);
206 }
207 } else {
208 DCHECK(!gc_stress_mode_);
209 }
Roland Levillainb81e9e92017-04-20 17:35:32 +0100210 // IsGcConcurrent() isn't known at compile time so we can optimize by not checking it for
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800211 // the BumpPointer or TLAB allocators. This is nice since it allows the entire if statement to be
212 // optimized out. And for the other allocators, AllocatorMayHaveConcurrentGC is a constant since
213 // the allocator_type should be constant propagated.
Hiroshi Yamauchi3e417802014-03-20 12:03:02 -0700214 if (AllocatorMayHaveConcurrentGC(allocator) && IsGcConcurrent()) {
Hans Boehmfb8b4e22018-09-05 16:45:42 -0700215 // New_num_bytes_allocated is zero if we didn't update num_bytes_allocated_.
216 // That's fine.
Hans Boehmc220f982018-10-12 16:15:45 -0700217 CheckConcurrentGCForJava(self, new_num_bytes_allocated, &obj);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800218 }
Mathieu Chartier4e305412014-02-19 10:54:44 -0800219 VerifyObject(obj);
220 self->VerifyStack();
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700221 return obj.Ptr();
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700222}
223
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800224// The size of a thread-local allocation stack in the number of references.
225static constexpr size_t kThreadLocalAllocationStackSize = 128;
226
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700227inline void Heap::PushOnAllocationStack(Thread* self, ObjPtr<mirror::Object>* obj) {
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800228 if (kUseThreadLocalAllocationStack) {
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700229 if (UNLIKELY(!self->PushOnThreadLocalAllocationStack(obj->Ptr()))) {
Mathieu Chartierc1790162014-05-23 10:54:50 -0700230 PushOnThreadLocalAllocationStackWithInternalGC(self, obj);
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800231 }
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700232 } else if (UNLIKELY(!allocation_stack_->AtomicPushBack(obj->Ptr()))) {
Mathieu Chartierc1790162014-05-23 10:54:50 -0700233 PushOnAllocationStackWithInternalGC(self, obj);
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800234 }
235}
236
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800237template <bool kInstrumented, typename PreFenceVisitor>
Mathieu Chartiera4f6af92015-08-11 17:35:25 -0700238inline mirror::Object* Heap::AllocLargeObject(Thread* self,
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700239 ObjPtr<mirror::Class>* klass,
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800240 size_t byte_count,
241 const PreFenceVisitor& pre_fence_visitor) {
Mathieu Chartier446f9ee2014-12-01 15:00:27 -0800242 // Save and restore the class in case it moves.
243 StackHandleScope<1> hs(self);
244 auto klass_wrapper = hs.NewHandleWrapper(klass);
245 return AllocObjectWithAllocator<kInstrumented, false, PreFenceVisitor>(self, *klass, byte_count,
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800246 kAllocatorTypeLOS,
247 pre_fence_visitor);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800248}
249
250template <const bool kInstrumented, const bool kGrow>
Mathieu Chartiera4f6af92015-08-11 17:35:25 -0700251inline mirror::Object* Heap::TryToAllocate(Thread* self,
252 AllocatorType allocator_type,
253 size_t alloc_size,
254 size_t* bytes_allocated,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700255 size_t* usable_size,
256 size_t* bytes_tl_bulk_allocated) {
Hans Boehmc220f982018-10-12 16:15:45 -0700257 if (allocator_type != kAllocatorTypeRegionTLAB &&
258 allocator_type != kAllocatorTypeTLAB &&
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700259 allocator_type != kAllocatorTypeRosAlloc &&
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800260 UNLIKELY(IsOutOfMemoryOnAllocation(allocator_type, alloc_size, kGrow))) {
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800261 return nullptr;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700262 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800263 mirror::Object* ret;
264 switch (allocator_type) {
265 case kAllocatorTypeBumpPointer: {
266 DCHECK(bump_pointer_space_ != nullptr);
267 alloc_size = RoundUp(alloc_size, space::BumpPointerSpace::kAlignment);
268 ret = bump_pointer_space_->AllocNonvirtual(alloc_size);
269 if (LIKELY(ret != nullptr)) {
270 *bytes_allocated = alloc_size;
Ian Rogers6fac4472014-02-25 17:01:10 -0800271 *usable_size = alloc_size;
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700272 *bytes_tl_bulk_allocated = alloc_size;
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800273 }
274 break;
275 }
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800276 case kAllocatorTypeRosAlloc: {
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700277 if (kInstrumented && UNLIKELY(is_running_on_memory_tool_)) {
Roland Levillain05e34f42018-05-24 13:19:05 +0000278 // If running on ASan, we should be using the instrumented path.
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700279 size_t max_bytes_tl_bulk_allocated = rosalloc_space_->MaxBytesBulkAllocatedFor(alloc_size);
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800280 if (UNLIKELY(IsOutOfMemoryOnAllocation(allocator_type,
281 max_bytes_tl_bulk_allocated,
282 kGrow))) {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700283 return nullptr;
284 }
285 ret = rosalloc_space_->Alloc(self, alloc_size, bytes_allocated, usable_size,
286 bytes_tl_bulk_allocated);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800287 } else {
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700288 DCHECK(!is_running_on_memory_tool_);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700289 size_t max_bytes_tl_bulk_allocated =
290 rosalloc_space_->MaxBytesBulkAllocatedForNonvirtual(alloc_size);
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800291 if (UNLIKELY(IsOutOfMemoryOnAllocation(allocator_type,
292 max_bytes_tl_bulk_allocated,
293 kGrow))) {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700294 return nullptr;
295 }
296 if (!kInstrumented) {
297 DCHECK(!rosalloc_space_->CanAllocThreadLocal(self, alloc_size));
298 }
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800299 ret = rosalloc_space_->AllocNonvirtual(self,
300 alloc_size,
301 bytes_allocated,
302 usable_size,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700303 bytes_tl_bulk_allocated);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800304 }
305 break;
306 }
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800307 case kAllocatorTypeDlMalloc: {
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700308 if (kInstrumented && UNLIKELY(is_running_on_memory_tool_)) {
Roland Levillain05e34f42018-05-24 13:19:05 +0000309 // If running on ASan, we should be using the instrumented path.
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800310 ret = dlmalloc_space_->Alloc(self,
311 alloc_size,
312 bytes_allocated,
313 usable_size,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700314 bytes_tl_bulk_allocated);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800315 } else {
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700316 DCHECK(!is_running_on_memory_tool_);
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800317 ret = dlmalloc_space_->AllocNonvirtual(self,
318 alloc_size,
319 bytes_allocated,
320 usable_size,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700321 bytes_tl_bulk_allocated);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800322 }
323 break;
324 }
325 case kAllocatorTypeNonMoving: {
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800326 ret = non_moving_space_->Alloc(self,
327 alloc_size,
328 bytes_allocated,
329 usable_size,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700330 bytes_tl_bulk_allocated);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800331 break;
332 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800333 case kAllocatorTypeLOS: {
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800334 ret = large_object_space_->Alloc(self,
335 alloc_size,
336 bytes_allocated,
337 usable_size,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700338 bytes_tl_bulk_allocated);
Hiroshi Yamauchi95a659f2013-11-22 14:43:45 -0800339 // Note that the bump pointer spaces aren't necessarily next to
340 // the other continuous spaces like the non-moving alloc space or
341 // the zygote space.
342 DCHECK(ret == nullptr || large_object_space_->Contains(ret));
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800343 break;
344 }
Nicolas Geoffray96172e02016-11-30 11:52:19 +0000345 case kAllocatorTypeRegion: {
346 DCHECK(region_space_ != nullptr);
347 alloc_size = RoundUp(alloc_size, space::RegionSpace::kAlignment);
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800348 ret = region_space_->AllocNonvirtual<false>(alloc_size,
349 bytes_allocated,
350 usable_size,
Nicolas Geoffray96172e02016-11-30 11:52:19 +0000351 bytes_tl_bulk_allocated);
352 break;
353 }
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800354 case kAllocatorTypeTLAB:
355 FALLTHROUGH_INTENDED;
Nicolas Geoffray96172e02016-11-30 11:52:19 +0000356 case kAllocatorTypeRegionTLAB: {
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800357 DCHECK_ALIGNED(alloc_size, kObjectAlignment);
358 static_assert(space::RegionSpace::kAlignment == space::BumpPointerSpace::kAlignment,
359 "mismatched alignments");
360 static_assert(kObjectAlignment == space::BumpPointerSpace::kAlignment,
361 "mismatched alignments");
Nicolas Geoffray96172e02016-11-30 11:52:19 +0000362 if (UNLIKELY(self->TlabSize() < alloc_size)) {
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800363 // kAllocatorTypeTLAB may be the allocator for region space TLAB if the GC is not marking,
364 // that is why the allocator is not passed down.
365 return AllocWithNewTLAB(self,
366 alloc_size,
367 kGrow,
368 bytes_allocated,
369 usable_size,
370 bytes_tl_bulk_allocated);
Nicolas Geoffray96172e02016-11-30 11:52:19 +0000371 }
372 // The allocation can't fail.
373 ret = self->AllocTlab(alloc_size);
374 DCHECK(ret != nullptr);
375 *bytes_allocated = alloc_size;
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800376 *bytes_tl_bulk_allocated = 0; // Allocated in an existing buffer.
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800377 *usable_size = alloc_size;
378 break;
379 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800380 default: {
381 LOG(FATAL) << "Invalid allocator type";
382 ret = nullptr;
383 }
384 }
385 return ret;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700386}
387
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700388inline bool Heap::ShouldAllocLargeObject(ObjPtr<mirror::Class> c, size_t byte_count) const {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700389 // We need to have a zygote space or else our newly allocated large object can end up in the
390 // Zygote resulting in it being prematurely freed.
391 // We can only do this for primitive objects since large objects will not be within the card table
392 // range. This also means that we rely on SetClass not dirtying the object's card.
Jeff Hao13e00912015-06-22 15:14:49 -0700393 return byte_count >= large_object_threshold_ && (c->IsPrimitiveArray() || c->IsStringClass());
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700394}
395
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800396inline bool Heap::IsOutOfMemoryOnAllocation(AllocatorType allocator_type,
397 size_t alloc_size,
398 bool grow) {
Hans Boehmc220f982018-10-12 16:15:45 -0700399 size_t old_target = target_footprint_.load(std::memory_order_relaxed);
400 while (true) {
401 size_t old_allocated = num_bytes_allocated_.load(std::memory_order_relaxed);
402 size_t new_footprint = old_allocated + alloc_size;
403 // Tests against heap limits are inherently approximate, since multiple allocations may
404 // race, and this is not atomic with the allocation.
405 if (UNLIKELY(new_footprint <= old_target)) {
406 return false;
407 } else if (UNLIKELY(new_footprint > growth_limit_)) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700408 return true;
409 }
Hans Boehmc220f982018-10-12 16:15:45 -0700410 // We are between target_footprint_ and growth_limit_ .
411 if (AllocatorMayHaveConcurrentGC(allocator_type) && IsGcConcurrent()) {
412 return false;
413 } else {
414 if (grow) {
415 if (target_footprint_.compare_exchange_weak(/*inout ref*/old_target, new_footprint,
416 std::memory_order_relaxed)) {
417 VlogHeapGrowth(old_target, new_footprint, alloc_size);
418 return false;
419 } // else try again.
420 } else {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700421 return true;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700422 }
423 }
424 }
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700425}
426
Hans Boehmc220f982018-10-12 16:15:45 -0700427inline bool Heap::ShouldConcurrentGCForJava(size_t new_num_bytes_allocated) {
428 // For a Java allocation, we only check whether the number of Java allocated bytes excceeds a
429 // threshold. By not considering native allocation here, we (a) ensure that Java heap bounds are
430 // maintained, and (b) reduce the cost of the check here.
431 return new_num_bytes_allocated >= concurrent_start_bytes_;
432}
433
434inline void Heap::CheckConcurrentGCForJava(Thread* self,
Mathieu Chartiera4f6af92015-08-11 17:35:25 -0700435 size_t new_num_bytes_allocated,
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700436 ObjPtr<mirror::Object>* obj) {
Hans Boehmc220f982018-10-12 16:15:45 -0700437 if (UNLIKELY(ShouldConcurrentGCForJava(new_num_bytes_allocated))) {
438 RequestConcurrentGCAndSaveObject(self, false /* force_full */, obj);
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700439 }
440}
441
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700442} // namespace gc
443} // namespace art
444
445#endif // ART_RUNTIME_GC_HEAP_INL_H_