blob: 948d23303cd8b50abe0e3ef8c01fbbc3ee488eb9 [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"
David Sehrc431b9d2018-03-02 12:01:51 -080025#include "base/utils.h"
Andreas Gamped4901292017-05-30 18:41:34 -070026#include "gc/accounting/atomic_stack.h"
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -080027#include "gc/accounting/card_table-inl.h"
Man Cao8c2ff642015-05-27 17:25:30 -070028#include "gc/allocation_record.h"
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -080029#include "gc/collector/semi_space.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070030#include "gc/space/bump_pointer_space-inl.h"
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070031#include "gc/space/dlmalloc_space-inl.h"
32#include "gc/space/large_object_space.h"
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080033#include "gc/space/region_space-inl.h"
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070034#include "gc/space/rosalloc_space-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070035#include "handle_scope-inl.h"
Mathieu Chartiera058fdf2016-10-06 15:13:58 -070036#include "obj_ptr-inl.h"
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070037#include "runtime.h"
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070038#include "thread-inl.h"
Andreas Gampe90b936d2017-01-31 08:58:55 -080039#include "verify_object.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 {
Roland Levillain9b869ea2018-01-31 13:46:11 +0000110 // Bytes allocated that takes bulk thread-local buffer allocations into account.
111 size_t bytes_tl_bulk_allocated = 0u;
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700112 obj = TryToAllocate<kInstrumented, false>(self, allocator, byte_count, &bytes_allocated,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700113 &usable_size, &bytes_tl_bulk_allocated);
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700114 if (UNLIKELY(obj == nullptr)) {
Mathieu Chartiereebc3af2016-02-29 18:13:38 -0800115 // AllocateInternalWithGc can cause thread suspension, if someone instruments the entrypoints
116 // or changes the allocator in a suspend point here, we need to retry the allocation.
117 obj = AllocateInternalWithGc(self,
118 allocator,
119 kInstrumented,
120 byte_count,
121 &bytes_allocated,
122 &usable_size,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700123 &bytes_tl_bulk_allocated, &klass);
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700124 if (obj == nullptr) {
Mathieu Chartiereebc3af2016-02-29 18:13:38 -0800125 // The only way that we can get a null return if there is no pending exception is if the
126 // allocator or instrumentation changed.
127 if (!self->IsExceptionPending()) {
128 // AllocObject will pick up the new allocator type, and instrumented as true is the safe
129 // default.
130 return AllocObject</*kInstrumented*/true>(self,
131 klass,
132 byte_count,
133 pre_fence_visitor);
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700134 }
135 return nullptr;
136 }
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -0700137 }
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700138 DCHECK_GT(bytes_allocated, 0u);
139 DCHECK_GT(usable_size, 0u);
140 obj->SetClass(klass);
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -0700141 if (kUseBakerReadBarrier) {
142 obj->AssertReadBarrierState();
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700143 }
144 if (collector::SemiSpace::kUseRememberedSet && UNLIKELY(allocator == kAllocatorTypeNonMoving)) {
145 // (Note this if statement will be constant folded away for the
146 // fast-path quick entry points.) Because SetClass() has no write
147 // barrier, if a non-moving space allocation, we need a write
148 // barrier as the class pointer may point to the bump pointer
149 // space (where the class pointer is an "old-to-young" reference,
150 // though rare) under the GSS collector with the remembered set
151 // enabled. We don't need this for kAllocatorTypeRosAlloc/DlMalloc
152 // cases because we don't directly allocate into the main alloc
153 // space (besides promotions) under the SS/GSS collector.
154 WriteBarrierField(obj, mirror::Object::ClassOffset(), klass);
155 }
156 pre_fence_visitor(obj, usable_size);
Hans Boehmb0171b92016-01-28 17:19:15 -0800157 QuasiAtomic::ThreadFenceForConstructor();
Roland Levillain9b869ea2018-01-31 13:46:11 +0000158 size_t num_bytes_allocated_before =
Orion Hodson88591fe2018-03-06 13:35:43 +0000159 num_bytes_allocated_.fetch_add(bytes_tl_bulk_allocated, std::memory_order_relaxed);
Roland Levillain9b869ea2018-01-31 13:46:11 +0000160 new_num_bytes_allocated = num_bytes_allocated_before + bytes_tl_bulk_allocated;
Mathieu Chartier34afcde2017-06-30 15:31:11 -0700161 if (bytes_tl_bulk_allocated > 0) {
162 // Only trace when we get an increase in the number of bytes allocated. This happens when
163 // obtaining a new TLAB and isn't often enough to hurt performance according to golem.
Roland Levillain9b869ea2018-01-31 13:46:11 +0000164 TraceHeapSize(new_num_bytes_allocated);
Mathieu Chartier34afcde2017-06-30 15:31:11 -0700165 }
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800166 }
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -0700167 if (kIsDebugBuild && Runtime::Current()->IsStarted()) {
168 CHECK_LE(obj->SizeOf(), usable_size);
169 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800170 // TODO: Deprecate.
171 if (kInstrumented) {
172 if (Runtime::Current()->HasStatsEnabled()) {
173 RuntimeStats* thread_stats = self->GetStats();
174 ++thread_stats->allocated_objects;
175 thread_stats->allocated_bytes += bytes_allocated;
176 RuntimeStats* global_stats = Runtime::Current()->GetStats();
177 ++global_stats->allocated_objects;
178 global_stats->allocated_bytes += bytes_allocated;
179 }
180 } else {
181 DCHECK(!Runtime::Current()->HasStatsEnabled());
182 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800183 if (kInstrumented) {
Man Cao8c2ff642015-05-27 17:25:30 -0700184 if (IsAllocTrackingEnabled()) {
Mathieu Chartier458b1052016-03-29 14:02:55 -0700185 // allocation_records_ is not null since it never becomes null after allocation tracking is
186 // enabled.
187 DCHECK(allocation_records_ != nullptr);
188 allocation_records_->RecordAllocation(self, &obj, bytes_allocated);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800189 }
Orion Hodson88591fe2018-03-06 13:35:43 +0000190 AllocationListener* l = alloc_listener_.load(std::memory_order_seq_cst);
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700191 if (l != nullptr) {
192 // Same as above. We assume that a listener that was once stored will never be deleted.
193 // Otherwise we'd have to perform this under a lock.
194 l->ObjectAllocated(self, &obj, bytes_allocated);
195 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800196 } else {
Man Cao8c2ff642015-05-27 17:25:30 -0700197 DCHECK(!IsAllocTrackingEnabled());
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800198 }
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800199 if (AllocatorHasAllocationStack(allocator)) {
200 PushOnAllocationStack(self, &obj);
201 }
Mathieu Chartier31000802015-06-14 14:14:37 -0700202 if (kInstrumented) {
203 if (gc_stress_mode_) {
204 CheckGcStressMode(self, &obj);
205 }
206 } else {
207 DCHECK(!gc_stress_mode_);
208 }
Roland Levillainb81e9e92017-04-20 17:35:32 +0100209 // IsGcConcurrent() isn't known at compile time so we can optimize by not checking it for
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800210 // the BumpPointer or TLAB allocators. This is nice since it allows the entire if statement to be
211 // optimized out. And for the other allocators, AllocatorMayHaveConcurrentGC is a constant since
212 // the allocator_type should be constant propagated.
Hiroshi Yamauchi3e417802014-03-20 12:03:02 -0700213 if (AllocatorMayHaveConcurrentGC(allocator) && IsGcConcurrent()) {
Mathieu Chartierf517f1a2014-03-06 15:52:27 -0800214 CheckConcurrentGC(self, new_num_bytes_allocated, &obj);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800215 }
Mathieu Chartier4e305412014-02-19 10:54:44 -0800216 VerifyObject(obj);
217 self->VerifyStack();
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700218 return obj.Ptr();
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700219}
220
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800221// The size of a thread-local allocation stack in the number of references.
222static constexpr size_t kThreadLocalAllocationStackSize = 128;
223
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700224inline void Heap::PushOnAllocationStack(Thread* self, ObjPtr<mirror::Object>* obj) {
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800225 if (kUseThreadLocalAllocationStack) {
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700226 if (UNLIKELY(!self->PushOnThreadLocalAllocationStack(obj->Ptr()))) {
Mathieu Chartierc1790162014-05-23 10:54:50 -0700227 PushOnThreadLocalAllocationStackWithInternalGC(self, obj);
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800228 }
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700229 } else if (UNLIKELY(!allocation_stack_->AtomicPushBack(obj->Ptr()))) {
Mathieu Chartierc1790162014-05-23 10:54:50 -0700230 PushOnAllocationStackWithInternalGC(self, obj);
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800231 }
232}
233
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800234template <bool kInstrumented, typename PreFenceVisitor>
Mathieu Chartiera4f6af92015-08-11 17:35:25 -0700235inline mirror::Object* Heap::AllocLargeObject(Thread* self,
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700236 ObjPtr<mirror::Class>* klass,
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800237 size_t byte_count,
238 const PreFenceVisitor& pre_fence_visitor) {
Mathieu Chartier446f9ee2014-12-01 15:00:27 -0800239 // Save and restore the class in case it moves.
240 StackHandleScope<1> hs(self);
241 auto klass_wrapper = hs.NewHandleWrapper(klass);
242 return AllocObjectWithAllocator<kInstrumented, false, PreFenceVisitor>(self, *klass, byte_count,
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800243 kAllocatorTypeLOS,
244 pre_fence_visitor);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800245}
246
247template <const bool kInstrumented, const bool kGrow>
Mathieu Chartiera4f6af92015-08-11 17:35:25 -0700248inline mirror::Object* Heap::TryToAllocate(Thread* self,
249 AllocatorType allocator_type,
250 size_t alloc_size,
251 size_t* bytes_allocated,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700252 size_t* usable_size,
253 size_t* bytes_tl_bulk_allocated) {
Mathieu Chartiera4f6af92015-08-11 17:35:25 -0700254 if (allocator_type != kAllocatorTypeTLAB &&
255 allocator_type != kAllocatorTypeRegionTLAB &&
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700256 allocator_type != kAllocatorTypeRosAlloc &&
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800257 UNLIKELY(IsOutOfMemoryOnAllocation(allocator_type, alloc_size, kGrow))) {
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800258 return nullptr;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700259 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800260 mirror::Object* ret;
261 switch (allocator_type) {
262 case kAllocatorTypeBumpPointer: {
263 DCHECK(bump_pointer_space_ != nullptr);
264 alloc_size = RoundUp(alloc_size, space::BumpPointerSpace::kAlignment);
265 ret = bump_pointer_space_->AllocNonvirtual(alloc_size);
266 if (LIKELY(ret != nullptr)) {
267 *bytes_allocated = alloc_size;
Ian Rogers6fac4472014-02-25 17:01:10 -0800268 *usable_size = alloc_size;
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700269 *bytes_tl_bulk_allocated = alloc_size;
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800270 }
271 break;
272 }
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800273 case kAllocatorTypeRosAlloc: {
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700274 if (kInstrumented && UNLIKELY(is_running_on_memory_tool_)) {
Andreas Gampe8b362a82018-05-22 20:54:14 +0000275 // If running on valgrind or asan, we should be using the instrumented path.
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700276 size_t max_bytes_tl_bulk_allocated = rosalloc_space_->MaxBytesBulkAllocatedFor(alloc_size);
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800277 if (UNLIKELY(IsOutOfMemoryOnAllocation(allocator_type,
278 max_bytes_tl_bulk_allocated,
279 kGrow))) {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700280 return nullptr;
281 }
282 ret = rosalloc_space_->Alloc(self, alloc_size, bytes_allocated, usable_size,
283 bytes_tl_bulk_allocated);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800284 } else {
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700285 DCHECK(!is_running_on_memory_tool_);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700286 size_t max_bytes_tl_bulk_allocated =
287 rosalloc_space_->MaxBytesBulkAllocatedForNonvirtual(alloc_size);
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800288 if (UNLIKELY(IsOutOfMemoryOnAllocation(allocator_type,
289 max_bytes_tl_bulk_allocated,
290 kGrow))) {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700291 return nullptr;
292 }
293 if (!kInstrumented) {
294 DCHECK(!rosalloc_space_->CanAllocThreadLocal(self, alloc_size));
295 }
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800296 ret = rosalloc_space_->AllocNonvirtual(self,
297 alloc_size,
298 bytes_allocated,
299 usable_size,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700300 bytes_tl_bulk_allocated);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800301 }
302 break;
303 }
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800304 case kAllocatorTypeDlMalloc: {
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700305 if (kInstrumented && UNLIKELY(is_running_on_memory_tool_)) {
Andreas Gampe8b362a82018-05-22 20:54:14 +0000306 // If running on valgrind, we should be using the instrumented path.
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800307 ret = dlmalloc_space_->Alloc(self,
308 alloc_size,
309 bytes_allocated,
310 usable_size,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700311 bytes_tl_bulk_allocated);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800312 } else {
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700313 DCHECK(!is_running_on_memory_tool_);
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800314 ret = dlmalloc_space_->AllocNonvirtual(self,
315 alloc_size,
316 bytes_allocated,
317 usable_size,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700318 bytes_tl_bulk_allocated);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800319 }
320 break;
321 }
322 case kAllocatorTypeNonMoving: {
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800323 ret = non_moving_space_->Alloc(self,
324 alloc_size,
325 bytes_allocated,
326 usable_size,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700327 bytes_tl_bulk_allocated);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800328 break;
329 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800330 case kAllocatorTypeLOS: {
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800331 ret = large_object_space_->Alloc(self,
332 alloc_size,
333 bytes_allocated,
334 usable_size,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700335 bytes_tl_bulk_allocated);
Hiroshi Yamauchi95a659f2013-11-22 14:43:45 -0800336 // Note that the bump pointer spaces aren't necessarily next to
337 // the other continuous spaces like the non-moving alloc space or
338 // the zygote space.
339 DCHECK(ret == nullptr || large_object_space_->Contains(ret));
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800340 break;
341 }
Nicolas Geoffray96172e02016-11-30 11:52:19 +0000342 case kAllocatorTypeRegion: {
343 DCHECK(region_space_ != nullptr);
344 alloc_size = RoundUp(alloc_size, space::RegionSpace::kAlignment);
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800345 ret = region_space_->AllocNonvirtual<false>(alloc_size,
346 bytes_allocated,
347 usable_size,
Nicolas Geoffray96172e02016-11-30 11:52:19 +0000348 bytes_tl_bulk_allocated);
349 break;
350 }
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800351 case kAllocatorTypeTLAB:
352 FALLTHROUGH_INTENDED;
Nicolas Geoffray96172e02016-11-30 11:52:19 +0000353 case kAllocatorTypeRegionTLAB: {
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800354 DCHECK_ALIGNED(alloc_size, kObjectAlignment);
355 static_assert(space::RegionSpace::kAlignment == space::BumpPointerSpace::kAlignment,
356 "mismatched alignments");
357 static_assert(kObjectAlignment == space::BumpPointerSpace::kAlignment,
358 "mismatched alignments");
Nicolas Geoffray96172e02016-11-30 11:52:19 +0000359 if (UNLIKELY(self->TlabSize() < alloc_size)) {
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800360 // kAllocatorTypeTLAB may be the allocator for region space TLAB if the GC is not marking,
361 // that is why the allocator is not passed down.
362 return AllocWithNewTLAB(self,
363 alloc_size,
364 kGrow,
365 bytes_allocated,
366 usable_size,
367 bytes_tl_bulk_allocated);
Nicolas Geoffray96172e02016-11-30 11:52:19 +0000368 }
369 // The allocation can't fail.
370 ret = self->AllocTlab(alloc_size);
371 DCHECK(ret != nullptr);
372 *bytes_allocated = alloc_size;
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800373 *bytes_tl_bulk_allocated = 0; // Allocated in an existing buffer.
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800374 *usable_size = alloc_size;
375 break;
376 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800377 default: {
378 LOG(FATAL) << "Invalid allocator type";
379 ret = nullptr;
380 }
381 }
382 return ret;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700383}
384
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700385inline bool Heap::ShouldAllocLargeObject(ObjPtr<mirror::Class> c, size_t byte_count) const {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700386 // We need to have a zygote space or else our newly allocated large object can end up in the
387 // Zygote resulting in it being prematurely freed.
388 // We can only do this for primitive objects since large objects will not be within the card table
389 // range. This also means that we rely on SetClass not dirtying the object's card.
Jeff Hao13e00912015-06-22 15:14:49 -0700390 return byte_count >= large_object_threshold_ && (c->IsPrimitiveArray() || c->IsStringClass());
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700391}
392
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800393inline bool Heap::IsOutOfMemoryOnAllocation(AllocatorType allocator_type,
394 size_t alloc_size,
395 bool grow) {
Orion Hodson88591fe2018-03-06 13:35:43 +0000396 size_t new_footprint = num_bytes_allocated_.load(std::memory_order_seq_cst) + alloc_size;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700397 if (UNLIKELY(new_footprint > max_allowed_footprint_)) {
398 if (UNLIKELY(new_footprint > growth_limit_)) {
399 return true;
400 }
Hiroshi Yamauchi3e417802014-03-20 12:03:02 -0700401 if (!AllocatorMayHaveConcurrentGC(allocator_type) || !IsGcConcurrent()) {
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800402 if (!grow) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700403 return true;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700404 }
Mathieu Chartier7bf82af2013-12-06 16:51:45 -0800405 // TODO: Grow for allocation is racy, fix it.
Andreas Gampe170331f2017-12-07 18:41:03 -0800406 VlogHeapGrowth(max_allowed_footprint_, new_footprint, alloc_size);
Mathieu Chartier7bf82af2013-12-06 16:51:45 -0800407 max_allowed_footprint_ = new_footprint;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700408 }
409 }
410 return false;
411}
412
Mathieu Chartiera4f6af92015-08-11 17:35:25 -0700413inline void Heap::CheckConcurrentGC(Thread* self,
414 size_t new_num_bytes_allocated,
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700415 ObjPtr<mirror::Object>* obj) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700416 if (UNLIKELY(new_num_bytes_allocated >= concurrent_start_bytes_)) {
Hiroshi Yamauchi0ae98992015-05-01 14:33:19 -0700417 RequestConcurrentGCAndSaveObject(self, false, obj);
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700418 }
419}
420
Mathieu Chartiera058fdf2016-10-06 15:13:58 -0700421inline void Heap::WriteBarrierField(ObjPtr<mirror::Object> dst,
422 MemberOffset offset ATTRIBUTE_UNUSED,
423 ObjPtr<mirror::Object> new_value ATTRIBUTE_UNUSED) {
424 card_table_->MarkCard(dst.Ptr());
425}
426
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700427inline void Heap::WriteBarrierArray(ObjPtr<mirror::Object> dst,
428 int start_offset ATTRIBUTE_UNUSED,
429 size_t length ATTRIBUTE_UNUSED) {
430 card_table_->MarkCard(dst.Ptr());
431}
432
433inline void Heap::WriteBarrierEveryFieldOf(ObjPtr<mirror::Object> obj) {
434 card_table_->MarkCard(obj.Ptr());
435}
436
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700437} // namespace gc
438} // namespace art
439
440#endif // ART_RUNTIME_GC_HEAP_INL_H_