blob: b0ca18e1a7527a9ce6a137de4227537349303c8d [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 Yamauchi38e68e92014-03-07 13:59:08 -080023#include "gc/accounting/card_table-inl.h"
Man Cao8c2ff642015-05-27 17:25:30 -070024#include "gc/allocation_record.h"
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -080025#include "gc/collector/semi_space.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070026#include "gc/space/bump_pointer_space-inl.h"
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070027#include "gc/space/dlmalloc_space-inl.h"
28#include "gc/space/large_object_space.h"
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080029#include "gc/space/region_space-inl.h"
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070030#include "gc/space/rosalloc_space-inl.h"
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070031#include "runtime.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070032#include "handle_scope-inl.h"
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070033#include "thread-inl.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010034#include "utils.h"
Mathieu Chartier4e305412014-02-19 10:54:44 -080035#include "verify_object-inl.h"
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070036
37namespace art {
38namespace gc {
39
Mathieu Chartier692fafd2013-11-29 17:24:40 -080040template <bool kInstrumented, bool kCheckLargeObject, typename PreFenceVisitor>
Mathieu Chartiera4f6af92015-08-11 17:35:25 -070041inline mirror::Object* Heap::AllocObjectWithAllocator(Thread* self,
42 mirror::Class* klass,
43 size_t byte_count,
44 AllocatorType allocator,
Mathieu Chartier1febddf2013-11-20 12:33:14 -080045 const PreFenceVisitor& pre_fence_visitor) {
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080046 if (kIsDebugBuild) {
47 CheckPreconditionsForAllocObject(klass, byte_count);
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070048 // Since allocation can cause a GC which will need to SuspendAll, make sure all allocations are
49 // done in the runnable state where suspension is expected.
50 CHECK_EQ(self->GetState(), kRunnable);
51 self->AssertThreadSuspensionIsAllowable();
Mathieu Chartier8502f722016-06-08 15:09:08 -070052 self->AssertNoPendingException();
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 }
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070070 // bytes allocated for the (individual) object.
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070071 size_t bytes_allocated;
72 size_t usable_size;
73 size_t new_num_bytes_allocated = 0;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080074 if (allocator == kAllocatorTypeTLAB || allocator == kAllocatorTypeRegionTLAB) {
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070075 byte_count = RoundUp(byte_count, space::BumpPointerSpace::kAlignment);
76 }
77 // If we have a thread local allocation we don't need to update bytes allocated.
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080078 if ((allocator == kAllocatorTypeTLAB || allocator == kAllocatorTypeRegionTLAB) &&
79 byte_count <= self->TlabSize()) {
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070080 obj = self->AllocTlab(byte_count);
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -070081 DCHECK(obj != nullptr) << "AllocTlab can't fail";
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070082 obj->SetClass(klass);
83 if (kUseBakerOrBrooksReadBarrier) {
84 if (kUseBrooksReadBarrier) {
85 obj->SetReadBarrierPointer(obj);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080086 }
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070087 obj->AssertReadBarrierPointer();
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080088 }
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070089 bytes_allocated = byte_count;
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -070090 usable_size = bytes_allocated;
91 pre_fence_visitor(obj, usable_size);
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070092 QuasiAtomic::ThreadFenceForConstructor();
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070093 } else if (!kInstrumented && allocator == kAllocatorTypeRosAlloc &&
94 (obj = rosalloc_space_->AllocThreadLocal(self, byte_count, &bytes_allocated)) &&
95 LIKELY(obj != nullptr)) {
Evgenii Stepanov1e133742015-05-20 12:30:59 -070096 DCHECK(!is_running_on_memory_tool_);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070097 obj->SetClass(klass);
98 if (kUseBakerOrBrooksReadBarrier) {
99 if (kUseBrooksReadBarrier) {
100 obj->SetReadBarrierPointer(obj);
101 }
102 obj->AssertReadBarrierPointer();
103 }
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);
139 if (kUseBakerOrBrooksReadBarrier) {
140 if (kUseBrooksReadBarrier) {
141 obj->SetReadBarrierPointer(obj);
142 }
143 obj->AssertReadBarrierPointer();
144 }
145 if (collector::SemiSpace::kUseRememberedSet && UNLIKELY(allocator == kAllocatorTypeNonMoving)) {
146 // (Note this if statement will be constant folded away for the
147 // fast-path quick entry points.) Because SetClass() has no write
148 // barrier, if a non-moving space allocation, we need a write
149 // barrier as the class pointer may point to the bump pointer
150 // space (where the class pointer is an "old-to-young" reference,
151 // though rare) under the GSS collector with the remembered set
152 // enabled. We don't need this for kAllocatorTypeRosAlloc/DlMalloc
153 // cases because we don't directly allocate into the main alloc
154 // space (besides promotions) under the SS/GSS collector.
155 WriteBarrierField(obj, mirror::Object::ClassOffset(), klass);
156 }
157 pre_fence_visitor(obj, usable_size);
Hans Boehmb0171b92016-01-28 17:19:15 -0800158 QuasiAtomic::ThreadFenceForConstructor();
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700159 new_num_bytes_allocated = static_cast<size_t>(
Hans Boehmb0171b92016-01-28 17:19:15 -0800160 num_bytes_allocated_.FetchAndAddRelaxed(bytes_tl_bulk_allocated)) + bytes_tl_bulk_allocated;
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800161 }
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -0700162 if (kIsDebugBuild && Runtime::Current()->IsStarted()) {
163 CHECK_LE(obj->SizeOf(), usable_size);
164 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800165 // TODO: Deprecate.
166 if (kInstrumented) {
167 if (Runtime::Current()->HasStatsEnabled()) {
168 RuntimeStats* thread_stats = self->GetStats();
169 ++thread_stats->allocated_objects;
170 thread_stats->allocated_bytes += bytes_allocated;
171 RuntimeStats* global_stats = Runtime::Current()->GetStats();
172 ++global_stats->allocated_objects;
173 global_stats->allocated_bytes += bytes_allocated;
174 }
175 } else {
176 DCHECK(!Runtime::Current()->HasStatsEnabled());
177 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800178 if (kInstrumented) {
Man Cao8c2ff642015-05-27 17:25:30 -0700179 if (IsAllocTrackingEnabled()) {
Mathieu Chartier458b1052016-03-29 14:02:55 -0700180 // allocation_records_ is not null since it never becomes null after allocation tracking is
181 // enabled.
182 DCHECK(allocation_records_ != nullptr);
183 allocation_records_->RecordAllocation(self, &obj, bytes_allocated);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800184 }
185 } else {
Man Cao8c2ff642015-05-27 17:25:30 -0700186 DCHECK(!IsAllocTrackingEnabled());
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800187 }
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800188 if (AllocatorHasAllocationStack(allocator)) {
189 PushOnAllocationStack(self, &obj);
190 }
Mathieu Chartier31000802015-06-14 14:14:37 -0700191 if (kInstrumented) {
192 if (gc_stress_mode_) {
193 CheckGcStressMode(self, &obj);
194 }
195 } else {
196 DCHECK(!gc_stress_mode_);
197 }
Hiroshi Yamauchi3e417802014-03-20 12:03:02 -0700198 // IsConcurrentGc() isn't known at compile time so we can optimize by not checking it for
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800199 // the BumpPointer or TLAB allocators. This is nice since it allows the entire if statement to be
200 // optimized out. And for the other allocators, AllocatorMayHaveConcurrentGC is a constant since
201 // the allocator_type should be constant propagated.
Hiroshi Yamauchi3e417802014-03-20 12:03:02 -0700202 if (AllocatorMayHaveConcurrentGC(allocator) && IsGcConcurrent()) {
Mathieu Chartierf517f1a2014-03-06 15:52:27 -0800203 CheckConcurrentGC(self, new_num_bytes_allocated, &obj);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800204 }
Mathieu Chartier4e305412014-02-19 10:54:44 -0800205 VerifyObject(obj);
206 self->VerifyStack();
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800207 return obj;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700208}
209
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800210// The size of a thread-local allocation stack in the number of references.
211static constexpr size_t kThreadLocalAllocationStackSize = 128;
212
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700213inline void Heap::PushOnAllocationStack(Thread* self, mirror::Object** obj) {
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800214 if (kUseThreadLocalAllocationStack) {
Mathieu Chartierc1790162014-05-23 10:54:50 -0700215 if (UNLIKELY(!self->PushOnThreadLocalAllocationStack(*obj))) {
216 PushOnThreadLocalAllocationStackWithInternalGC(self, obj);
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800217 }
Mathieu Chartierc1790162014-05-23 10:54:50 -0700218 } else if (UNLIKELY(!allocation_stack_->AtomicPushBack(*obj))) {
219 PushOnAllocationStackWithInternalGC(self, obj);
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800220 }
221}
222
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800223template <bool kInstrumented, typename PreFenceVisitor>
Mathieu Chartiera4f6af92015-08-11 17:35:25 -0700224inline mirror::Object* Heap::AllocLargeObject(Thread* self,
225 mirror::Class** klass,
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800226 size_t byte_count,
227 const PreFenceVisitor& pre_fence_visitor) {
Mathieu Chartier446f9ee2014-12-01 15:00:27 -0800228 // Save and restore the class in case it moves.
229 StackHandleScope<1> hs(self);
230 auto klass_wrapper = hs.NewHandleWrapper(klass);
231 return AllocObjectWithAllocator<kInstrumented, false, PreFenceVisitor>(self, *klass, byte_count,
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800232 kAllocatorTypeLOS,
233 pre_fence_visitor);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800234}
235
236template <const bool kInstrumented, const bool kGrow>
Mathieu Chartiera4f6af92015-08-11 17:35:25 -0700237inline mirror::Object* Heap::TryToAllocate(Thread* self,
238 AllocatorType allocator_type,
239 size_t alloc_size,
240 size_t* bytes_allocated,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700241 size_t* usable_size,
242 size_t* bytes_tl_bulk_allocated) {
Mathieu Chartiera4f6af92015-08-11 17:35:25 -0700243 if (allocator_type != kAllocatorTypeTLAB &&
244 allocator_type != kAllocatorTypeRegionTLAB &&
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700245 allocator_type != kAllocatorTypeRosAlloc &&
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700246 UNLIKELY(IsOutOfMemoryOnAllocation<kGrow>(allocator_type, alloc_size))) {
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800247 return nullptr;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700248 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800249 mirror::Object* ret;
250 switch (allocator_type) {
251 case kAllocatorTypeBumpPointer: {
252 DCHECK(bump_pointer_space_ != nullptr);
253 alloc_size = RoundUp(alloc_size, space::BumpPointerSpace::kAlignment);
254 ret = bump_pointer_space_->AllocNonvirtual(alloc_size);
255 if (LIKELY(ret != nullptr)) {
256 *bytes_allocated = alloc_size;
Ian Rogers6fac4472014-02-25 17:01:10 -0800257 *usable_size = alloc_size;
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700258 *bytes_tl_bulk_allocated = alloc_size;
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800259 }
260 break;
261 }
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800262 case kAllocatorTypeRosAlloc: {
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700263 if (kInstrumented && UNLIKELY(is_running_on_memory_tool_)) {
264 // If running on valgrind or asan, we should be using the instrumented path.
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700265 size_t max_bytes_tl_bulk_allocated = rosalloc_space_->MaxBytesBulkAllocatedFor(alloc_size);
266 if (UNLIKELY(IsOutOfMemoryOnAllocation<kGrow>(allocator_type,
267 max_bytes_tl_bulk_allocated))) {
268 return nullptr;
269 }
270 ret = rosalloc_space_->Alloc(self, alloc_size, bytes_allocated, usable_size,
271 bytes_tl_bulk_allocated);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800272 } else {
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700273 DCHECK(!is_running_on_memory_tool_);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700274 size_t max_bytes_tl_bulk_allocated =
275 rosalloc_space_->MaxBytesBulkAllocatedForNonvirtual(alloc_size);
276 if (UNLIKELY(IsOutOfMemoryOnAllocation<kGrow>(allocator_type,
277 max_bytes_tl_bulk_allocated))) {
278 return nullptr;
279 }
280 if (!kInstrumented) {
281 DCHECK(!rosalloc_space_->CanAllocThreadLocal(self, alloc_size));
282 }
283 ret = rosalloc_space_->AllocNonvirtual(self, alloc_size, bytes_allocated, usable_size,
284 bytes_tl_bulk_allocated);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800285 }
286 break;
287 }
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800288 case kAllocatorTypeDlMalloc: {
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700289 if (kInstrumented && UNLIKELY(is_running_on_memory_tool_)) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800290 // If running on valgrind, we should be using the instrumented path.
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700291 ret = dlmalloc_space_->Alloc(self, alloc_size, bytes_allocated, usable_size,
292 bytes_tl_bulk_allocated);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800293 } else {
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700294 DCHECK(!is_running_on_memory_tool_);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700295 ret = dlmalloc_space_->AllocNonvirtual(self, alloc_size, bytes_allocated, usable_size,
296 bytes_tl_bulk_allocated);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800297 }
298 break;
299 }
300 case kAllocatorTypeNonMoving: {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700301 ret = non_moving_space_->Alloc(self, alloc_size, bytes_allocated, usable_size,
302 bytes_tl_bulk_allocated);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800303 break;
304 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800305 case kAllocatorTypeLOS: {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700306 ret = large_object_space_->Alloc(self, alloc_size, bytes_allocated, usable_size,
307 bytes_tl_bulk_allocated);
Hiroshi Yamauchi95a659f2013-11-22 14:43:45 -0800308 // Note that the bump pointer spaces aren't necessarily next to
309 // the other continuous spaces like the non-moving alloc space or
310 // the zygote space.
311 DCHECK(ret == nullptr || large_object_space_->Contains(ret));
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800312 break;
313 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800314 case kAllocatorTypeTLAB: {
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700315 DCHECK_ALIGNED(alloc_size, space::BumpPointerSpace::kAlignment);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800316 if (UNLIKELY(self->TlabSize() < alloc_size)) {
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700317 const size_t new_tlab_size = alloc_size + kDefaultTLABSize;
318 if (UNLIKELY(IsOutOfMemoryOnAllocation<kGrow>(allocator_type, new_tlab_size))) {
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800319 return nullptr;
320 }
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700321 // Try allocating a new thread local buffer, if the allocaiton fails the space must be
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700322 // full so return null.
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700323 if (!bump_pointer_space_->AllocNewTlab(self, new_tlab_size)) {
324 return nullptr;
325 }
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700326 *bytes_tl_bulk_allocated = new_tlab_size;
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700327 } else {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700328 *bytes_tl_bulk_allocated = 0;
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800329 }
330 // The allocation can't fail.
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800331 ret = self->AllocTlab(alloc_size);
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800332 DCHECK(ret != nullptr);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700333 *bytes_allocated = alloc_size;
Hiroshi Yamauchi5ccd4982014-03-11 12:19:04 -0700334 *usable_size = alloc_size;
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800335 break;
336 }
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800337 case kAllocatorTypeRegion: {
338 DCHECK(region_space_ != nullptr);
339 alloc_size = RoundUp(alloc_size, space::RegionSpace::kAlignment);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700340 ret = region_space_->AllocNonvirtual<false>(alloc_size, bytes_allocated, usable_size,
341 bytes_tl_bulk_allocated);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800342 break;
343 }
344 case kAllocatorTypeRegionTLAB: {
345 DCHECK(region_space_ != nullptr);
346 DCHECK_ALIGNED(alloc_size, space::RegionSpace::kAlignment);
347 if (UNLIKELY(self->TlabSize() < alloc_size)) {
348 if (space::RegionSpace::kRegionSize >= alloc_size) {
349 // Non-large. Check OOME for a tlab.
350 if (LIKELY(!IsOutOfMemoryOnAllocation<kGrow>(allocator_type, space::RegionSpace::kRegionSize))) {
351 // Try to allocate a tlab.
352 if (!region_space_->AllocNewTlab(self)) {
353 // Failed to allocate a tlab. Try non-tlab.
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700354 ret = region_space_->AllocNonvirtual<false>(alloc_size, bytes_allocated, usable_size,
355 bytes_tl_bulk_allocated);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800356 return ret;
357 }
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700358 *bytes_tl_bulk_allocated = space::RegionSpace::kRegionSize;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800359 // Fall-through.
360 } else {
361 // Check OOME for a non-tlab allocation.
362 if (!IsOutOfMemoryOnAllocation<kGrow>(allocator_type, alloc_size)) {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700363 ret = region_space_->AllocNonvirtual<false>(alloc_size, bytes_allocated, usable_size,
364 bytes_tl_bulk_allocated);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800365 return ret;
366 } else {
367 // Neither tlab or non-tlab works. Give up.
368 return nullptr;
369 }
370 }
371 } else {
372 // Large. Check OOME.
373 if (LIKELY(!IsOutOfMemoryOnAllocation<kGrow>(allocator_type, alloc_size))) {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700374 ret = region_space_->AllocNonvirtual<false>(alloc_size, bytes_allocated, usable_size,
375 bytes_tl_bulk_allocated);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800376 return ret;
377 } else {
378 return nullptr;
379 }
380 }
381 } else {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700382 *bytes_tl_bulk_allocated = 0; // Allocated in an existing buffer.
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800383 }
384 // The allocation can't fail.
385 ret = self->AllocTlab(alloc_size);
386 DCHECK(ret != nullptr);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700387 *bytes_allocated = alloc_size;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800388 *usable_size = alloc_size;
389 break;
390 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800391 default: {
392 LOG(FATAL) << "Invalid allocator type";
393 ret = nullptr;
394 }
395 }
396 return ret;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700397}
398
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800399inline bool Heap::ShouldAllocLargeObject(mirror::Class* c, size_t byte_count) const {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700400 // We need to have a zygote space or else our newly allocated large object can end up in the
401 // Zygote resulting in it being prematurely freed.
402 // We can only do this for primitive objects since large objects will not be within the card table
403 // range. This also means that we rely on SetClass not dirtying the object's card.
Jeff Hao13e00912015-06-22 15:14:49 -0700404 return byte_count >= large_object_threshold_ && (c->IsPrimitiveArray() || c->IsStringClass());
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700405}
406
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800407template <bool kGrow>
408inline bool Heap::IsOutOfMemoryOnAllocation(AllocatorType allocator_type, size_t alloc_size) {
Ian Rogers3e5cf302014-05-20 16:40:37 -0700409 size_t new_footprint = num_bytes_allocated_.LoadSequentiallyConsistent() + alloc_size;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700410 if (UNLIKELY(new_footprint > max_allowed_footprint_)) {
411 if (UNLIKELY(new_footprint > growth_limit_)) {
412 return true;
413 }
Hiroshi Yamauchi3e417802014-03-20 12:03:02 -0700414 if (!AllocatorMayHaveConcurrentGC(allocator_type) || !IsGcConcurrent()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800415 if (!kGrow) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700416 return true;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700417 }
Mathieu Chartier7bf82af2013-12-06 16:51:45 -0800418 // TODO: Grow for allocation is racy, fix it.
419 VLOG(heap) << "Growing heap from " << PrettySize(max_allowed_footprint_) << " to "
420 << PrettySize(new_footprint) << " for a " << PrettySize(alloc_size) << " allocation";
421 max_allowed_footprint_ = new_footprint;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700422 }
423 }
424 return false;
425}
426
Mathieu Chartiera4f6af92015-08-11 17:35:25 -0700427inline void Heap::CheckConcurrentGC(Thread* self,
428 size_t new_num_bytes_allocated,
Mathieu Chartierf517f1a2014-03-06 15:52:27 -0800429 mirror::Object** obj) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700430 if (UNLIKELY(new_num_bytes_allocated >= concurrent_start_bytes_)) {
Hiroshi Yamauchi0ae98992015-05-01 14:33:19 -0700431 RequestConcurrentGCAndSaveObject(self, false, obj);
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700432 }
433}
434
435} // namespace gc
436} // namespace art
437
438#endif // ART_RUNTIME_GC_HEAP_INL_H_