blob: 00adefb095a9ca6799e622a1de3df3929bf1a247 [file] [log] [blame]
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_RUNTIME_GC_HEAP_INL_H_
18#define ART_RUNTIME_GC_HEAP_INL_H_
19
20#include "heap.h"
21
Andreas Gampe27fa96c2016-10-07 15:05:24 -070022#include "allocation_listener.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010023#include "base/time_utils.h"
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -080024#include "gc/accounting/card_table-inl.h"
Man Cao8c2ff642015-05-27 17:25:30 -070025#include "gc/allocation_record.h"
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -080026#include "gc/collector/semi_space.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070027#include "gc/space/bump_pointer_space-inl.h"
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070028#include "gc/space/dlmalloc_space-inl.h"
29#include "gc/space/large_object_space.h"
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080030#include "gc/space/region_space-inl.h"
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070031#include "gc/space/rosalloc_space-inl.h"
Mathieu Chartiera058fdf2016-10-06 15:13:58 -070032#include "obj_ptr-inl.h"
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070033#include "runtime.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070034#include "handle_scope-inl.h"
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070035#include "thread-inl.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010036#include "utils.h"
Mathieu Chartier4e305412014-02-19 10:54:44 -080037#include "verify_object-inl.h"
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070038
39namespace art {
40namespace gc {
41
Mathieu Chartier692fafd2013-11-29 17:24:40 -080042template <bool kInstrumented, bool kCheckLargeObject, typename PreFenceVisitor>
Mathieu Chartiera4f6af92015-08-11 17:35:25 -070043inline mirror::Object* Heap::AllocObjectWithAllocator(Thread* self,
44 mirror::Class* klass,
45 size_t byte_count,
46 AllocatorType allocator,
Mathieu Chartier1febddf2013-11-20 12:33:14 -080047 const PreFenceVisitor& pre_fence_visitor) {
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080048 if (kIsDebugBuild) {
49 CheckPreconditionsForAllocObject(klass, byte_count);
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070050 // Since allocation can cause a GC which will need to SuspendAll, make sure all allocations are
51 // done in the runnable state where suspension is expected.
52 CHECK_EQ(self->GetState(), kRunnable);
53 self->AssertThreadSuspensionIsAllowable();
Mathieu Chartier8502f722016-06-08 15:09:08 -070054 self->AssertNoPendingException();
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070055 self->PoisonObjectPointers();
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080056 }
Mathieu Chartierc528dba2013-11-26 12:00:11 -080057 // Need to check that we arent the large object allocator since the large object allocation code
58 // path this function. If we didn't check we would have an infinite loop.
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080059 mirror::Object* obj;
Mathieu Chartier446f9ee2014-12-01 15:00:27 -080060 if (kCheckLargeObject && UNLIKELY(ShouldAllocLargeObject(klass, byte_count))) {
61 obj = AllocLargeObject<kInstrumented, PreFenceVisitor>(self, &klass, byte_count,
62 pre_fence_visitor);
63 if (obj != nullptr) {
64 return obj;
65 } else {
66 // There should be an OOM exception, since we are retrying, clear it.
67 self->ClearException();
68 }
69 // If the large object allocation failed, try to use the normal spaces (main space,
70 // non moving space). This can happen if there is significant virtual address space
71 // fragmentation.
72 }
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070073 // bytes allocated for the (individual) object.
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070074 size_t bytes_allocated;
75 size_t usable_size;
76 size_t new_num_bytes_allocated = 0;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080077 if (allocator == kAllocatorTypeTLAB || allocator == kAllocatorTypeRegionTLAB) {
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070078 byte_count = RoundUp(byte_count, space::BumpPointerSpace::kAlignment);
79 }
80 // If we have a thread local allocation we don't need to update bytes allocated.
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080081 if ((allocator == kAllocatorTypeTLAB || allocator == kAllocatorTypeRegionTLAB) &&
82 byte_count <= self->TlabSize()) {
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070083 obj = self->AllocTlab(byte_count);
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -070084 DCHECK(obj != nullptr) << "AllocTlab can't fail";
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070085 obj->SetClass(klass);
86 if (kUseBakerOrBrooksReadBarrier) {
87 if (kUseBrooksReadBarrier) {
88 obj->SetReadBarrierPointer(obj);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080089 }
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070090 obj->AssertReadBarrierPointer();
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080091 }
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070092 bytes_allocated = byte_count;
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -070093 usable_size = bytes_allocated;
94 pre_fence_visitor(obj, usable_size);
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070095 QuasiAtomic::ThreadFenceForConstructor();
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070096 } else if (!kInstrumented && allocator == kAllocatorTypeRosAlloc &&
97 (obj = rosalloc_space_->AllocThreadLocal(self, byte_count, &bytes_allocated)) &&
98 LIKELY(obj != nullptr)) {
Evgenii Stepanov1e133742015-05-20 12:30:59 -070099 DCHECK(!is_running_on_memory_tool_);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700100 obj->SetClass(klass);
101 if (kUseBakerOrBrooksReadBarrier) {
102 if (kUseBrooksReadBarrier) {
103 obj->SetReadBarrierPointer(obj);
104 }
105 obj->AssertReadBarrierPointer();
106 }
107 usable_size = bytes_allocated;
108 pre_fence_visitor(obj, usable_size);
109 QuasiAtomic::ThreadFenceForConstructor();
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700110 } else {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700111 // bytes allocated that takes bulk thread-local buffer allocations into account.
112 size_t bytes_tl_bulk_allocated = 0;
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700113 obj = TryToAllocate<kInstrumented, false>(self, allocator, byte_count, &bytes_allocated,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700114 &usable_size, &bytes_tl_bulk_allocated);
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700115 if (UNLIKELY(obj == nullptr)) {
Mathieu Chartiereebc3af2016-02-29 18:13:38 -0800116 // AllocateInternalWithGc can cause thread suspension, if someone instruments the entrypoints
117 // or changes the allocator in a suspend point here, we need to retry the allocation.
118 obj = AllocateInternalWithGc(self,
119 allocator,
120 kInstrumented,
121 byte_count,
122 &bytes_allocated,
123 &usable_size,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700124 &bytes_tl_bulk_allocated, &klass);
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700125 if (obj == nullptr) {
Mathieu Chartiereebc3af2016-02-29 18:13:38 -0800126 // The only way that we can get a null return if there is no pending exception is if the
127 // allocator or instrumentation changed.
128 if (!self->IsExceptionPending()) {
129 // AllocObject will pick up the new allocator type, and instrumented as true is the safe
130 // default.
131 return AllocObject</*kInstrumented*/true>(self,
132 klass,
133 byte_count,
134 pre_fence_visitor);
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700135 }
136 return nullptr;
137 }
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -0700138 }
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700139 DCHECK_GT(bytes_allocated, 0u);
140 DCHECK_GT(usable_size, 0u);
141 obj->SetClass(klass);
142 if (kUseBakerOrBrooksReadBarrier) {
143 if (kUseBrooksReadBarrier) {
144 obj->SetReadBarrierPointer(obj);
145 }
146 obj->AssertReadBarrierPointer();
147 }
148 if (collector::SemiSpace::kUseRememberedSet && UNLIKELY(allocator == kAllocatorTypeNonMoving)) {
149 // (Note this if statement will be constant folded away for the
150 // fast-path quick entry points.) Because SetClass() has no write
151 // barrier, if a non-moving space allocation, we need a write
152 // barrier as the class pointer may point to the bump pointer
153 // space (where the class pointer is an "old-to-young" reference,
154 // though rare) under the GSS collector with the remembered set
155 // enabled. We don't need this for kAllocatorTypeRosAlloc/DlMalloc
156 // cases because we don't directly allocate into the main alloc
157 // space (besides promotions) under the SS/GSS collector.
158 WriteBarrierField(obj, mirror::Object::ClassOffset(), klass);
159 }
160 pre_fence_visitor(obj, usable_size);
Hans Boehmb0171b92016-01-28 17:19:15 -0800161 QuasiAtomic::ThreadFenceForConstructor();
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700162 new_num_bytes_allocated = static_cast<size_t>(
Hans Boehmb0171b92016-01-28 17:19:15 -0800163 num_bytes_allocated_.FetchAndAddRelaxed(bytes_tl_bulk_allocated)) + bytes_tl_bulk_allocated;
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800164 }
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -0700165 if (kIsDebugBuild && Runtime::Current()->IsStarted()) {
166 CHECK_LE(obj->SizeOf(), usable_size);
167 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800168 // TODO: Deprecate.
169 if (kInstrumented) {
170 if (Runtime::Current()->HasStatsEnabled()) {
171 RuntimeStats* thread_stats = self->GetStats();
172 ++thread_stats->allocated_objects;
173 thread_stats->allocated_bytes += bytes_allocated;
174 RuntimeStats* global_stats = Runtime::Current()->GetStats();
175 ++global_stats->allocated_objects;
176 global_stats->allocated_bytes += bytes_allocated;
177 }
178 } else {
179 DCHECK(!Runtime::Current()->HasStatsEnabled());
180 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800181 if (kInstrumented) {
Man Cao8c2ff642015-05-27 17:25:30 -0700182 if (IsAllocTrackingEnabled()) {
Mathieu Chartier458b1052016-03-29 14:02:55 -0700183 // allocation_records_ is not null since it never becomes null after allocation tracking is
184 // enabled.
185 DCHECK(allocation_records_ != nullptr);
186 allocation_records_->RecordAllocation(self, &obj, bytes_allocated);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800187 }
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700188 AllocationListener* l = alloc_listener_.LoadSequentiallyConsistent();
189 if (l != nullptr) {
190 // Same as above. We assume that a listener that was once stored will never be deleted.
191 // Otherwise we'd have to perform this under a lock.
192 l->ObjectAllocated(self, &obj, bytes_allocated);
193 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800194 } else {
Man Cao8c2ff642015-05-27 17:25:30 -0700195 DCHECK(!IsAllocTrackingEnabled());
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800196 }
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800197 if (AllocatorHasAllocationStack(allocator)) {
198 PushOnAllocationStack(self, &obj);
199 }
Mathieu Chartier31000802015-06-14 14:14:37 -0700200 if (kInstrumented) {
201 if (gc_stress_mode_) {
202 CheckGcStressMode(self, &obj);
203 }
204 } else {
205 DCHECK(!gc_stress_mode_);
206 }
Hiroshi Yamauchi3e417802014-03-20 12:03:02 -0700207 // IsConcurrentGc() isn't known at compile time so we can optimize by not checking it for
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800208 // the BumpPointer or TLAB allocators. This is nice since it allows the entire if statement to be
209 // optimized out. And for the other allocators, AllocatorMayHaveConcurrentGC is a constant since
210 // the allocator_type should be constant propagated.
Hiroshi Yamauchi3e417802014-03-20 12:03:02 -0700211 if (AllocatorMayHaveConcurrentGC(allocator) && IsGcConcurrent()) {
Mathieu Chartierf517f1a2014-03-06 15:52:27 -0800212 CheckConcurrentGC(self, new_num_bytes_allocated, &obj);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800213 }
Mathieu Chartier4e305412014-02-19 10:54:44 -0800214 VerifyObject(obj);
215 self->VerifyStack();
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800216 return obj;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700217}
218
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800219// The size of a thread-local allocation stack in the number of references.
220static constexpr size_t kThreadLocalAllocationStackSize = 128;
221
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700222inline void Heap::PushOnAllocationStack(Thread* self, mirror::Object** obj) {
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800223 if (kUseThreadLocalAllocationStack) {
Mathieu Chartierc1790162014-05-23 10:54:50 -0700224 if (UNLIKELY(!self->PushOnThreadLocalAllocationStack(*obj))) {
225 PushOnThreadLocalAllocationStackWithInternalGC(self, obj);
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800226 }
Mathieu Chartierc1790162014-05-23 10:54:50 -0700227 } else if (UNLIKELY(!allocation_stack_->AtomicPushBack(*obj))) {
228 PushOnAllocationStackWithInternalGC(self, obj);
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800229 }
230}
231
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800232template <bool kInstrumented, typename PreFenceVisitor>
Mathieu Chartiera4f6af92015-08-11 17:35:25 -0700233inline mirror::Object* Heap::AllocLargeObject(Thread* self,
234 mirror::Class** klass,
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800235 size_t byte_count,
236 const PreFenceVisitor& pre_fence_visitor) {
Mathieu Chartier446f9ee2014-12-01 15:00:27 -0800237 // Save and restore the class in case it moves.
238 StackHandleScope<1> hs(self);
239 auto klass_wrapper = hs.NewHandleWrapper(klass);
240 return AllocObjectWithAllocator<kInstrumented, false, PreFenceVisitor>(self, *klass, byte_count,
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800241 kAllocatorTypeLOS,
242 pre_fence_visitor);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800243}
244
245template <const bool kInstrumented, const bool kGrow>
Mathieu Chartiera4f6af92015-08-11 17:35:25 -0700246inline mirror::Object* Heap::TryToAllocate(Thread* self,
247 AllocatorType allocator_type,
248 size_t alloc_size,
249 size_t* bytes_allocated,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700250 size_t* usable_size,
251 size_t* bytes_tl_bulk_allocated) {
Mathieu Chartiera4f6af92015-08-11 17:35:25 -0700252 if (allocator_type != kAllocatorTypeTLAB &&
253 allocator_type != kAllocatorTypeRegionTLAB &&
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700254 allocator_type != kAllocatorTypeRosAlloc &&
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700255 UNLIKELY(IsOutOfMemoryOnAllocation<kGrow>(allocator_type, alloc_size))) {
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800256 return nullptr;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700257 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800258 mirror::Object* ret;
259 switch (allocator_type) {
260 case kAllocatorTypeBumpPointer: {
261 DCHECK(bump_pointer_space_ != nullptr);
262 alloc_size = RoundUp(alloc_size, space::BumpPointerSpace::kAlignment);
263 ret = bump_pointer_space_->AllocNonvirtual(alloc_size);
264 if (LIKELY(ret != nullptr)) {
265 *bytes_allocated = alloc_size;
Ian Rogers6fac4472014-02-25 17:01:10 -0800266 *usable_size = alloc_size;
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700267 *bytes_tl_bulk_allocated = alloc_size;
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800268 }
269 break;
270 }
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800271 case kAllocatorTypeRosAlloc: {
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700272 if (kInstrumented && UNLIKELY(is_running_on_memory_tool_)) {
273 // If running on valgrind or asan, we should be using the instrumented path.
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700274 size_t max_bytes_tl_bulk_allocated = rosalloc_space_->MaxBytesBulkAllocatedFor(alloc_size);
275 if (UNLIKELY(IsOutOfMemoryOnAllocation<kGrow>(allocator_type,
276 max_bytes_tl_bulk_allocated))) {
277 return nullptr;
278 }
279 ret = rosalloc_space_->Alloc(self, alloc_size, bytes_allocated, usable_size,
280 bytes_tl_bulk_allocated);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800281 } else {
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700282 DCHECK(!is_running_on_memory_tool_);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700283 size_t max_bytes_tl_bulk_allocated =
284 rosalloc_space_->MaxBytesBulkAllocatedForNonvirtual(alloc_size);
285 if (UNLIKELY(IsOutOfMemoryOnAllocation<kGrow>(allocator_type,
286 max_bytes_tl_bulk_allocated))) {
287 return nullptr;
288 }
289 if (!kInstrumented) {
290 DCHECK(!rosalloc_space_->CanAllocThreadLocal(self, alloc_size));
291 }
292 ret = rosalloc_space_->AllocNonvirtual(self, alloc_size, bytes_allocated, usable_size,
293 bytes_tl_bulk_allocated);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800294 }
295 break;
296 }
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800297 case kAllocatorTypeDlMalloc: {
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700298 if (kInstrumented && UNLIKELY(is_running_on_memory_tool_)) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800299 // If running on valgrind, we should be using the instrumented path.
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700300 ret = dlmalloc_space_->Alloc(self, alloc_size, bytes_allocated, usable_size,
301 bytes_tl_bulk_allocated);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800302 } else {
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700303 DCHECK(!is_running_on_memory_tool_);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700304 ret = dlmalloc_space_->AllocNonvirtual(self, alloc_size, bytes_allocated, usable_size,
305 bytes_tl_bulk_allocated);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800306 }
307 break;
308 }
309 case kAllocatorTypeNonMoving: {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700310 ret = non_moving_space_->Alloc(self, alloc_size, bytes_allocated, usable_size,
311 bytes_tl_bulk_allocated);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800312 break;
313 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800314 case kAllocatorTypeLOS: {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700315 ret = large_object_space_->Alloc(self, alloc_size, bytes_allocated, usable_size,
316 bytes_tl_bulk_allocated);
Hiroshi Yamauchi95a659f2013-11-22 14:43:45 -0800317 // Note that the bump pointer spaces aren't necessarily next to
318 // the other continuous spaces like the non-moving alloc space or
319 // the zygote space.
320 DCHECK(ret == nullptr || large_object_space_->Contains(ret));
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800321 break;
322 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800323 case kAllocatorTypeTLAB: {
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700324 DCHECK_ALIGNED(alloc_size, space::BumpPointerSpace::kAlignment);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800325 if (UNLIKELY(self->TlabSize() < alloc_size)) {
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700326 const size_t new_tlab_size = alloc_size + kDefaultTLABSize;
327 if (UNLIKELY(IsOutOfMemoryOnAllocation<kGrow>(allocator_type, new_tlab_size))) {
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800328 return nullptr;
329 }
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700330 // Try allocating a new thread local buffer, if the allocaiton fails the space must be
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700331 // full so return null.
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700332 if (!bump_pointer_space_->AllocNewTlab(self, new_tlab_size)) {
333 return nullptr;
334 }
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700335 *bytes_tl_bulk_allocated = new_tlab_size;
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700336 } else {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700337 *bytes_tl_bulk_allocated = 0;
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800338 }
339 // The allocation can't fail.
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800340 ret = self->AllocTlab(alloc_size);
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800341 DCHECK(ret != nullptr);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700342 *bytes_allocated = alloc_size;
Hiroshi Yamauchi5ccd4982014-03-11 12:19:04 -0700343 *usable_size = alloc_size;
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800344 break;
345 }
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800346 case kAllocatorTypeRegion: {
347 DCHECK(region_space_ != nullptr);
348 alloc_size = RoundUp(alloc_size, space::RegionSpace::kAlignment);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700349 ret = region_space_->AllocNonvirtual<false>(alloc_size, bytes_allocated, usable_size,
350 bytes_tl_bulk_allocated);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800351 break;
352 }
353 case kAllocatorTypeRegionTLAB: {
354 DCHECK(region_space_ != nullptr);
355 DCHECK_ALIGNED(alloc_size, space::RegionSpace::kAlignment);
356 if (UNLIKELY(self->TlabSize() < alloc_size)) {
357 if (space::RegionSpace::kRegionSize >= alloc_size) {
358 // Non-large. Check OOME for a tlab.
359 if (LIKELY(!IsOutOfMemoryOnAllocation<kGrow>(allocator_type, space::RegionSpace::kRegionSize))) {
360 // Try to allocate a tlab.
361 if (!region_space_->AllocNewTlab(self)) {
362 // Failed to allocate a tlab. Try non-tlab.
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 }
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700367 *bytes_tl_bulk_allocated = space::RegionSpace::kRegionSize;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800368 // Fall-through.
369 } else {
370 // Check OOME for a non-tlab allocation.
371 if (!IsOutOfMemoryOnAllocation<kGrow>(allocator_type, alloc_size)) {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700372 ret = region_space_->AllocNonvirtual<false>(alloc_size, bytes_allocated, usable_size,
373 bytes_tl_bulk_allocated);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800374 return ret;
375 } else {
376 // Neither tlab or non-tlab works. Give up.
377 return nullptr;
378 }
379 }
380 } else {
381 // Large. Check OOME.
382 if (LIKELY(!IsOutOfMemoryOnAllocation<kGrow>(allocator_type, alloc_size))) {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700383 ret = region_space_->AllocNonvirtual<false>(alloc_size, bytes_allocated, usable_size,
384 bytes_tl_bulk_allocated);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800385 return ret;
386 } else {
387 return nullptr;
388 }
389 }
390 } else {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700391 *bytes_tl_bulk_allocated = 0; // Allocated in an existing buffer.
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800392 }
393 // The allocation can't fail.
394 ret = self->AllocTlab(alloc_size);
395 DCHECK(ret != nullptr);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700396 *bytes_allocated = alloc_size;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800397 *usable_size = alloc_size;
398 break;
399 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800400 default: {
401 LOG(FATAL) << "Invalid allocator type";
402 ret = nullptr;
403 }
404 }
405 return ret;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700406}
407
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800408inline bool Heap::ShouldAllocLargeObject(mirror::Class* c, size_t byte_count) const {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700409 // We need to have a zygote space or else our newly allocated large object can end up in the
410 // Zygote resulting in it being prematurely freed.
411 // We can only do this for primitive objects since large objects will not be within the card table
412 // range. This also means that we rely on SetClass not dirtying the object's card.
Jeff Hao13e00912015-06-22 15:14:49 -0700413 return byte_count >= large_object_threshold_ && (c->IsPrimitiveArray() || c->IsStringClass());
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700414}
415
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800416template <bool kGrow>
417inline bool Heap::IsOutOfMemoryOnAllocation(AllocatorType allocator_type, size_t alloc_size) {
Ian Rogers3e5cf302014-05-20 16:40:37 -0700418 size_t new_footprint = num_bytes_allocated_.LoadSequentiallyConsistent() + alloc_size;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700419 if (UNLIKELY(new_footprint > max_allowed_footprint_)) {
420 if (UNLIKELY(new_footprint > growth_limit_)) {
421 return true;
422 }
Hiroshi Yamauchi3e417802014-03-20 12:03:02 -0700423 if (!AllocatorMayHaveConcurrentGC(allocator_type) || !IsGcConcurrent()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800424 if (!kGrow) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700425 return true;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700426 }
Mathieu Chartier7bf82af2013-12-06 16:51:45 -0800427 // TODO: Grow for allocation is racy, fix it.
428 VLOG(heap) << "Growing heap from " << PrettySize(max_allowed_footprint_) << " to "
429 << PrettySize(new_footprint) << " for a " << PrettySize(alloc_size) << " allocation";
430 max_allowed_footprint_ = new_footprint;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700431 }
432 }
433 return false;
434}
435
Mathieu Chartiera4f6af92015-08-11 17:35:25 -0700436inline void Heap::CheckConcurrentGC(Thread* self,
437 size_t new_num_bytes_allocated,
Mathieu Chartierf517f1a2014-03-06 15:52:27 -0800438 mirror::Object** obj) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700439 if (UNLIKELY(new_num_bytes_allocated >= concurrent_start_bytes_)) {
Hiroshi Yamauchi0ae98992015-05-01 14:33:19 -0700440 RequestConcurrentGCAndSaveObject(self, false, obj);
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700441 }
442}
443
Mathieu Chartiera058fdf2016-10-06 15:13:58 -0700444inline void Heap::WriteBarrierField(ObjPtr<mirror::Object> dst,
445 MemberOffset offset ATTRIBUTE_UNUSED,
446 ObjPtr<mirror::Object> new_value ATTRIBUTE_UNUSED) {
447 card_table_->MarkCard(dst.Ptr());
448}
449
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700450} // namespace gc
451} // namespace art
452
453#endif // ART_RUNTIME_GC_HEAP_INL_H_