blob: b8c24521a208343ac9316e9effcdf6ffffc3dea9 [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);
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070067 size_t bytes_allocated;
68 size_t usable_size;
69 size_t new_num_bytes_allocated = 0;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080070 if (allocator == kAllocatorTypeTLAB || allocator == kAllocatorTypeRegionTLAB) {
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070071 byte_count = RoundUp(byte_count, space::BumpPointerSpace::kAlignment);
72 }
73 // If we have a thread local allocation we don't need to update bytes allocated.
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080074 if ((allocator == kAllocatorTypeTLAB || allocator == kAllocatorTypeRegionTLAB) &&
75 byte_count <= self->TlabSize()) {
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070076 obj = self->AllocTlab(byte_count);
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -070077 DCHECK(obj != nullptr) << "AllocTlab can't fail";
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070078 obj->SetClass(klass);
79 if (kUseBakerOrBrooksReadBarrier) {
80 if (kUseBrooksReadBarrier) {
81 obj->SetReadBarrierPointer(obj);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080082 }
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070083 obj->AssertReadBarrierPointer();
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080084 }
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070085 bytes_allocated = byte_count;
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -070086 usable_size = bytes_allocated;
87 pre_fence_visitor(obj, usable_size);
Mathieu Chartier14cc9be2014-07-11 10:26:37 -070088 QuasiAtomic::ThreadFenceForConstructor();
89 } else {
90 obj = TryToAllocate<kInstrumented, false>(self, allocator, byte_count, &bytes_allocated,
91 &usable_size);
92 if (UNLIKELY(obj == nullptr)) {
93 bool is_current_allocator = allocator == GetCurrentAllocator();
94 obj = AllocateInternalWithGc(self, allocator, byte_count, &bytes_allocated, &usable_size,
95 &klass);
96 if (obj == nullptr) {
97 bool after_is_current_allocator = allocator == GetCurrentAllocator();
Mathieu Chartier8e705192014-08-20 18:19:23 -070098 // If there is a pending exception, fail the allocation right away since the next one
99 // could cause OOM and abort the runtime.
100 if (!self->IsExceptionPending() && is_current_allocator && !after_is_current_allocator) {
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700101 // If the allocator changed, we need to restart the allocation.
102 return AllocObject<kInstrumented>(self, klass, byte_count, pre_fence_visitor);
103 }
104 return nullptr;
105 }
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -0700106 }
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700107 DCHECK_GT(bytes_allocated, 0u);
108 DCHECK_GT(usable_size, 0u);
109 obj->SetClass(klass);
110 if (kUseBakerOrBrooksReadBarrier) {
111 if (kUseBrooksReadBarrier) {
112 obj->SetReadBarrierPointer(obj);
113 }
114 obj->AssertReadBarrierPointer();
115 }
116 if (collector::SemiSpace::kUseRememberedSet && UNLIKELY(allocator == kAllocatorTypeNonMoving)) {
117 // (Note this if statement will be constant folded away for the
118 // fast-path quick entry points.) Because SetClass() has no write
119 // barrier, if a non-moving space allocation, we need a write
120 // barrier as the class pointer may point to the bump pointer
121 // space (where the class pointer is an "old-to-young" reference,
122 // though rare) under the GSS collector with the remembered set
123 // enabled. We don't need this for kAllocatorTypeRosAlloc/DlMalloc
124 // cases because we don't directly allocate into the main alloc
125 // space (besides promotions) under the SS/GSS collector.
126 WriteBarrierField(obj, mirror::Object::ClassOffset(), klass);
127 }
128 pre_fence_visitor(obj, usable_size);
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700129 new_num_bytes_allocated =
130 static_cast<size_t>(num_bytes_allocated_.FetchAndAddSequentiallyConsistent(bytes_allocated))
131 + bytes_allocated;
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800132 }
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -0700133 if (kIsDebugBuild && Runtime::Current()->IsStarted()) {
134 CHECK_LE(obj->SizeOf(), usable_size);
135 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800136 // TODO: Deprecate.
137 if (kInstrumented) {
138 if (Runtime::Current()->HasStatsEnabled()) {
139 RuntimeStats* thread_stats = self->GetStats();
140 ++thread_stats->allocated_objects;
141 thread_stats->allocated_bytes += bytes_allocated;
142 RuntimeStats* global_stats = Runtime::Current()->GetStats();
143 ++global_stats->allocated_objects;
144 global_stats->allocated_bytes += bytes_allocated;
145 }
146 } else {
147 DCHECK(!Runtime::Current()->HasStatsEnabled());
148 }
149 if (AllocatorHasAllocationStack(allocator)) {
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700150 PushOnAllocationStack(self, &obj);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800151 }
152 if (kInstrumented) {
153 if (Dbg::IsAllocTrackingEnabled()) {
Ian Rogers844506b2014-09-12 19:59:33 -0700154 Dbg::RecordAllocation(self, klass, bytes_allocated);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800155 }
156 } else {
157 DCHECK(!Dbg::IsAllocTrackingEnabled());
158 }
Hiroshi Yamauchi3e417802014-03-20 12:03:02 -0700159 // IsConcurrentGc() isn't known at compile time so we can optimize by not checking it for
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800160 // the BumpPointer or TLAB allocators. This is nice since it allows the entire if statement to be
161 // optimized out. And for the other allocators, AllocatorMayHaveConcurrentGC is a constant since
162 // the allocator_type should be constant propagated.
Hiroshi Yamauchi3e417802014-03-20 12:03:02 -0700163 if (AllocatorMayHaveConcurrentGC(allocator) && IsGcConcurrent()) {
Mathieu Chartierf517f1a2014-03-06 15:52:27 -0800164 CheckConcurrentGC(self, new_num_bytes_allocated, &obj);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800165 }
Mathieu Chartier4e305412014-02-19 10:54:44 -0800166 VerifyObject(obj);
167 self->VerifyStack();
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800168 return obj;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700169}
170
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800171// The size of a thread-local allocation stack in the number of references.
172static constexpr size_t kThreadLocalAllocationStackSize = 128;
173
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700174inline void Heap::PushOnAllocationStack(Thread* self, mirror::Object** obj) {
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800175 if (kUseThreadLocalAllocationStack) {
Mathieu Chartierc1790162014-05-23 10:54:50 -0700176 if (UNLIKELY(!self->PushOnThreadLocalAllocationStack(*obj))) {
177 PushOnThreadLocalAllocationStackWithInternalGC(self, obj);
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800178 }
Mathieu Chartierc1790162014-05-23 10:54:50 -0700179 } else if (UNLIKELY(!allocation_stack_->AtomicPushBack(*obj))) {
180 PushOnAllocationStackWithInternalGC(self, obj);
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800181 }
182}
183
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800184template <bool kInstrumented, typename PreFenceVisitor>
Mathieu Chartier446f9ee2014-12-01 15:00:27 -0800185inline mirror::Object* Heap::AllocLargeObject(Thread* self, mirror::Class** klass,
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800186 size_t byte_count,
187 const PreFenceVisitor& pre_fence_visitor) {
Mathieu Chartier446f9ee2014-12-01 15:00:27 -0800188 // Save and restore the class in case it moves.
189 StackHandleScope<1> hs(self);
190 auto klass_wrapper = hs.NewHandleWrapper(klass);
191 return AllocObjectWithAllocator<kInstrumented, false, PreFenceVisitor>(self, *klass, byte_count,
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800192 kAllocatorTypeLOS,
193 pre_fence_visitor);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800194}
195
196template <const bool kInstrumented, const bool kGrow>
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800197inline mirror::Object* Heap::TryToAllocate(Thread* self, AllocatorType allocator_type,
Ian Rogers6fac4472014-02-25 17:01:10 -0800198 size_t alloc_size, size_t* bytes_allocated,
199 size_t* usable_size) {
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800200 if (allocator_type != kAllocatorTypeTLAB && allocator_type != kAllocatorTypeRegionTLAB &&
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700201 UNLIKELY(IsOutOfMemoryOnAllocation<kGrow>(allocator_type, alloc_size))) {
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800202 return nullptr;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700203 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800204 mirror::Object* ret;
205 switch (allocator_type) {
206 case kAllocatorTypeBumpPointer: {
207 DCHECK(bump_pointer_space_ != nullptr);
208 alloc_size = RoundUp(alloc_size, space::BumpPointerSpace::kAlignment);
209 ret = bump_pointer_space_->AllocNonvirtual(alloc_size);
210 if (LIKELY(ret != nullptr)) {
211 *bytes_allocated = alloc_size;
Ian Rogers6fac4472014-02-25 17:01:10 -0800212 *usable_size = alloc_size;
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800213 }
214 break;
215 }
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800216 case kAllocatorTypeRosAlloc: {
217 if (kInstrumented && UNLIKELY(running_on_valgrind_)) {
218 // If running on valgrind, we should be using the instrumented path.
Ian Rogers6fac4472014-02-25 17:01:10 -0800219 ret = rosalloc_space_->Alloc(self, alloc_size, bytes_allocated, usable_size);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800220 } else {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800221 DCHECK(!running_on_valgrind_);
Ian Rogers6fac4472014-02-25 17:01:10 -0800222 ret = rosalloc_space_->AllocNonvirtual(self, alloc_size, bytes_allocated, usable_size);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800223 }
224 break;
225 }
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800226 case kAllocatorTypeDlMalloc: {
227 if (kInstrumented && UNLIKELY(running_on_valgrind_)) {
228 // If running on valgrind, we should be using the instrumented path.
Ian Rogers6fac4472014-02-25 17:01:10 -0800229 ret = dlmalloc_space_->Alloc(self, alloc_size, bytes_allocated, usable_size);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800230 } else {
231 DCHECK(!running_on_valgrind_);
Ian Rogers6fac4472014-02-25 17:01:10 -0800232 ret = dlmalloc_space_->AllocNonvirtual(self, alloc_size, bytes_allocated, usable_size);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800233 }
234 break;
235 }
236 case kAllocatorTypeNonMoving: {
Ian Rogers6fac4472014-02-25 17:01:10 -0800237 ret = non_moving_space_->Alloc(self, alloc_size, bytes_allocated, usable_size);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800238 break;
239 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800240 case kAllocatorTypeLOS: {
Ian Rogers6fac4472014-02-25 17:01:10 -0800241 ret = large_object_space_->Alloc(self, alloc_size, bytes_allocated, usable_size);
Hiroshi Yamauchi95a659f2013-11-22 14:43:45 -0800242 // Note that the bump pointer spaces aren't necessarily next to
243 // the other continuous spaces like the non-moving alloc space or
244 // the zygote space.
245 DCHECK(ret == nullptr || large_object_space_->Contains(ret));
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800246 break;
247 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800248 case kAllocatorTypeTLAB: {
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700249 DCHECK_ALIGNED(alloc_size, space::BumpPointerSpace::kAlignment);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800250 if (UNLIKELY(self->TlabSize() < alloc_size)) {
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700251 const size_t new_tlab_size = alloc_size + kDefaultTLABSize;
252 if (UNLIKELY(IsOutOfMemoryOnAllocation<kGrow>(allocator_type, new_tlab_size))) {
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800253 return nullptr;
254 }
Mathieu Chartier14cc9be2014-07-11 10:26:37 -0700255 // Try allocating a new thread local buffer, if the allocaiton fails the space must be
256 // full so return nullptr.
257 if (!bump_pointer_space_->AllocNewTlab(self, new_tlab_size)) {
258 return nullptr;
259 }
260 *bytes_allocated = new_tlab_size;
261 } else {
262 *bytes_allocated = 0;
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800263 }
264 // The allocation can't fail.
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800265 ret = self->AllocTlab(alloc_size);
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800266 DCHECK(ret != nullptr);
Hiroshi Yamauchi5ccd4982014-03-11 12:19:04 -0700267 *usable_size = alloc_size;
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800268 break;
269 }
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800270 case kAllocatorTypeRegion: {
271 DCHECK(region_space_ != nullptr);
272 alloc_size = RoundUp(alloc_size, space::RegionSpace::kAlignment);
273 ret = region_space_->AllocNonvirtual<false>(alloc_size, bytes_allocated, usable_size);
274 break;
275 }
276 case kAllocatorTypeRegionTLAB: {
277 DCHECK(region_space_ != nullptr);
278 DCHECK_ALIGNED(alloc_size, space::RegionSpace::kAlignment);
279 if (UNLIKELY(self->TlabSize() < alloc_size)) {
280 if (space::RegionSpace::kRegionSize >= alloc_size) {
281 // Non-large. Check OOME for a tlab.
282 if (LIKELY(!IsOutOfMemoryOnAllocation<kGrow>(allocator_type, space::RegionSpace::kRegionSize))) {
283 // Try to allocate a tlab.
284 if (!region_space_->AllocNewTlab(self)) {
285 // Failed to allocate a tlab. Try non-tlab.
286 ret = region_space_->AllocNonvirtual<false>(alloc_size, bytes_allocated, usable_size);
287 return ret;
288 }
289 *bytes_allocated = space::RegionSpace::kRegionSize;
290 // Fall-through.
291 } else {
292 // Check OOME for a non-tlab allocation.
293 if (!IsOutOfMemoryOnAllocation<kGrow>(allocator_type, alloc_size)) {
294 ret = region_space_->AllocNonvirtual<false>(alloc_size, bytes_allocated, usable_size);
295 return ret;
296 } else {
297 // Neither tlab or non-tlab works. Give up.
298 return nullptr;
299 }
300 }
301 } else {
302 // Large. Check OOME.
303 if (LIKELY(!IsOutOfMemoryOnAllocation<kGrow>(allocator_type, alloc_size))) {
304 ret = region_space_->AllocNonvirtual<false>(alloc_size, bytes_allocated, usable_size);
305 return ret;
306 } else {
307 return nullptr;
308 }
309 }
310 } else {
311 *bytes_allocated = 0;
312 }
313 // The allocation can't fail.
314 ret = self->AllocTlab(alloc_size);
315 DCHECK(ret != nullptr);
316 *usable_size = alloc_size;
317 break;
318 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800319 default: {
320 LOG(FATAL) << "Invalid allocator type";
321 ret = nullptr;
322 }
323 }
324 return ret;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700325}
326
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700327inline Heap::AllocationTimer::AllocationTimer(Heap* heap, mirror::Object** allocated_obj_ptr)
328 : heap_(heap), allocated_obj_ptr_(allocated_obj_ptr) {
329 if (kMeasureAllocationTime) {
330 allocation_start_time_ = NanoTime() / kTimeAdjust;
331 }
332}
333
334inline Heap::AllocationTimer::~AllocationTimer() {
335 if (kMeasureAllocationTime) {
336 mirror::Object* allocated_obj = *allocated_obj_ptr_;
337 // Only if the allocation succeeded, record the time.
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800338 if (allocated_obj != nullptr) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700339 uint64_t allocation_end_time = NanoTime() / kTimeAdjust;
Ian Rogers3e5cf302014-05-20 16:40:37 -0700340 heap_->total_allocation_time_.FetchAndAddSequentiallyConsistent(allocation_end_time - allocation_start_time_);
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700341 }
342 }
Andreas Gampec8ccf682014-09-29 20:07:43 -0700343}
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700344
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800345inline bool Heap::ShouldAllocLargeObject(mirror::Class* c, size_t byte_count) const {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700346 // We need to have a zygote space or else our newly allocated large object can end up in the
347 // Zygote resulting in it being prematurely freed.
348 // We can only do this for primitive objects since large objects will not be within the card table
349 // range. This also means that we rely on SetClass not dirtying the object's card.
Mathieu Chartierbd0a6532014-02-27 11:14:21 -0800350 return byte_count >= large_object_threshold_ && c->IsPrimitiveArray();
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700351}
352
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800353template <bool kGrow>
354inline bool Heap::IsOutOfMemoryOnAllocation(AllocatorType allocator_type, size_t alloc_size) {
Ian Rogers3e5cf302014-05-20 16:40:37 -0700355 size_t new_footprint = num_bytes_allocated_.LoadSequentiallyConsistent() + alloc_size;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700356 if (UNLIKELY(new_footprint > max_allowed_footprint_)) {
357 if (UNLIKELY(new_footprint > growth_limit_)) {
358 return true;
359 }
Hiroshi Yamauchi3e417802014-03-20 12:03:02 -0700360 if (!AllocatorMayHaveConcurrentGC(allocator_type) || !IsGcConcurrent()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800361 if (!kGrow) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700362 return true;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700363 }
Mathieu Chartier7bf82af2013-12-06 16:51:45 -0800364 // TODO: Grow for allocation is racy, fix it.
365 VLOG(heap) << "Growing heap from " << PrettySize(max_allowed_footprint_) << " to "
366 << PrettySize(new_footprint) << " for a " << PrettySize(alloc_size) << " allocation";
367 max_allowed_footprint_ = new_footprint;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700368 }
369 }
370 return false;
371}
372
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800373inline void Heap::CheckConcurrentGC(Thread* self, size_t new_num_bytes_allocated,
Mathieu Chartierf517f1a2014-03-06 15:52:27 -0800374 mirror::Object** obj) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700375 if (UNLIKELY(new_num_bytes_allocated >= concurrent_start_bytes_)) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700376 RequestConcurrentGCAndSaveObject(self, obj);
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700377 }
378}
379
380} // namespace gc
381} // namespace art
382
383#endif // ART_RUNTIME_GC_HEAP_INL_H_