blob: 6735961591ca2281f7873ed7f1755434e5e06893 [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"
Vladimir Marko80afd022015-05-19 18:08:00 +010023#include "base/time_utils.h"
Andreas Gamped4901292017-05-30 18:41:34 -070024#include "gc/accounting/atomic_stack.h"
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -080025#include "gc/accounting/card_table-inl.h"
Man Cao8c2ff642015-05-27 17:25:30 -070026#include "gc/allocation_record.h"
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -080027#include "gc/collector/semi_space.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070028#include "gc/space/bump_pointer_space-inl.h"
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070029#include "gc/space/dlmalloc_space-inl.h"
30#include "gc/space/large_object_space.h"
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080031#include "gc/space/region_space-inl.h"
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070032#include "gc/space/rosalloc_space-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070033#include "handle_scope-inl.h"
Mathieu Chartiera058fdf2016-10-06 15:13:58 -070034#include "obj_ptr-inl.h"
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070035#include "runtime.h"
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070036#include "thread-inl.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010037#include "utils.h"
Andreas Gampe90b936d2017-01-31 08:58:55 -080038#include "verify_object.h"
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070039
40namespace art {
41namespace gc {
42
Mathieu Chartier692fafd2013-11-29 17:24:40 -080043template <bool kInstrumented, bool kCheckLargeObject, typename PreFenceVisitor>
Mathieu Chartiera4f6af92015-08-11 17:35:25 -070044inline mirror::Object* Heap::AllocObjectWithAllocator(Thread* self,
Mathieu Chartier9d156d52016-10-06 17:44:26 -070045 ObjPtr<mirror::Class> klass,
Mathieu Chartiera4f6af92015-08-11 17:35:25 -070046 size_t byte_count,
47 AllocatorType allocator,
Mathieu Chartier1febddf2013-11-20 12:33:14 -080048 const PreFenceVisitor& pre_fence_visitor) {
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080049 if (kIsDebugBuild) {
50 CheckPreconditionsForAllocObject(klass, byte_count);
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070051 // Since allocation can cause a GC which will need to SuspendAll, make sure all allocations are
52 // done in the runnable state where suspension is expected.
53 CHECK_EQ(self->GetState(), kRunnable);
54 self->AssertThreadSuspensionIsAllowable();
Mathieu Chartier8502f722016-06-08 15:09:08 -070055 self->AssertNoPendingException();
Mathieu Chartier9d156d52016-10-06 17:44:26 -070056 // Make sure to preserve klass.
57 StackHandleScope<1> hs(self);
58 HandleWrapperObjPtr<mirror::Class> h = hs.NewHandleWrapper(&klass);
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070059 self->PoisonObjectPointers();
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080060 }
Roland Levillainb81e9e92017-04-20 17:35:32 +010061 // Need to check that we aren't the large object allocator since the large object allocation code
62 // path includes this function. If we didn't check we would have an infinite loop.
Mathieu Chartier9d156d52016-10-06 17:44:26 -070063 ObjPtr<mirror::Object> obj;
Mathieu Chartier446f9ee2014-12-01 15:00:27 -080064 if (kCheckLargeObject && UNLIKELY(ShouldAllocLargeObject(klass, byte_count))) {
65 obj = AllocLargeObject<kInstrumented, PreFenceVisitor>(self, &klass, byte_count,
66 pre_fence_visitor);
67 if (obj != nullptr) {
Mathieu Chartier9d156d52016-10-06 17:44:26 -070068 return obj.Ptr();
Mathieu Chartier446f9ee2014-12-01 15:00:27 -080069 } else {
70 // There should be an OOM exception, since we are retrying, clear it.
71 self->ClearException();
72 }
73 // If the large object allocation failed, try to use the normal spaces (main space,
74 // non moving space). This can happen if there is significant virtual address space
75 // fragmentation.
76 }
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070077 // bytes allocated for the (individual) object.
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070078 size_t bytes_allocated;
79 size_t usable_size;
80 size_t new_num_bytes_allocated = 0;
Mathieu Chartier6bc77742017-04-18 17:46:23 -070081 if (IsTLABAllocator(allocator)) {
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070082 byte_count = RoundUp(byte_count, space::BumpPointerSpace::kAlignment);
83 }
84 // If we have a thread local allocation we don't need to update bytes allocated.
Mathieu Chartier6bc77742017-04-18 17:46:23 -070085 if (IsTLABAllocator(allocator) && byte_count <= self->TlabSize()) {
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070086 obj = self->AllocTlab(byte_count);
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -070087 DCHECK(obj != nullptr) << "AllocTlab can't fail";
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070088 obj->SetClass(klass);
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -070089 if (kUseBakerReadBarrier) {
90 obj->AssertReadBarrierState();
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080091 }
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070092 bytes_allocated = byte_count;
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -070093 usable_size = bytes_allocated;
94 pre_fence_visitor(obj, usable_size);
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070095 QuasiAtomic::ThreadFenceForConstructor();
Mathieu Chartier9d156d52016-10-06 17:44:26 -070096 } else if (
97 !kInstrumented && allocator == kAllocatorTypeRosAlloc &&
98 (obj = rosalloc_space_->AllocThreadLocal(self, byte_count, &bytes_allocated)) != nullptr &&
99 LIKELY(obj != nullptr)) {
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700100 DCHECK(!is_running_on_memory_tool_);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700101 obj->SetClass(klass);
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -0700102 if (kUseBakerReadBarrier) {
103 obj->AssertReadBarrierState();
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700104 }
105 usable_size = bytes_allocated;
106 pre_fence_visitor(obj, usable_size);
107 QuasiAtomic::ThreadFenceForConstructor();
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700108 } else {
Roland Levillain9b869ea2018-01-31 13:46:11 +0000109 // Bytes allocated that takes bulk thread-local buffer allocations into account.
110 size_t bytes_tl_bulk_allocated = 0u;
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700111 obj = TryToAllocate<kInstrumented, false>(self, allocator, byte_count, &bytes_allocated,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700112 &usable_size, &bytes_tl_bulk_allocated);
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700113 if (UNLIKELY(obj == nullptr)) {
Mathieu Chartiereebc3af2016-02-29 18:13:38 -0800114 // AllocateInternalWithGc can cause thread suspension, if someone instruments the entrypoints
115 // or changes the allocator in a suspend point here, we need to retry the allocation.
116 obj = AllocateInternalWithGc(self,
117 allocator,
118 kInstrumented,
119 byte_count,
120 &bytes_allocated,
121 &usable_size,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700122 &bytes_tl_bulk_allocated, &klass);
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700123 if (obj == nullptr) {
Mathieu Chartiereebc3af2016-02-29 18:13:38 -0800124 // The only way that we can get a null return if there is no pending exception is if the
125 // allocator or instrumentation changed.
126 if (!self->IsExceptionPending()) {
127 // AllocObject will pick up the new allocator type, and instrumented as true is the safe
128 // default.
129 return AllocObject</*kInstrumented*/true>(self,
130 klass,
131 byte_count,
132 pre_fence_visitor);
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700133 }
134 return nullptr;
135 }
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -0700136 }
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700137 DCHECK_GT(bytes_allocated, 0u);
138 DCHECK_GT(usable_size, 0u);
139 obj->SetClass(klass);
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -0700140 if (kUseBakerReadBarrier) {
141 obj->AssertReadBarrierState();
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700142 }
143 if (collector::SemiSpace::kUseRememberedSet && UNLIKELY(allocator == kAllocatorTypeNonMoving)) {
144 // (Note this if statement will be constant folded away for the
145 // fast-path quick entry points.) Because SetClass() has no write
146 // barrier, if a non-moving space allocation, we need a write
147 // barrier as the class pointer may point to the bump pointer
148 // space (where the class pointer is an "old-to-young" reference,
149 // though rare) under the GSS collector with the remembered set
150 // enabled. We don't need this for kAllocatorTypeRosAlloc/DlMalloc
151 // cases because we don't directly allocate into the main alloc
152 // space (besides promotions) under the SS/GSS collector.
153 WriteBarrierField(obj, mirror::Object::ClassOffset(), klass);
154 }
155 pre_fence_visitor(obj, usable_size);
Hans Boehmb0171b92016-01-28 17:19:15 -0800156 QuasiAtomic::ThreadFenceForConstructor();
Roland Levillain9b869ea2018-01-31 13:46:11 +0000157 size_t num_bytes_allocated_before =
158 num_bytes_allocated_.FetchAndAddRelaxed(bytes_tl_bulk_allocated);
159 new_num_bytes_allocated = num_bytes_allocated_before + bytes_tl_bulk_allocated;
Mathieu Chartier34afcde2017-06-30 15:31:11 -0700160 if (bytes_tl_bulk_allocated > 0) {
161 // Only trace when we get an increase in the number of bytes allocated. This happens when
162 // obtaining a new TLAB and isn't often enough to hurt performance according to golem.
Roland Levillain9b869ea2018-01-31 13:46:11 +0000163 TraceHeapSize(new_num_bytes_allocated);
Mathieu Chartier34afcde2017-06-30 15:31:11 -0700164 }
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800165 }
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -0700166 if (kIsDebugBuild && Runtime::Current()->IsStarted()) {
167 CHECK_LE(obj->SizeOf(), usable_size);
168 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800169 // TODO: Deprecate.
170 if (kInstrumented) {
171 if (Runtime::Current()->HasStatsEnabled()) {
172 RuntimeStats* thread_stats = self->GetStats();
173 ++thread_stats->allocated_objects;
174 thread_stats->allocated_bytes += bytes_allocated;
175 RuntimeStats* global_stats = Runtime::Current()->GetStats();
176 ++global_stats->allocated_objects;
177 global_stats->allocated_bytes += bytes_allocated;
178 }
179 } else {
180 DCHECK(!Runtime::Current()->HasStatsEnabled());
181 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800182 if (kInstrumented) {
Man Cao8c2ff642015-05-27 17:25:30 -0700183 if (IsAllocTrackingEnabled()) {
Mathieu Chartier458b1052016-03-29 14:02:55 -0700184 // allocation_records_ is not null since it never becomes null after allocation tracking is
185 // enabled.
186 DCHECK(allocation_records_ != nullptr);
187 allocation_records_->RecordAllocation(self, &obj, bytes_allocated);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800188 }
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700189 AllocationListener* l = alloc_listener_.LoadSequentiallyConsistent();
190 if (l != nullptr) {
191 // Same as above. We assume that a listener that was once stored will never be deleted.
192 // Otherwise we'd have to perform this under a lock.
193 l->ObjectAllocated(self, &obj, bytes_allocated);
194 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800195 } else {
Man Cao8c2ff642015-05-27 17:25:30 -0700196 DCHECK(!IsAllocTrackingEnabled());
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800197 }
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800198 if (AllocatorHasAllocationStack(allocator)) {
199 PushOnAllocationStack(self, &obj);
200 }
Mathieu Chartier31000802015-06-14 14:14:37 -0700201 if (kInstrumented) {
202 if (gc_stress_mode_) {
203 CheckGcStressMode(self, &obj);
204 }
205 } else {
206 DCHECK(!gc_stress_mode_);
207 }
Roland Levillainb81e9e92017-04-20 17:35:32 +0100208 // IsGcConcurrent() isn't known at compile time so we can optimize by not checking it for
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800209 // the BumpPointer or TLAB allocators. This is nice since it allows the entire if statement to be
210 // optimized out. And for the other allocators, AllocatorMayHaveConcurrentGC is a constant since
211 // the allocator_type should be constant propagated.
Hiroshi Yamauchi3e417802014-03-20 12:03:02 -0700212 if (AllocatorMayHaveConcurrentGC(allocator) && IsGcConcurrent()) {
Mathieu Chartierf517f1a2014-03-06 15:52:27 -0800213 CheckConcurrentGC(self, new_num_bytes_allocated, &obj);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800214 }
Mathieu Chartier4e305412014-02-19 10:54:44 -0800215 VerifyObject(obj);
216 self->VerifyStack();
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700217 return obj.Ptr();
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700218}
219
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800220// The size of a thread-local allocation stack in the number of references.
221static constexpr size_t kThreadLocalAllocationStackSize = 128;
222
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700223inline void Heap::PushOnAllocationStack(Thread* self, ObjPtr<mirror::Object>* obj) {
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800224 if (kUseThreadLocalAllocationStack) {
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700225 if (UNLIKELY(!self->PushOnThreadLocalAllocationStack(obj->Ptr()))) {
Mathieu Chartierc1790162014-05-23 10:54:50 -0700226 PushOnThreadLocalAllocationStackWithInternalGC(self, obj);
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800227 }
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700228 } else if (UNLIKELY(!allocation_stack_->AtomicPushBack(obj->Ptr()))) {
Mathieu Chartierc1790162014-05-23 10:54:50 -0700229 PushOnAllocationStackWithInternalGC(self, obj);
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800230 }
231}
232
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800233template <bool kInstrumented, typename PreFenceVisitor>
Mathieu Chartiera4f6af92015-08-11 17:35:25 -0700234inline mirror::Object* Heap::AllocLargeObject(Thread* self,
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700235 ObjPtr<mirror::Class>* klass,
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800236 size_t byte_count,
237 const PreFenceVisitor& pre_fence_visitor) {
Mathieu Chartier446f9ee2014-12-01 15:00:27 -0800238 // Save and restore the class in case it moves.
239 StackHandleScope<1> hs(self);
240 auto klass_wrapper = hs.NewHandleWrapper(klass);
241 return AllocObjectWithAllocator<kInstrumented, false, PreFenceVisitor>(self, *klass, byte_count,
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800242 kAllocatorTypeLOS,
243 pre_fence_visitor);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800244}
245
246template <const bool kInstrumented, const bool kGrow>
Mathieu Chartiera4f6af92015-08-11 17:35:25 -0700247inline mirror::Object* Heap::TryToAllocate(Thread* self,
248 AllocatorType allocator_type,
249 size_t alloc_size,
250 size_t* bytes_allocated,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700251 size_t* usable_size,
252 size_t* bytes_tl_bulk_allocated) {
Mathieu Chartiera4f6af92015-08-11 17:35:25 -0700253 if (allocator_type != kAllocatorTypeTLAB &&
254 allocator_type != kAllocatorTypeRegionTLAB &&
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700255 allocator_type != kAllocatorTypeRosAlloc &&
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800256 UNLIKELY(IsOutOfMemoryOnAllocation(allocator_type, alloc_size, kGrow))) {
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800257 return nullptr;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700258 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800259 mirror::Object* ret;
260 switch (allocator_type) {
261 case kAllocatorTypeBumpPointer: {
262 DCHECK(bump_pointer_space_ != nullptr);
263 alloc_size = RoundUp(alloc_size, space::BumpPointerSpace::kAlignment);
264 ret = bump_pointer_space_->AllocNonvirtual(alloc_size);
265 if (LIKELY(ret != nullptr)) {
266 *bytes_allocated = alloc_size;
Ian Rogers6fac4472014-02-25 17:01:10 -0800267 *usable_size = alloc_size;
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700268 *bytes_tl_bulk_allocated = alloc_size;
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800269 }
270 break;
271 }
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800272 case kAllocatorTypeRosAlloc: {
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700273 if (kInstrumented && UNLIKELY(is_running_on_memory_tool_)) {
274 // If running on valgrind or asan, we should be using the instrumented path.
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700275 size_t max_bytes_tl_bulk_allocated = rosalloc_space_->MaxBytesBulkAllocatedFor(alloc_size);
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800276 if (UNLIKELY(IsOutOfMemoryOnAllocation(allocator_type,
277 max_bytes_tl_bulk_allocated,
278 kGrow))) {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700279 return nullptr;
280 }
281 ret = rosalloc_space_->Alloc(self, alloc_size, bytes_allocated, usable_size,
282 bytes_tl_bulk_allocated);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800283 } else {
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700284 DCHECK(!is_running_on_memory_tool_);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700285 size_t max_bytes_tl_bulk_allocated =
286 rosalloc_space_->MaxBytesBulkAllocatedForNonvirtual(alloc_size);
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800287 if (UNLIKELY(IsOutOfMemoryOnAllocation(allocator_type,
288 max_bytes_tl_bulk_allocated,
289 kGrow))) {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700290 return nullptr;
291 }
292 if (!kInstrumented) {
293 DCHECK(!rosalloc_space_->CanAllocThreadLocal(self, alloc_size));
294 }
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800295 ret = rosalloc_space_->AllocNonvirtual(self,
296 alloc_size,
297 bytes_allocated,
298 usable_size,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700299 bytes_tl_bulk_allocated);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800300 }
301 break;
302 }
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800303 case kAllocatorTypeDlMalloc: {
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700304 if (kInstrumented && UNLIKELY(is_running_on_memory_tool_)) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800305 // If running on valgrind, we should be using the instrumented path.
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800306 ret = dlmalloc_space_->Alloc(self,
307 alloc_size,
308 bytes_allocated,
309 usable_size,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700310 bytes_tl_bulk_allocated);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800311 } else {
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700312 DCHECK(!is_running_on_memory_tool_);
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800313 ret = dlmalloc_space_->AllocNonvirtual(self,
314 alloc_size,
315 bytes_allocated,
316 usable_size,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700317 bytes_tl_bulk_allocated);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800318 }
319 break;
320 }
321 case kAllocatorTypeNonMoving: {
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800322 ret = non_moving_space_->Alloc(self,
323 alloc_size,
324 bytes_allocated,
325 usable_size,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700326 bytes_tl_bulk_allocated);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800327 break;
328 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800329 case kAllocatorTypeLOS: {
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800330 ret = large_object_space_->Alloc(self,
331 alloc_size,
332 bytes_allocated,
333 usable_size,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700334 bytes_tl_bulk_allocated);
Hiroshi Yamauchi95a659f2013-11-22 14:43:45 -0800335 // Note that the bump pointer spaces aren't necessarily next to
336 // the other continuous spaces like the non-moving alloc space or
337 // the zygote space.
338 DCHECK(ret == nullptr || large_object_space_->Contains(ret));
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800339 break;
340 }
Nicolas Geoffray96172e02016-11-30 11:52:19 +0000341 case kAllocatorTypeRegion: {
342 DCHECK(region_space_ != nullptr);
343 alloc_size = RoundUp(alloc_size, space::RegionSpace::kAlignment);
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800344 ret = region_space_->AllocNonvirtual<false>(alloc_size,
345 bytes_allocated,
346 usable_size,
Nicolas Geoffray96172e02016-11-30 11:52:19 +0000347 bytes_tl_bulk_allocated);
348 break;
349 }
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800350 case kAllocatorTypeTLAB:
351 FALLTHROUGH_INTENDED;
Nicolas Geoffray96172e02016-11-30 11:52:19 +0000352 case kAllocatorTypeRegionTLAB: {
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800353 DCHECK_ALIGNED(alloc_size, kObjectAlignment);
354 static_assert(space::RegionSpace::kAlignment == space::BumpPointerSpace::kAlignment,
355 "mismatched alignments");
356 static_assert(kObjectAlignment == space::BumpPointerSpace::kAlignment,
357 "mismatched alignments");
Nicolas Geoffray96172e02016-11-30 11:52:19 +0000358 if (UNLIKELY(self->TlabSize() < alloc_size)) {
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800359 // kAllocatorTypeTLAB may be the allocator for region space TLAB if the GC is not marking,
360 // that is why the allocator is not passed down.
361 return AllocWithNewTLAB(self,
362 alloc_size,
363 kGrow,
364 bytes_allocated,
365 usable_size,
366 bytes_tl_bulk_allocated);
Nicolas Geoffray96172e02016-11-30 11:52:19 +0000367 }
368 // The allocation can't fail.
369 ret = self->AllocTlab(alloc_size);
370 DCHECK(ret != nullptr);
371 *bytes_allocated = alloc_size;
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800372 *bytes_tl_bulk_allocated = 0; // Allocated in an existing buffer.
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800373 *usable_size = alloc_size;
374 break;
375 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800376 default: {
377 LOG(FATAL) << "Invalid allocator type";
378 ret = nullptr;
379 }
380 }
381 return ret;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700382}
383
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700384inline bool Heap::ShouldAllocLargeObject(ObjPtr<mirror::Class> c, size_t byte_count) const {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700385 // We need to have a zygote space or else our newly allocated large object can end up in the
386 // Zygote resulting in it being prematurely freed.
387 // We can only do this for primitive objects since large objects will not be within the card table
388 // range. This also means that we rely on SetClass not dirtying the object's card.
Jeff Hao13e00912015-06-22 15:14:49 -0700389 return byte_count >= large_object_threshold_ && (c->IsPrimitiveArray() || c->IsStringClass());
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700390}
391
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800392inline bool Heap::IsOutOfMemoryOnAllocation(AllocatorType allocator_type,
393 size_t alloc_size,
394 bool grow) {
Ian Rogers3e5cf302014-05-20 16:40:37 -0700395 size_t new_footprint = num_bytes_allocated_.LoadSequentiallyConsistent() + alloc_size;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700396 if (UNLIKELY(new_footprint > max_allowed_footprint_)) {
397 if (UNLIKELY(new_footprint > growth_limit_)) {
398 return true;
399 }
Hiroshi Yamauchi3e417802014-03-20 12:03:02 -0700400 if (!AllocatorMayHaveConcurrentGC(allocator_type) || !IsGcConcurrent()) {
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800401 if (!grow) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700402 return true;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700403 }
Mathieu Chartier7bf82af2013-12-06 16:51:45 -0800404 // TODO: Grow for allocation is racy, fix it.
Andreas Gampe170331f2017-12-07 18:41:03 -0800405 VlogHeapGrowth(max_allowed_footprint_, new_footprint, alloc_size);
Mathieu Chartier7bf82af2013-12-06 16:51:45 -0800406 max_allowed_footprint_ = new_footprint;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700407 }
408 }
409 return false;
410}
411
Mathieu Chartiera4f6af92015-08-11 17:35:25 -0700412inline void Heap::CheckConcurrentGC(Thread* self,
413 size_t new_num_bytes_allocated,
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700414 ObjPtr<mirror::Object>* obj) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700415 if (UNLIKELY(new_num_bytes_allocated >= concurrent_start_bytes_)) {
Hiroshi Yamauchi0ae98992015-05-01 14:33:19 -0700416 RequestConcurrentGCAndSaveObject(self, false, obj);
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700417 }
418}
419
Mathieu Chartiera058fdf2016-10-06 15:13:58 -0700420inline void Heap::WriteBarrierField(ObjPtr<mirror::Object> dst,
421 MemberOffset offset ATTRIBUTE_UNUSED,
422 ObjPtr<mirror::Object> new_value ATTRIBUTE_UNUSED) {
423 card_table_->MarkCard(dst.Ptr());
424}
425
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700426inline void Heap::WriteBarrierArray(ObjPtr<mirror::Object> dst,
427 int start_offset ATTRIBUTE_UNUSED,
428 size_t length ATTRIBUTE_UNUSED) {
429 card_table_->MarkCard(dst.Ptr());
430}
431
432inline void Heap::WriteBarrierEveryFieldOf(ObjPtr<mirror::Object> obj) {
433 card_table_->MarkCard(obj.Ptr());
434}
435
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700436} // namespace gc
437} // namespace art
438
439#endif // ART_RUNTIME_GC_HEAP_INL_H_