blob: 952bf85045c9c3e68d4aaad9c7bc1d3ac5e45afd [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 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 */
Carl Shapiro69759ea2011-07-21 18:13:35 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "mark_sweep.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070018
Carl Shapiro58551df2011-07-24 03:09:51 -070019#include <climits>
20#include <vector>
21
Elliott Hughes410c0c82011-09-01 17:58:25 -070022#include "class_loader.h"
Brian Carlstrom693267a2011-09-06 09:25:34 -070023#include "dex_cache.h"
Carl Shapiro58551df2011-07-24 03:09:51 -070024#include "heap.h"
Elliott Hughes410c0c82011-09-01 17:58:25 -070025#include "indirect_reference_table.h"
26#include "intern_table.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070027#include "logging.h"
28#include "macros.h"
29#include "mark_stack.h"
Elliott Hughesc33a32b2011-10-11 18:18:07 -070030#include "monitor.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070031#include "object.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070032#include "runtime.h"
Carl Shapiro58551df2011-07-24 03:09:51 -070033#include "space.h"
Elliott Hughes307f75d2011-10-12 18:04:40 -070034#include "timing_logger.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070035#include "thread.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070036
Carl Shapiro69759ea2011-07-21 18:13:35 -070037namespace art {
38
Jesse Wilson078f9b02011-11-18 17:51:47 -050039void MarkSweep::Init() {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070040 mark_stack_ = MarkStack::Create();
Carl Shapiro58551df2011-07-24 03:09:51 -070041 mark_bitmap_ = Heap::GetMarkBits();
42 live_bitmap_ = Heap::GetLiveBits();
43
44 // TODO: if concurrent, clear the card table.
45
buzbee0d966cf2011-09-08 17:34:58 -070046 // TODO: if concurrent, enable card marking in compiler
47
Carl Shapiro58551df2011-07-24 03:09:51 -070048 // TODO: check that the mark bitmap is entirely clear.
Carl Shapiro58551df2011-07-24 03:09:51 -070049}
50
Elliott Hughesb0663112011-10-19 18:16:37 -070051inline void MarkSweep::MarkObject0(const Object* obj, bool check_finger) {
Carl Shapiro69759ea2011-07-21 18:13:35 -070052 DCHECK(obj != NULL);
53 if (obj < condemned_) {
54 DCHECK(IsMarked(obj));
55 return;
56 }
57 bool is_marked = mark_bitmap_->Test(obj);
58 // This object was not previously marked.
59 if (!is_marked) {
60 mark_bitmap_->Set(obj);
61 if (check_finger && obj < finger_) {
62 // The object must be pushed on to the mark stack.
63 mark_stack_->Push(obj);
64 }
65 }
66}
67
68// Used to mark objects when recursing. Recursion is done by moving
69// the finger across the bitmaps in address order and marking child
70// objects. Any newly-marked objects whose addresses are lower than
71// the finger won't be visited by the bitmap scan, so those objects
72// need to be added to the mark stack.
Elliott Hughesb0663112011-10-19 18:16:37 -070073inline void MarkSweep::MarkObject(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -070074 if (obj != NULL) {
75 MarkObject0(obj, true);
76 }
77}
78
Elliott Hughescf4c6c42011-09-01 15:16:42 -070079void MarkSweep::MarkObjectVisitor(const Object* root, void* arg) {
Brian Carlstrom1f870082011-08-23 16:02:11 -070080 DCHECK(root != NULL);
81 DCHECK(arg != NULL);
82 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
Ian Rogers5d76c432011-10-31 21:42:49 -070083 DCHECK(mark_sweep->finger_ == NULL); // no point to check finger if it is NULL
84 mark_sweep->MarkObject0(root, false);
Brian Carlstrom1f870082011-08-23 16:02:11 -070085}
86
Carl Shapiro69759ea2011-07-21 18:13:35 -070087// Marks all objects in the root set.
88void MarkSweep::MarkRoots() {
Brian Carlstrom1f870082011-08-23 16:02:11 -070089 Runtime::Current()->VisitRoots(MarkObjectVisitor, this);
Carl Shapiro69759ea2011-07-21 18:13:35 -070090}
91
Ian Rogers5d76c432011-10-31 21:42:49 -070092void MarkSweep::ScanImageRootVisitor(Object* root, void* arg) {
93 DCHECK(root != NULL);
94 DCHECK(arg != NULL);
95 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
96 DCHECK(mark_sweep->finger_ == NULL); // no point to check finger if it is NULL
97 mark_sweep->MarkObject0(root, false);
98 mark_sweep->ScanObject(root);
99}
100
101// Marks all objects that are in images and have been touched by the mutator
102void MarkSweep::ScanDirtyImageRoots() {
103 const std::vector<Space*>& spaces = Heap::GetSpaces();
104 CardTable* card_table = Heap::GetCardTable();
105 for (size_t i = 0; i < spaces.size(); ++i) {
106 if (spaces[i]->IsImageSpace()) {
Ian Rogers30fab402012-01-23 15:43:46 -0800107 byte* begin = spaces[i]->Begin();
108 byte* end = spaces[i]->End();
109 card_table->Scan(begin, end, ScanImageRootVisitor, this);
Ian Rogers5d76c432011-10-31 21:42:49 -0700110 }
111 }
112}
113
114void MarkSweep::CheckBitmapCallback(Object* obj, void* finger, void* arg) {
115 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
116 mark_sweep->finger_ = reinterpret_cast<Object*>(finger);
117 mark_sweep->CheckObject(obj);
118}
119
Carl Shapiro58551df2011-07-24 03:09:51 -0700120void MarkSweep::ScanBitmapCallback(Object* obj, void* finger, void* arg) {
121 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
122 mark_sweep->finger_ = reinterpret_cast<Object*>(finger);
123 mark_sweep->ScanObject(obj);
124}
125
126// Populates the mark stack based on the set of marked objects and
127// recursively marks until the mark stack is emptied.
128void MarkSweep::RecursiveMark() {
Brian Carlstrom1f870082011-08-23 16:02:11 -0700129 // RecursiveMark will build the lists of known instances of the Reference classes.
130 // See DelayReferenceReferent for details.
131 CHECK(soft_reference_list_ == NULL);
132 CHECK(weak_reference_list_ == NULL);
133 CHECK(finalizer_reference_list_ == NULL);
134 CHECK(phantom_reference_list_ == NULL);
135 CHECK(cleared_reference_list_ == NULL);
136
Carl Shapiro58551df2011-07-24 03:09:51 -0700137 void* arg = reinterpret_cast<void*>(this);
138 const std::vector<Space*>& spaces = Heap::GetSpaces();
139 for (size_t i = 0; i < spaces.size(); ++i) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700140#ifndef NDEBUG
Ian Rogers30fab402012-01-23 15:43:46 -0800141 uintptr_t begin = reinterpret_cast<uintptr_t>(spaces[i]->Begin());
142 uintptr_t end = reinterpret_cast<uintptr_t>(spaces[i]->End());
Ian Rogers5d76c432011-10-31 21:42:49 -0700143 if (!spaces[i]->IsImageSpace()) {
Ian Rogers30fab402012-01-23 15:43:46 -0800144 mark_bitmap_->ScanWalk(begin, end, &MarkSweep::ScanBitmapCallback, arg);
Ian Rogers5d76c432011-10-31 21:42:49 -0700145 } else{
Ian Rogers30fab402012-01-23 15:43:46 -0800146 mark_bitmap_->ScanWalk(begin, end, &MarkSweep::CheckBitmapCallback, arg);
Ian Rogers5d76c432011-10-31 21:42:49 -0700147 }
148#else
Elliott Hughes307f75d2011-10-12 18:04:40 -0700149 if (!spaces[i]->IsImageSpace()) {
Ian Rogers30fab402012-01-23 15:43:46 -0800150 uintptr_t begin = reinterpret_cast<uintptr_t>(spaces[i]->Begin());
151 uintptr_t end = reinterpret_cast<uintptr_t>(spaces[i]->End());
152 mark_bitmap_->ScanWalk(begin, end, &MarkSweep::ScanBitmapCallback, arg);
Carl Shapiro58551df2011-07-24 03:09:51 -0700153 }
Ian Rogers5d76c432011-10-31 21:42:49 -0700154#endif
Carl Shapiro58551df2011-07-24 03:09:51 -0700155 }
156 finger_ = reinterpret_cast<Object*>(~0);
Ian Rogers5d76c432011-10-31 21:42:49 -0700157 // TODO: tune the frequency of emptying the mark stack
Carl Shapiro58551df2011-07-24 03:09:51 -0700158 ProcessMarkStack();
159}
160
161void MarkSweep::ReMarkRoots() {
Elliott Hughes53b61312011-08-12 18:28:20 -0700162 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700163}
164
Elliott Hughes410c0c82011-09-01 17:58:25 -0700165void MarkSweep::SweepJniWeakGlobals() {
166 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
167 MutexLock mu(vm->weak_globals_lock);
168 IndirectReferenceTable* table = &vm->weak_globals;
169 typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto
170 for (It it = table->begin(), end = table->end(); it != end; ++it) {
171 const Object** entry = *it;
172 if (!IsMarked(*entry)) {
173 *entry = kClearedJniWeakGlobal;
174 }
175 }
176}
177
Elliott Hughes410c0c82011-09-01 17:58:25 -0700178void MarkSweep::SweepSystemWeaks() {
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700179 Runtime::Current()->GetInternTable()->SweepInternTableWeaks(IsMarked, this);
180 Runtime::Current()->GetMonitorList()->SweepMonitorList(IsMarked, this);
Elliott Hughes410c0c82011-09-01 17:58:25 -0700181 SweepJniWeakGlobals();
Carl Shapiro58551df2011-07-24 03:09:51 -0700182}
183
Ian Rogers30fab402012-01-23 15:43:46 -0800184void MarkSweep::SweepCallback(size_t num_ptrs, Object** ptrs, void* arg) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700185 // TODO: lock heap if concurrent
Elliott Hughes307f75d2011-10-12 18:04:40 -0700186 size_t freed_objects = num_ptrs;
187 size_t freed_bytes = 0;
Ian Rogers30fab402012-01-23 15:43:46 -0800188 AllocSpace* space = static_cast<AllocSpace*>(arg);
Ian Rogers5d76c432011-10-31 21:42:49 -0700189 // Use a bulk free, that merges consecutive objects before freeing or free per object?
190 // Documentation suggests better free performance with merging, but this may be at the expensive
191 // of allocation.
192 // TODO: investigate performance
Ian Rogers30fab402012-01-23 15:43:46 -0800193 static const bool kUseFreeList = true;
194 if (kUseFreeList) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700195 for (size_t i = 0; i < num_ptrs; ++i) {
196 Object* obj = static_cast<Object*>(ptrs[i]);
Ian Rogers30fab402012-01-23 15:43:46 -0800197 freed_bytes += space->AllocationSize(obj);
Ian Rogers5d76c432011-10-31 21:42:49 -0700198 Heap::GetLiveBits()->Clear(obj);
199 }
Ian Rogers30fab402012-01-23 15:43:46 -0800200 // AllocSpace::FreeList clears the value in ptrs, so perform after clearing the live bit
201 space->FreeList(num_ptrs, ptrs);
Ian Rogers5d76c432011-10-31 21:42:49 -0700202 } else {
203 for (size_t i = 0; i < num_ptrs; ++i) {
204 Object* obj = static_cast<Object*>(ptrs[i]);
Ian Rogers30fab402012-01-23 15:43:46 -0800205 freed_bytes += space->AllocationSize(obj);
Ian Rogers5d76c432011-10-31 21:42:49 -0700206 Heap::GetLiveBits()->Clear(obj);
Ian Rogers30fab402012-01-23 15:43:46 -0800207 space->Free(obj);
Ian Rogers5d76c432011-10-31 21:42:49 -0700208 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700209 }
Elliott Hughes307f75d2011-10-12 18:04:40 -0700210 Heap::RecordFreeLocked(freed_objects, freed_bytes);
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700211 // TODO: unlock heap if concurrent
Carl Shapiro58551df2011-07-24 03:09:51 -0700212}
213
214void MarkSweep::Sweep() {
Elliott Hughes2da50362011-10-10 16:57:08 -0700215 SweepSystemWeaks();
216
Carl Shapiro58551df2011-07-24 03:09:51 -0700217 const std::vector<Space*>& spaces = Heap::GetSpaces();
218 for (size_t i = 0; i < spaces.size(); ++i) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700219 if (!spaces[i]->IsImageSpace()) {
Ian Rogers30fab402012-01-23 15:43:46 -0800220 uintptr_t begin = reinterpret_cast<uintptr_t>(spaces[i]->Begin());
221 uintptr_t end = reinterpret_cast<uintptr_t>(spaces[i]->End());
Carl Shapiro58551df2011-07-24 03:09:51 -0700222 void* arg = static_cast<void*>(spaces[i]);
Ian Rogers30fab402012-01-23 15:43:46 -0800223 HeapBitmap::SweepWalk(*live_bitmap_, *mark_bitmap_, begin, end,
Carl Shapiro58551df2011-07-24 03:09:51 -0700224 &MarkSweep::SweepCallback, arg);
225 }
226 }
227}
228
Carl Shapiro69759ea2011-07-21 18:13:35 -0700229// Scans instance fields.
Elliott Hughesb0663112011-10-19 18:16:37 -0700230inline void MarkSweep::ScanInstanceFields(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700231 DCHECK(obj != NULL);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700232 Class* klass = obj->GetClass();
233 DCHECK(klass != NULL);
Elliott Hughes2da50362011-10-10 16:57:08 -0700234 ScanFields(obj, klass->GetReferenceInstanceOffsets(), false);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700235}
236
Ian Rogers5d76c432011-10-31 21:42:49 -0700237inline void MarkSweep::CheckInstanceFields(const Object* obj) {
238 Class* klass = obj->GetClass();
239 CheckFields(obj, klass->GetReferenceInstanceOffsets(), false);
240}
241
Brian Carlstrom4873d462011-08-21 15:23:39 -0700242// Scans static storage on a Class.
Elliott Hughesb0663112011-10-19 18:16:37 -0700243inline void MarkSweep::ScanStaticFields(const Class* klass) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700244 DCHECK(klass != NULL);
Elliott Hughesadb460d2011-10-05 17:02:34 -0700245 ScanFields(klass, klass->GetReferenceStaticOffsets(), true);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700246}
247
Ian Rogers5d76c432011-10-31 21:42:49 -0700248inline void MarkSweep::CheckStaticFields(const Class* klass) {
249 CheckFields(klass, klass->GetReferenceStaticOffsets(), true);
250}
251
Elliott Hughesb0663112011-10-19 18:16:37 -0700252inline void MarkSweep::ScanFields(const Object* obj, uint32_t ref_offsets, bool is_static) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700253 if (ref_offsets != CLASS_WALK_SUPER) {
254 // Found a reference offset bitmap. Mark the specified offsets.
255 while (ref_offsets != 0) {
256 size_t right_shift = CLZ(ref_offsets);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700257 MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
258 const Object* ref = obj->GetFieldObject<const Object*>(byte_offset, false);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700259 MarkObject(ref);
260 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
261 }
262 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700263 // There is no reference offset bitmap. In the non-static case,
264 // walk up the class inheritance hierarchy and find reference
265 // offsets the hard way. In the static case, just consider this
266 // class.
267 for (const Class* klass = is_static ? obj->AsClass() : obj->GetClass();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700268 klass != NULL;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700269 klass = is_static ? NULL : klass->GetSuperClass()) {
270 size_t num_reference_fields = (is_static
271 ? klass->NumReferenceStaticFields()
272 : klass->NumReferenceInstanceFields());
273 for (size_t i = 0; i < num_reference_fields; ++i) {
274 Field* field = (is_static
275 ? klass->GetStaticField(i)
276 : klass->GetInstanceField(i));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700277 MemberOffset field_offset = field->GetOffset();
278 const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700279 MarkObject(ref);
280 }
281 }
282 }
283}
284
Ian Rogers5d76c432011-10-31 21:42:49 -0700285inline void MarkSweep::CheckReference(const Object* obj, const Object* ref, MemberOffset offset, bool is_static) {
Ian Rogers30fab402012-01-23 15:43:46 -0800286 AllocSpace* alloc_space = Heap::GetAllocSpace();
Ian Rogers5d76c432011-10-31 21:42:49 -0700287 if (alloc_space->Contains(ref)) {
288 bool is_marked = mark_bitmap_->Test(ref);
289 if(!is_marked) {
Ian Rogers30fab402012-01-23 15:43:46 -0800290 LOG(INFO) << *alloc_space;
291 LOG(WARNING) << (is_static ? "Static ref'" : "Instance ref'") << PrettyTypeOf(ref)
292 << "' (" << (void*)ref << ") in '" << PrettyTypeOf(obj)
293 << "' (" << (void*)obj << ") at offset "
294 << (void*)offset.Int32Value() << " wasn't marked";
Ian Rogers5d76c432011-10-31 21:42:49 -0700295 bool obj_marked = Heap::GetCardTable()->IsDirty(obj);
296 if (!obj_marked) {
297 LOG(WARNING) << "Object '" << PrettyTypeOf(obj) << "' (" << (void*)obj
298 << ") contains references to the alloc space, but wasn't card marked";
299 }
300 }
301 }
302}
303
304inline void MarkSweep::CheckFields(const Object* obj, uint32_t ref_offsets, bool is_static) {
305 if (ref_offsets != CLASS_WALK_SUPER) {
306 // Found a reference offset bitmap. Mark the specified offsets.
307 while (ref_offsets != 0) {
308 size_t right_shift = CLZ(ref_offsets);
309 MemberOffset field_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
310 const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false);
311 CheckReference(obj, ref, field_offset, is_static);
312 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
313 }
314 } else {
315 // There is no reference offset bitmap. In the non-static case,
316 // walk up the class inheritance hierarchy and find reference
317 // offsets the hard way. In the static case, just consider this
318 // class.
319 for (const Class* klass = is_static ? obj->AsClass() : obj->GetClass();
320 klass != NULL;
321 klass = is_static ? NULL : klass->GetSuperClass()) {
322 size_t num_reference_fields = (is_static
323 ? klass->NumReferenceStaticFields()
324 : klass->NumReferenceInstanceFields());
325 for (size_t i = 0; i < num_reference_fields; ++i) {
326 Field* field = (is_static
327 ? klass->GetStaticField(i)
328 : klass->GetInstanceField(i));
329 MemberOffset field_offset = field->GetOffset();
330 const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false);
331 CheckReference(obj, ref, field_offset, is_static);
332 }
333 }
334 }
335}
336
Carl Shapiro69759ea2011-07-21 18:13:35 -0700337// Scans the header, static field references, and interface pointers
338// of a class object.
Elliott Hughesb0663112011-10-19 18:16:37 -0700339inline void MarkSweep::ScanClass(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700340#ifndef NDEBUG
341 ++class_count_;
342#endif
Brian Carlstrom693267a2011-09-06 09:25:34 -0700343 ScanInstanceFields(obj);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700344 ScanStaticFields(obj->AsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700345}
346
Ian Rogers5d76c432011-10-31 21:42:49 -0700347inline void MarkSweep::CheckClass(const Object* obj) {
348 CheckInstanceFields(obj);
349 CheckStaticFields(obj->AsClass());
350}
351
Carl Shapiro69759ea2011-07-21 18:13:35 -0700352// Scans the header of all array objects. If the array object is
353// specialized to a reference type, scans the array data as well.
Elliott Hughesb0663112011-10-19 18:16:37 -0700354inline void MarkSweep::ScanArray(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700355#ifndef NDEBUG
356 ++array_count_;
357#endif
Carl Shapiro69759ea2011-07-21 18:13:35 -0700358 MarkObject(obj->GetClass());
359 if (obj->IsObjectArray()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700360 const ObjectArray<Object>* array = obj->AsObjectArray<Object>();
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700361 for (int32_t i = 0; i < array->GetLength(); ++i) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700362 const Object* element = array->GetWithoutChecks(i);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700363 MarkObject(element);
364 }
365 }
366}
367
Ian Rogers5d76c432011-10-31 21:42:49 -0700368inline void MarkSweep::CheckArray(const Object* obj) {
369 CheckReference(obj, obj->GetClass(), Object::ClassOffset(), false);
370 if (obj->IsObjectArray()) {
371 const ObjectArray<Object>* array = obj->AsObjectArray<Object>();
372 for (int32_t i = 0; i < array->GetLength(); ++i) {
373 const Object* element = array->GetWithoutChecks(i);
Ian Rogersa15e67d2012-02-28 13:51:55 -0800374 size_t width = sizeof(Object*);
375 CheckReference(obj, element, MemberOffset(i * width +
376 Array::DataOffset(width).Int32Value()), false);
Ian Rogers5d76c432011-10-31 21:42:49 -0700377 }
378 }
379}
380
Carl Shapiro69759ea2011-07-21 18:13:35 -0700381// Process the "referent" field in a java.lang.ref.Reference. If the
382// referent has not yet been marked, put it on the appropriate list in
383// the gcHeap for later processing.
384void MarkSweep::DelayReferenceReferent(Object* obj) {
385 DCHECK(obj != NULL);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700386 Class* klass = obj->GetClass();
387 DCHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700388 DCHECK(klass->IsReferenceClass());
389 Object* pending = obj->GetFieldObject<Object*>(Heap::GetReferencePendingNextOffset(), false);
Elliott Hughesadb460d2011-10-05 17:02:34 -0700390 Object* referent = Heap::GetReferenceReferent(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700391 if (pending == NULL && referent != NULL && !IsMarked(referent)) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700392 Object** list = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700393 if (klass->IsSoftReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700394 list = &soft_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700395 } else if (klass->IsWeakReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700396 list = &weak_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700397 } else if (klass->IsFinalizerReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700398 list = &finalizer_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700399 } else if (klass->IsPhantomReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700400 list = &phantom_reference_list_;
401 }
Brian Carlstrom0796af02011-10-12 14:31:45 -0700402 DCHECK(list != NULL) << PrettyClass(klass) << " " << std::hex << klass->GetAccessFlags();
Elliott Hughesadb460d2011-10-05 17:02:34 -0700403 Heap::EnqueuePendingReference(obj, list);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700404 }
405}
406
407// Scans the header and field references of a data object. If the
408// scanned object is a reference subclass, it is scheduled for later
Elliott Hughesadb460d2011-10-05 17:02:34 -0700409// processing.
Elliott Hughesb0663112011-10-19 18:16:37 -0700410inline void MarkSweep::ScanOther(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700411#ifndef NDEBUG
412 ++other_count_;
413#endif
Carl Shapiro69759ea2011-07-21 18:13:35 -0700414 ScanInstanceFields(obj);
Elliott Hughes352a4242011-10-31 15:15:21 -0700415 if (obj->GetClass()->IsReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700416 DelayReferenceReferent(const_cast<Object*>(obj));
417 }
418}
419
Ian Rogers5d76c432011-10-31 21:42:49 -0700420inline void MarkSweep::CheckOther(const Object* obj) {
421 CheckInstanceFields(obj);
422}
423
Carl Shapiro69759ea2011-07-21 18:13:35 -0700424// Scans an object reference. Determines the type of the reference
425// and dispatches to a specialized scanning routine.
Elliott Hughesb0663112011-10-19 18:16:37 -0700426inline void MarkSweep::ScanObject(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700427 DCHECK(obj != NULL);
428 DCHECK(obj->GetClass() != NULL);
429 DCHECK(IsMarked(obj));
430 if (obj->IsClass()) {
431 ScanClass(obj);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700432 } else if (obj->IsArrayInstance()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700433 ScanArray(obj);
434 } else {
Carl Shapiro58551df2011-07-24 03:09:51 -0700435 ScanOther(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700436 }
437}
438
Ian Rogers5d76c432011-10-31 21:42:49 -0700439// Check to see that all alloc space references are marked for the given object
440inline void MarkSweep::CheckObject(const Object* obj) {
441 DCHECK(obj != NULL);
442 DCHECK(obj->GetClass() != NULL);
443 DCHECK(IsMarked(obj));
444 if (obj->IsClass()) {
445 CheckClass(obj);
446 } else if (obj->IsArrayInstance()) {
447 CheckArray(obj);
448 } else {
449 CheckOther(obj);
450 }
451}
452
453// Scan anything that's on the mark stack.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700454void MarkSweep::ProcessMarkStack() {
Ian Rogers5d76c432011-10-31 21:42:49 -0700455 Space* alloc_space = Heap::GetAllocSpace();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700456 while (!mark_stack_->IsEmpty()) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700457 const Object* obj = mark_stack_->Pop();
Ian Rogers5d76c432011-10-31 21:42:49 -0700458 if (alloc_space->Contains(obj)) {
459 ScanObject(obj);
460 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700461 }
462}
463
464void MarkSweep::ScanDirtyObjects() {
465 ProcessMarkStack();
466}
467
Carl Shapiro69759ea2011-07-21 18:13:35 -0700468// Walks the reference list marking any references subject to the
469// reference clearing policy. References with a black referent are
470// removed from the list. References with white referents biased
471// toward saving are blackened and also removed from the list.
472void MarkSweep::PreserveSomeSoftReferences(Object** list) {
473 DCHECK(list != NULL);
474 Object* clear = NULL;
475 size_t counter = 0;
476 while (*list != NULL) {
Elliott Hughesadb460d2011-10-05 17:02:34 -0700477 Object* ref = Heap::DequeuePendingReference(list);
478 Object* referent = Heap::GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700479 if (referent == NULL) {
480 // Referent was cleared by the user during marking.
481 continue;
482 }
483 bool is_marked = IsMarked(referent);
484 if (!is_marked && ((++counter) & 1)) {
485 // Referent is white and biased toward saving, mark it.
486 MarkObject(referent);
487 is_marked = true;
488 }
489 if (!is_marked) {
490 // Referent is white, queue it for clearing.
Elliott Hughesadb460d2011-10-05 17:02:34 -0700491 Heap::EnqueuePendingReference(ref, &clear);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700492 }
493 }
494 *list = clear;
495 // Restart the mark with the newly black references added to the
496 // root set.
497 ProcessMarkStack();
498}
499
500// Unlink the reference list clearing references objects with white
501// referents. Cleared references registered to a reference queue are
502// scheduled for appending by the heap worker thread.
503void MarkSweep::ClearWhiteReferences(Object** list) {
504 DCHECK(list != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700505 while (*list != NULL) {
Elliott Hughesadb460d2011-10-05 17:02:34 -0700506 Object* ref = Heap::DequeuePendingReference(list);
507 Object* referent = Heap::GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700508 if (referent != NULL && !IsMarked(referent)) {
509 // Referent is white, clear it.
Elliott Hughesadb460d2011-10-05 17:02:34 -0700510 Heap::ClearReferenceReferent(ref);
511 if (Heap::IsEnqueuable(ref)) {
512 Heap::EnqueueReference(ref, &cleared_reference_list_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700513 }
514 }
515 }
516 DCHECK(*list == NULL);
517}
518
519// Enqueues finalizer references with white referents. White
520// referents are blackened, moved to the zombie field, and the
521// referent field is cleared.
522void MarkSweep::EnqueueFinalizerReferences(Object** list) {
523 DCHECK(list != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700524 MemberOffset zombie_offset = Heap::GetFinalizerReferenceZombieOffset();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700525 bool has_enqueued = false;
526 while (*list != NULL) {
Elliott Hughesadb460d2011-10-05 17:02:34 -0700527 Object* ref = Heap::DequeuePendingReference(list);
528 Object* referent = Heap::GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700529 if (referent != NULL && !IsMarked(referent)) {
530 MarkObject(referent);
531 // If the referent is non-null the reference must queuable.
Elliott Hughesadb460d2011-10-05 17:02:34 -0700532 DCHECK(Heap::IsEnqueuable(ref));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700533 ref->SetFieldObject(zombie_offset, referent, false);
Elliott Hughesadb460d2011-10-05 17:02:34 -0700534 Heap::ClearReferenceReferent(ref);
535 Heap::EnqueueReference(ref, &cleared_reference_list_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700536 has_enqueued = true;
537 }
538 }
539 if (has_enqueued) {
540 ProcessMarkStack();
541 }
542 DCHECK(*list == NULL);
543}
544
Carl Shapiro58551df2011-07-24 03:09:51 -0700545// Process reference class instances and schedule finalizations.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700546void MarkSweep::ProcessReferences(Object** soft_references, bool clear_soft,
547 Object** weak_references,
548 Object** finalizer_references,
549 Object** phantom_references) {
550 DCHECK(soft_references != NULL);
551 DCHECK(weak_references != NULL);
552 DCHECK(finalizer_references != NULL);
553 DCHECK(phantom_references != NULL);
554
555 // Unless we are in the zygote or required to clear soft references
556 // with white references, preserve some white referents.
557 if (clear_soft) {
558 PreserveSomeSoftReferences(soft_references);
559 }
560
561 // Clear all remaining soft and weak references with white
562 // referents.
563 ClearWhiteReferences(soft_references);
564 ClearWhiteReferences(weak_references);
565
566 // Preserve all white objects with finalize methods and schedule
567 // them for finalization.
568 EnqueueFinalizerReferences(finalizer_references);
569
570 // Clear all f-reachable soft and weak references with white
571 // referents.
572 ClearWhiteReferences(soft_references);
573 ClearWhiteReferences(weak_references);
574
575 // Clear all phantom references with white referents.
576 ClearWhiteReferences(phantom_references);
577
578 // At this point all reference lists should be empty.
579 DCHECK(*soft_references == NULL);
580 DCHECK(*weak_references == NULL);
581 DCHECK(*finalizer_references == NULL);
582 DCHECK(*phantom_references == NULL);
583}
584
Carl Shapiro69759ea2011-07-21 18:13:35 -0700585MarkSweep::~MarkSweep() {
Elliott Hughes352a4242011-10-31 15:15:21 -0700586#ifndef NDEBUG
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800587 VLOG(heap) << "MarkSweep scanned classes=" << class_count_ << " arrays=" << array_count_ << " other=" << other_count_;
Elliott Hughes352a4242011-10-31 15:15:21 -0700588#endif
Carl Shapiro58551df2011-07-24 03:09:51 -0700589 delete mark_stack_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700590 mark_bitmap_->Clear();
591}
592
593} // namespace art