blob: b814432c1d76bc6e872c47b3cdece2a7949ad6bc [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 Chartiera4f6af92015-08-11 17:35:25 -070042inline mirror::Object* Heap::AllocObjectWithAllocator(Thread* self,
43 mirror::Class* klass,
44 size_t byte_count,
45 AllocatorType allocator,
Mathieu Chartier1febddf2013-11-20 12:33:14 -080046 const PreFenceVisitor& pre_fence_visitor) {
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080047 if (kIsDebugBuild) {
48 CheckPreconditionsForAllocObject(klass, byte_count);
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070049 // Since allocation can cause a GC which will need to SuspendAll, make sure all allocations are
50 // done in the runnable state where suspension is expected.
51 CHECK_EQ(self->GetState(), kRunnable);
52 self->AssertThreadSuspensionIsAllowable();
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080053 }
Mathieu Chartierc528dba2013-11-26 12:00:11 -080054 // Need to check that we arent the large object allocator since the large object allocation code
55 // path this function. If we didn't check we would have an infinite loop.
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080056 mirror::Object* obj;
Mathieu Chartier446f9ee2014-12-01 15:00:27 -080057 if (kCheckLargeObject && UNLIKELY(ShouldAllocLargeObject(klass, byte_count))) {
58 obj = AllocLargeObject<kInstrumented, PreFenceVisitor>(self, &klass, byte_count,
59 pre_fence_visitor);
60 if (obj != nullptr) {
61 return obj;
62 } else {
63 // There should be an OOM exception, since we are retrying, clear it.
64 self->ClearException();
65 }
66 // If the large object allocation failed, try to use the normal spaces (main space,
67 // non moving space). This can happen if there is significant virtual address space
68 // fragmentation.
69 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080070 AllocationTimer alloc_timer(this, &obj);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070071 // bytes allocated for the (individual) object.
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070072 size_t bytes_allocated;
73 size_t usable_size;
74 size_t new_num_bytes_allocated = 0;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080075 if (allocator == kAllocatorTypeTLAB || allocator == kAllocatorTypeRegionTLAB) {
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070076 byte_count = RoundUp(byte_count, space::BumpPointerSpace::kAlignment);
77 }
78 // If we have a thread local allocation we don't need to update bytes allocated.
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080079 if ((allocator == kAllocatorTypeTLAB || allocator == kAllocatorTypeRegionTLAB) &&
80 byte_count <= self->TlabSize()) {
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070081 obj = self->AllocTlab(byte_count);
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -070082 DCHECK(obj != nullptr) << "AllocTlab can't fail";
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070083 obj->SetClass(klass);
84 if (kUseBakerOrBrooksReadBarrier) {
85 if (kUseBrooksReadBarrier) {
86 obj->SetReadBarrierPointer(obj);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080087 }
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070088 obj->AssertReadBarrierPointer();
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080089 }
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070090 bytes_allocated = byte_count;
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -070091 usable_size = bytes_allocated;
92 pre_fence_visitor(obj, usable_size);
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070093 QuasiAtomic::ThreadFenceForConstructor();
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070094 } else if (!kInstrumented && allocator == kAllocatorTypeRosAlloc &&
95 (obj = rosalloc_space_->AllocThreadLocal(self, byte_count, &bytes_allocated)) &&
96 LIKELY(obj != nullptr)) {
Evgenii Stepanov1e133742015-05-20 12:30:59 -070097 DCHECK(!is_running_on_memory_tool_);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070098 obj->SetClass(klass);
99 if (kUseBakerOrBrooksReadBarrier) {
100 if (kUseBrooksReadBarrier) {
101 obj->SetReadBarrierPointer(obj);
102 }
103 obj->AssertReadBarrierPointer();
104 }
105 usable_size = bytes_allocated;
106 pre_fence_visitor(obj, usable_size);
107 QuasiAtomic::ThreadFenceForConstructor();
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700108 } else {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700109 // bytes allocated that takes bulk thread-local buffer allocations into account.
110 size_t bytes_tl_bulk_allocated = 0;
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)) {
114 bool is_current_allocator = allocator == GetCurrentAllocator();
115 obj = AllocateInternalWithGc(self, allocator, byte_count, &bytes_allocated, &usable_size,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700116 &bytes_tl_bulk_allocated, &klass);
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700117 if (obj == nullptr) {
118 bool after_is_current_allocator = allocator == GetCurrentAllocator();
Mathieu Chartier8e705192014-08-20 18:19:23 -0700119 // If there is a pending exception, fail the allocation right away since the next one
120 // could cause OOM and abort the runtime.
121 if (!self->IsExceptionPending() && is_current_allocator && !after_is_current_allocator) {
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700122 // If the allocator changed, we need to restart the allocation.
123 return AllocObject<kInstrumented>(self, klass, byte_count, pre_fence_visitor);
124 }
125 return nullptr;
126 }
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -0700127 }
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700128 DCHECK_GT(bytes_allocated, 0u);
129 DCHECK_GT(usable_size, 0u);
130 obj->SetClass(klass);
131 if (kUseBakerOrBrooksReadBarrier) {
132 if (kUseBrooksReadBarrier) {
133 obj->SetReadBarrierPointer(obj);
134 }
135 obj->AssertReadBarrierPointer();
136 }
137 if (collector::SemiSpace::kUseRememberedSet && UNLIKELY(allocator == kAllocatorTypeNonMoving)) {
138 // (Note this if statement will be constant folded away for the
139 // fast-path quick entry points.) Because SetClass() has no write
140 // barrier, if a non-moving space allocation, we need a write
141 // barrier as the class pointer may point to the bump pointer
142 // space (where the class pointer is an "old-to-young" reference,
143 // though rare) under the GSS collector with the remembered set
144 // enabled. We don't need this for kAllocatorTypeRosAlloc/DlMalloc
145 // cases because we don't directly allocate into the main alloc
146 // space (besides promotions) under the SS/GSS collector.
147 WriteBarrierField(obj, mirror::Object::ClassOffset(), klass);
148 }
149 pre_fence_visitor(obj, usable_size);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700150 new_num_bytes_allocated = static_cast<size_t>(
151 num_bytes_allocated_.FetchAndAddSequentiallyConsistent(bytes_tl_bulk_allocated))
152 + bytes_tl_bulk_allocated;
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800153 }
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -0700154 if (kIsDebugBuild && Runtime::Current()->IsStarted()) {
155 CHECK_LE(obj->SizeOf(), usable_size);
156 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800157 // TODO: Deprecate.
158 if (kInstrumented) {
159 if (Runtime::Current()->HasStatsEnabled()) {
160 RuntimeStats* thread_stats = self->GetStats();
161 ++thread_stats->allocated_objects;
162 thread_stats->allocated_bytes += bytes_allocated;
163 RuntimeStats* global_stats = Runtime::Current()->GetStats();
164 ++global_stats->allocated_objects;
165 global_stats->allocated_bytes += bytes_allocated;
166 }
167 } else {
168 DCHECK(!Runtime::Current()->HasStatsEnabled());
169 }
170 if (AllocatorHasAllocationStack(allocator)) {
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700171 PushOnAllocationStack(self, &obj);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800172 }
173 if (kInstrumented) {
Man Cao8c2ff642015-05-27 17:25:30 -0700174 if (IsAllocTrackingEnabled()) {
Man Cao42c3c332015-06-23 16:38:25 -0700175 // Use obj->GetClass() instead of klass, because PushOnAllocationStack() could move klass
176 AllocRecordObjectMap::RecordAllocation(self, obj, obj->GetClass(), bytes_allocated);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800177 }
178 } else {
Man Cao8c2ff642015-05-27 17:25:30 -0700179 DCHECK(!IsAllocTrackingEnabled());
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800180 }
Mathieu Chartier31000802015-06-14 14:14:37 -0700181 if (kInstrumented) {
182 if (gc_stress_mode_) {
183 CheckGcStressMode(self, &obj);
184 }
185 } else {
186 DCHECK(!gc_stress_mode_);
187 }
Hiroshi Yamauchi3e417802014-03-20 12:03:02 -0700188 // IsConcurrentGc() isn't known at compile time so we can optimize by not checking it for
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800189 // the BumpPointer or TLAB allocators. This is nice since it allows the entire if statement to be
190 // optimized out. And for the other allocators, AllocatorMayHaveConcurrentGC is a constant since
191 // the allocator_type should be constant propagated.
Hiroshi Yamauchi3e417802014-03-20 12:03:02 -0700192 if (AllocatorMayHaveConcurrentGC(allocator) && IsGcConcurrent()) {
Mathieu Chartierf517f1a2014-03-06 15:52:27 -0800193 CheckConcurrentGC(self, new_num_bytes_allocated, &obj);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800194 }
Mathieu Chartier4e305412014-02-19 10:54:44 -0800195 VerifyObject(obj);
196 self->VerifyStack();
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800197 return obj;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700198}
199
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800200// The size of a thread-local allocation stack in the number of references.
201static constexpr size_t kThreadLocalAllocationStackSize = 128;
202
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700203inline void Heap::PushOnAllocationStack(Thread* self, mirror::Object** obj) {
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800204 if (kUseThreadLocalAllocationStack) {
Mathieu Chartierc1790162014-05-23 10:54:50 -0700205 if (UNLIKELY(!self->PushOnThreadLocalAllocationStack(*obj))) {
206 PushOnThreadLocalAllocationStackWithInternalGC(self, obj);
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800207 }
Mathieu Chartierc1790162014-05-23 10:54:50 -0700208 } else if (UNLIKELY(!allocation_stack_->AtomicPushBack(*obj))) {
209 PushOnAllocationStackWithInternalGC(self, obj);
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800210 }
211}
212
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800213template <bool kInstrumented, typename PreFenceVisitor>
Mathieu Chartiera4f6af92015-08-11 17:35:25 -0700214inline mirror::Object* Heap::AllocLargeObject(Thread* self,
215 mirror::Class** klass,
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800216 size_t byte_count,
217 const PreFenceVisitor& pre_fence_visitor) {
Mathieu Chartier446f9ee2014-12-01 15:00:27 -0800218 // Save and restore the class in case it moves.
219 StackHandleScope<1> hs(self);
220 auto klass_wrapper = hs.NewHandleWrapper(klass);
221 return AllocObjectWithAllocator<kInstrumented, false, PreFenceVisitor>(self, *klass, byte_count,
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800222 kAllocatorTypeLOS,
223 pre_fence_visitor);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800224}
225
226template <const bool kInstrumented, const bool kGrow>
Mathieu Chartiera4f6af92015-08-11 17:35:25 -0700227inline mirror::Object* Heap::TryToAllocate(Thread* self,
228 AllocatorType allocator_type,
229 size_t alloc_size,
230 size_t* bytes_allocated,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700231 size_t* usable_size,
232 size_t* bytes_tl_bulk_allocated) {
Mathieu Chartiera4f6af92015-08-11 17:35:25 -0700233 if (allocator_type != kAllocatorTypeTLAB &&
234 allocator_type != kAllocatorTypeRegionTLAB &&
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700235 allocator_type != kAllocatorTypeRosAlloc &&
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700236 UNLIKELY(IsOutOfMemoryOnAllocation<kGrow>(allocator_type, alloc_size))) {
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800237 return nullptr;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700238 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800239 mirror::Object* ret;
240 switch (allocator_type) {
241 case kAllocatorTypeBumpPointer: {
242 DCHECK(bump_pointer_space_ != nullptr);
243 alloc_size = RoundUp(alloc_size, space::BumpPointerSpace::kAlignment);
244 ret = bump_pointer_space_->AllocNonvirtual(alloc_size);
245 if (LIKELY(ret != nullptr)) {
246 *bytes_allocated = alloc_size;
Ian Rogers6fac4472014-02-25 17:01:10 -0800247 *usable_size = alloc_size;
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700248 *bytes_tl_bulk_allocated = alloc_size;
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800249 }
250 break;
251 }
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800252 case kAllocatorTypeRosAlloc: {
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700253 if (kInstrumented && UNLIKELY(is_running_on_memory_tool_)) {
254 // If running on valgrind or asan, we should be using the instrumented path.
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700255 size_t max_bytes_tl_bulk_allocated = rosalloc_space_->MaxBytesBulkAllocatedFor(alloc_size);
256 if (UNLIKELY(IsOutOfMemoryOnAllocation<kGrow>(allocator_type,
257 max_bytes_tl_bulk_allocated))) {
258 return nullptr;
259 }
260 ret = rosalloc_space_->Alloc(self, alloc_size, bytes_allocated, usable_size,
261 bytes_tl_bulk_allocated);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800262 } else {
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700263 DCHECK(!is_running_on_memory_tool_);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700264 size_t max_bytes_tl_bulk_allocated =
265 rosalloc_space_->MaxBytesBulkAllocatedForNonvirtual(alloc_size);
266 if (UNLIKELY(IsOutOfMemoryOnAllocation<kGrow>(allocator_type,
267 max_bytes_tl_bulk_allocated))) {
268 return nullptr;
269 }
270 if (!kInstrumented) {
271 DCHECK(!rosalloc_space_->CanAllocThreadLocal(self, alloc_size));
272 }
273 ret = rosalloc_space_->AllocNonvirtual(self, alloc_size, bytes_allocated, usable_size,
274 bytes_tl_bulk_allocated);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800275 }
276 break;
277 }
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800278 case kAllocatorTypeDlMalloc: {
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700279 if (kInstrumented && UNLIKELY(is_running_on_memory_tool_)) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800280 // If running on valgrind, we should be using the instrumented path.
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700281 ret = dlmalloc_space_->Alloc(self, alloc_size, bytes_allocated, usable_size,
282 bytes_tl_bulk_allocated);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800283 } else {
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700284 DCHECK(!is_running_on_memory_tool_);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700285 ret = dlmalloc_space_->AllocNonvirtual(self, alloc_size, bytes_allocated, usable_size,
286 bytes_tl_bulk_allocated);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800287 }
288 break;
289 }
290 case kAllocatorTypeNonMoving: {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700291 ret = non_moving_space_->Alloc(self, alloc_size, bytes_allocated, usable_size,
292 bytes_tl_bulk_allocated);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800293 break;
294 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800295 case kAllocatorTypeLOS: {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700296 ret = large_object_space_->Alloc(self, alloc_size, bytes_allocated, usable_size,
297 bytes_tl_bulk_allocated);
Hiroshi Yamauchi95a659f2013-11-22 14:43:45 -0800298 // Note that the bump pointer spaces aren't necessarily next to
299 // the other continuous spaces like the non-moving alloc space or
300 // the zygote space.
301 DCHECK(ret == nullptr || large_object_space_->Contains(ret));
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800302 break;
303 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800304 case kAllocatorTypeTLAB: {
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700305 DCHECK_ALIGNED(alloc_size, space::BumpPointerSpace::kAlignment);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800306 if (UNLIKELY(self->TlabSize() < alloc_size)) {
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700307 const size_t new_tlab_size = alloc_size + kDefaultTLABSize;
308 if (UNLIKELY(IsOutOfMemoryOnAllocation<kGrow>(allocator_type, new_tlab_size))) {
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800309 return nullptr;
310 }
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700311 // Try allocating a new thread local buffer, if the allocaiton fails the space must be
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700312 // full so return null.
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700313 if (!bump_pointer_space_->AllocNewTlab(self, new_tlab_size)) {
314 return nullptr;
315 }
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700316 *bytes_tl_bulk_allocated = new_tlab_size;
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700317 } else {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700318 *bytes_tl_bulk_allocated = 0;
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800319 }
320 // The allocation can't fail.
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800321 ret = self->AllocTlab(alloc_size);
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800322 DCHECK(ret != nullptr);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700323 *bytes_allocated = alloc_size;
Hiroshi Yamauchi5ccd4982014-03-11 12:19:04 -0700324 *usable_size = alloc_size;
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800325 break;
326 }
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800327 case kAllocatorTypeRegion: {
328 DCHECK(region_space_ != nullptr);
329 alloc_size = RoundUp(alloc_size, space::RegionSpace::kAlignment);
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 break;
333 }
334 case kAllocatorTypeRegionTLAB: {
335 DCHECK(region_space_ != nullptr);
336 DCHECK_ALIGNED(alloc_size, space::RegionSpace::kAlignment);
337 if (UNLIKELY(self->TlabSize() < alloc_size)) {
338 if (space::RegionSpace::kRegionSize >= alloc_size) {
339 // Non-large. Check OOME for a tlab.
340 if (LIKELY(!IsOutOfMemoryOnAllocation<kGrow>(allocator_type, space::RegionSpace::kRegionSize))) {
341 // Try to allocate a tlab.
342 if (!region_space_->AllocNewTlab(self)) {
343 // Failed to allocate a tlab. Try non-tlab.
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700344 ret = region_space_->AllocNonvirtual<false>(alloc_size, bytes_allocated, usable_size,
345 bytes_tl_bulk_allocated);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800346 return ret;
347 }
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700348 *bytes_tl_bulk_allocated = space::RegionSpace::kRegionSize;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800349 // Fall-through.
350 } else {
351 // Check OOME for a non-tlab allocation.
352 if (!IsOutOfMemoryOnAllocation<kGrow>(allocator_type, alloc_size)) {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700353 ret = region_space_->AllocNonvirtual<false>(alloc_size, bytes_allocated, usable_size,
354 bytes_tl_bulk_allocated);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800355 return ret;
356 } else {
357 // Neither tlab or non-tlab works. Give up.
358 return nullptr;
359 }
360 }
361 } else {
362 // Large. Check OOME.
363 if (LIKELY(!IsOutOfMemoryOnAllocation<kGrow>(allocator_type, alloc_size))) {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700364 ret = region_space_->AllocNonvirtual<false>(alloc_size, bytes_allocated, usable_size,
365 bytes_tl_bulk_allocated);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800366 return ret;
367 } else {
368 return nullptr;
369 }
370 }
371 } else {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700372 *bytes_tl_bulk_allocated = 0; // Allocated in an existing buffer.
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800373 }
374 // The allocation can't fail.
375 ret = self->AllocTlab(alloc_size);
376 DCHECK(ret != nullptr);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700377 *bytes_allocated = alloc_size;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800378 *usable_size = alloc_size;
379 break;
380 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800381 default: {
382 LOG(FATAL) << "Invalid allocator type";
383 ret = nullptr;
384 }
385 }
386 return ret;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700387}
388
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700389inline Heap::AllocationTimer::AllocationTimer(Heap* heap, mirror::Object** allocated_obj_ptr)
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700390 : heap_(heap), allocated_obj_ptr_(allocated_obj_ptr),
391 allocation_start_time_(kMeasureAllocationTime ? NanoTime() / kTimeAdjust : 0u) { }
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700392
393inline Heap::AllocationTimer::~AllocationTimer() {
394 if (kMeasureAllocationTime) {
395 mirror::Object* allocated_obj = *allocated_obj_ptr_;
396 // Only if the allocation succeeded, record the time.
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800397 if (allocated_obj != nullptr) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700398 uint64_t allocation_end_time = NanoTime() / kTimeAdjust;
Mathieu Chartiera4f6af92015-08-11 17:35:25 -0700399 heap_->total_allocation_time_.FetchAndAddSequentiallyConsistent(
400 allocation_end_time - allocation_start_time_);
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700401 }
402 }
Andreas Gampec8ccf682014-09-29 20:07:43 -0700403}
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700404
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800405inline bool Heap::ShouldAllocLargeObject(mirror::Class* c, size_t byte_count) const {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700406 // We need to have a zygote space or else our newly allocated large object can end up in the
407 // Zygote resulting in it being prematurely freed.
408 // We can only do this for primitive objects since large objects will not be within the card table
409 // range. This also means that we rely on SetClass not dirtying the object's card.
Jeff Hao13e00912015-06-22 15:14:49 -0700410 return byte_count >= large_object_threshold_ && (c->IsPrimitiveArray() || c->IsStringClass());
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700411}
412
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800413template <bool kGrow>
414inline bool Heap::IsOutOfMemoryOnAllocation(AllocatorType allocator_type, size_t alloc_size) {
Ian Rogers3e5cf302014-05-20 16:40:37 -0700415 size_t new_footprint = num_bytes_allocated_.LoadSequentiallyConsistent() + alloc_size;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700416 if (UNLIKELY(new_footprint > max_allowed_footprint_)) {
417 if (UNLIKELY(new_footprint > growth_limit_)) {
418 return true;
419 }
Hiroshi Yamauchi3e417802014-03-20 12:03:02 -0700420 if (!AllocatorMayHaveConcurrentGC(allocator_type) || !IsGcConcurrent()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800421 if (!kGrow) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700422 return true;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700423 }
Mathieu Chartier7bf82af2013-12-06 16:51:45 -0800424 // TODO: Grow for allocation is racy, fix it.
425 VLOG(heap) << "Growing heap from " << PrettySize(max_allowed_footprint_) << " to "
426 << PrettySize(new_footprint) << " for a " << PrettySize(alloc_size) << " allocation";
427 max_allowed_footprint_ = new_footprint;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700428 }
429 }
430 return false;
431}
432
Mathieu Chartiera4f6af92015-08-11 17:35:25 -0700433inline void Heap::CheckConcurrentGC(Thread* self,
434 size_t new_num_bytes_allocated,
Mathieu Chartierf517f1a2014-03-06 15:52:27 -0800435 mirror::Object** obj) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700436 if (UNLIKELY(new_num_bytes_allocated >= concurrent_start_bytes_)) {
Hiroshi Yamauchi0ae98992015-05-01 14:33:19 -0700437 RequestConcurrentGCAndSaveObject(self, false, obj);
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700438 }
439}
440
441} // namespace gc
442} // namespace art
443
444#endif // ART_RUNTIME_GC_HEAP_INL_H_