blob: 3b66fbcaa9b3f5b7b634cd35267afb11d94ddcde [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)) {
Mathieu Chartierf75dce42019-04-08 09:36:23 -0700146 // (Note this if statement will be constant folded away for the fast-path quick entry
147 // points.) Because SetClass() has no write barrier, the GC may need a write barrier in the
148 // case the object is non movable and points to a recently allocated movable class.
Mathieu Chartier88ea61e2018-06-20 17:45:41 -0700149 WriteBarrier::ForFieldWrite(obj, mirror::Object::ClassOffset(), klass);
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700150 }
151 pre_fence_visitor(obj, usable_size);
Hans Boehmb0171b92016-01-28 17:19:15 -0800152 QuasiAtomic::ThreadFenceForConstructor();
Mathieu Chartier34afcde2017-06-30 15:31:11 -0700153 if (bytes_tl_bulk_allocated > 0) {
Hans Boehmfb8b4e22018-09-05 16:45:42 -0700154 size_t num_bytes_allocated_before =
155 num_bytes_allocated_.fetch_add(bytes_tl_bulk_allocated, std::memory_order_relaxed);
156 new_num_bytes_allocated = num_bytes_allocated_before + bytes_tl_bulk_allocated;
Mathieu Chartier34afcde2017-06-30 15:31:11 -0700157 // Only trace when we get an increase in the number of bytes allocated. This happens when
158 // obtaining a new TLAB and isn't often enough to hurt performance according to golem.
Roland Levillain9b869ea2018-01-31 13:46:11 +0000159 TraceHeapSize(new_num_bytes_allocated);
Mathieu Chartier34afcde2017-06-30 15:31:11 -0700160 }
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800161 }
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -0700162 if (kIsDebugBuild && Runtime::Current()->IsStarted()) {
163 CHECK_LE(obj->SizeOf(), usable_size);
164 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800165 // TODO: Deprecate.
166 if (kInstrumented) {
167 if (Runtime::Current()->HasStatsEnabled()) {
168 RuntimeStats* thread_stats = self->GetStats();
169 ++thread_stats->allocated_objects;
170 thread_stats->allocated_bytes += bytes_allocated;
171 RuntimeStats* global_stats = Runtime::Current()->GetStats();
172 ++global_stats->allocated_objects;
173 global_stats->allocated_bytes += bytes_allocated;
174 }
175 } else {
176 DCHECK(!Runtime::Current()->HasStatsEnabled());
177 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800178 if (kInstrumented) {
Man Cao8c2ff642015-05-27 17:25:30 -0700179 if (IsAllocTrackingEnabled()) {
Mathieu Chartier458b1052016-03-29 14:02:55 -0700180 // allocation_records_ is not null since it never becomes null after allocation tracking is
181 // enabled.
182 DCHECK(allocation_records_ != nullptr);
183 allocation_records_->RecordAllocation(self, &obj, bytes_allocated);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800184 }
Orion Hodson88591fe2018-03-06 13:35:43 +0000185 AllocationListener* l = alloc_listener_.load(std::memory_order_seq_cst);
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700186 if (l != nullptr) {
187 // Same as above. We assume that a listener that was once stored will never be deleted.
188 // Otherwise we'd have to perform this under a lock.
189 l->ObjectAllocated(self, &obj, bytes_allocated);
190 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800191 } else {
Man Cao8c2ff642015-05-27 17:25:30 -0700192 DCHECK(!IsAllocTrackingEnabled());
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800193 }
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800194 if (AllocatorHasAllocationStack(allocator)) {
195 PushOnAllocationStack(self, &obj);
196 }
Mathieu Chartier31000802015-06-14 14:14:37 -0700197 if (kInstrumented) {
198 if (gc_stress_mode_) {
199 CheckGcStressMode(self, &obj);
200 }
201 } else {
202 DCHECK(!gc_stress_mode_);
203 }
Roland Levillainb81e9e92017-04-20 17:35:32 +0100204 // IsGcConcurrent() isn't known at compile time so we can optimize by not checking it for
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800205 // the BumpPointer or TLAB allocators. This is nice since it allows the entire if statement to be
206 // optimized out. And for the other allocators, AllocatorMayHaveConcurrentGC is a constant since
207 // the allocator_type should be constant propagated.
Hiroshi Yamauchi3e417802014-03-20 12:03:02 -0700208 if (AllocatorMayHaveConcurrentGC(allocator) && IsGcConcurrent()) {
Hans Boehmfb8b4e22018-09-05 16:45:42 -0700209 // New_num_bytes_allocated is zero if we didn't update num_bytes_allocated_.
210 // That's fine.
Hans Boehmc220f982018-10-12 16:15:45 -0700211 CheckConcurrentGCForJava(self, new_num_bytes_allocated, &obj);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800212 }
Mathieu Chartier4e305412014-02-19 10:54:44 -0800213 VerifyObject(obj);
214 self->VerifyStack();
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700215 return obj.Ptr();
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700216}
217
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800218// The size of a thread-local allocation stack in the number of references.
219static constexpr size_t kThreadLocalAllocationStackSize = 128;
220
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700221inline void Heap::PushOnAllocationStack(Thread* self, ObjPtr<mirror::Object>* obj) {
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800222 if (kUseThreadLocalAllocationStack) {
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700223 if (UNLIKELY(!self->PushOnThreadLocalAllocationStack(obj->Ptr()))) {
Mathieu Chartierc1790162014-05-23 10:54:50 -0700224 PushOnThreadLocalAllocationStackWithInternalGC(self, obj);
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800225 }
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700226 } else if (UNLIKELY(!allocation_stack_->AtomicPushBack(obj->Ptr()))) {
Mathieu Chartierc1790162014-05-23 10:54:50 -0700227 PushOnAllocationStackWithInternalGC(self, obj);
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800228 }
229}
230
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800231template <bool kInstrumented, typename PreFenceVisitor>
Mathieu Chartiera4f6af92015-08-11 17:35:25 -0700232inline mirror::Object* Heap::AllocLargeObject(Thread* self,
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700233 ObjPtr<mirror::Class>* klass,
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800234 size_t byte_count,
235 const PreFenceVisitor& pre_fence_visitor) {
Mathieu Chartier446f9ee2014-12-01 15:00:27 -0800236 // Save and restore the class in case it moves.
237 StackHandleScope<1> hs(self);
238 auto klass_wrapper = hs.NewHandleWrapper(klass);
239 return AllocObjectWithAllocator<kInstrumented, false, PreFenceVisitor>(self, *klass, byte_count,
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800240 kAllocatorTypeLOS,
241 pre_fence_visitor);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800242}
243
244template <const bool kInstrumented, const bool kGrow>
Mathieu Chartiera4f6af92015-08-11 17:35:25 -0700245inline mirror::Object* Heap::TryToAllocate(Thread* self,
246 AllocatorType allocator_type,
247 size_t alloc_size,
248 size_t* bytes_allocated,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700249 size_t* usable_size,
250 size_t* bytes_tl_bulk_allocated) {
Hans Boehmc220f982018-10-12 16:15:45 -0700251 if (allocator_type != kAllocatorTypeRegionTLAB &&
252 allocator_type != kAllocatorTypeTLAB &&
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700253 allocator_type != kAllocatorTypeRosAlloc &&
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800254 UNLIKELY(IsOutOfMemoryOnAllocation(allocator_type, alloc_size, kGrow))) {
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800255 return nullptr;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700256 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800257 mirror::Object* ret;
258 switch (allocator_type) {
259 case kAllocatorTypeBumpPointer: {
260 DCHECK(bump_pointer_space_ != nullptr);
261 alloc_size = RoundUp(alloc_size, space::BumpPointerSpace::kAlignment);
262 ret = bump_pointer_space_->AllocNonvirtual(alloc_size);
263 if (LIKELY(ret != nullptr)) {
264 *bytes_allocated = alloc_size;
Ian Rogers6fac4472014-02-25 17:01:10 -0800265 *usable_size = alloc_size;
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700266 *bytes_tl_bulk_allocated = alloc_size;
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800267 }
268 break;
269 }
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800270 case kAllocatorTypeRosAlloc: {
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700271 if (kInstrumented && UNLIKELY(is_running_on_memory_tool_)) {
Roland Levillain05e34f42018-05-24 13:19:05 +0000272 // If running on ASan, we should be using the instrumented path.
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700273 size_t max_bytes_tl_bulk_allocated = rosalloc_space_->MaxBytesBulkAllocatedFor(alloc_size);
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800274 if (UNLIKELY(IsOutOfMemoryOnAllocation(allocator_type,
275 max_bytes_tl_bulk_allocated,
276 kGrow))) {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700277 return nullptr;
278 }
279 ret = rosalloc_space_->Alloc(self, alloc_size, bytes_allocated, usable_size,
280 bytes_tl_bulk_allocated);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800281 } else {
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700282 DCHECK(!is_running_on_memory_tool_);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700283 size_t max_bytes_tl_bulk_allocated =
284 rosalloc_space_->MaxBytesBulkAllocatedForNonvirtual(alloc_size);
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800285 if (UNLIKELY(IsOutOfMemoryOnAllocation(allocator_type,
286 max_bytes_tl_bulk_allocated,
287 kGrow))) {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700288 return nullptr;
289 }
290 if (!kInstrumented) {
291 DCHECK(!rosalloc_space_->CanAllocThreadLocal(self, alloc_size));
292 }
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800293 ret = rosalloc_space_->AllocNonvirtual(self,
294 alloc_size,
295 bytes_allocated,
296 usable_size,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700297 bytes_tl_bulk_allocated);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800298 }
299 break;
300 }
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800301 case kAllocatorTypeDlMalloc: {
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700302 if (kInstrumented && UNLIKELY(is_running_on_memory_tool_)) {
Roland Levillain05e34f42018-05-24 13:19:05 +0000303 // If running on ASan, we should be using the instrumented path.
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800304 ret = dlmalloc_space_->Alloc(self,
305 alloc_size,
306 bytes_allocated,
307 usable_size,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700308 bytes_tl_bulk_allocated);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800309 } else {
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700310 DCHECK(!is_running_on_memory_tool_);
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800311 ret = dlmalloc_space_->AllocNonvirtual(self,
312 alloc_size,
313 bytes_allocated,
314 usable_size,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700315 bytes_tl_bulk_allocated);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800316 }
317 break;
318 }
319 case kAllocatorTypeNonMoving: {
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800320 ret = non_moving_space_->Alloc(self,
321 alloc_size,
322 bytes_allocated,
323 usable_size,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700324 bytes_tl_bulk_allocated);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800325 break;
326 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800327 case kAllocatorTypeLOS: {
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800328 ret = large_object_space_->Alloc(self,
329 alloc_size,
330 bytes_allocated,
331 usable_size,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700332 bytes_tl_bulk_allocated);
Hiroshi Yamauchi95a659f2013-11-22 14:43:45 -0800333 // Note that the bump pointer spaces aren't necessarily next to
334 // the other continuous spaces like the non-moving alloc space or
335 // the zygote space.
336 DCHECK(ret == nullptr || large_object_space_->Contains(ret));
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800337 break;
338 }
Nicolas Geoffray96172e02016-11-30 11:52:19 +0000339 case kAllocatorTypeRegion: {
340 DCHECK(region_space_ != nullptr);
341 alloc_size = RoundUp(alloc_size, space::RegionSpace::kAlignment);
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800342 ret = region_space_->AllocNonvirtual<false>(alloc_size,
343 bytes_allocated,
344 usable_size,
Nicolas Geoffray96172e02016-11-30 11:52:19 +0000345 bytes_tl_bulk_allocated);
346 break;
347 }
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800348 case kAllocatorTypeTLAB:
349 FALLTHROUGH_INTENDED;
Nicolas Geoffray96172e02016-11-30 11:52:19 +0000350 case kAllocatorTypeRegionTLAB: {
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800351 DCHECK_ALIGNED(alloc_size, kObjectAlignment);
352 static_assert(space::RegionSpace::kAlignment == space::BumpPointerSpace::kAlignment,
353 "mismatched alignments");
354 static_assert(kObjectAlignment == space::BumpPointerSpace::kAlignment,
355 "mismatched alignments");
Nicolas Geoffray96172e02016-11-30 11:52:19 +0000356 if (UNLIKELY(self->TlabSize() < alloc_size)) {
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800357 // kAllocatorTypeTLAB may be the allocator for region space TLAB if the GC is not marking,
358 // that is why the allocator is not passed down.
359 return AllocWithNewTLAB(self,
360 alloc_size,
361 kGrow,
362 bytes_allocated,
363 usable_size,
364 bytes_tl_bulk_allocated);
Nicolas Geoffray96172e02016-11-30 11:52:19 +0000365 }
366 // The allocation can't fail.
367 ret = self->AllocTlab(alloc_size);
368 DCHECK(ret != nullptr);
369 *bytes_allocated = alloc_size;
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800370 *bytes_tl_bulk_allocated = 0; // Allocated in an existing buffer.
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800371 *usable_size = alloc_size;
372 break;
373 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800374 default: {
375 LOG(FATAL) << "Invalid allocator type";
376 ret = nullptr;
377 }
378 }
379 return ret;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700380}
381
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700382inline bool Heap::ShouldAllocLargeObject(ObjPtr<mirror::Class> c, size_t byte_count) const {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700383 // We need to have a zygote space or else our newly allocated large object can end up in the
384 // Zygote resulting in it being prematurely freed.
385 // We can only do this for primitive objects since large objects will not be within the card table
386 // range. This also means that we rely on SetClass not dirtying the object's card.
Jeff Hao13e00912015-06-22 15:14:49 -0700387 return byte_count >= large_object_threshold_ && (c->IsPrimitiveArray() || c->IsStringClass());
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700388}
389
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800390inline bool Heap::IsOutOfMemoryOnAllocation(AllocatorType allocator_type,
391 size_t alloc_size,
392 bool grow) {
Hans Boehmc220f982018-10-12 16:15:45 -0700393 size_t old_target = target_footprint_.load(std::memory_order_relaxed);
394 while (true) {
395 size_t old_allocated = num_bytes_allocated_.load(std::memory_order_relaxed);
396 size_t new_footprint = old_allocated + alloc_size;
397 // Tests against heap limits are inherently approximate, since multiple allocations may
398 // race, and this is not atomic with the allocation.
399 if (UNLIKELY(new_footprint <= old_target)) {
400 return false;
401 } else if (UNLIKELY(new_footprint > growth_limit_)) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700402 return true;
403 }
Hans Boehmc220f982018-10-12 16:15:45 -0700404 // We are between target_footprint_ and growth_limit_ .
405 if (AllocatorMayHaveConcurrentGC(allocator_type) && IsGcConcurrent()) {
406 return false;
407 } else {
408 if (grow) {
409 if (target_footprint_.compare_exchange_weak(/*inout ref*/old_target, new_footprint,
410 std::memory_order_relaxed)) {
411 VlogHeapGrowth(old_target, new_footprint, alloc_size);
412 return false;
413 } // else try again.
414 } else {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700415 return true;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700416 }
417 }
418 }
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700419}
420
Hans Boehmc220f982018-10-12 16:15:45 -0700421inline bool Heap::ShouldConcurrentGCForJava(size_t new_num_bytes_allocated) {
422 // For a Java allocation, we only check whether the number of Java allocated bytes excceeds a
423 // threshold. By not considering native allocation here, we (a) ensure that Java heap bounds are
424 // maintained, and (b) reduce the cost of the check here.
425 return new_num_bytes_allocated >= concurrent_start_bytes_;
426}
427
428inline void Heap::CheckConcurrentGCForJava(Thread* self,
Mathieu Chartiera4f6af92015-08-11 17:35:25 -0700429 size_t new_num_bytes_allocated,
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700430 ObjPtr<mirror::Object>* obj) {
Hans Boehmc220f982018-10-12 16:15:45 -0700431 if (UNLIKELY(ShouldConcurrentGCForJava(new_num_bytes_allocated))) {
432 RequestConcurrentGCAndSaveObject(self, false /* force_full */, obj);
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700433 }
434}
435
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700436} // namespace gc
437} // namespace art
438
439#endif // ART_RUNTIME_GC_HEAP_INL_H_