Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 1 | /* |
| 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 Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 23 | #include "gc/accounting/card_table-inl.h" |
| 24 | #include "gc/collector/semi_space.h" |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 25 | #include "gc/space/bump_pointer_space-inl.h" |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 26 | #include "gc/space/dlmalloc_space-inl.h" |
| 27 | #include "gc/space/large_object_space.h" |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 28 | #include "gc/space/rosalloc_space-inl.h" |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 29 | #include "runtime.h" |
Mathieu Chartier | c645f1d | 2014-03-06 18:11:53 -0800 | [diff] [blame] | 30 | #include "sirt_ref-inl.h" |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 31 | #include "thread.h" |
| 32 | #include "thread-inl.h" |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 33 | #include "verify_object-inl.h" |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 34 | |
| 35 | namespace art { |
| 36 | namespace gc { |
| 37 | |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 38 | template <bool kInstrumented, bool kCheckLargeObject, typename PreFenceVisitor> |
Mathieu Chartier | 1febddf | 2013-11-20 12:33:14 -0800 | [diff] [blame] | 39 | inline mirror::Object* Heap::AllocObjectWithAllocator(Thread* self, mirror::Class* klass, |
| 40 | size_t byte_count, AllocatorType allocator, |
| 41 | const PreFenceVisitor& pre_fence_visitor) { |
Mathieu Chartier | c645f1d | 2014-03-06 18:11:53 -0800 | [diff] [blame] | 42 | if (kIsDebugBuild) { |
| 43 | CheckPreconditionsForAllocObject(klass, byte_count); |
| 44 | } |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 45 | // 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 | DCHECK_EQ(self->GetState(), kRunnable); |
| 48 | self->AssertThreadSuspensionIsAllowable(); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 49 | // Need to check that we arent the large object allocator since the large object allocation code |
| 50 | // path this function. If we didn't check we would have an infinite loop. |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 51 | if (kCheckLargeObject && UNLIKELY(ShouldAllocLargeObject(klass, byte_count))) { |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 52 | return AllocLargeObject<kInstrumented, PreFenceVisitor>(self, klass, byte_count, |
| 53 | pre_fence_visitor); |
| 54 | } |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 55 | mirror::Object* obj; |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 56 | AllocationTimer alloc_timer(this, &obj); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 57 | size_t bytes_allocated, usable_size; |
| 58 | obj = TryToAllocate<kInstrumented, false>(self, allocator, byte_count, &bytes_allocated, |
| 59 | &usable_size); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 60 | if (UNLIKELY(obj == nullptr)) { |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 61 | bool is_current_allocator = allocator == GetCurrentAllocator(); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 62 | obj = AllocateInternalWithGc(self, allocator, byte_count, &bytes_allocated, &usable_size, |
| 63 | &klass); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 64 | if (obj == nullptr) { |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 65 | bool after_is_current_allocator = allocator == GetCurrentAllocator(); |
| 66 | if (is_current_allocator && !after_is_current_allocator) { |
| 67 | // If the allocator changed, we need to restart the allocation. |
| 68 | return AllocObject<kInstrumented>(self, klass, byte_count); |
| 69 | } |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 70 | return nullptr; |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 71 | } |
| 72 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 73 | DCHECK_GT(bytes_allocated, 0u); |
| 74 | DCHECK_GT(usable_size, 0u); |
Mathieu Chartier | 1febddf | 2013-11-20 12:33:14 -0800 | [diff] [blame] | 75 | obj->SetClass(klass); |
Hiroshi Yamauchi | 624468c | 2014-03-31 15:14:47 -0700 | [diff] [blame] | 76 | if (kUseBakerOrBrooksReadBarrier) { |
| 77 | if (kUseBrooksReadBarrier) { |
| 78 | obj->SetReadBarrierPointer(obj); |
| 79 | } |
| 80 | obj->AssertReadBarrierPointer(); |
Hiroshi Yamauchi | 9d04a20 | 2014-01-31 13:35:49 -0800 | [diff] [blame] | 81 | } |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 82 | if (collector::SemiSpace::kUseRememberedSet && UNLIKELY(allocator == kAllocatorTypeNonMoving)) { |
| 83 | // (Note this if statement will be constant folded away for the |
| 84 | // fast-path quick entry points.) Because SetClass() has no write |
| 85 | // barrier, if a non-moving space allocation, we need a write |
| 86 | // barrier as the class pointer may point to the bump pointer |
| 87 | // space (where the class pointer is an "old-to-young" reference, |
| 88 | // though rare) under the GSS collector with the remembered set |
| 89 | // enabled. We don't need this for kAllocatorTypeRosAlloc/DlMalloc |
| 90 | // cases because we don't directly allocate into the main alloc |
| 91 | // space (besides promotions) under the SS/GSS collector. |
| 92 | WriteBarrierField(obj, mirror::Object::ClassOffset(), klass); |
| 93 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 94 | pre_fence_visitor(obj, usable_size); |
Ian Rogers | a55cf41 | 2014-02-27 00:31:26 -0800 | [diff] [blame] | 95 | if (kIsDebugBuild && Runtime::Current()->IsStarted()) { |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 96 | CHECK_LE(obj->SizeOf(), usable_size); |
| 97 | } |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 98 | const size_t new_num_bytes_allocated = |
Ian Rogers | b122a4b | 2013-11-19 18:00:50 -0800 | [diff] [blame] | 99 | static_cast<size_t>(num_bytes_allocated_.FetchAndAdd(bytes_allocated)) + bytes_allocated; |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 100 | // TODO: Deprecate. |
| 101 | if (kInstrumented) { |
| 102 | if (Runtime::Current()->HasStatsEnabled()) { |
| 103 | RuntimeStats* thread_stats = self->GetStats(); |
| 104 | ++thread_stats->allocated_objects; |
| 105 | thread_stats->allocated_bytes += bytes_allocated; |
| 106 | RuntimeStats* global_stats = Runtime::Current()->GetStats(); |
| 107 | ++global_stats->allocated_objects; |
| 108 | global_stats->allocated_bytes += bytes_allocated; |
| 109 | } |
| 110 | } else { |
| 111 | DCHECK(!Runtime::Current()->HasStatsEnabled()); |
| 112 | } |
| 113 | if (AllocatorHasAllocationStack(allocator)) { |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 114 | PushOnAllocationStack(self, obj); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 115 | } |
| 116 | if (kInstrumented) { |
| 117 | if (Dbg::IsAllocTrackingEnabled()) { |
Mathieu Chartier | 1febddf | 2013-11-20 12:33:14 -0800 | [diff] [blame] | 118 | Dbg::RecordAllocation(klass, bytes_allocated); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 119 | } |
| 120 | } else { |
| 121 | DCHECK(!Dbg::IsAllocTrackingEnabled()); |
| 122 | } |
Hiroshi Yamauchi | 3e41780 | 2014-03-20 12:03:02 -0700 | [diff] [blame] | 123 | // IsConcurrentGc() isn't known at compile time so we can optimize by not checking it for |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 124 | // the BumpPointer or TLAB allocators. This is nice since it allows the entire if statement to be |
| 125 | // optimized out. And for the other allocators, AllocatorMayHaveConcurrentGC is a constant since |
| 126 | // the allocator_type should be constant propagated. |
Hiroshi Yamauchi | 3e41780 | 2014-03-20 12:03:02 -0700 | [diff] [blame] | 127 | if (AllocatorMayHaveConcurrentGC(allocator) && IsGcConcurrent()) { |
Mathieu Chartier | f517f1a | 2014-03-06 15:52:27 -0800 | [diff] [blame] | 128 | CheckConcurrentGC(self, new_num_bytes_allocated, &obj); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 129 | } |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 130 | VerifyObject(obj); |
| 131 | self->VerifyStack(); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 132 | return obj; |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 135 | // The size of a thread-local allocation stack in the number of references. |
| 136 | static constexpr size_t kThreadLocalAllocationStackSize = 128; |
| 137 | |
| 138 | inline void Heap::PushOnAllocationStack(Thread* self, mirror::Object* obj) { |
| 139 | if (kUseThreadLocalAllocationStack) { |
| 140 | bool success = self->PushOnThreadLocalAllocationStack(obj); |
| 141 | if (UNLIKELY(!success)) { |
| 142 | // Slow path. Allocate a new thread-local allocation stack. |
| 143 | mirror::Object** start_address; |
| 144 | mirror::Object** end_address; |
| 145 | while (!allocation_stack_->AtomicBumpBack(kThreadLocalAllocationStackSize, |
| 146 | &start_address, &end_address)) { |
| 147 | CollectGarbageInternal(collector::kGcTypeSticky, kGcCauseForAlloc, false); |
| 148 | } |
| 149 | self->SetThreadLocalAllocationStack(start_address, end_address); |
| 150 | // Retry on the new thread-local allocation stack. |
| 151 | success = self->PushOnThreadLocalAllocationStack(obj); |
| 152 | // Must succeed. |
| 153 | CHECK(success); |
| 154 | } |
| 155 | } else { |
| 156 | // This is safe to do since the GC will never free objects which are neither in the allocation |
| 157 | // stack or the live bitmap. |
| 158 | while (!allocation_stack_->AtomicPushBack(obj)) { |
| 159 | CollectGarbageInternal(collector::kGcTypeSticky, kGcCauseForAlloc, false); |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 164 | template <bool kInstrumented, typename PreFenceVisitor> |
| 165 | inline mirror::Object* Heap::AllocLargeObject(Thread* self, mirror::Class* klass, |
| 166 | size_t byte_count, |
| 167 | const PreFenceVisitor& pre_fence_visitor) { |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 168 | return AllocObjectWithAllocator<kInstrumented, false, PreFenceVisitor>(self, klass, byte_count, |
| 169 | kAllocatorTypeLOS, |
| 170 | pre_fence_visitor); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | template <const bool kInstrumented, const bool kGrow> |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 174 | inline mirror::Object* Heap::TryToAllocate(Thread* self, AllocatorType allocator_type, |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 175 | size_t alloc_size, size_t* bytes_allocated, |
| 176 | size_t* usable_size) { |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 177 | if (UNLIKELY(IsOutOfMemoryOnAllocation<kGrow>(allocator_type, alloc_size))) { |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 178 | return nullptr; |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 179 | } |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 180 | mirror::Object* ret; |
| 181 | switch (allocator_type) { |
| 182 | case kAllocatorTypeBumpPointer: { |
| 183 | DCHECK(bump_pointer_space_ != nullptr); |
| 184 | alloc_size = RoundUp(alloc_size, space::BumpPointerSpace::kAlignment); |
| 185 | ret = bump_pointer_space_->AllocNonvirtual(alloc_size); |
| 186 | if (LIKELY(ret != nullptr)) { |
| 187 | *bytes_allocated = alloc_size; |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 188 | *usable_size = alloc_size; |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 189 | } |
| 190 | break; |
| 191 | } |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 192 | case kAllocatorTypeRosAlloc: { |
| 193 | if (kInstrumented && UNLIKELY(running_on_valgrind_)) { |
| 194 | // If running on valgrind, we should be using the instrumented path. |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 195 | ret = rosalloc_space_->Alloc(self, alloc_size, bytes_allocated, usable_size); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 196 | } else { |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 197 | DCHECK(!running_on_valgrind_); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 198 | ret = rosalloc_space_->AllocNonvirtual(self, alloc_size, bytes_allocated, usable_size); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 199 | } |
| 200 | break; |
| 201 | } |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 202 | case kAllocatorTypeDlMalloc: { |
| 203 | if (kInstrumented && UNLIKELY(running_on_valgrind_)) { |
| 204 | // If running on valgrind, we should be using the instrumented path. |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 205 | ret = dlmalloc_space_->Alloc(self, alloc_size, bytes_allocated, usable_size); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 206 | } else { |
| 207 | DCHECK(!running_on_valgrind_); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 208 | ret = dlmalloc_space_->AllocNonvirtual(self, alloc_size, bytes_allocated, usable_size); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 209 | } |
| 210 | break; |
| 211 | } |
| 212 | case kAllocatorTypeNonMoving: { |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 213 | ret = non_moving_space_->Alloc(self, alloc_size, bytes_allocated, usable_size); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 214 | break; |
| 215 | } |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 216 | case kAllocatorTypeLOS: { |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 217 | ret = large_object_space_->Alloc(self, alloc_size, bytes_allocated, usable_size); |
Hiroshi Yamauchi | 95a659f | 2013-11-22 14:43:45 -0800 | [diff] [blame] | 218 | // Note that the bump pointer spaces aren't necessarily next to |
| 219 | // the other continuous spaces like the non-moving alloc space or |
| 220 | // the zygote space. |
| 221 | DCHECK(ret == nullptr || large_object_space_->Contains(ret)); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 222 | break; |
| 223 | } |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 224 | case kAllocatorTypeTLAB: { |
| 225 | alloc_size = RoundUp(alloc_size, space::BumpPointerSpace::kAlignment); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 226 | if (UNLIKELY(self->TlabSize() < alloc_size)) { |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 227 | // Try allocating a new thread local buffer, if the allocaiton fails the space must be |
| 228 | // full so return nullptr. |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 229 | if (!bump_pointer_space_->AllocNewTlab(self, alloc_size + kDefaultTLABSize)) { |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 230 | return nullptr; |
| 231 | } |
| 232 | } |
| 233 | // The allocation can't fail. |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 234 | ret = self->AllocTlab(alloc_size); |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 235 | DCHECK(ret != nullptr); |
| 236 | *bytes_allocated = alloc_size; |
Hiroshi Yamauchi | 5ccd498 | 2014-03-11 12:19:04 -0700 | [diff] [blame] | 237 | *usable_size = alloc_size; |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 238 | break; |
| 239 | } |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 240 | default: { |
| 241 | LOG(FATAL) << "Invalid allocator type"; |
| 242 | ret = nullptr; |
| 243 | } |
| 244 | } |
| 245 | return ret; |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 248 | inline Heap::AllocationTimer::AllocationTimer(Heap* heap, mirror::Object** allocated_obj_ptr) |
| 249 | : heap_(heap), allocated_obj_ptr_(allocated_obj_ptr) { |
| 250 | if (kMeasureAllocationTime) { |
| 251 | allocation_start_time_ = NanoTime() / kTimeAdjust; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | inline Heap::AllocationTimer::~AllocationTimer() { |
| 256 | if (kMeasureAllocationTime) { |
| 257 | mirror::Object* allocated_obj = *allocated_obj_ptr_; |
| 258 | // Only if the allocation succeeded, record the time. |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 259 | if (allocated_obj != nullptr) { |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 260 | uint64_t allocation_end_time = NanoTime() / kTimeAdjust; |
Ian Rogers | b122a4b | 2013-11-19 18:00:50 -0800 | [diff] [blame] | 261 | heap_->total_allocation_time_.FetchAndAdd(allocation_end_time - allocation_start_time_); |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | }; |
| 265 | |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 266 | inline bool Heap::ShouldAllocLargeObject(mirror::Class* c, size_t byte_count) const { |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 267 | // We need to have a zygote space or else our newly allocated large object can end up in the |
| 268 | // Zygote resulting in it being prematurely freed. |
| 269 | // We can only do this for primitive objects since large objects will not be within the card table |
| 270 | // range. This also means that we rely on SetClass not dirtying the object's card. |
Mathieu Chartier | bd0a653 | 2014-02-27 11:14:21 -0800 | [diff] [blame] | 271 | return byte_count >= large_object_threshold_ && c->IsPrimitiveArray(); |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 274 | template <bool kGrow> |
| 275 | inline bool Heap::IsOutOfMemoryOnAllocation(AllocatorType allocator_type, size_t alloc_size) { |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 276 | size_t new_footprint = num_bytes_allocated_ + alloc_size; |
| 277 | if (UNLIKELY(new_footprint > max_allowed_footprint_)) { |
| 278 | if (UNLIKELY(new_footprint > growth_limit_)) { |
| 279 | return true; |
| 280 | } |
Hiroshi Yamauchi | 3e41780 | 2014-03-20 12:03:02 -0700 | [diff] [blame] | 281 | if (!AllocatorMayHaveConcurrentGC(allocator_type) || !IsGcConcurrent()) { |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 282 | if (!kGrow) { |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 283 | return true; |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 284 | } |
Mathieu Chartier | 7bf82af | 2013-12-06 16:51:45 -0800 | [diff] [blame] | 285 | // TODO: Grow for allocation is racy, fix it. |
| 286 | VLOG(heap) << "Growing heap from " << PrettySize(max_allowed_footprint_) << " to " |
| 287 | << PrettySize(new_footprint) << " for a " << PrettySize(alloc_size) << " allocation"; |
| 288 | max_allowed_footprint_ = new_footprint; |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | return false; |
| 292 | } |
| 293 | |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 294 | inline void Heap::CheckConcurrentGC(Thread* self, size_t new_num_bytes_allocated, |
Mathieu Chartier | f517f1a | 2014-03-06 15:52:27 -0800 | [diff] [blame] | 295 | mirror::Object** obj) { |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 296 | if (UNLIKELY(new_num_bytes_allocated >= concurrent_start_bytes_)) { |
| 297 | // The SirtRef is necessary since the calls in RequestConcurrentGC are a safepoint. |
Mathieu Chartier | f517f1a | 2014-03-06 15:52:27 -0800 | [diff] [blame] | 298 | SirtRef<mirror::Object> ref(self, *obj); |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 299 | RequestConcurrentGC(self); |
Mathieu Chartier | f517f1a | 2014-03-06 15:52:27 -0800 | [diff] [blame] | 300 | // Restore obj in case it moved. |
| 301 | *obj = ref.get(); |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | |
| 305 | } // namespace gc |
| 306 | } // namespace art |
| 307 | |
| 308 | #endif // ART_RUNTIME_GC_HEAP_INL_H_ |