blob: 873eadc46ae2d00731f52d933bc39599f00c0448 [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"
23#include "gc/space/dlmalloc_space-inl.h"
24#include "gc/space/large_object_space.h"
25#include "object_utils.h"
26#include "runtime.h"
27#include "thread.h"
28#include "thread-inl.h"
29
30namespace art {
31namespace gc {
32
33inline mirror::Object* Heap::AllocObjectUninstrumented(Thread* self, mirror::Class* c, size_t byte_count) {
34 DebugCheckPreconditionsForAllobObject(c, byte_count);
35 mirror::Object* obj;
36 size_t bytes_allocated;
37 AllocationTimer alloc_timer(this, &obj);
38 bool large_object_allocation = TryAllocLargeObjectUninstrumented(self, c, byte_count,
39 &obj, &bytes_allocated);
40 if (LIKELY(!large_object_allocation)) {
41 // Non-large object allocation.
42 obj = AllocateUninstrumented(self, alloc_space_, byte_count, &bytes_allocated);
43 // Ensure that we did not allocate into a zygote space.
44 DCHECK(obj == NULL || !have_zygote_space_ || !FindSpaceFromObject(obj, false)->IsZygoteSpace());
45 }
46 if (LIKELY(obj != NULL)) {
47 obj->SetClass(c);
48 // Record allocation after since we want to use the atomic add for the atomic fence to guard
49 // the SetClass since we do not want the class to appear NULL in another thread.
50 size_t new_num_bytes_allocated = RecordAllocationUninstrumented(bytes_allocated, obj);
51 DCHECK(!Dbg::IsAllocTrackingEnabled());
52 CheckConcurrentGC(self, new_num_bytes_allocated, obj);
53 if (kDesiredHeapVerification > kNoHeapVerification) {
54 VerifyObject(obj);
55 }
56 return obj;
57 }
58 ThrowOutOfMemoryError(self, byte_count, large_object_allocation);
59 return NULL;
60}
61
62inline size_t Heap::RecordAllocationUninstrumented(size_t size, mirror::Object* obj) {
63 DCHECK(obj != NULL);
64 DCHECK_GT(size, 0u);
65 size_t old_num_bytes_allocated = static_cast<size_t>(num_bytes_allocated_.fetch_add(size));
66
67 DCHECK(!Runtime::Current()->HasStatsEnabled());
68
69 // This is safe to do since the GC will never free objects which are neither in the allocation
70 // stack or the live bitmap.
71 while (!allocation_stack_->AtomicPushBack(obj)) {
72 CollectGarbageInternal(collector::kGcTypeSticky, kGcCauseForAlloc, false);
73 }
74
75 return old_num_bytes_allocated + size;
76}
77
78inline mirror::Object* Heap::TryToAllocateUninstrumented(Thread* self, space::AllocSpace* space, size_t alloc_size,
79 bool grow, size_t* bytes_allocated) {
80 if (UNLIKELY(IsOutOfMemoryOnAllocation(alloc_size, grow))) {
81 return NULL;
82 }
83 DCHECK(!running_on_valgrind_);
84 return space->Alloc(self, alloc_size, bytes_allocated);
85}
86
87// DlMallocSpace-specific version.
88inline mirror::Object* Heap::TryToAllocateUninstrumented(Thread* self, space::DlMallocSpace* space, size_t alloc_size,
89 bool grow, size_t* bytes_allocated) {
90 if (UNLIKELY(IsOutOfMemoryOnAllocation(alloc_size, grow))) {
91 return NULL;
92 }
93 DCHECK(!running_on_valgrind_);
94 return space->AllocNonvirtual(self, alloc_size, bytes_allocated);
95}
96
97template <class T>
98inline mirror::Object* Heap::AllocateUninstrumented(Thread* self, T* space, size_t alloc_size,
99 size_t* bytes_allocated) {
100 // Since allocation can cause a GC which will need to SuspendAll, make sure all allocations are
101 // done in the runnable state where suspension is expected.
102 DCHECK_EQ(self->GetState(), kRunnable);
103 self->AssertThreadSuspensionIsAllowable();
104
105 mirror::Object* ptr = TryToAllocateUninstrumented(self, space, alloc_size, false, bytes_allocated);
106 if (LIKELY(ptr != NULL)) {
107 return ptr;
108 }
109 return AllocateInternalWithGc(self, space, alloc_size, bytes_allocated);
110}
111
112inline bool Heap::TryAllocLargeObjectUninstrumented(Thread* self, mirror::Class* c, size_t byte_count,
113 mirror::Object** obj_ptr, size_t* bytes_allocated) {
114 bool large_object_allocation = ShouldAllocLargeObject(c, byte_count);
115 if (UNLIKELY(large_object_allocation)) {
116 mirror::Object* obj = AllocateUninstrumented(self, large_object_space_, byte_count, bytes_allocated);
117 // Make sure that our large object didn't get placed anywhere within the space interval or else
118 // it breaks the immune range.
119 DCHECK(obj == NULL ||
120 reinterpret_cast<byte*>(obj) < continuous_spaces_.front()->Begin() ||
121 reinterpret_cast<byte*>(obj) >= continuous_spaces_.back()->End());
122 *obj_ptr = obj;
123 }
124 return large_object_allocation;
125}
126
127inline void Heap::DebugCheckPreconditionsForAllobObject(mirror::Class* c, size_t byte_count) {
128 DCHECK(c == NULL || (c->IsClassClass() && byte_count >= sizeof(mirror::Class)) ||
129 (c->IsVariableSize() || c->GetObjectSize() == byte_count) ||
Ian Rogersdfb325e2013-10-30 01:00:44 -0700130 strlen(ClassHelper(c).GetDescriptor()) == 0);
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700131 DCHECK_GE(byte_count, sizeof(mirror::Object));
132}
133
134inline Heap::AllocationTimer::AllocationTimer(Heap* heap, mirror::Object** allocated_obj_ptr)
135 : heap_(heap), allocated_obj_ptr_(allocated_obj_ptr) {
136 if (kMeasureAllocationTime) {
137 allocation_start_time_ = NanoTime() / kTimeAdjust;
138 }
139}
140
141inline Heap::AllocationTimer::~AllocationTimer() {
142 if (kMeasureAllocationTime) {
143 mirror::Object* allocated_obj = *allocated_obj_ptr_;
144 // Only if the allocation succeeded, record the time.
145 if (allocated_obj != NULL) {
146 uint64_t allocation_end_time = NanoTime() / kTimeAdjust;
147 heap_->total_allocation_time_.fetch_add(allocation_end_time - allocation_start_time_);
148 }
149 }
150};
151
152inline bool Heap::ShouldAllocLargeObject(mirror::Class* c, size_t byte_count) {
153 // We need to have a zygote space or else our newly allocated large object can end up in the
154 // Zygote resulting in it being prematurely freed.
155 // We can only do this for primitive objects since large objects will not be within the card table
156 // range. This also means that we rely on SetClass not dirtying the object's card.
157 return byte_count >= kLargeObjectThreshold && have_zygote_space_ && c->IsPrimitiveArray();
158}
159
160inline bool Heap::IsOutOfMemoryOnAllocation(size_t alloc_size, bool grow) {
161 size_t new_footprint = num_bytes_allocated_ + alloc_size;
162 if (UNLIKELY(new_footprint > max_allowed_footprint_)) {
163 if (UNLIKELY(new_footprint > growth_limit_)) {
164 return true;
165 }
166 if (!concurrent_gc_) {
167 if (!grow) {
168 return true;
169 } else {
170 max_allowed_footprint_ = new_footprint;
171 }
172 }
173 }
174 return false;
175}
176
177inline void Heap::CheckConcurrentGC(Thread* self, size_t new_num_bytes_allocated, mirror::Object* obj) {
178 if (UNLIKELY(new_num_bytes_allocated >= concurrent_start_bytes_)) {
179 // The SirtRef is necessary since the calls in RequestConcurrentGC are a safepoint.
180 SirtRef<mirror::Object> ref(self, obj);
181 RequestConcurrentGC(self);
182 }
183}
184
185} // namespace gc
186} // namespace art
187
188#endif // ART_RUNTIME_GC_HEAP_INL_H_