blob: a50d12545705cddfbc689989677d35955c4319b5 [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"
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -080024#include "gc/accounting/card_table-inl.h"
Man Cao8c2ff642015-05-27 17:25:30 -070025#include "gc/allocation_record.h"
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -080026#include "gc/collector/semi_space.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070027#include "gc/space/bump_pointer_space-inl.h"
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070028#include "gc/space/dlmalloc_space-inl.h"
29#include "gc/space/large_object_space.h"
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080030#include "gc/space/region_space-inl.h"
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070031#include "gc/space/rosalloc_space-inl.h"
Mathieu Chartiera058fdf2016-10-06 15:13:58 -070032#include "obj_ptr-inl.h"
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070033#include "runtime.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070034#include "handle_scope-inl.h"
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070035#include "thread-inl.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010036#include "utils.h"
Andreas Gampe90b936d2017-01-31 08:58:55 -080037#include "verify_object.h"
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070038
39namespace art {
40namespace gc {
41
Mathieu Chartier692fafd2013-11-29 17:24:40 -080042template <bool kInstrumented, bool kCheckLargeObject, typename PreFenceVisitor>
Mathieu Chartiera4f6af92015-08-11 17:35:25 -070043inline mirror::Object* Heap::AllocObjectWithAllocator(Thread* self,
Mathieu Chartier9d156d52016-10-06 17:44:26 -070044 ObjPtr<mirror::Class> klass,
Mathieu Chartiera4f6af92015-08-11 17:35:25 -070045 size_t byte_count,
46 AllocatorType allocator,
Mathieu Chartier1febddf2013-11-20 12:33:14 -080047 const PreFenceVisitor& pre_fence_visitor) {
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080048 if (kIsDebugBuild) {
49 CheckPreconditionsForAllocObject(klass, byte_count);
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070050 // Since allocation can cause a GC which will need to SuspendAll, make sure all allocations are
51 // done in the runnable state where suspension is expected.
52 CHECK_EQ(self->GetState(), kRunnable);
53 self->AssertThreadSuspensionIsAllowable();
Mathieu Chartier8502f722016-06-08 15:09:08 -070054 self->AssertNoPendingException();
Mathieu Chartier9d156d52016-10-06 17:44:26 -070055 // Make sure to preserve klass.
56 StackHandleScope<1> hs(self);
57 HandleWrapperObjPtr<mirror::Class> h = hs.NewHandleWrapper(&klass);
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070058 self->PoisonObjectPointers();
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080059 }
Mathieu Chartierc528dba2013-11-26 12:00:11 -080060 // Need to check that we arent the large object allocator since the large object allocation code
61 // path this function. If we didn't check we would have an infinite loop.
Mathieu Chartier9d156d52016-10-06 17:44:26 -070062 ObjPtr<mirror::Object> obj;
Mathieu Chartier446f9ee2014-12-01 15:00:27 -080063 if (kCheckLargeObject && UNLIKELY(ShouldAllocLargeObject(klass, byte_count))) {
64 obj = AllocLargeObject<kInstrumented, PreFenceVisitor>(self, &klass, byte_count,
65 pre_fence_visitor);
66 if (obj != nullptr) {
Mathieu Chartier9d156d52016-10-06 17:44:26 -070067 return obj.Ptr();
Mathieu Chartier446f9ee2014-12-01 15:00:27 -080068 } else {
69 // There should be an OOM exception, since we are retrying, clear it.
70 self->ClearException();
71 }
72 // If the large object allocation failed, try to use the normal spaces (main space,
73 // non moving space). This can happen if there is significant virtual address space
74 // fragmentation.
75 }
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070076 // bytes allocated for the (individual) object.
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070077 size_t bytes_allocated;
78 size_t usable_size;
79 size_t new_num_bytes_allocated = 0;
Mathieu Chartier6bc77742017-04-18 17:46:23 -070080 if (IsTLABAllocator(allocator)) {
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070081 byte_count = RoundUp(byte_count, space::BumpPointerSpace::kAlignment);
82 }
83 // If we have a thread local allocation we don't need to update bytes allocated.
Mathieu Chartier6bc77742017-04-18 17:46:23 -070084 if (IsTLABAllocator(allocator) && byte_count <= self->TlabSize()) {
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070085 obj = self->AllocTlab(byte_count);
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -070086 DCHECK(obj != nullptr) << "AllocTlab can't fail";
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070087 obj->SetClass(klass);
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -070088 if (kUseBakerReadBarrier) {
89 obj->AssertReadBarrierState();
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080090 }
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070091 bytes_allocated = byte_count;
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -070092 usable_size = bytes_allocated;
93 pre_fence_visitor(obj, usable_size);
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070094 QuasiAtomic::ThreadFenceForConstructor();
Mathieu Chartier9d156d52016-10-06 17:44:26 -070095 } else if (
96 !kInstrumented && allocator == kAllocatorTypeRosAlloc &&
97 (obj = rosalloc_space_->AllocThreadLocal(self, byte_count, &bytes_allocated)) != nullptr &&
98 LIKELY(obj != nullptr)) {
Evgenii Stepanov1e133742015-05-20 12:30:59 -070099 DCHECK(!is_running_on_memory_tool_);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700100 obj->SetClass(klass);
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -0700101 if (kUseBakerReadBarrier) {
102 obj->AssertReadBarrierState();
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700103 }
104 usable_size = bytes_allocated;
105 pre_fence_visitor(obj, usable_size);
106 QuasiAtomic::ThreadFenceForConstructor();
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700107 } else {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700108 // bytes allocated that takes bulk thread-local buffer allocations into account.
109 size_t bytes_tl_bulk_allocated = 0;
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700110 obj = TryToAllocate<kInstrumented, false>(self, allocator, byte_count, &bytes_allocated,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700111 &usable_size, &bytes_tl_bulk_allocated);
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700112 if (UNLIKELY(obj == nullptr)) {
Mathieu Chartiereebc3af2016-02-29 18:13:38 -0800113 // AllocateInternalWithGc can cause thread suspension, if someone instruments the entrypoints
114 // or changes the allocator in a suspend point here, we need to retry the allocation.
115 obj = AllocateInternalWithGc(self,
116 allocator,
117 kInstrumented,
118 byte_count,
119 &bytes_allocated,
120 &usable_size,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700121 &bytes_tl_bulk_allocated, &klass);
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700122 if (obj == nullptr) {
Mathieu Chartiereebc3af2016-02-29 18:13:38 -0800123 // The only way that we can get a null return if there is no pending exception is if the
124 // allocator or instrumentation changed.
125 if (!self->IsExceptionPending()) {
126 // AllocObject will pick up the new allocator type, and instrumented as true is the safe
127 // default.
128 return AllocObject</*kInstrumented*/true>(self,
129 klass,
130 byte_count,
131 pre_fence_visitor);
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700132 }
133 return nullptr;
134 }
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -0700135 }
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700136 DCHECK_GT(bytes_allocated, 0u);
137 DCHECK_GT(usable_size, 0u);
138 obj->SetClass(klass);
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -0700139 if (kUseBakerReadBarrier) {
140 obj->AssertReadBarrierState();
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700141 }
142 if (collector::SemiSpace::kUseRememberedSet && UNLIKELY(allocator == kAllocatorTypeNonMoving)) {
143 // (Note this if statement will be constant folded away for the
144 // fast-path quick entry points.) Because SetClass() has no write
145 // barrier, if a non-moving space allocation, we need a write
146 // barrier as the class pointer may point to the bump pointer
147 // space (where the class pointer is an "old-to-young" reference,
148 // though rare) under the GSS collector with the remembered set
149 // enabled. We don't need this for kAllocatorTypeRosAlloc/DlMalloc
150 // cases because we don't directly allocate into the main alloc
151 // space (besides promotions) under the SS/GSS collector.
152 WriteBarrierField(obj, mirror::Object::ClassOffset(), klass);
153 }
154 pre_fence_visitor(obj, usable_size);
Hans Boehmb0171b92016-01-28 17:19:15 -0800155 QuasiAtomic::ThreadFenceForConstructor();
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700156 new_num_bytes_allocated = static_cast<size_t>(
Hans Boehmb0171b92016-01-28 17:19:15 -0800157 num_bytes_allocated_.FetchAndAddRelaxed(bytes_tl_bulk_allocated)) + bytes_tl_bulk_allocated;
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800158 }
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -0700159 if (kIsDebugBuild && Runtime::Current()->IsStarted()) {
160 CHECK_LE(obj->SizeOf(), usable_size);
161 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800162 // TODO: Deprecate.
163 if (kInstrumented) {
164 if (Runtime::Current()->HasStatsEnabled()) {
165 RuntimeStats* thread_stats = self->GetStats();
166 ++thread_stats->allocated_objects;
167 thread_stats->allocated_bytes += bytes_allocated;
168 RuntimeStats* global_stats = Runtime::Current()->GetStats();
169 ++global_stats->allocated_objects;
170 global_stats->allocated_bytes += bytes_allocated;
171 }
172 } else {
173 DCHECK(!Runtime::Current()->HasStatsEnabled());
174 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800175 if (kInstrumented) {
Man Cao8c2ff642015-05-27 17:25:30 -0700176 if (IsAllocTrackingEnabled()) {
Mathieu Chartier458b1052016-03-29 14:02:55 -0700177 // allocation_records_ is not null since it never becomes null after allocation tracking is
178 // enabled.
179 DCHECK(allocation_records_ != nullptr);
180 allocation_records_->RecordAllocation(self, &obj, bytes_allocated);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800181 }
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700182 AllocationListener* l = alloc_listener_.LoadSequentiallyConsistent();
183 if (l != nullptr) {
184 // Same as above. We assume that a listener that was once stored will never be deleted.
185 // Otherwise we'd have to perform this under a lock.
186 l->ObjectAllocated(self, &obj, bytes_allocated);
187 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800188 } else {
Man Cao8c2ff642015-05-27 17:25:30 -0700189 DCHECK(!IsAllocTrackingEnabled());
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800190 }
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800191 if (AllocatorHasAllocationStack(allocator)) {
192 PushOnAllocationStack(self, &obj);
193 }
Mathieu Chartier31000802015-06-14 14:14:37 -0700194 if (kInstrumented) {
195 if (gc_stress_mode_) {
196 CheckGcStressMode(self, &obj);
197 }
198 } else {
199 DCHECK(!gc_stress_mode_);
200 }
Hiroshi Yamauchi3e417802014-03-20 12:03:02 -0700201 // IsConcurrentGc() isn't known at compile time so we can optimize by not checking it for
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800202 // the BumpPointer or TLAB allocators. This is nice since it allows the entire if statement to be
203 // optimized out. And for the other allocators, AllocatorMayHaveConcurrentGC is a constant since
204 // the allocator_type should be constant propagated.
Hiroshi Yamauchi3e417802014-03-20 12:03:02 -0700205 if (AllocatorMayHaveConcurrentGC(allocator) && IsGcConcurrent()) {
Mathieu Chartierf517f1a2014-03-06 15:52:27 -0800206 CheckConcurrentGC(self, new_num_bytes_allocated, &obj);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800207 }
Mathieu Chartier4e305412014-02-19 10:54:44 -0800208 VerifyObject(obj);
209 self->VerifyStack();
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700210 return obj.Ptr();
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700211}
212
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800213// The size of a thread-local allocation stack in the number of references.
214static constexpr size_t kThreadLocalAllocationStackSize = 128;
215
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700216inline void Heap::PushOnAllocationStack(Thread* self, ObjPtr<mirror::Object>* obj) {
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800217 if (kUseThreadLocalAllocationStack) {
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700218 if (UNLIKELY(!self->PushOnThreadLocalAllocationStack(obj->Ptr()))) {
Mathieu Chartierc1790162014-05-23 10:54:50 -0700219 PushOnThreadLocalAllocationStackWithInternalGC(self, obj);
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800220 }
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700221 } else if (UNLIKELY(!allocation_stack_->AtomicPushBack(obj->Ptr()))) {
Mathieu Chartierc1790162014-05-23 10:54:50 -0700222 PushOnAllocationStackWithInternalGC(self, obj);
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800223 }
224}
225
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800226template <bool kInstrumented, typename PreFenceVisitor>
Mathieu Chartiera4f6af92015-08-11 17:35:25 -0700227inline mirror::Object* Heap::AllocLargeObject(Thread* self,
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700228 ObjPtr<mirror::Class>* klass,
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800229 size_t byte_count,
230 const PreFenceVisitor& pre_fence_visitor) {
Mathieu Chartier446f9ee2014-12-01 15:00:27 -0800231 // Save and restore the class in case it moves.
232 StackHandleScope<1> hs(self);
233 auto klass_wrapper = hs.NewHandleWrapper(klass);
234 return AllocObjectWithAllocator<kInstrumented, false, PreFenceVisitor>(self, *klass, byte_count,
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800235 kAllocatorTypeLOS,
236 pre_fence_visitor);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800237}
238
239template <const bool kInstrumented, const bool kGrow>
Mathieu Chartiera4f6af92015-08-11 17:35:25 -0700240inline mirror::Object* Heap::TryToAllocate(Thread* self,
241 AllocatorType allocator_type,
242 size_t alloc_size,
243 size_t* bytes_allocated,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700244 size_t* usable_size,
245 size_t* bytes_tl_bulk_allocated) {
Mathieu Chartiera4f6af92015-08-11 17:35:25 -0700246 if (allocator_type != kAllocatorTypeTLAB &&
247 allocator_type != kAllocatorTypeRegionTLAB &&
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700248 allocator_type != kAllocatorTypeRosAlloc &&
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800249 UNLIKELY(IsOutOfMemoryOnAllocation(allocator_type, alloc_size, kGrow))) {
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800250 return nullptr;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700251 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800252 mirror::Object* ret;
253 switch (allocator_type) {
254 case kAllocatorTypeBumpPointer: {
255 DCHECK(bump_pointer_space_ != nullptr);
256 alloc_size = RoundUp(alloc_size, space::BumpPointerSpace::kAlignment);
257 ret = bump_pointer_space_->AllocNonvirtual(alloc_size);
258 if (LIKELY(ret != nullptr)) {
259 *bytes_allocated = alloc_size;
Ian Rogers6fac4472014-02-25 17:01:10 -0800260 *usable_size = alloc_size;
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700261 *bytes_tl_bulk_allocated = alloc_size;
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800262 }
263 break;
264 }
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800265 case kAllocatorTypeRosAlloc: {
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700266 if (kInstrumented && UNLIKELY(is_running_on_memory_tool_)) {
267 // If running on valgrind or asan, we should be using the instrumented path.
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700268 size_t max_bytes_tl_bulk_allocated = rosalloc_space_->MaxBytesBulkAllocatedFor(alloc_size);
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800269 if (UNLIKELY(IsOutOfMemoryOnAllocation(allocator_type,
270 max_bytes_tl_bulk_allocated,
271 kGrow))) {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700272 return nullptr;
273 }
274 ret = rosalloc_space_->Alloc(self, alloc_size, bytes_allocated, usable_size,
275 bytes_tl_bulk_allocated);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800276 } else {
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700277 DCHECK(!is_running_on_memory_tool_);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700278 size_t max_bytes_tl_bulk_allocated =
279 rosalloc_space_->MaxBytesBulkAllocatedForNonvirtual(alloc_size);
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800280 if (UNLIKELY(IsOutOfMemoryOnAllocation(allocator_type,
281 max_bytes_tl_bulk_allocated,
282 kGrow))) {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700283 return nullptr;
284 }
285 if (!kInstrumented) {
286 DCHECK(!rosalloc_space_->CanAllocThreadLocal(self, alloc_size));
287 }
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800288 ret = rosalloc_space_->AllocNonvirtual(self,
289 alloc_size,
290 bytes_allocated,
291 usable_size,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700292 bytes_tl_bulk_allocated);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800293 }
294 break;
295 }
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800296 case kAllocatorTypeDlMalloc: {
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700297 if (kInstrumented && UNLIKELY(is_running_on_memory_tool_)) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800298 // If running on valgrind, we should be using the instrumented path.
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800299 ret = dlmalloc_space_->Alloc(self,
300 alloc_size,
301 bytes_allocated,
302 usable_size,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700303 bytes_tl_bulk_allocated);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800304 } else {
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700305 DCHECK(!is_running_on_memory_tool_);
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800306 ret = dlmalloc_space_->AllocNonvirtual(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 }
312 break;
313 }
314 case kAllocatorTypeNonMoving: {
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800315 ret = non_moving_space_->Alloc(self,
316 alloc_size,
317 bytes_allocated,
318 usable_size,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700319 bytes_tl_bulk_allocated);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800320 break;
321 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800322 case kAllocatorTypeLOS: {
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800323 ret = large_object_space_->Alloc(self,
324 alloc_size,
325 bytes_allocated,
326 usable_size,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700327 bytes_tl_bulk_allocated);
Hiroshi Yamauchi95a659f2013-11-22 14:43:45 -0800328 // Note that the bump pointer spaces aren't necessarily next to
329 // the other continuous spaces like the non-moving alloc space or
330 // the zygote space.
331 DCHECK(ret == nullptr || large_object_space_->Contains(ret));
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800332 break;
333 }
Nicolas Geoffray96172e02016-11-30 11:52:19 +0000334 case kAllocatorTypeRegion: {
335 DCHECK(region_space_ != nullptr);
336 alloc_size = RoundUp(alloc_size, space::RegionSpace::kAlignment);
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800337 ret = region_space_->AllocNonvirtual<false>(alloc_size,
338 bytes_allocated,
339 usable_size,
Nicolas Geoffray96172e02016-11-30 11:52:19 +0000340 bytes_tl_bulk_allocated);
341 break;
342 }
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800343 case kAllocatorTypeTLAB:
344 FALLTHROUGH_INTENDED;
Nicolas Geoffray96172e02016-11-30 11:52:19 +0000345 case kAllocatorTypeRegionTLAB: {
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800346 DCHECK_ALIGNED(alloc_size, kObjectAlignment);
347 static_assert(space::RegionSpace::kAlignment == space::BumpPointerSpace::kAlignment,
348 "mismatched alignments");
349 static_assert(kObjectAlignment == space::BumpPointerSpace::kAlignment,
350 "mismatched alignments");
Nicolas Geoffray96172e02016-11-30 11:52:19 +0000351 if (UNLIKELY(self->TlabSize() < alloc_size)) {
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800352 // kAllocatorTypeTLAB may be the allocator for region space TLAB if the GC is not marking,
353 // that is why the allocator is not passed down.
354 return AllocWithNewTLAB(self,
355 alloc_size,
356 kGrow,
357 bytes_allocated,
358 usable_size,
359 bytes_tl_bulk_allocated);
Nicolas Geoffray96172e02016-11-30 11:52:19 +0000360 }
361 // The allocation can't fail.
362 ret = self->AllocTlab(alloc_size);
363 DCHECK(ret != nullptr);
364 *bytes_allocated = alloc_size;
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800365 *bytes_tl_bulk_allocated = 0; // Allocated in an existing buffer.
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800366 *usable_size = alloc_size;
367 break;
368 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800369 default: {
370 LOG(FATAL) << "Invalid allocator type";
371 ret = nullptr;
372 }
373 }
374 return ret;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700375}
376
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700377inline bool Heap::ShouldAllocLargeObject(ObjPtr<mirror::Class> c, size_t byte_count) const {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700378 // We need to have a zygote space or else our newly allocated large object can end up in the
379 // Zygote resulting in it being prematurely freed.
380 // We can only do this for primitive objects since large objects will not be within the card table
381 // range. This also means that we rely on SetClass not dirtying the object's card.
Jeff Hao13e00912015-06-22 15:14:49 -0700382 return byte_count >= large_object_threshold_ && (c->IsPrimitiveArray() || c->IsStringClass());
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700383}
384
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800385inline bool Heap::IsOutOfMemoryOnAllocation(AllocatorType allocator_type,
386 size_t alloc_size,
387 bool grow) {
Ian Rogers3e5cf302014-05-20 16:40:37 -0700388 size_t new_footprint = num_bytes_allocated_.LoadSequentiallyConsistent() + alloc_size;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700389 if (UNLIKELY(new_footprint > max_allowed_footprint_)) {
390 if (UNLIKELY(new_footprint > growth_limit_)) {
391 return true;
392 }
Hiroshi Yamauchi3e417802014-03-20 12:03:02 -0700393 if (!AllocatorMayHaveConcurrentGC(allocator_type) || !IsGcConcurrent()) {
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800394 if (!grow) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700395 return true;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700396 }
Mathieu Chartier7bf82af2013-12-06 16:51:45 -0800397 // TODO: Grow for allocation is racy, fix it.
398 VLOG(heap) << "Growing heap from " << PrettySize(max_allowed_footprint_) << " to "
399 << PrettySize(new_footprint) << " for a " << PrettySize(alloc_size) << " allocation";
400 max_allowed_footprint_ = new_footprint;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700401 }
402 }
403 return false;
404}
405
Mathieu Chartiera4f6af92015-08-11 17:35:25 -0700406inline void Heap::CheckConcurrentGC(Thread* self,
407 size_t new_num_bytes_allocated,
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700408 ObjPtr<mirror::Object>* obj) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700409 if (UNLIKELY(new_num_bytes_allocated >= concurrent_start_bytes_)) {
Hiroshi Yamauchi0ae98992015-05-01 14:33:19 -0700410 RequestConcurrentGCAndSaveObject(self, false, obj);
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700411 }
412}
413
Mathieu Chartiera058fdf2016-10-06 15:13:58 -0700414inline void Heap::WriteBarrierField(ObjPtr<mirror::Object> dst,
415 MemberOffset offset ATTRIBUTE_UNUSED,
416 ObjPtr<mirror::Object> new_value ATTRIBUTE_UNUSED) {
417 card_table_->MarkCard(dst.Ptr());
418}
419
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700420inline void Heap::WriteBarrierArray(ObjPtr<mirror::Object> dst,
421 int start_offset ATTRIBUTE_UNUSED,
422 size_t length ATTRIBUTE_UNUSED) {
423 card_table_->MarkCard(dst.Ptr());
424}
425
426inline void Heap::WriteBarrierEveryFieldOf(ObjPtr<mirror::Object> obj) {
427 card_table_->MarkCard(obj.Ptr());
428}
429
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700430} // namespace gc
431} // namespace art
432
433#endif // ART_RUNTIME_GC_HEAP_INL_H_