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 | |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 22 | #include "allocation_listener.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 23 | #include "base/time_utils.h" |
Andreas Gampe | d490129 | 2017-05-30 18:41:34 -0700 | [diff] [blame] | 24 | #include "gc/accounting/atomic_stack.h" |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 25 | #include "gc/accounting/card_table-inl.h" |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 26 | #include "gc/allocation_record.h" |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 27 | #include "gc/collector/semi_space.h" |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 28 | #include "gc/space/bump_pointer_space-inl.h" |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 29 | #include "gc/space/dlmalloc_space-inl.h" |
| 30 | #include "gc/space/large_object_space.h" |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 31 | #include "gc/space/region_space-inl.h" |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 32 | #include "gc/space/rosalloc_space-inl.h" |
Mathieu Chartier | a058fdf | 2016-10-06 15:13:58 -0700 | [diff] [blame] | 33 | #include "obj_ptr-inl.h" |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 34 | #include "runtime.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 35 | #include "handle_scope-inl.h" |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 36 | #include "thread-inl.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 37 | #include "utils.h" |
Andreas Gampe | 90b936d | 2017-01-31 08:58:55 -0800 | [diff] [blame] | 38 | #include "verify_object.h" |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 39 | |
| 40 | namespace art { |
| 41 | namespace gc { |
| 42 | |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 43 | template <bool kInstrumented, bool kCheckLargeObject, typename PreFenceVisitor> |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 44 | inline mirror::Object* Heap::AllocObjectWithAllocator(Thread* self, |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 45 | ObjPtr<mirror::Class> klass, |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 46 | size_t byte_count, |
| 47 | AllocatorType allocator, |
Mathieu Chartier | 1febddf | 2013-11-20 12:33:14 -0800 | [diff] [blame] | 48 | const PreFenceVisitor& pre_fence_visitor) { |
Mathieu Chartier | c645f1d | 2014-03-06 18:11:53 -0800 | [diff] [blame] | 49 | if (kIsDebugBuild) { |
| 50 | CheckPreconditionsForAllocObject(klass, byte_count); |
Mathieu Chartier | 14cc9be | 2014-07-11 10:26:37 -0700 | [diff] [blame] | 51 | // Since allocation can cause a GC which will need to SuspendAll, make sure all allocations are |
| 52 | // done in the runnable state where suspension is expected. |
| 53 | CHECK_EQ(self->GetState(), kRunnable); |
| 54 | self->AssertThreadSuspensionIsAllowable(); |
Mathieu Chartier | 8502f72 | 2016-06-08 15:09:08 -0700 | [diff] [blame] | 55 | self->AssertNoPendingException(); |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 56 | // Make sure to preserve klass. |
| 57 | StackHandleScope<1> hs(self); |
| 58 | HandleWrapperObjPtr<mirror::Class> h = hs.NewHandleWrapper(&klass); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 59 | self->PoisonObjectPointers(); |
Mathieu Chartier | c645f1d | 2014-03-06 18:11:53 -0800 | [diff] [blame] | 60 | } |
Roland Levillain | b81e9e9 | 2017-04-20 17:35:32 +0100 | [diff] [blame] | 61 | // Need to check that we aren't the large object allocator since the large object allocation code |
| 62 | // path includes this function. If we didn't check we would have an infinite loop. |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 63 | ObjPtr<mirror::Object> obj; |
Mathieu Chartier | 446f9ee | 2014-12-01 15:00:27 -0800 | [diff] [blame] | 64 | if (kCheckLargeObject && UNLIKELY(ShouldAllocLargeObject(klass, byte_count))) { |
| 65 | obj = AllocLargeObject<kInstrumented, PreFenceVisitor>(self, &klass, byte_count, |
| 66 | pre_fence_visitor); |
| 67 | if (obj != nullptr) { |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 68 | return obj.Ptr(); |
Mathieu Chartier | 446f9ee | 2014-12-01 15:00:27 -0800 | [diff] [blame] | 69 | } else { |
| 70 | // There should be an OOM exception, since we are retrying, clear it. |
| 71 | self->ClearException(); |
| 72 | } |
| 73 | // If the large object allocation failed, try to use the normal spaces (main space, |
| 74 | // non moving space). This can happen if there is significant virtual address space |
| 75 | // fragmentation. |
| 76 | } |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 77 | // bytes allocated for the (individual) object. |
Mathieu Chartier | 14cc9be | 2014-07-11 10:26:37 -0700 | [diff] [blame] | 78 | size_t bytes_allocated; |
| 79 | size_t usable_size; |
| 80 | size_t new_num_bytes_allocated = 0; |
Mathieu Chartier | 6bc7774 | 2017-04-18 17:46:23 -0700 | [diff] [blame] | 81 | if (IsTLABAllocator(allocator)) { |
Mathieu Chartier | 14cc9be | 2014-07-11 10:26:37 -0700 | [diff] [blame] | 82 | byte_count = RoundUp(byte_count, space::BumpPointerSpace::kAlignment); |
| 83 | } |
| 84 | // If we have a thread local allocation we don't need to update bytes allocated. |
Mathieu Chartier | 6bc7774 | 2017-04-18 17:46:23 -0700 | [diff] [blame] | 85 | if (IsTLABAllocator(allocator) && byte_count <= self->TlabSize()) { |
Mathieu Chartier | 14cc9be | 2014-07-11 10:26:37 -0700 | [diff] [blame] | 86 | obj = self->AllocTlab(byte_count); |
Mathieu Chartier | fd22d5b | 2014-07-14 10:16:05 -0700 | [diff] [blame] | 87 | DCHECK(obj != nullptr) << "AllocTlab can't fail"; |
Mathieu Chartier | 14cc9be | 2014-07-11 10:26:37 -0700 | [diff] [blame] | 88 | obj->SetClass(klass); |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 89 | if (kUseBakerReadBarrier) { |
| 90 | obj->AssertReadBarrierState(); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 91 | } |
Mathieu Chartier | 14cc9be | 2014-07-11 10:26:37 -0700 | [diff] [blame] | 92 | bytes_allocated = byte_count; |
Mathieu Chartier | fd22d5b | 2014-07-14 10:16:05 -0700 | [diff] [blame] | 93 | usable_size = bytes_allocated; |
| 94 | pre_fence_visitor(obj, usable_size); |
Mathieu Chartier | 14cc9be | 2014-07-11 10:26:37 -0700 | [diff] [blame] | 95 | QuasiAtomic::ThreadFenceForConstructor(); |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 96 | } else if ( |
| 97 | !kInstrumented && allocator == kAllocatorTypeRosAlloc && |
| 98 | (obj = rosalloc_space_->AllocThreadLocal(self, byte_count, &bytes_allocated)) != nullptr && |
| 99 | LIKELY(obj != nullptr)) { |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 100 | DCHECK(!is_running_on_memory_tool_); |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 101 | obj->SetClass(klass); |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 102 | if (kUseBakerReadBarrier) { |
| 103 | obj->AssertReadBarrierState(); |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 104 | } |
| 105 | usable_size = bytes_allocated; |
| 106 | pre_fence_visitor(obj, usable_size); |
| 107 | QuasiAtomic::ThreadFenceForConstructor(); |
Mathieu Chartier | 14cc9be | 2014-07-11 10:26:37 -0700 | [diff] [blame] | 108 | } else { |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 109 | // bytes allocated that takes bulk thread-local buffer allocations into account. |
| 110 | size_t bytes_tl_bulk_allocated = 0; |
Mathieu Chartier | 14cc9be | 2014-07-11 10:26:37 -0700 | [diff] [blame] | 111 | obj = TryToAllocate<kInstrumented, false>(self, allocator, byte_count, &bytes_allocated, |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 112 | &usable_size, &bytes_tl_bulk_allocated); |
Mathieu Chartier | 14cc9be | 2014-07-11 10:26:37 -0700 | [diff] [blame] | 113 | if (UNLIKELY(obj == nullptr)) { |
Mathieu Chartier | eebc3af | 2016-02-29 18:13:38 -0800 | [diff] [blame] | 114 | // AllocateInternalWithGc can cause thread suspension, if someone instruments the entrypoints |
| 115 | // or changes the allocator in a suspend point here, we need to retry the allocation. |
| 116 | obj = AllocateInternalWithGc(self, |
| 117 | allocator, |
| 118 | kInstrumented, |
| 119 | byte_count, |
| 120 | &bytes_allocated, |
| 121 | &usable_size, |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 122 | &bytes_tl_bulk_allocated, &klass); |
Mathieu Chartier | 14cc9be | 2014-07-11 10:26:37 -0700 | [diff] [blame] | 123 | if (obj == nullptr) { |
Mathieu Chartier | eebc3af | 2016-02-29 18:13:38 -0800 | [diff] [blame] | 124 | // The only way that we can get a null return if there is no pending exception is if the |
| 125 | // allocator or instrumentation changed. |
| 126 | if (!self->IsExceptionPending()) { |
| 127 | // AllocObject will pick up the new allocator type, and instrumented as true is the safe |
| 128 | // default. |
| 129 | return AllocObject</*kInstrumented*/true>(self, |
| 130 | klass, |
| 131 | byte_count, |
| 132 | pre_fence_visitor); |
Mathieu Chartier | 14cc9be | 2014-07-11 10:26:37 -0700 | [diff] [blame] | 133 | } |
| 134 | return nullptr; |
| 135 | } |
Hiroshi Yamauchi | 624468c | 2014-03-31 15:14:47 -0700 | [diff] [blame] | 136 | } |
Mathieu Chartier | 14cc9be | 2014-07-11 10:26:37 -0700 | [diff] [blame] | 137 | DCHECK_GT(bytes_allocated, 0u); |
| 138 | DCHECK_GT(usable_size, 0u); |
| 139 | obj->SetClass(klass); |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 140 | if (kUseBakerReadBarrier) { |
| 141 | obj->AssertReadBarrierState(); |
Mathieu Chartier | 14cc9be | 2014-07-11 10:26:37 -0700 | [diff] [blame] | 142 | } |
| 143 | if (collector::SemiSpace::kUseRememberedSet && UNLIKELY(allocator == kAllocatorTypeNonMoving)) { |
| 144 | // (Note this if statement will be constant folded away for the |
| 145 | // fast-path quick entry points.) Because SetClass() has no write |
| 146 | // barrier, if a non-moving space allocation, we need a write |
| 147 | // barrier as the class pointer may point to the bump pointer |
| 148 | // space (where the class pointer is an "old-to-young" reference, |
| 149 | // though rare) under the GSS collector with the remembered set |
| 150 | // enabled. We don't need this for kAllocatorTypeRosAlloc/DlMalloc |
| 151 | // cases because we don't directly allocate into the main alloc |
| 152 | // space (besides promotions) under the SS/GSS collector. |
| 153 | WriteBarrierField(obj, mirror::Object::ClassOffset(), klass); |
| 154 | } |
| 155 | pre_fence_visitor(obj, usable_size); |
Hans Boehm | b0171b9 | 2016-01-28 17:19:15 -0800 | [diff] [blame] | 156 | QuasiAtomic::ThreadFenceForConstructor(); |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 157 | new_num_bytes_allocated = static_cast<size_t>( |
Hans Boehm | b0171b9 | 2016-01-28 17:19:15 -0800 | [diff] [blame] | 158 | num_bytes_allocated_.FetchAndAddRelaxed(bytes_tl_bulk_allocated)) + bytes_tl_bulk_allocated; |
Hiroshi Yamauchi | 9d04a20 | 2014-01-31 13:35:49 -0800 | [diff] [blame] | 159 | } |
Mathieu Chartier | fd22d5b | 2014-07-14 10:16:05 -0700 | [diff] [blame] | 160 | if (kIsDebugBuild && Runtime::Current()->IsStarted()) { |
| 161 | CHECK_LE(obj->SizeOf(), usable_size); |
| 162 | } |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 163 | // TODO: Deprecate. |
| 164 | if (kInstrumented) { |
| 165 | if (Runtime::Current()->HasStatsEnabled()) { |
| 166 | RuntimeStats* thread_stats = self->GetStats(); |
| 167 | ++thread_stats->allocated_objects; |
| 168 | thread_stats->allocated_bytes += bytes_allocated; |
| 169 | RuntimeStats* global_stats = Runtime::Current()->GetStats(); |
| 170 | ++global_stats->allocated_objects; |
| 171 | global_stats->allocated_bytes += bytes_allocated; |
| 172 | } |
| 173 | } else { |
| 174 | DCHECK(!Runtime::Current()->HasStatsEnabled()); |
| 175 | } |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 176 | if (kInstrumented) { |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 177 | if (IsAllocTrackingEnabled()) { |
Mathieu Chartier | 458b105 | 2016-03-29 14:02:55 -0700 | [diff] [blame] | 178 | // allocation_records_ is not null since it never becomes null after allocation tracking is |
| 179 | // enabled. |
| 180 | DCHECK(allocation_records_ != nullptr); |
| 181 | allocation_records_->RecordAllocation(self, &obj, bytes_allocated); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 182 | } |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 183 | AllocationListener* l = alloc_listener_.LoadSequentiallyConsistent(); |
| 184 | if (l != nullptr) { |
| 185 | // Same as above. We assume that a listener that was once stored will never be deleted. |
| 186 | // Otherwise we'd have to perform this under a lock. |
| 187 | l->ObjectAllocated(self, &obj, bytes_allocated); |
| 188 | } |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 189 | } else { |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 190 | DCHECK(!IsAllocTrackingEnabled()); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 191 | } |
Mathieu Chartier | 14b0a5d | 2016-03-11 17:22:23 -0800 | [diff] [blame] | 192 | if (AllocatorHasAllocationStack(allocator)) { |
| 193 | PushOnAllocationStack(self, &obj); |
| 194 | } |
Mathieu Chartier | 3100080 | 2015-06-14 14:14:37 -0700 | [diff] [blame] | 195 | if (kInstrumented) { |
| 196 | if (gc_stress_mode_) { |
| 197 | CheckGcStressMode(self, &obj); |
| 198 | } |
| 199 | } else { |
| 200 | DCHECK(!gc_stress_mode_); |
| 201 | } |
Roland Levillain | b81e9e9 | 2017-04-20 17:35:32 +0100 | [diff] [blame] | 202 | // IsGcConcurrent() 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] | 203 | // the BumpPointer or TLAB allocators. This is nice since it allows the entire if statement to be |
| 204 | // optimized out. And for the other allocators, AllocatorMayHaveConcurrentGC is a constant since |
| 205 | // the allocator_type should be constant propagated. |
Hiroshi Yamauchi | 3e41780 | 2014-03-20 12:03:02 -0700 | [diff] [blame] | 206 | if (AllocatorMayHaveConcurrentGC(allocator) && IsGcConcurrent()) { |
Mathieu Chartier | f517f1a | 2014-03-06 15:52:27 -0800 | [diff] [blame] | 207 | CheckConcurrentGC(self, new_num_bytes_allocated, &obj); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 208 | } |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 209 | VerifyObject(obj); |
| 210 | self->VerifyStack(); |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 211 | return obj.Ptr(); |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 214 | // The size of a thread-local allocation stack in the number of references. |
| 215 | static constexpr size_t kThreadLocalAllocationStackSize = 128; |
| 216 | |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 217 | inline void Heap::PushOnAllocationStack(Thread* self, ObjPtr<mirror::Object>* obj) { |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 218 | if (kUseThreadLocalAllocationStack) { |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 219 | if (UNLIKELY(!self->PushOnThreadLocalAllocationStack(obj->Ptr()))) { |
Mathieu Chartier | c179016 | 2014-05-23 10:54:50 -0700 | [diff] [blame] | 220 | PushOnThreadLocalAllocationStackWithInternalGC(self, obj); |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 221 | } |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 222 | } else if (UNLIKELY(!allocation_stack_->AtomicPushBack(obj->Ptr()))) { |
Mathieu Chartier | c179016 | 2014-05-23 10:54:50 -0700 | [diff] [blame] | 223 | PushOnAllocationStackWithInternalGC(self, obj); |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 227 | template <bool kInstrumented, typename PreFenceVisitor> |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 228 | inline mirror::Object* Heap::AllocLargeObject(Thread* self, |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 229 | ObjPtr<mirror::Class>* klass, |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 230 | size_t byte_count, |
| 231 | const PreFenceVisitor& pre_fence_visitor) { |
Mathieu Chartier | 446f9ee | 2014-12-01 15:00:27 -0800 | [diff] [blame] | 232 | // Save and restore the class in case it moves. |
| 233 | StackHandleScope<1> hs(self); |
| 234 | auto klass_wrapper = hs.NewHandleWrapper(klass); |
| 235 | return AllocObjectWithAllocator<kInstrumented, false, PreFenceVisitor>(self, *klass, byte_count, |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 236 | kAllocatorTypeLOS, |
| 237 | pre_fence_visitor); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | template <const bool kInstrumented, const bool kGrow> |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 241 | inline mirror::Object* Heap::TryToAllocate(Thread* self, |
| 242 | AllocatorType allocator_type, |
| 243 | size_t alloc_size, |
| 244 | size_t* bytes_allocated, |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 245 | size_t* usable_size, |
| 246 | size_t* bytes_tl_bulk_allocated) { |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 247 | if (allocator_type != kAllocatorTypeTLAB && |
| 248 | allocator_type != kAllocatorTypeRegionTLAB && |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 249 | allocator_type != kAllocatorTypeRosAlloc && |
Mathieu Chartier | 5ace201 | 2016-11-30 10:15:41 -0800 | [diff] [blame] | 250 | UNLIKELY(IsOutOfMemoryOnAllocation(allocator_type, alloc_size, kGrow))) { |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 251 | return nullptr; |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 252 | } |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 253 | mirror::Object* ret; |
| 254 | switch (allocator_type) { |
| 255 | case kAllocatorTypeBumpPointer: { |
| 256 | DCHECK(bump_pointer_space_ != nullptr); |
| 257 | alloc_size = RoundUp(alloc_size, space::BumpPointerSpace::kAlignment); |
| 258 | ret = bump_pointer_space_->AllocNonvirtual(alloc_size); |
| 259 | if (LIKELY(ret != nullptr)) { |
| 260 | *bytes_allocated = alloc_size; |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 261 | *usable_size = alloc_size; |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 262 | *bytes_tl_bulk_allocated = alloc_size; |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 263 | } |
| 264 | break; |
| 265 | } |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 266 | case kAllocatorTypeRosAlloc: { |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 267 | if (kInstrumented && UNLIKELY(is_running_on_memory_tool_)) { |
| 268 | // If running on valgrind or asan, we should be using the instrumented path. |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 269 | size_t max_bytes_tl_bulk_allocated = rosalloc_space_->MaxBytesBulkAllocatedFor(alloc_size); |
Mathieu Chartier | 5ace201 | 2016-11-30 10:15:41 -0800 | [diff] [blame] | 270 | if (UNLIKELY(IsOutOfMemoryOnAllocation(allocator_type, |
| 271 | max_bytes_tl_bulk_allocated, |
| 272 | kGrow))) { |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 273 | return nullptr; |
| 274 | } |
| 275 | ret = rosalloc_space_->Alloc(self, alloc_size, bytes_allocated, usable_size, |
| 276 | bytes_tl_bulk_allocated); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 277 | } else { |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 278 | DCHECK(!is_running_on_memory_tool_); |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 279 | size_t max_bytes_tl_bulk_allocated = |
| 280 | rosalloc_space_->MaxBytesBulkAllocatedForNonvirtual(alloc_size); |
Mathieu Chartier | 5ace201 | 2016-11-30 10:15:41 -0800 | [diff] [blame] | 281 | if (UNLIKELY(IsOutOfMemoryOnAllocation(allocator_type, |
| 282 | max_bytes_tl_bulk_allocated, |
| 283 | kGrow))) { |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 284 | return nullptr; |
| 285 | } |
| 286 | if (!kInstrumented) { |
| 287 | DCHECK(!rosalloc_space_->CanAllocThreadLocal(self, alloc_size)); |
| 288 | } |
Mathieu Chartier | 5ace201 | 2016-11-30 10:15:41 -0800 | [diff] [blame] | 289 | ret = rosalloc_space_->AllocNonvirtual(self, |
| 290 | alloc_size, |
| 291 | bytes_allocated, |
| 292 | usable_size, |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 293 | bytes_tl_bulk_allocated); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 294 | } |
| 295 | break; |
| 296 | } |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 297 | case kAllocatorTypeDlMalloc: { |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 298 | if (kInstrumented && UNLIKELY(is_running_on_memory_tool_)) { |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 299 | // If running on valgrind, we should be using the instrumented path. |
Mathieu Chartier | 5ace201 | 2016-11-30 10:15:41 -0800 | [diff] [blame] | 300 | ret = dlmalloc_space_->Alloc(self, |
| 301 | alloc_size, |
| 302 | bytes_allocated, |
| 303 | usable_size, |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 304 | bytes_tl_bulk_allocated); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 305 | } else { |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 306 | DCHECK(!is_running_on_memory_tool_); |
Mathieu Chartier | 5ace201 | 2016-11-30 10:15:41 -0800 | [diff] [blame] | 307 | ret = dlmalloc_space_->AllocNonvirtual(self, |
| 308 | alloc_size, |
| 309 | bytes_allocated, |
| 310 | usable_size, |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 311 | bytes_tl_bulk_allocated); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 312 | } |
| 313 | break; |
| 314 | } |
| 315 | case kAllocatorTypeNonMoving: { |
Mathieu Chartier | 5ace201 | 2016-11-30 10:15:41 -0800 | [diff] [blame] | 316 | ret = non_moving_space_->Alloc(self, |
| 317 | alloc_size, |
| 318 | bytes_allocated, |
| 319 | usable_size, |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 320 | bytes_tl_bulk_allocated); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 321 | break; |
| 322 | } |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 323 | case kAllocatorTypeLOS: { |
Mathieu Chartier | 5ace201 | 2016-11-30 10:15:41 -0800 | [diff] [blame] | 324 | ret = large_object_space_->Alloc(self, |
| 325 | alloc_size, |
| 326 | bytes_allocated, |
| 327 | usable_size, |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 328 | bytes_tl_bulk_allocated); |
Hiroshi Yamauchi | 95a659f | 2013-11-22 14:43:45 -0800 | [diff] [blame] | 329 | // Note that the bump pointer spaces aren't necessarily next to |
| 330 | // the other continuous spaces like the non-moving alloc space or |
| 331 | // the zygote space. |
| 332 | DCHECK(ret == nullptr || large_object_space_->Contains(ret)); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 333 | break; |
| 334 | } |
Nicolas Geoffray | 96172e0 | 2016-11-30 11:52:19 +0000 | [diff] [blame] | 335 | case kAllocatorTypeRegion: { |
| 336 | DCHECK(region_space_ != nullptr); |
| 337 | alloc_size = RoundUp(alloc_size, space::RegionSpace::kAlignment); |
Mathieu Chartier | 5ace201 | 2016-11-30 10:15:41 -0800 | [diff] [blame] | 338 | ret = region_space_->AllocNonvirtual<false>(alloc_size, |
| 339 | bytes_allocated, |
| 340 | usable_size, |
Nicolas Geoffray | 96172e0 | 2016-11-30 11:52:19 +0000 | [diff] [blame] | 341 | bytes_tl_bulk_allocated); |
| 342 | break; |
| 343 | } |
Mathieu Chartier | 5ace201 | 2016-11-30 10:15:41 -0800 | [diff] [blame] | 344 | case kAllocatorTypeTLAB: |
| 345 | FALLTHROUGH_INTENDED; |
Nicolas Geoffray | 96172e0 | 2016-11-30 11:52:19 +0000 | [diff] [blame] | 346 | case kAllocatorTypeRegionTLAB: { |
Mathieu Chartier | 5ace201 | 2016-11-30 10:15:41 -0800 | [diff] [blame] | 347 | DCHECK_ALIGNED(alloc_size, kObjectAlignment); |
| 348 | static_assert(space::RegionSpace::kAlignment == space::BumpPointerSpace::kAlignment, |
| 349 | "mismatched alignments"); |
| 350 | static_assert(kObjectAlignment == space::BumpPointerSpace::kAlignment, |
| 351 | "mismatched alignments"); |
Nicolas Geoffray | 96172e0 | 2016-11-30 11:52:19 +0000 | [diff] [blame] | 352 | if (UNLIKELY(self->TlabSize() < alloc_size)) { |
Mathieu Chartier | 5ace201 | 2016-11-30 10:15:41 -0800 | [diff] [blame] | 353 | // kAllocatorTypeTLAB may be the allocator for region space TLAB if the GC is not marking, |
| 354 | // that is why the allocator is not passed down. |
| 355 | return AllocWithNewTLAB(self, |
| 356 | alloc_size, |
| 357 | kGrow, |
| 358 | bytes_allocated, |
| 359 | usable_size, |
| 360 | bytes_tl_bulk_allocated); |
Nicolas Geoffray | 96172e0 | 2016-11-30 11:52:19 +0000 | [diff] [blame] | 361 | } |
| 362 | // The allocation can't fail. |
| 363 | ret = self->AllocTlab(alloc_size); |
| 364 | DCHECK(ret != nullptr); |
| 365 | *bytes_allocated = alloc_size; |
Mathieu Chartier | 5ace201 | 2016-11-30 10:15:41 -0800 | [diff] [blame] | 366 | *bytes_tl_bulk_allocated = 0; // Allocated in an existing buffer. |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 367 | *usable_size = alloc_size; |
| 368 | break; |
| 369 | } |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 370 | default: { |
| 371 | LOG(FATAL) << "Invalid allocator type"; |
| 372 | ret = nullptr; |
| 373 | } |
| 374 | } |
| 375 | return ret; |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 378 | inline bool Heap::ShouldAllocLargeObject(ObjPtr<mirror::Class> c, size_t byte_count) const { |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 379 | // We need to have a zygote space or else our newly allocated large object can end up in the |
| 380 | // Zygote resulting in it being prematurely freed. |
| 381 | // We can only do this for primitive objects since large objects will not be within the card table |
| 382 | // range. This also means that we rely on SetClass not dirtying the object's card. |
Jeff Hao | 13e0091 | 2015-06-22 15:14:49 -0700 | [diff] [blame] | 383 | return byte_count >= large_object_threshold_ && (c->IsPrimitiveArray() || c->IsStringClass()); |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Mathieu Chartier | 5ace201 | 2016-11-30 10:15:41 -0800 | [diff] [blame] | 386 | inline bool Heap::IsOutOfMemoryOnAllocation(AllocatorType allocator_type, |
| 387 | size_t alloc_size, |
| 388 | bool grow) { |
Ian Rogers | 3e5cf30 | 2014-05-20 16:40:37 -0700 | [diff] [blame] | 389 | size_t new_footprint = num_bytes_allocated_.LoadSequentiallyConsistent() + alloc_size; |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 390 | if (UNLIKELY(new_footprint > max_allowed_footprint_)) { |
| 391 | if (UNLIKELY(new_footprint > growth_limit_)) { |
| 392 | return true; |
| 393 | } |
Hiroshi Yamauchi | 3e41780 | 2014-03-20 12:03:02 -0700 | [diff] [blame] | 394 | if (!AllocatorMayHaveConcurrentGC(allocator_type) || !IsGcConcurrent()) { |
Mathieu Chartier | 5ace201 | 2016-11-30 10:15:41 -0800 | [diff] [blame] | 395 | if (!grow) { |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 396 | return true; |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 397 | } |
Mathieu Chartier | 7bf82af | 2013-12-06 16:51:45 -0800 | [diff] [blame] | 398 | // TODO: Grow for allocation is racy, fix it. |
| 399 | VLOG(heap) << "Growing heap from " << PrettySize(max_allowed_footprint_) << " to " |
| 400 | << PrettySize(new_footprint) << " for a " << PrettySize(alloc_size) << " allocation"; |
| 401 | max_allowed_footprint_ = new_footprint; |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | return false; |
| 405 | } |
| 406 | |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 407 | inline void Heap::CheckConcurrentGC(Thread* self, |
| 408 | size_t new_num_bytes_allocated, |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 409 | ObjPtr<mirror::Object>* obj) { |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 410 | if (UNLIKELY(new_num_bytes_allocated >= concurrent_start_bytes_)) { |
Hiroshi Yamauchi | 0ae9899 | 2015-05-01 14:33:19 -0700 | [diff] [blame] | 411 | RequestConcurrentGCAndSaveObject(self, false, obj); |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 412 | } |
| 413 | } |
| 414 | |
Mathieu Chartier | a058fdf | 2016-10-06 15:13:58 -0700 | [diff] [blame] | 415 | inline void Heap::WriteBarrierField(ObjPtr<mirror::Object> dst, |
| 416 | MemberOffset offset ATTRIBUTE_UNUSED, |
| 417 | ObjPtr<mirror::Object> new_value ATTRIBUTE_UNUSED) { |
| 418 | card_table_->MarkCard(dst.Ptr()); |
| 419 | } |
| 420 | |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 421 | inline void Heap::WriteBarrierArray(ObjPtr<mirror::Object> dst, |
| 422 | int start_offset ATTRIBUTE_UNUSED, |
| 423 | size_t length ATTRIBUTE_UNUSED) { |
| 424 | card_table_->MarkCard(dst.Ptr()); |
| 425 | } |
| 426 | |
| 427 | inline void Heap::WriteBarrierEveryFieldOf(ObjPtr<mirror::Object> obj) { |
| 428 | card_table_->MarkCard(obj.Ptr()); |
| 429 | } |
| 430 | |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 431 | } // namespace gc |
| 432 | } // namespace art |
| 433 | |
| 434 | #endif // ART_RUNTIME_GC_HEAP_INL_H_ |