blob: 08cbea21721e5199a59064e79d614716194ce91a [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.
Lokesh Gidraea5b4292019-08-08 16:27:21 -0700159 if (region_space_) {
160 // With CC collector, during a GC cycle, the heap usage increases as
161 // there are two copies of evacuated objects. Therefore, add evac-bytes
162 // to the heap size. When the GC cycle is not running, evac-bytes
163 // are 0, as required.
164 TraceHeapSize(new_num_bytes_allocated + region_space_->EvacBytes());
165 } else {
166 TraceHeapSize(new_num_bytes_allocated);
167 }
Mathieu Chartier34afcde2017-06-30 15:31:11 -0700168 }
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800169 }
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -0700170 if (kIsDebugBuild && Runtime::Current()->IsStarted()) {
171 CHECK_LE(obj->SizeOf(), usable_size);
172 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800173 // TODO: Deprecate.
174 if (kInstrumented) {
175 if (Runtime::Current()->HasStatsEnabled()) {
176 RuntimeStats* thread_stats = self->GetStats();
177 ++thread_stats->allocated_objects;
178 thread_stats->allocated_bytes += bytes_allocated;
179 RuntimeStats* global_stats = Runtime::Current()->GetStats();
180 ++global_stats->allocated_objects;
181 global_stats->allocated_bytes += bytes_allocated;
182 }
183 } else {
184 DCHECK(!Runtime::Current()->HasStatsEnabled());
185 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800186 if (kInstrumented) {
Man Cao8c2ff642015-05-27 17:25:30 -0700187 if (IsAllocTrackingEnabled()) {
Mathieu Chartier458b1052016-03-29 14:02:55 -0700188 // allocation_records_ is not null since it never becomes null after allocation tracking is
189 // enabled.
190 DCHECK(allocation_records_ != nullptr);
191 allocation_records_->RecordAllocation(self, &obj, bytes_allocated);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800192 }
Orion Hodson88591fe2018-03-06 13:35:43 +0000193 AllocationListener* l = alloc_listener_.load(std::memory_order_seq_cst);
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700194 if (l != nullptr) {
195 // Same as above. We assume that a listener that was once stored will never be deleted.
196 // Otherwise we'd have to perform this under a lock.
197 l->ObjectAllocated(self, &obj, bytes_allocated);
198 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800199 } else {
Man Cao8c2ff642015-05-27 17:25:30 -0700200 DCHECK(!IsAllocTrackingEnabled());
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800201 }
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800202 if (AllocatorHasAllocationStack(allocator)) {
203 PushOnAllocationStack(self, &obj);
204 }
Mathieu Chartier31000802015-06-14 14:14:37 -0700205 if (kInstrumented) {
206 if (gc_stress_mode_) {
207 CheckGcStressMode(self, &obj);
208 }
209 } else {
210 DCHECK(!gc_stress_mode_);
211 }
Roland Levillainb81e9e92017-04-20 17:35:32 +0100212 // IsGcConcurrent() isn't known at compile time so we can optimize by not checking it for
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800213 // the BumpPointer or TLAB allocators. This is nice since it allows the entire if statement to be
214 // optimized out. And for the other allocators, AllocatorMayHaveConcurrentGC is a constant since
215 // the allocator_type should be constant propagated.
Hiroshi Yamauchi3e417802014-03-20 12:03:02 -0700216 if (AllocatorMayHaveConcurrentGC(allocator) && IsGcConcurrent()) {
Hans Boehmfb8b4e22018-09-05 16:45:42 -0700217 // New_num_bytes_allocated is zero if we didn't update num_bytes_allocated_.
218 // That's fine.
Hans Boehmc220f982018-10-12 16:15:45 -0700219 CheckConcurrentGCForJava(self, new_num_bytes_allocated, &obj);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800220 }
Mathieu Chartier4e305412014-02-19 10:54:44 -0800221 VerifyObject(obj);
222 self->VerifyStack();
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700223 return obj.Ptr();
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700224}
225
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800226// The size of a thread-local allocation stack in the number of references.
227static constexpr size_t kThreadLocalAllocationStackSize = 128;
228
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700229inline void Heap::PushOnAllocationStack(Thread* self, ObjPtr<mirror::Object>* obj) {
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800230 if (kUseThreadLocalAllocationStack) {
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700231 if (UNLIKELY(!self->PushOnThreadLocalAllocationStack(obj->Ptr()))) {
Mathieu Chartierc1790162014-05-23 10:54:50 -0700232 PushOnThreadLocalAllocationStackWithInternalGC(self, obj);
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800233 }
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700234 } else if (UNLIKELY(!allocation_stack_->AtomicPushBack(obj->Ptr()))) {
Mathieu Chartierc1790162014-05-23 10:54:50 -0700235 PushOnAllocationStackWithInternalGC(self, obj);
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800236 }
237}
238
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800239template <bool kInstrumented, typename PreFenceVisitor>
Mathieu Chartiera4f6af92015-08-11 17:35:25 -0700240inline mirror::Object* Heap::AllocLargeObject(Thread* self,
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700241 ObjPtr<mirror::Class>* klass,
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800242 size_t byte_count,
243 const PreFenceVisitor& pre_fence_visitor) {
Mathieu Chartier446f9ee2014-12-01 15:00:27 -0800244 // Save and restore the class in case it moves.
245 StackHandleScope<1> hs(self);
246 auto klass_wrapper = hs.NewHandleWrapper(klass);
247 return AllocObjectWithAllocator<kInstrumented, false, PreFenceVisitor>(self, *klass, byte_count,
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800248 kAllocatorTypeLOS,
249 pre_fence_visitor);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800250}
251
252template <const bool kInstrumented, const bool kGrow>
Mathieu Chartiera4f6af92015-08-11 17:35:25 -0700253inline mirror::Object* Heap::TryToAllocate(Thread* self,
254 AllocatorType allocator_type,
255 size_t alloc_size,
256 size_t* bytes_allocated,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700257 size_t* usable_size,
258 size_t* bytes_tl_bulk_allocated) {
Hans Boehmc220f982018-10-12 16:15:45 -0700259 if (allocator_type != kAllocatorTypeRegionTLAB &&
260 allocator_type != kAllocatorTypeTLAB &&
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700261 allocator_type != kAllocatorTypeRosAlloc &&
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800262 UNLIKELY(IsOutOfMemoryOnAllocation(allocator_type, alloc_size, kGrow))) {
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800263 return nullptr;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700264 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800265 mirror::Object* ret;
266 switch (allocator_type) {
267 case kAllocatorTypeBumpPointer: {
268 DCHECK(bump_pointer_space_ != nullptr);
269 alloc_size = RoundUp(alloc_size, space::BumpPointerSpace::kAlignment);
270 ret = bump_pointer_space_->AllocNonvirtual(alloc_size);
271 if (LIKELY(ret != nullptr)) {
272 *bytes_allocated = alloc_size;
Ian Rogers6fac4472014-02-25 17:01:10 -0800273 *usable_size = alloc_size;
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700274 *bytes_tl_bulk_allocated = alloc_size;
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800275 }
276 break;
277 }
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800278 case kAllocatorTypeRosAlloc: {
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700279 if (kInstrumented && UNLIKELY(is_running_on_memory_tool_)) {
Roland Levillain05e34f42018-05-24 13:19:05 +0000280 // If running on ASan, we should be using the instrumented path.
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700281 size_t max_bytes_tl_bulk_allocated = rosalloc_space_->MaxBytesBulkAllocatedFor(alloc_size);
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800282 if (UNLIKELY(IsOutOfMemoryOnAllocation(allocator_type,
283 max_bytes_tl_bulk_allocated,
284 kGrow))) {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700285 return nullptr;
286 }
287 ret = rosalloc_space_->Alloc(self, alloc_size, bytes_allocated, usable_size,
288 bytes_tl_bulk_allocated);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800289 } else {
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700290 DCHECK(!is_running_on_memory_tool_);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700291 size_t max_bytes_tl_bulk_allocated =
292 rosalloc_space_->MaxBytesBulkAllocatedForNonvirtual(alloc_size);
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800293 if (UNLIKELY(IsOutOfMemoryOnAllocation(allocator_type,
294 max_bytes_tl_bulk_allocated,
295 kGrow))) {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700296 return nullptr;
297 }
298 if (!kInstrumented) {
299 DCHECK(!rosalloc_space_->CanAllocThreadLocal(self, alloc_size));
300 }
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800301 ret = rosalloc_space_->AllocNonvirtual(self,
302 alloc_size,
303 bytes_allocated,
304 usable_size,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700305 bytes_tl_bulk_allocated);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800306 }
307 break;
308 }
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800309 case kAllocatorTypeDlMalloc: {
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700310 if (kInstrumented && UNLIKELY(is_running_on_memory_tool_)) {
Roland Levillain05e34f42018-05-24 13:19:05 +0000311 // If running on ASan, we should be using the instrumented path.
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800312 ret = dlmalloc_space_->Alloc(self,
313 alloc_size,
314 bytes_allocated,
315 usable_size,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700316 bytes_tl_bulk_allocated);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800317 } else {
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700318 DCHECK(!is_running_on_memory_tool_);
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800319 ret = dlmalloc_space_->AllocNonvirtual(self,
320 alloc_size,
321 bytes_allocated,
322 usable_size,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700323 bytes_tl_bulk_allocated);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800324 }
325 break;
326 }
327 case kAllocatorTypeNonMoving: {
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800328 ret = non_moving_space_->Alloc(self,
329 alloc_size,
330 bytes_allocated,
331 usable_size,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700332 bytes_tl_bulk_allocated);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800333 break;
334 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800335 case kAllocatorTypeLOS: {
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800336 ret = large_object_space_->Alloc(self,
337 alloc_size,
338 bytes_allocated,
339 usable_size,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700340 bytes_tl_bulk_allocated);
Hiroshi Yamauchi95a659f2013-11-22 14:43:45 -0800341 // Note that the bump pointer spaces aren't necessarily next to
342 // the other continuous spaces like the non-moving alloc space or
343 // the zygote space.
344 DCHECK(ret == nullptr || large_object_space_->Contains(ret));
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800345 break;
346 }
Nicolas Geoffray96172e02016-11-30 11:52:19 +0000347 case kAllocatorTypeRegion: {
348 DCHECK(region_space_ != nullptr);
349 alloc_size = RoundUp(alloc_size, space::RegionSpace::kAlignment);
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800350 ret = region_space_->AllocNonvirtual<false>(alloc_size,
351 bytes_allocated,
352 usable_size,
Nicolas Geoffray96172e02016-11-30 11:52:19 +0000353 bytes_tl_bulk_allocated);
354 break;
355 }
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800356 case kAllocatorTypeTLAB:
357 FALLTHROUGH_INTENDED;
Nicolas Geoffray96172e02016-11-30 11:52:19 +0000358 case kAllocatorTypeRegionTLAB: {
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800359 DCHECK_ALIGNED(alloc_size, kObjectAlignment);
360 static_assert(space::RegionSpace::kAlignment == space::BumpPointerSpace::kAlignment,
361 "mismatched alignments");
362 static_assert(kObjectAlignment == space::BumpPointerSpace::kAlignment,
363 "mismatched alignments");
Nicolas Geoffray96172e02016-11-30 11:52:19 +0000364 if (UNLIKELY(self->TlabSize() < alloc_size)) {
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800365 // kAllocatorTypeTLAB may be the allocator for region space TLAB if the GC is not marking,
366 // that is why the allocator is not passed down.
367 return AllocWithNewTLAB(self,
368 alloc_size,
369 kGrow,
370 bytes_allocated,
371 usable_size,
372 bytes_tl_bulk_allocated);
Nicolas Geoffray96172e02016-11-30 11:52:19 +0000373 }
374 // The allocation can't fail.
375 ret = self->AllocTlab(alloc_size);
376 DCHECK(ret != nullptr);
377 *bytes_allocated = alloc_size;
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800378 *bytes_tl_bulk_allocated = 0; // Allocated in an existing buffer.
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800379 *usable_size = alloc_size;
380 break;
381 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800382 default: {
383 LOG(FATAL) << "Invalid allocator type";
384 ret = nullptr;
385 }
386 }
387 return ret;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700388}
389
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700390inline bool Heap::ShouldAllocLargeObject(ObjPtr<mirror::Class> c, size_t byte_count) const {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700391 // We need to have a zygote space or else our newly allocated large object can end up in the
392 // Zygote resulting in it being prematurely freed.
393 // We can only do this for primitive objects since large objects will not be within the card table
394 // range. This also means that we rely on SetClass not dirtying the object's card.
Jeff Hao13e00912015-06-22 15:14:49 -0700395 return byte_count >= large_object_threshold_ && (c->IsPrimitiveArray() || c->IsStringClass());
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700396}
397
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800398inline bool Heap::IsOutOfMemoryOnAllocation(AllocatorType allocator_type,
399 size_t alloc_size,
400 bool grow) {
Hans Boehmc220f982018-10-12 16:15:45 -0700401 size_t old_target = target_footprint_.load(std::memory_order_relaxed);
402 while (true) {
403 size_t old_allocated = num_bytes_allocated_.load(std::memory_order_relaxed);
404 size_t new_footprint = old_allocated + alloc_size;
405 // Tests against heap limits are inherently approximate, since multiple allocations may
406 // race, and this is not atomic with the allocation.
407 if (UNLIKELY(new_footprint <= old_target)) {
408 return false;
409 } else if (UNLIKELY(new_footprint > growth_limit_)) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700410 return true;
411 }
Hans Boehmc220f982018-10-12 16:15:45 -0700412 // We are between target_footprint_ and growth_limit_ .
413 if (AllocatorMayHaveConcurrentGC(allocator_type) && IsGcConcurrent()) {
414 return false;
415 } else {
416 if (grow) {
417 if (target_footprint_.compare_exchange_weak(/*inout ref*/old_target, new_footprint,
418 std::memory_order_relaxed)) {
419 VlogHeapGrowth(old_target, new_footprint, alloc_size);
420 return false;
421 } // else try again.
422 } else {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700423 return true;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700424 }
425 }
426 }
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700427}
428
Hans Boehmc220f982018-10-12 16:15:45 -0700429inline bool Heap::ShouldConcurrentGCForJava(size_t new_num_bytes_allocated) {
430 // For a Java allocation, we only check whether the number of Java allocated bytes excceeds a
431 // threshold. By not considering native allocation here, we (a) ensure that Java heap bounds are
432 // maintained, and (b) reduce the cost of the check here.
433 return new_num_bytes_allocated >= concurrent_start_bytes_;
434}
435
436inline void Heap::CheckConcurrentGCForJava(Thread* self,
Mathieu Chartiera4f6af92015-08-11 17:35:25 -0700437 size_t new_num_bytes_allocated,
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700438 ObjPtr<mirror::Object>* obj) {
Hans Boehmc220f982018-10-12 16:15:45 -0700439 if (UNLIKELY(ShouldConcurrentGCForJava(new_num_bytes_allocated))) {
440 RequestConcurrentGCAndSaveObject(self, false /* force_full */, obj);
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700441 }
442}
443
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700444} // namespace gc
445} // namespace art
446
447#endif // ART_RUNTIME_GC_HEAP_INL_H_