blob: 3e562054440d4f582214e1f6937b7d9a6601ee9b [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
22#include "debugger.h"
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -080023#include "gc/accounting/card_table-inl.h"
24#include "gc/collector/semi_space.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070025#include "gc/space/bump_pointer_space-inl.h"
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070026#include "gc/space/dlmalloc_space-inl.h"
27#include "gc/space/large_object_space.h"
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080028#include "gc/space/region_space-inl.h"
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070029#include "gc/space/rosalloc_space-inl.h"
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070030#include "runtime.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070031#include "handle_scope-inl.h"
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070032#include "thread.h"
33#include "thread-inl.h"
Mathieu Chartier4e305412014-02-19 10:54:44 -080034#include "verify_object-inl.h"
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070035
36namespace art {
37namespace gc {
38
Mathieu Chartier692fafd2013-11-29 17:24:40 -080039template <bool kInstrumented, bool kCheckLargeObject, typename PreFenceVisitor>
Mathieu Chartier1febddf2013-11-20 12:33:14 -080040inline mirror::Object* Heap::AllocObjectWithAllocator(Thread* self, mirror::Class* klass,
41 size_t byte_count, AllocatorType allocator,
42 const PreFenceVisitor& pre_fence_visitor) {
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080043 if (kIsDebugBuild) {
44 CheckPreconditionsForAllocObject(klass, byte_count);
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070045 // Since allocation can cause a GC which will need to SuspendAll, make sure all allocations are
46 // done in the runnable state where suspension is expected.
47 CHECK_EQ(self->GetState(), kRunnable);
48 self->AssertThreadSuspensionIsAllowable();
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080049 }
Mathieu Chartierc528dba2013-11-26 12:00:11 -080050 // Need to check that we arent the large object allocator since the large object allocation code
51 // path this function. If we didn't check we would have an infinite loop.
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080052 mirror::Object* obj;
Mathieu Chartier446f9ee2014-12-01 15:00:27 -080053 if (kCheckLargeObject && UNLIKELY(ShouldAllocLargeObject(klass, byte_count))) {
54 obj = AllocLargeObject<kInstrumented, PreFenceVisitor>(self, &klass, byte_count,
55 pre_fence_visitor);
56 if (obj != nullptr) {
57 return obj;
58 } else {
59 // There should be an OOM exception, since we are retrying, clear it.
60 self->ClearException();
61 }
62 // If the large object allocation failed, try to use the normal spaces (main space,
63 // non moving space). This can happen if there is significant virtual address space
64 // fragmentation.
65 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080066 AllocationTimer alloc_timer(this, &obj);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070067 // bytes allocated for the (individual) object.
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070068 size_t bytes_allocated;
69 size_t usable_size;
70 size_t new_num_bytes_allocated = 0;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080071 if (allocator == kAllocatorTypeTLAB || allocator == kAllocatorTypeRegionTLAB) {
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070072 byte_count = RoundUp(byte_count, space::BumpPointerSpace::kAlignment);
73 }
74 // If we have a thread local allocation we don't need to update bytes allocated.
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080075 if ((allocator == kAllocatorTypeTLAB || allocator == kAllocatorTypeRegionTLAB) &&
76 byte_count <= self->TlabSize()) {
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070077 obj = self->AllocTlab(byte_count);
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -070078 DCHECK(obj != nullptr) << "AllocTlab can't fail";
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070079 obj->SetClass(klass);
80 if (kUseBakerOrBrooksReadBarrier) {
81 if (kUseBrooksReadBarrier) {
82 obj->SetReadBarrierPointer(obj);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080083 }
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070084 obj->AssertReadBarrierPointer();
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080085 }
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070086 bytes_allocated = byte_count;
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -070087 usable_size = bytes_allocated;
88 pre_fence_visitor(obj, usable_size);
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070089 QuasiAtomic::ThreadFenceForConstructor();
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070090 } else if (!kInstrumented && allocator == kAllocatorTypeRosAlloc &&
91 (obj = rosalloc_space_->AllocThreadLocal(self, byte_count, &bytes_allocated)) &&
92 LIKELY(obj != nullptr)) {
93 DCHECK(!running_on_valgrind_);
94 obj->SetClass(klass);
95 if (kUseBakerOrBrooksReadBarrier) {
96 if (kUseBrooksReadBarrier) {
97 obj->SetReadBarrierPointer(obj);
98 }
99 obj->AssertReadBarrierPointer();
100 }
101 usable_size = bytes_allocated;
102 pre_fence_visitor(obj, usable_size);
103 QuasiAtomic::ThreadFenceForConstructor();
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700104 } else {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700105 // bytes allocated that takes bulk thread-local buffer allocations into account.
106 size_t bytes_tl_bulk_allocated = 0;
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700107 obj = TryToAllocate<kInstrumented, false>(self, allocator, byte_count, &bytes_allocated,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700108 &usable_size, &bytes_tl_bulk_allocated);
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700109 if (UNLIKELY(obj == nullptr)) {
110 bool is_current_allocator = allocator == GetCurrentAllocator();
111 obj = AllocateInternalWithGc(self, allocator, byte_count, &bytes_allocated, &usable_size,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700112 &bytes_tl_bulk_allocated, &klass);
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700113 if (obj == nullptr) {
114 bool after_is_current_allocator = allocator == GetCurrentAllocator();
Mathieu Chartier8e705192014-08-20 18:19:23 -0700115 // If there is a pending exception, fail the allocation right away since the next one
116 // could cause OOM and abort the runtime.
117 if (!self->IsExceptionPending() && is_current_allocator && !after_is_current_allocator) {
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700118 // If the allocator changed, we need to restart the allocation.
119 return AllocObject<kInstrumented>(self, klass, byte_count, pre_fence_visitor);
120 }
121 return nullptr;
122 }
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -0700123 }
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700124 DCHECK_GT(bytes_allocated, 0u);
125 DCHECK_GT(usable_size, 0u);
126 obj->SetClass(klass);
127 if (kUseBakerOrBrooksReadBarrier) {
128 if (kUseBrooksReadBarrier) {
129 obj->SetReadBarrierPointer(obj);
130 }
131 obj->AssertReadBarrierPointer();
132 }
133 if (collector::SemiSpace::kUseRememberedSet && UNLIKELY(allocator == kAllocatorTypeNonMoving)) {
134 // (Note this if statement will be constant folded away for the
135 // fast-path quick entry points.) Because SetClass() has no write
136 // barrier, if a non-moving space allocation, we need a write
137 // barrier as the class pointer may point to the bump pointer
138 // space (where the class pointer is an "old-to-young" reference,
139 // though rare) under the GSS collector with the remembered set
140 // enabled. We don't need this for kAllocatorTypeRosAlloc/DlMalloc
141 // cases because we don't directly allocate into the main alloc
142 // space (besides promotions) under the SS/GSS collector.
143 WriteBarrierField(obj, mirror::Object::ClassOffset(), klass);
144 }
145 pre_fence_visitor(obj, usable_size);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700146 new_num_bytes_allocated = static_cast<size_t>(
147 num_bytes_allocated_.FetchAndAddSequentiallyConsistent(bytes_tl_bulk_allocated))
148 + bytes_tl_bulk_allocated;
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800149 }
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -0700150 if (kIsDebugBuild && Runtime::Current()->IsStarted()) {
151 CHECK_LE(obj->SizeOf(), usable_size);
152 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800153 // TODO: Deprecate.
154 if (kInstrumented) {
155 if (Runtime::Current()->HasStatsEnabled()) {
156 RuntimeStats* thread_stats = self->GetStats();
157 ++thread_stats->allocated_objects;
158 thread_stats->allocated_bytes += bytes_allocated;
159 RuntimeStats* global_stats = Runtime::Current()->GetStats();
160 ++global_stats->allocated_objects;
161 global_stats->allocated_bytes += bytes_allocated;
162 }
163 } else {
164 DCHECK(!Runtime::Current()->HasStatsEnabled());
165 }
166 if (AllocatorHasAllocationStack(allocator)) {
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700167 PushOnAllocationStack(self, &obj);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800168 }
169 if (kInstrumented) {
170 if (Dbg::IsAllocTrackingEnabled()) {
Ian Rogers844506b2014-09-12 19:59:33 -0700171 Dbg::RecordAllocation(self, klass, bytes_allocated);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800172 }
173 } else {
174 DCHECK(!Dbg::IsAllocTrackingEnabled());
175 }
Hiroshi Yamauchi3e417802014-03-20 12:03:02 -0700176 // IsConcurrentGc() isn't known at compile time so we can optimize by not checking it for
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800177 // the BumpPointer or TLAB allocators. This is nice since it allows the entire if statement to be
178 // optimized out. And for the other allocators, AllocatorMayHaveConcurrentGC is a constant since
179 // the allocator_type should be constant propagated.
Hiroshi Yamauchi3e417802014-03-20 12:03:02 -0700180 if (AllocatorMayHaveConcurrentGC(allocator) && IsGcConcurrent()) {
Mathieu Chartierf517f1a2014-03-06 15:52:27 -0800181 CheckConcurrentGC(self, new_num_bytes_allocated, &obj);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800182 }
Mathieu Chartier4e305412014-02-19 10:54:44 -0800183 VerifyObject(obj);
184 self->VerifyStack();
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800185 return obj;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700186}
187
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800188// The size of a thread-local allocation stack in the number of references.
189static constexpr size_t kThreadLocalAllocationStackSize = 128;
190
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700191inline void Heap::PushOnAllocationStack(Thread* self, mirror::Object** obj) {
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800192 if (kUseThreadLocalAllocationStack) {
Mathieu Chartierc1790162014-05-23 10:54:50 -0700193 if (UNLIKELY(!self->PushOnThreadLocalAllocationStack(*obj))) {
194 PushOnThreadLocalAllocationStackWithInternalGC(self, obj);
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800195 }
Mathieu Chartierc1790162014-05-23 10:54:50 -0700196 } else if (UNLIKELY(!allocation_stack_->AtomicPushBack(*obj))) {
197 PushOnAllocationStackWithInternalGC(self, obj);
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800198 }
199}
200
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800201template <bool kInstrumented, typename PreFenceVisitor>
Mathieu Chartier446f9ee2014-12-01 15:00:27 -0800202inline mirror::Object* Heap::AllocLargeObject(Thread* self, mirror::Class** klass,
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800203 size_t byte_count,
204 const PreFenceVisitor& pre_fence_visitor) {
Mathieu Chartier446f9ee2014-12-01 15:00:27 -0800205 // Save and restore the class in case it moves.
206 StackHandleScope<1> hs(self);
207 auto klass_wrapper = hs.NewHandleWrapper(klass);
208 return AllocObjectWithAllocator<kInstrumented, false, PreFenceVisitor>(self, *klass, byte_count,
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800209 kAllocatorTypeLOS,
210 pre_fence_visitor);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800211}
212
213template <const bool kInstrumented, const bool kGrow>
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800214inline mirror::Object* Heap::TryToAllocate(Thread* self, AllocatorType allocator_type,
Ian Rogers6fac4472014-02-25 17:01:10 -0800215 size_t alloc_size, size_t* bytes_allocated,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700216 size_t* usable_size,
217 size_t* bytes_tl_bulk_allocated) {
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800218 if (allocator_type != kAllocatorTypeTLAB && allocator_type != kAllocatorTypeRegionTLAB &&
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700219 allocator_type != kAllocatorTypeRosAlloc &&
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700220 UNLIKELY(IsOutOfMemoryOnAllocation<kGrow>(allocator_type, alloc_size))) {
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800221 return nullptr;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700222 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800223 mirror::Object* ret;
224 switch (allocator_type) {
225 case kAllocatorTypeBumpPointer: {
226 DCHECK(bump_pointer_space_ != nullptr);
227 alloc_size = RoundUp(alloc_size, space::BumpPointerSpace::kAlignment);
228 ret = bump_pointer_space_->AllocNonvirtual(alloc_size);
229 if (LIKELY(ret != nullptr)) {
230 *bytes_allocated = alloc_size;
Ian Rogers6fac4472014-02-25 17:01:10 -0800231 *usable_size = alloc_size;
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700232 *bytes_tl_bulk_allocated = alloc_size;
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800233 }
234 break;
235 }
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800236 case kAllocatorTypeRosAlloc: {
237 if (kInstrumented && UNLIKELY(running_on_valgrind_)) {
238 // If running on valgrind, we should be using the instrumented path.
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700239 size_t max_bytes_tl_bulk_allocated = rosalloc_space_->MaxBytesBulkAllocatedFor(alloc_size);
240 if (UNLIKELY(IsOutOfMemoryOnAllocation<kGrow>(allocator_type,
241 max_bytes_tl_bulk_allocated))) {
242 return nullptr;
243 }
244 ret = rosalloc_space_->Alloc(self, alloc_size, bytes_allocated, usable_size,
245 bytes_tl_bulk_allocated);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800246 } else {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800247 DCHECK(!running_on_valgrind_);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700248 size_t max_bytes_tl_bulk_allocated =
249 rosalloc_space_->MaxBytesBulkAllocatedForNonvirtual(alloc_size);
250 if (UNLIKELY(IsOutOfMemoryOnAllocation<kGrow>(allocator_type,
251 max_bytes_tl_bulk_allocated))) {
252 return nullptr;
253 }
254 if (!kInstrumented) {
255 DCHECK(!rosalloc_space_->CanAllocThreadLocal(self, alloc_size));
256 }
257 ret = rosalloc_space_->AllocNonvirtual(self, alloc_size, bytes_allocated, usable_size,
258 bytes_tl_bulk_allocated);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800259 }
260 break;
261 }
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800262 case kAllocatorTypeDlMalloc: {
263 if (kInstrumented && UNLIKELY(running_on_valgrind_)) {
264 // If running on valgrind, we should be using the instrumented path.
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700265 ret = dlmalloc_space_->Alloc(self, alloc_size, bytes_allocated, usable_size,
266 bytes_tl_bulk_allocated);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800267 } else {
268 DCHECK(!running_on_valgrind_);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700269 ret = dlmalloc_space_->AllocNonvirtual(self, alloc_size, bytes_allocated, usable_size,
270 bytes_tl_bulk_allocated);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800271 }
272 break;
273 }
274 case kAllocatorTypeNonMoving: {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700275 ret = non_moving_space_->Alloc(self, alloc_size, bytes_allocated, usable_size,
276 bytes_tl_bulk_allocated);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800277 break;
278 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800279 case kAllocatorTypeLOS: {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700280 ret = large_object_space_->Alloc(self, alloc_size, bytes_allocated, usable_size,
281 bytes_tl_bulk_allocated);
Hiroshi Yamauchi95a659f2013-11-22 14:43:45 -0800282 // Note that the bump pointer spaces aren't necessarily next to
283 // the other continuous spaces like the non-moving alloc space or
284 // the zygote space.
285 DCHECK(ret == nullptr || large_object_space_->Contains(ret));
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800286 break;
287 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800288 case kAllocatorTypeTLAB: {
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700289 DCHECK_ALIGNED(alloc_size, space::BumpPointerSpace::kAlignment);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800290 if (UNLIKELY(self->TlabSize() < alloc_size)) {
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700291 const size_t new_tlab_size = alloc_size + kDefaultTLABSize;
292 if (UNLIKELY(IsOutOfMemoryOnAllocation<kGrow>(allocator_type, new_tlab_size))) {
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800293 return nullptr;
294 }
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700295 // Try allocating a new thread local buffer, if the allocaiton fails the space must be
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700296 // full so return null.
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700297 if (!bump_pointer_space_->AllocNewTlab(self, new_tlab_size)) {
298 return nullptr;
299 }
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700300 *bytes_tl_bulk_allocated = new_tlab_size;
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700301 } else {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700302 *bytes_tl_bulk_allocated = 0;
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800303 }
304 // The allocation can't fail.
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800305 ret = self->AllocTlab(alloc_size);
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800306 DCHECK(ret != nullptr);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700307 *bytes_allocated = alloc_size;
Hiroshi Yamauchi5ccd4982014-03-11 12:19:04 -0700308 *usable_size = alloc_size;
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800309 break;
310 }
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800311 case kAllocatorTypeRegion: {
312 DCHECK(region_space_ != nullptr);
313 alloc_size = RoundUp(alloc_size, space::RegionSpace::kAlignment);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700314 ret = region_space_->AllocNonvirtual<false>(alloc_size, bytes_allocated, usable_size,
315 bytes_tl_bulk_allocated);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800316 break;
317 }
318 case kAllocatorTypeRegionTLAB: {
319 DCHECK(region_space_ != nullptr);
320 DCHECK_ALIGNED(alloc_size, space::RegionSpace::kAlignment);
321 if (UNLIKELY(self->TlabSize() < alloc_size)) {
322 if (space::RegionSpace::kRegionSize >= alloc_size) {
323 // Non-large. Check OOME for a tlab.
324 if (LIKELY(!IsOutOfMemoryOnAllocation<kGrow>(allocator_type, space::RegionSpace::kRegionSize))) {
325 // Try to allocate a tlab.
326 if (!region_space_->AllocNewTlab(self)) {
327 // Failed to allocate a tlab. Try non-tlab.
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700328 ret = region_space_->AllocNonvirtual<false>(alloc_size, bytes_allocated, usable_size,
329 bytes_tl_bulk_allocated);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800330 return ret;
331 }
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700332 *bytes_tl_bulk_allocated = space::RegionSpace::kRegionSize;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800333 // Fall-through.
334 } else {
335 // Check OOME for a non-tlab allocation.
336 if (!IsOutOfMemoryOnAllocation<kGrow>(allocator_type, alloc_size)) {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700337 ret = region_space_->AllocNonvirtual<false>(alloc_size, bytes_allocated, usable_size,
338 bytes_tl_bulk_allocated);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800339 return ret;
340 } else {
341 // Neither tlab or non-tlab works. Give up.
342 return nullptr;
343 }
344 }
345 } else {
346 // Large. Check OOME.
347 if (LIKELY(!IsOutOfMemoryOnAllocation<kGrow>(allocator_type, alloc_size))) {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700348 ret = region_space_->AllocNonvirtual<false>(alloc_size, bytes_allocated, usable_size,
349 bytes_tl_bulk_allocated);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800350 return ret;
351 } else {
352 return nullptr;
353 }
354 }
355 } else {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700356 *bytes_tl_bulk_allocated = 0; // Allocated in an existing buffer.
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800357 }
358 // The allocation can't fail.
359 ret = self->AllocTlab(alloc_size);
360 DCHECK(ret != nullptr);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700361 *bytes_allocated = alloc_size;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800362 *usable_size = alloc_size;
363 break;
364 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800365 default: {
366 LOG(FATAL) << "Invalid allocator type";
367 ret = nullptr;
368 }
369 }
370 return ret;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700371}
372
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700373inline Heap::AllocationTimer::AllocationTimer(Heap* heap, mirror::Object** allocated_obj_ptr)
374 : heap_(heap), allocated_obj_ptr_(allocated_obj_ptr) {
375 if (kMeasureAllocationTime) {
376 allocation_start_time_ = NanoTime() / kTimeAdjust;
377 }
378}
379
380inline Heap::AllocationTimer::~AllocationTimer() {
381 if (kMeasureAllocationTime) {
382 mirror::Object* allocated_obj = *allocated_obj_ptr_;
383 // Only if the allocation succeeded, record the time.
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800384 if (allocated_obj != nullptr) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700385 uint64_t allocation_end_time = NanoTime() / kTimeAdjust;
Ian Rogers3e5cf302014-05-20 16:40:37 -0700386 heap_->total_allocation_time_.FetchAndAddSequentiallyConsistent(allocation_end_time - allocation_start_time_);
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700387 }
388 }
Andreas Gampec8ccf682014-09-29 20:07:43 -0700389}
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700390
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800391inline bool Heap::ShouldAllocLargeObject(mirror::Class* c, size_t byte_count) const {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700392 // We need to have a zygote space or else our newly allocated large object can end up in the
393 // Zygote resulting in it being prematurely freed.
394 // We can only do this for primitive objects since large objects will not be within the card table
395 // range. This also means that we rely on SetClass not dirtying the object's card.
Mathieu Chartierbd0a6532014-02-27 11:14:21 -0800396 return byte_count >= large_object_threshold_ && c->IsPrimitiveArray();
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700397}
398
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800399template <bool kGrow>
400inline bool Heap::IsOutOfMemoryOnAllocation(AllocatorType allocator_type, size_t alloc_size) {
Ian Rogers3e5cf302014-05-20 16:40:37 -0700401 size_t new_footprint = num_bytes_allocated_.LoadSequentiallyConsistent() + alloc_size;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700402 if (UNLIKELY(new_footprint > max_allowed_footprint_)) {
403 if (UNLIKELY(new_footprint > growth_limit_)) {
404 return true;
405 }
Hiroshi Yamauchi3e417802014-03-20 12:03:02 -0700406 if (!AllocatorMayHaveConcurrentGC(allocator_type) || !IsGcConcurrent()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800407 if (!kGrow) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700408 return true;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700409 }
Mathieu Chartier7bf82af2013-12-06 16:51:45 -0800410 // TODO: Grow for allocation is racy, fix it.
411 VLOG(heap) << "Growing heap from " << PrettySize(max_allowed_footprint_) << " to "
412 << PrettySize(new_footprint) << " for a " << PrettySize(alloc_size) << " allocation";
413 max_allowed_footprint_ = new_footprint;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700414 }
415 }
416 return false;
417}
418
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800419inline void Heap::CheckConcurrentGC(Thread* self, size_t new_num_bytes_allocated,
Mathieu Chartierf517f1a2014-03-06 15:52:27 -0800420 mirror::Object** obj) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700421 if (UNLIKELY(new_num_bytes_allocated >= concurrent_start_bytes_)) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700422 RequestConcurrentGCAndSaveObject(self, obj);
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700423 }
424}
425
426} // namespace gc
427} // namespace art
428
429#endif // ART_RUNTIME_GC_HEAP_INL_H_