blob: ee4568ecea1970c087b8f6e3be2472b18c8a115b [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
Vladimir Marko80afd022015-05-19 18:08:00 +010022#include "base/time_utils.h"
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070023#include "debugger.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"
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070032#include "runtime.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070033#include "handle_scope-inl.h"
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070034#include "thread-inl.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010035#include "utils.h"
Mathieu Chartier4e305412014-02-19 10:54:44 -080036#include "verify_object-inl.h"
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070037
38namespace art {
39namespace gc {
40
Mathieu Chartier692fafd2013-11-29 17:24:40 -080041template <bool kInstrumented, bool kCheckLargeObject, typename PreFenceVisitor>
Mathieu Chartier1febddf2013-11-20 12:33:14 -080042inline mirror::Object* Heap::AllocObjectWithAllocator(Thread* self, mirror::Class* klass,
43 size_t byte_count, AllocatorType allocator,
44 const PreFenceVisitor& pre_fence_visitor) {
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080045 if (kIsDebugBuild) {
46 CheckPreconditionsForAllocObject(klass, byte_count);
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070047 // Since allocation can cause a GC which will need to SuspendAll, make sure all allocations are
48 // done in the runnable state where suspension is expected.
49 CHECK_EQ(self->GetState(), kRunnable);
50 self->AssertThreadSuspensionIsAllowable();
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080051 }
Mathieu Chartierc528dba2013-11-26 12:00:11 -080052 // Need to check that we arent the large object allocator since the large object allocation code
53 // path this function. If we didn't check we would have an infinite loop.
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080054 mirror::Object* obj;
Mathieu Chartier446f9ee2014-12-01 15:00:27 -080055 if (kCheckLargeObject && UNLIKELY(ShouldAllocLargeObject(klass, byte_count))) {
56 obj = AllocLargeObject<kInstrumented, PreFenceVisitor>(self, &klass, byte_count,
57 pre_fence_visitor);
58 if (obj != nullptr) {
59 return obj;
60 } else {
61 // There should be an OOM exception, since we are retrying, clear it.
62 self->ClearException();
63 }
64 // If the large object allocation failed, try to use the normal spaces (main space,
65 // non moving space). This can happen if there is significant virtual address space
66 // fragmentation.
67 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080068 AllocationTimer alloc_timer(this, &obj);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070069 // bytes allocated for the (individual) object.
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070070 size_t bytes_allocated;
71 size_t usable_size;
72 size_t new_num_bytes_allocated = 0;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080073 if (allocator == kAllocatorTypeTLAB || allocator == kAllocatorTypeRegionTLAB) {
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070074 byte_count = RoundUp(byte_count, space::BumpPointerSpace::kAlignment);
75 }
76 // If we have a thread local allocation we don't need to update bytes allocated.
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080077 if ((allocator == kAllocatorTypeTLAB || allocator == kAllocatorTypeRegionTLAB) &&
78 byte_count <= self->TlabSize()) {
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070079 obj = self->AllocTlab(byte_count);
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -070080 DCHECK(obj != nullptr) << "AllocTlab can't fail";
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070081 obj->SetClass(klass);
82 if (kUseBakerOrBrooksReadBarrier) {
83 if (kUseBrooksReadBarrier) {
84 obj->SetReadBarrierPointer(obj);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080085 }
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070086 obj->AssertReadBarrierPointer();
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080087 }
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070088 bytes_allocated = byte_count;
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -070089 usable_size = bytes_allocated;
90 pre_fence_visitor(obj, usable_size);
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070091 QuasiAtomic::ThreadFenceForConstructor();
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070092 } else if (!kInstrumented && allocator == kAllocatorTypeRosAlloc &&
93 (obj = rosalloc_space_->AllocThreadLocal(self, byte_count, &bytes_allocated)) &&
94 LIKELY(obj != nullptr)) {
95 DCHECK(!running_on_valgrind_);
96 obj->SetClass(klass);
97 if (kUseBakerOrBrooksReadBarrier) {
98 if (kUseBrooksReadBarrier) {
99 obj->SetReadBarrierPointer(obj);
100 }
101 obj->AssertReadBarrierPointer();
102 }
103 usable_size = bytes_allocated;
104 pre_fence_visitor(obj, usable_size);
105 QuasiAtomic::ThreadFenceForConstructor();
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700106 } else {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700107 // bytes allocated that takes bulk thread-local buffer allocations into account.
108 size_t bytes_tl_bulk_allocated = 0;
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700109 obj = TryToAllocate<kInstrumented, false>(self, allocator, byte_count, &bytes_allocated,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700110 &usable_size, &bytes_tl_bulk_allocated);
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700111 if (UNLIKELY(obj == nullptr)) {
112 bool is_current_allocator = allocator == GetCurrentAllocator();
113 obj = AllocateInternalWithGc(self, allocator, byte_count, &bytes_allocated, &usable_size,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700114 &bytes_tl_bulk_allocated, &klass);
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700115 if (obj == nullptr) {
116 bool after_is_current_allocator = allocator == GetCurrentAllocator();
Mathieu Chartier8e705192014-08-20 18:19:23 -0700117 // If there is a pending exception, fail the allocation right away since the next one
118 // could cause OOM and abort the runtime.
119 if (!self->IsExceptionPending() && is_current_allocator && !after_is_current_allocator) {
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700120 // If the allocator changed, we need to restart the allocation.
121 return AllocObject<kInstrumented>(self, klass, byte_count, pre_fence_visitor);
122 }
123 return nullptr;
124 }
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -0700125 }
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700126 DCHECK_GT(bytes_allocated, 0u);
127 DCHECK_GT(usable_size, 0u);
128 obj->SetClass(klass);
129 if (kUseBakerOrBrooksReadBarrier) {
130 if (kUseBrooksReadBarrier) {
131 obj->SetReadBarrierPointer(obj);
132 }
133 obj->AssertReadBarrierPointer();
134 }
135 if (collector::SemiSpace::kUseRememberedSet && UNLIKELY(allocator == kAllocatorTypeNonMoving)) {
136 // (Note this if statement will be constant folded away for the
137 // fast-path quick entry points.) Because SetClass() has no write
138 // barrier, if a non-moving space allocation, we need a write
139 // barrier as the class pointer may point to the bump pointer
140 // space (where the class pointer is an "old-to-young" reference,
141 // though rare) under the GSS collector with the remembered set
142 // enabled. We don't need this for kAllocatorTypeRosAlloc/DlMalloc
143 // cases because we don't directly allocate into the main alloc
144 // space (besides promotions) under the SS/GSS collector.
145 WriteBarrierField(obj, mirror::Object::ClassOffset(), klass);
146 }
147 pre_fence_visitor(obj, usable_size);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700148 new_num_bytes_allocated = static_cast<size_t>(
149 num_bytes_allocated_.FetchAndAddSequentiallyConsistent(bytes_tl_bulk_allocated))
150 + bytes_tl_bulk_allocated;
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800151 }
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -0700152 if (kIsDebugBuild && Runtime::Current()->IsStarted()) {
153 CHECK_LE(obj->SizeOf(), usable_size);
154 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800155 // TODO: Deprecate.
156 if (kInstrumented) {
157 if (Runtime::Current()->HasStatsEnabled()) {
158 RuntimeStats* thread_stats = self->GetStats();
159 ++thread_stats->allocated_objects;
160 thread_stats->allocated_bytes += bytes_allocated;
161 RuntimeStats* global_stats = Runtime::Current()->GetStats();
162 ++global_stats->allocated_objects;
163 global_stats->allocated_bytes += bytes_allocated;
164 }
165 } else {
166 DCHECK(!Runtime::Current()->HasStatsEnabled());
167 }
168 if (AllocatorHasAllocationStack(allocator)) {
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700169 PushOnAllocationStack(self, &obj);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800170 }
171 if (kInstrumented) {
Man Cao8c2ff642015-05-27 17:25:30 -0700172 if (IsAllocTrackingEnabled()) {
173 AllocRecordObjectMap::RecordAllocation(self, obj, bytes_allocated);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800174 }
175 } else {
Man Cao8c2ff642015-05-27 17:25:30 -0700176 DCHECK(!IsAllocTrackingEnabled());
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800177 }
Hiroshi Yamauchi3e417802014-03-20 12:03:02 -0700178 // IsConcurrentGc() isn't known at compile time so we can optimize by not checking it for
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800179 // the BumpPointer or TLAB allocators. This is nice since it allows the entire if statement to be
180 // optimized out. And for the other allocators, AllocatorMayHaveConcurrentGC is a constant since
181 // the allocator_type should be constant propagated.
Hiroshi Yamauchi3e417802014-03-20 12:03:02 -0700182 if (AllocatorMayHaveConcurrentGC(allocator) && IsGcConcurrent()) {
Mathieu Chartierf517f1a2014-03-06 15:52:27 -0800183 CheckConcurrentGC(self, new_num_bytes_allocated, &obj);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800184 }
Mathieu Chartier4e305412014-02-19 10:54:44 -0800185 VerifyObject(obj);
186 self->VerifyStack();
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800187 return obj;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700188}
189
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800190// The size of a thread-local allocation stack in the number of references.
191static constexpr size_t kThreadLocalAllocationStackSize = 128;
192
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700193inline void Heap::PushOnAllocationStack(Thread* self, mirror::Object** obj) {
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800194 if (kUseThreadLocalAllocationStack) {
Mathieu Chartierc1790162014-05-23 10:54:50 -0700195 if (UNLIKELY(!self->PushOnThreadLocalAllocationStack(*obj))) {
196 PushOnThreadLocalAllocationStackWithInternalGC(self, obj);
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800197 }
Mathieu Chartierc1790162014-05-23 10:54:50 -0700198 } else if (UNLIKELY(!allocation_stack_->AtomicPushBack(*obj))) {
199 PushOnAllocationStackWithInternalGC(self, obj);
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800200 }
201}
202
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800203template <bool kInstrumented, typename PreFenceVisitor>
Mathieu Chartier446f9ee2014-12-01 15:00:27 -0800204inline mirror::Object* Heap::AllocLargeObject(Thread* self, mirror::Class** klass,
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800205 size_t byte_count,
206 const PreFenceVisitor& pre_fence_visitor) {
Mathieu Chartier446f9ee2014-12-01 15:00:27 -0800207 // Save and restore the class in case it moves.
208 StackHandleScope<1> hs(self);
209 auto klass_wrapper = hs.NewHandleWrapper(klass);
210 return AllocObjectWithAllocator<kInstrumented, false, PreFenceVisitor>(self, *klass, byte_count,
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800211 kAllocatorTypeLOS,
212 pre_fence_visitor);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800213}
214
215template <const bool kInstrumented, const bool kGrow>
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800216inline mirror::Object* Heap::TryToAllocate(Thread* self, AllocatorType allocator_type,
Ian Rogers6fac4472014-02-25 17:01:10 -0800217 size_t alloc_size, size_t* bytes_allocated,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700218 size_t* usable_size,
219 size_t* bytes_tl_bulk_allocated) {
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800220 if (allocator_type != kAllocatorTypeTLAB && allocator_type != kAllocatorTypeRegionTLAB &&
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700221 allocator_type != kAllocatorTypeRosAlloc &&
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700222 UNLIKELY(IsOutOfMemoryOnAllocation<kGrow>(allocator_type, alloc_size))) {
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800223 return nullptr;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700224 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800225 mirror::Object* ret;
226 switch (allocator_type) {
227 case kAllocatorTypeBumpPointer: {
228 DCHECK(bump_pointer_space_ != nullptr);
229 alloc_size = RoundUp(alloc_size, space::BumpPointerSpace::kAlignment);
230 ret = bump_pointer_space_->AllocNonvirtual(alloc_size);
231 if (LIKELY(ret != nullptr)) {
232 *bytes_allocated = alloc_size;
Ian Rogers6fac4472014-02-25 17:01:10 -0800233 *usable_size = alloc_size;
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700234 *bytes_tl_bulk_allocated = alloc_size;
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800235 }
236 break;
237 }
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800238 case kAllocatorTypeRosAlloc: {
239 if (kInstrumented && UNLIKELY(running_on_valgrind_)) {
240 // If running on valgrind, we should be using the instrumented path.
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700241 size_t max_bytes_tl_bulk_allocated = rosalloc_space_->MaxBytesBulkAllocatedFor(alloc_size);
242 if (UNLIKELY(IsOutOfMemoryOnAllocation<kGrow>(allocator_type,
243 max_bytes_tl_bulk_allocated))) {
244 return nullptr;
245 }
246 ret = rosalloc_space_->Alloc(self, alloc_size, bytes_allocated, usable_size,
247 bytes_tl_bulk_allocated);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800248 } else {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800249 DCHECK(!running_on_valgrind_);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700250 size_t max_bytes_tl_bulk_allocated =
251 rosalloc_space_->MaxBytesBulkAllocatedForNonvirtual(alloc_size);
252 if (UNLIKELY(IsOutOfMemoryOnAllocation<kGrow>(allocator_type,
253 max_bytes_tl_bulk_allocated))) {
254 return nullptr;
255 }
256 if (!kInstrumented) {
257 DCHECK(!rosalloc_space_->CanAllocThreadLocal(self, alloc_size));
258 }
259 ret = rosalloc_space_->AllocNonvirtual(self, alloc_size, bytes_allocated, usable_size,
260 bytes_tl_bulk_allocated);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800261 }
262 break;
263 }
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800264 case kAllocatorTypeDlMalloc: {
265 if (kInstrumented && UNLIKELY(running_on_valgrind_)) {
266 // If running on valgrind, we should be using the instrumented path.
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700267 ret = dlmalloc_space_->Alloc(self, alloc_size, bytes_allocated, usable_size,
268 bytes_tl_bulk_allocated);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800269 } else {
270 DCHECK(!running_on_valgrind_);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700271 ret = dlmalloc_space_->AllocNonvirtual(self, alloc_size, bytes_allocated, usable_size,
272 bytes_tl_bulk_allocated);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800273 }
274 break;
275 }
276 case kAllocatorTypeNonMoving: {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700277 ret = non_moving_space_->Alloc(self, alloc_size, bytes_allocated, usable_size,
278 bytes_tl_bulk_allocated);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800279 break;
280 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800281 case kAllocatorTypeLOS: {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700282 ret = large_object_space_->Alloc(self, alloc_size, bytes_allocated, usable_size,
283 bytes_tl_bulk_allocated);
Hiroshi Yamauchi95a659f2013-11-22 14:43:45 -0800284 // Note that the bump pointer spaces aren't necessarily next to
285 // the other continuous spaces like the non-moving alloc space or
286 // the zygote space.
287 DCHECK(ret == nullptr || large_object_space_->Contains(ret));
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800288 break;
289 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800290 case kAllocatorTypeTLAB: {
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700291 DCHECK_ALIGNED(alloc_size, space::BumpPointerSpace::kAlignment);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800292 if (UNLIKELY(self->TlabSize() < alloc_size)) {
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700293 const size_t new_tlab_size = alloc_size + kDefaultTLABSize;
294 if (UNLIKELY(IsOutOfMemoryOnAllocation<kGrow>(allocator_type, new_tlab_size))) {
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800295 return nullptr;
296 }
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700297 // Try allocating a new thread local buffer, if the allocaiton fails the space must be
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700298 // full so return null.
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700299 if (!bump_pointer_space_->AllocNewTlab(self, new_tlab_size)) {
300 return nullptr;
301 }
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700302 *bytes_tl_bulk_allocated = new_tlab_size;
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700303 } else {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700304 *bytes_tl_bulk_allocated = 0;
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800305 }
306 // The allocation can't fail.
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800307 ret = self->AllocTlab(alloc_size);
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800308 DCHECK(ret != nullptr);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700309 *bytes_allocated = alloc_size;
Hiroshi Yamauchi5ccd4982014-03-11 12:19:04 -0700310 *usable_size = alloc_size;
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800311 break;
312 }
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800313 case kAllocatorTypeRegion: {
314 DCHECK(region_space_ != nullptr);
315 alloc_size = RoundUp(alloc_size, space::RegionSpace::kAlignment);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700316 ret = region_space_->AllocNonvirtual<false>(alloc_size, bytes_allocated, usable_size,
317 bytes_tl_bulk_allocated);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800318 break;
319 }
320 case kAllocatorTypeRegionTLAB: {
321 DCHECK(region_space_ != nullptr);
322 DCHECK_ALIGNED(alloc_size, space::RegionSpace::kAlignment);
323 if (UNLIKELY(self->TlabSize() < alloc_size)) {
324 if (space::RegionSpace::kRegionSize >= alloc_size) {
325 // Non-large. Check OOME for a tlab.
326 if (LIKELY(!IsOutOfMemoryOnAllocation<kGrow>(allocator_type, space::RegionSpace::kRegionSize))) {
327 // Try to allocate a tlab.
328 if (!region_space_->AllocNewTlab(self)) {
329 // Failed to allocate a tlab. Try non-tlab.
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700330 ret = region_space_->AllocNonvirtual<false>(alloc_size, bytes_allocated, usable_size,
331 bytes_tl_bulk_allocated);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800332 return ret;
333 }
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700334 *bytes_tl_bulk_allocated = space::RegionSpace::kRegionSize;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800335 // Fall-through.
336 } else {
337 // Check OOME for a non-tlab allocation.
338 if (!IsOutOfMemoryOnAllocation<kGrow>(allocator_type, alloc_size)) {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700339 ret = region_space_->AllocNonvirtual<false>(alloc_size, bytes_allocated, usable_size,
340 bytes_tl_bulk_allocated);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800341 return ret;
342 } else {
343 // Neither tlab or non-tlab works. Give up.
344 return nullptr;
345 }
346 }
347 } else {
348 // Large. Check OOME.
349 if (LIKELY(!IsOutOfMemoryOnAllocation<kGrow>(allocator_type, alloc_size))) {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700350 ret = region_space_->AllocNonvirtual<false>(alloc_size, bytes_allocated, usable_size,
351 bytes_tl_bulk_allocated);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800352 return ret;
353 } else {
354 return nullptr;
355 }
356 }
357 } else {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700358 *bytes_tl_bulk_allocated = 0; // Allocated in an existing buffer.
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800359 }
360 // The allocation can't fail.
361 ret = self->AllocTlab(alloc_size);
362 DCHECK(ret != nullptr);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700363 *bytes_allocated = alloc_size;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800364 *usable_size = alloc_size;
365 break;
366 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800367 default: {
368 LOG(FATAL) << "Invalid allocator type";
369 ret = nullptr;
370 }
371 }
372 return ret;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700373}
374
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700375inline Heap::AllocationTimer::AllocationTimer(Heap* heap, mirror::Object** allocated_obj_ptr)
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700376 : heap_(heap), allocated_obj_ptr_(allocated_obj_ptr),
377 allocation_start_time_(kMeasureAllocationTime ? NanoTime() / kTimeAdjust : 0u) { }
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700378
379inline Heap::AllocationTimer::~AllocationTimer() {
380 if (kMeasureAllocationTime) {
381 mirror::Object* allocated_obj = *allocated_obj_ptr_;
382 // Only if the allocation succeeded, record the time.
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800383 if (allocated_obj != nullptr) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700384 uint64_t allocation_end_time = NanoTime() / kTimeAdjust;
Ian Rogers3e5cf302014-05-20 16:40:37 -0700385 heap_->total_allocation_time_.FetchAndAddSequentiallyConsistent(allocation_end_time - allocation_start_time_);
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700386 }
387 }
Andreas Gampec8ccf682014-09-29 20:07:43 -0700388}
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700389
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800390inline bool Heap::ShouldAllocLargeObject(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.
Mathieu Chartierbd0a6532014-02-27 11:14:21 -0800395 return byte_count >= large_object_threshold_ && c->IsPrimitiveArray();
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700396}
397
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800398template <bool kGrow>
399inline bool Heap::IsOutOfMemoryOnAllocation(AllocatorType allocator_type, size_t alloc_size) {
Ian Rogers3e5cf302014-05-20 16:40:37 -0700400 size_t new_footprint = num_bytes_allocated_.LoadSequentiallyConsistent() + alloc_size;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700401 if (UNLIKELY(new_footprint > max_allowed_footprint_)) {
402 if (UNLIKELY(new_footprint > growth_limit_)) {
403 return true;
404 }
Hiroshi Yamauchi3e417802014-03-20 12:03:02 -0700405 if (!AllocatorMayHaveConcurrentGC(allocator_type) || !IsGcConcurrent()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800406 if (!kGrow) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700407 return true;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700408 }
Mathieu Chartier7bf82af2013-12-06 16:51:45 -0800409 // TODO: Grow for allocation is racy, fix it.
410 VLOG(heap) << "Growing heap from " << PrettySize(max_allowed_footprint_) << " to "
411 << PrettySize(new_footprint) << " for a " << PrettySize(alloc_size) << " allocation";
412 max_allowed_footprint_ = new_footprint;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700413 }
414 }
415 return false;
416}
417
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800418inline void Heap::CheckConcurrentGC(Thread* self, size_t new_num_bytes_allocated,
Mathieu Chartierf517f1a2014-03-06 15:52:27 -0800419 mirror::Object** obj) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700420 if (UNLIKELY(new_num_bytes_allocated >= concurrent_start_bytes_)) {
Hiroshi Yamauchi0ae98992015-05-01 14:33:19 -0700421 RequestConcurrentGCAndSaveObject(self, false, obj);
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700422 }
423}
424
425} // namespace gc
426} // namespace art
427
428#endif // ART_RUNTIME_GC_HEAP_INL_H_