blob: bf4f1bf4b0bcb759262ded77ff2b096347e7044c [file] [log] [blame]
Carl Shapiro69759ea2011-07-21 18:13:35 -07001// Copyright 2011 Google Inc. All Rights Reserved.
Carl Shapiro69759ea2011-07-21 18:13:35 -07002
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "mark_sweep.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -07004
Carl Shapiro58551df2011-07-24 03:09:51 -07005#include <climits>
6#include <vector>
7
Elliott Hughes410c0c82011-09-01 17:58:25 -07008#include "class_loader.h"
Brian Carlstrom693267a2011-09-06 09:25:34 -07009#include "dex_cache.h"
Carl Shapiro58551df2011-07-24 03:09:51 -070010#include "heap.h"
Elliott Hughes410c0c82011-09-01 17:58:25 -070011#include "indirect_reference_table.h"
12#include "intern_table.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070013#include "logging.h"
14#include "macros.h"
15#include "mark_stack.h"
Elliott Hughesc33a32b2011-10-11 18:18:07 -070016#include "monitor.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "object.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070018#include "runtime.h"
Carl Shapiro58551df2011-07-24 03:09:51 -070019#include "space.h"
Elliott Hughes307f75d2011-10-12 18:04:40 -070020#include "timing_logger.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070021#include "thread.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070022
Carl Shapiro69759ea2011-07-21 18:13:35 -070023namespace art {
24
Carl Shapiro58551df2011-07-24 03:09:51 -070025bool MarkSweep::Init() {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070026 mark_stack_ = MarkStack::Create();
Carl Shapiro58551df2011-07-24 03:09:51 -070027 if (mark_stack_ == NULL) {
28 return false;
29 }
30
31 mark_bitmap_ = Heap::GetMarkBits();
32 live_bitmap_ = Heap::GetLiveBits();
33
34 // TODO: if concurrent, clear the card table.
35
buzbee0d966cf2011-09-08 17:34:58 -070036 // TODO: if concurrent, enable card marking in compiler
37
Carl Shapiro58551df2011-07-24 03:09:51 -070038 // TODO: check that the mark bitmap is entirely clear.
39
40 return true;
41}
42
Elliott Hughesb0663112011-10-19 18:16:37 -070043inline void MarkSweep::MarkObject0(const Object* obj, bool check_finger) {
Carl Shapiro69759ea2011-07-21 18:13:35 -070044 DCHECK(obj != NULL);
45 if (obj < condemned_) {
46 DCHECK(IsMarked(obj));
47 return;
48 }
49 bool is_marked = mark_bitmap_->Test(obj);
50 // This object was not previously marked.
51 if (!is_marked) {
52 mark_bitmap_->Set(obj);
53 if (check_finger && obj < finger_) {
54 // The object must be pushed on to the mark stack.
55 mark_stack_->Push(obj);
56 }
57 }
58}
59
60// Used to mark objects when recursing. Recursion is done by moving
61// the finger across the bitmaps in address order and marking child
62// objects. Any newly-marked objects whose addresses are lower than
63// the finger won't be visited by the bitmap scan, so those objects
64// need to be added to the mark stack.
Elliott Hughesb0663112011-10-19 18:16:37 -070065inline void MarkSweep::MarkObject(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -070066 if (obj != NULL) {
67 MarkObject0(obj, true);
68 }
69}
70
Elliott Hughescf4c6c42011-09-01 15:16:42 -070071void MarkSweep::MarkObjectVisitor(const Object* root, void* arg) {
Brian Carlstrom1f870082011-08-23 16:02:11 -070072 DCHECK(root != NULL);
73 DCHECK(arg != NULL);
74 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
Ian Rogers5d76c432011-10-31 21:42:49 -070075 DCHECK(mark_sweep->finger_ == NULL); // no point to check finger if it is NULL
76 mark_sweep->MarkObject0(root, false);
Brian Carlstrom1f870082011-08-23 16:02:11 -070077}
78
Carl Shapiro69759ea2011-07-21 18:13:35 -070079// Marks all objects in the root set.
80void MarkSweep::MarkRoots() {
Brian Carlstrom1f870082011-08-23 16:02:11 -070081 Runtime::Current()->VisitRoots(MarkObjectVisitor, this);
Carl Shapiro69759ea2011-07-21 18:13:35 -070082}
83
Ian Rogers5d76c432011-10-31 21:42:49 -070084void MarkSweep::ScanImageRootVisitor(Object* root, void* arg) {
85 DCHECK(root != NULL);
86 DCHECK(arg != NULL);
87 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
88 DCHECK(mark_sweep->finger_ == NULL); // no point to check finger if it is NULL
89 mark_sweep->MarkObject0(root, false);
90 mark_sweep->ScanObject(root);
91}
92
93// Marks all objects that are in images and have been touched by the mutator
94void MarkSweep::ScanDirtyImageRoots() {
95 const std::vector<Space*>& spaces = Heap::GetSpaces();
96 CardTable* card_table = Heap::GetCardTable();
97 for (size_t i = 0; i < spaces.size(); ++i) {
98 if (spaces[i]->IsImageSpace()) {
99 byte* base = spaces[i]->GetBase();
100 byte* limit = spaces[i]->GetLimit();
101 card_table->Scan(base, limit, ScanImageRootVisitor, this);
102 }
103 }
104}
105
106void MarkSweep::CheckBitmapCallback(Object* obj, void* finger, void* arg) {
107 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
108 mark_sweep->finger_ = reinterpret_cast<Object*>(finger);
109 mark_sweep->CheckObject(obj);
110}
111
Carl Shapiro58551df2011-07-24 03:09:51 -0700112void MarkSweep::ScanBitmapCallback(Object* obj, void* finger, void* arg) {
113 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
114 mark_sweep->finger_ = reinterpret_cast<Object*>(finger);
115 mark_sweep->ScanObject(obj);
116}
117
118// Populates the mark stack based on the set of marked objects and
119// recursively marks until the mark stack is emptied.
120void MarkSweep::RecursiveMark() {
Brian Carlstrom1f870082011-08-23 16:02:11 -0700121 // RecursiveMark will build the lists of known instances of the Reference classes.
122 // See DelayReferenceReferent for details.
123 CHECK(soft_reference_list_ == NULL);
124 CHECK(weak_reference_list_ == NULL);
125 CHECK(finalizer_reference_list_ == NULL);
126 CHECK(phantom_reference_list_ == NULL);
127 CHECK(cleared_reference_list_ == NULL);
128
Carl Shapiro58551df2011-07-24 03:09:51 -0700129 void* arg = reinterpret_cast<void*>(this);
130 const std::vector<Space*>& spaces = Heap::GetSpaces();
131 for (size_t i = 0; i < spaces.size(); ++i) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700132#ifndef NDEBUG
133 uintptr_t base = reinterpret_cast<uintptr_t>(spaces[i]->GetBase());
134 uintptr_t limit = reinterpret_cast<uintptr_t>(spaces[i]->GetLimit());
135 if (!spaces[i]->IsImageSpace()) {
136 mark_bitmap_->ScanWalk(base, limit, &MarkSweep::ScanBitmapCallback, arg);
137 } else{
138 mark_bitmap_->ScanWalk(base, limit, &MarkSweep::CheckBitmapCallback, arg);
139 }
140#else
Elliott Hughes307f75d2011-10-12 18:04:40 -0700141 if (!spaces[i]->IsImageSpace()) {
Carl Shapiro58551df2011-07-24 03:09:51 -0700142 uintptr_t base = reinterpret_cast<uintptr_t>(spaces[i]->GetBase());
Ian Rogers5d76c432011-10-31 21:42:49 -0700143 uintptr_t limit = reinterpret_cast<uintptr_t>(spaces[i]->GetLimit());
144 mark_bitmap_->ScanWalk(base, limit, &MarkSweep::ScanBitmapCallback, arg);
Carl Shapiro58551df2011-07-24 03:09:51 -0700145 }
Ian Rogers5d76c432011-10-31 21:42:49 -0700146#endif
Carl Shapiro58551df2011-07-24 03:09:51 -0700147 }
148 finger_ = reinterpret_cast<Object*>(~0);
Ian Rogers5d76c432011-10-31 21:42:49 -0700149 // TODO: tune the frequency of emptying the mark stack
Carl Shapiro58551df2011-07-24 03:09:51 -0700150 ProcessMarkStack();
151}
152
153void MarkSweep::ReMarkRoots() {
Elliott Hughes53b61312011-08-12 18:28:20 -0700154 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700155}
156
Elliott Hughes410c0c82011-09-01 17:58:25 -0700157void MarkSweep::SweepJniWeakGlobals() {
158 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
159 MutexLock mu(vm->weak_globals_lock);
160 IndirectReferenceTable* table = &vm->weak_globals;
161 typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto
162 for (It it = table->begin(), end = table->end(); it != end; ++it) {
163 const Object** entry = *it;
164 if (!IsMarked(*entry)) {
165 *entry = kClearedJniWeakGlobal;
166 }
167 }
168}
169
Elliott Hughes410c0c82011-09-01 17:58:25 -0700170void MarkSweep::SweepSystemWeaks() {
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700171 Runtime::Current()->GetInternTable()->SweepInternTableWeaks(IsMarked, this);
172 Runtime::Current()->GetMonitorList()->SweepMonitorList(IsMarked, this);
Elliott Hughes410c0c82011-09-01 17:58:25 -0700173 SweepJniWeakGlobals();
Carl Shapiro58551df2011-07-24 03:09:51 -0700174}
175
Brian Carlstrom78128a62011-09-15 17:21:19 -0700176void MarkSweep::SweepCallback(size_t num_ptrs, void** ptrs, void* arg) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700177 // TODO: lock heap if concurrent
Elliott Hughes307f75d2011-10-12 18:04:40 -0700178 size_t freed_objects = num_ptrs;
179 size_t freed_bytes = 0;
Carl Shapiro58551df2011-07-24 03:09:51 -0700180 Space* space = static_cast<Space*>(arg);
Ian Rogers5d76c432011-10-31 21:42:49 -0700181 // Use a bulk free, that merges consecutive objects before freeing or free per object?
182 // Documentation suggests better free performance with merging, but this may be at the expensive
183 // of allocation.
184 // TODO: investigate performance
185 static const bool kFreeUsingMerge = true;
186 if (kFreeUsingMerge) {
187 freed_bytes = space->FreeList(num_ptrs, ptrs);
188 for (size_t i = 0; i < num_ptrs; ++i) {
189 Object* obj = static_cast<Object*>(ptrs[i]);
190 Heap::GetLiveBits()->Clear(obj);
191 }
192 } else {
193 for (size_t i = 0; i < num_ptrs; ++i) {
194 Object* obj = static_cast<Object*>(ptrs[i]);
195 Heap::GetLiveBits()->Clear(obj);
196 freed_bytes += space->Free(obj);
197 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700198 }
Elliott Hughes307f75d2011-10-12 18:04:40 -0700199 Heap::RecordFreeLocked(freed_objects, freed_bytes);
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700200 // TODO: unlock heap if concurrent
Carl Shapiro58551df2011-07-24 03:09:51 -0700201}
202
203void MarkSweep::Sweep() {
Elliott Hughes2da50362011-10-10 16:57:08 -0700204 SweepSystemWeaks();
205
Carl Shapiro58551df2011-07-24 03:09:51 -0700206 const std::vector<Space*>& spaces = Heap::GetSpaces();
207 for (size_t i = 0; i < spaces.size(); ++i) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700208 if (!spaces[i]->IsImageSpace()) {
Carl Shapiro58551df2011-07-24 03:09:51 -0700209 uintptr_t base = reinterpret_cast<uintptr_t>(spaces[i]->GetBase());
210 uintptr_t limit = reinterpret_cast<uintptr_t>(spaces[i]->GetLimit());
211 void* arg = static_cast<void*>(spaces[i]);
212 HeapBitmap::SweepWalk(*live_bitmap_, *mark_bitmap_, base, limit,
213 &MarkSweep::SweepCallback, arg);
214 }
215 }
216}
217
Carl Shapiro69759ea2011-07-21 18:13:35 -0700218// Scans instance fields.
Elliott Hughesb0663112011-10-19 18:16:37 -0700219inline void MarkSweep::ScanInstanceFields(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700220 DCHECK(obj != NULL);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700221 Class* klass = obj->GetClass();
222 DCHECK(klass != NULL);
Elliott Hughes2da50362011-10-10 16:57:08 -0700223 ScanFields(obj, klass->GetReferenceInstanceOffsets(), false);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700224}
225
Ian Rogers5d76c432011-10-31 21:42:49 -0700226inline void MarkSweep::CheckInstanceFields(const Object* obj) {
227 Class* klass = obj->GetClass();
228 CheckFields(obj, klass->GetReferenceInstanceOffsets(), false);
229}
230
Brian Carlstrom4873d462011-08-21 15:23:39 -0700231// Scans static storage on a Class.
Elliott Hughesb0663112011-10-19 18:16:37 -0700232inline void MarkSweep::ScanStaticFields(const Class* klass) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700233 DCHECK(klass != NULL);
Elliott Hughesadb460d2011-10-05 17:02:34 -0700234 ScanFields(klass, klass->GetReferenceStaticOffsets(), true);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700235}
236
Ian Rogers5d76c432011-10-31 21:42:49 -0700237inline void MarkSweep::CheckStaticFields(const Class* klass) {
238 CheckFields(klass, klass->GetReferenceStaticOffsets(), true);
239}
240
Elliott Hughesb0663112011-10-19 18:16:37 -0700241inline void MarkSweep::ScanFields(const Object* obj, uint32_t ref_offsets, bool is_static) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700242 if (ref_offsets != CLASS_WALK_SUPER) {
243 // Found a reference offset bitmap. Mark the specified offsets.
244 while (ref_offsets != 0) {
245 size_t right_shift = CLZ(ref_offsets);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700246 MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
247 const Object* ref = obj->GetFieldObject<const Object*>(byte_offset, false);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700248 MarkObject(ref);
249 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
250 }
251 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700252 // There is no reference offset bitmap. In the non-static case,
253 // walk up the class inheritance hierarchy and find reference
254 // offsets the hard way. In the static case, just consider this
255 // class.
256 for (const Class* klass = is_static ? obj->AsClass() : obj->GetClass();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700257 klass != NULL;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700258 klass = is_static ? NULL : klass->GetSuperClass()) {
259 size_t num_reference_fields = (is_static
260 ? klass->NumReferenceStaticFields()
261 : klass->NumReferenceInstanceFields());
262 for (size_t i = 0; i < num_reference_fields; ++i) {
263 Field* field = (is_static
264 ? klass->GetStaticField(i)
265 : klass->GetInstanceField(i));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700266 MemberOffset field_offset = field->GetOffset();
267 const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700268 MarkObject(ref);
269 }
270 }
271 }
272}
273
Ian Rogers5d76c432011-10-31 21:42:49 -0700274inline void MarkSweep::CheckReference(const Object* obj, const Object* ref, MemberOffset offset, bool is_static) {
275 Space* alloc_space = Heap::GetAllocSpace();
276 if (alloc_space->Contains(ref)) {
277 bool is_marked = mark_bitmap_->Test(ref);
278 if(!is_marked) {
279 LOG(INFO) << StringPrintf("Alloc space %p-%p (%s)", alloc_space->GetBase(), alloc_space->GetLimit(), alloc_space->GetName().c_str());
280 LOG(WARNING) << (is_static ? "Static ref'" : "Instance ref'") << PrettyTypeOf(ref) << "' (" << (void*)ref
281 << ") in '" << PrettyTypeOf(obj) << "' (" << (void*)obj << ") at offset "
282 << (void*)offset.Int32Value() << " wasn't marked";
283 bool obj_marked = Heap::GetCardTable()->IsDirty(obj);
284 if (!obj_marked) {
285 LOG(WARNING) << "Object '" << PrettyTypeOf(obj) << "' (" << (void*)obj
286 << ") contains references to the alloc space, but wasn't card marked";
287 }
288 }
289 }
290}
291
292inline void MarkSweep::CheckFields(const Object* obj, uint32_t ref_offsets, bool is_static) {
293 if (ref_offsets != CLASS_WALK_SUPER) {
294 // Found a reference offset bitmap. Mark the specified offsets.
295 while (ref_offsets != 0) {
296 size_t right_shift = CLZ(ref_offsets);
297 MemberOffset field_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
298 const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false);
299 CheckReference(obj, ref, field_offset, is_static);
300 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
301 }
302 } else {
303 // There is no reference offset bitmap. In the non-static case,
304 // walk up the class inheritance hierarchy and find reference
305 // offsets the hard way. In the static case, just consider this
306 // class.
307 for (const Class* klass = is_static ? obj->AsClass() : obj->GetClass();
308 klass != NULL;
309 klass = is_static ? NULL : klass->GetSuperClass()) {
310 size_t num_reference_fields = (is_static
311 ? klass->NumReferenceStaticFields()
312 : klass->NumReferenceInstanceFields());
313 for (size_t i = 0; i < num_reference_fields; ++i) {
314 Field* field = (is_static
315 ? klass->GetStaticField(i)
316 : klass->GetInstanceField(i));
317 MemberOffset field_offset = field->GetOffset();
318 const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false);
319 CheckReference(obj, ref, field_offset, is_static);
320 }
321 }
322 }
323}
324
Carl Shapiro69759ea2011-07-21 18:13:35 -0700325// Scans the header, static field references, and interface pointers
326// of a class object.
Elliott Hughesb0663112011-10-19 18:16:37 -0700327inline void MarkSweep::ScanClass(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700328#ifndef NDEBUG
329 ++class_count_;
330#endif
Brian Carlstrom693267a2011-09-06 09:25:34 -0700331 ScanInstanceFields(obj);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700332 ScanStaticFields(obj->AsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700333}
334
Ian Rogers5d76c432011-10-31 21:42:49 -0700335inline void MarkSweep::CheckClass(const Object* obj) {
336 CheckInstanceFields(obj);
337 CheckStaticFields(obj->AsClass());
338}
339
Carl Shapiro69759ea2011-07-21 18:13:35 -0700340// Scans the header of all array objects. If the array object is
341// specialized to a reference type, scans the array data as well.
Elliott Hughesb0663112011-10-19 18:16:37 -0700342inline void MarkSweep::ScanArray(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700343#ifndef NDEBUG
344 ++array_count_;
345#endif
Carl Shapiro69759ea2011-07-21 18:13:35 -0700346 MarkObject(obj->GetClass());
347 if (obj->IsObjectArray()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700348 const ObjectArray<Object>* array = obj->AsObjectArray<Object>();
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700349 for (int32_t i = 0; i < array->GetLength(); ++i) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700350 const Object* element = array->GetWithoutChecks(i);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700351 MarkObject(element);
352 }
353 }
354}
355
Ian Rogers5d76c432011-10-31 21:42:49 -0700356inline void MarkSweep::CheckArray(const Object* obj) {
357 CheckReference(obj, obj->GetClass(), Object::ClassOffset(), false);
358 if (obj->IsObjectArray()) {
359 const ObjectArray<Object>* array = obj->AsObjectArray<Object>();
360 for (int32_t i = 0; i < array->GetLength(); ++i) {
361 const Object* element = array->GetWithoutChecks(i);
362 CheckReference(obj, element, MemberOffset(i * sizeof(Object*) +
363 Array::DataOffset().Int32Value()), false);
364 }
365 }
366}
367
Carl Shapiro69759ea2011-07-21 18:13:35 -0700368// Process the "referent" field in a java.lang.ref.Reference. If the
369// referent has not yet been marked, put it on the appropriate list in
370// the gcHeap for later processing.
371void MarkSweep::DelayReferenceReferent(Object* obj) {
372 DCHECK(obj != NULL);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700373 Class* klass = obj->GetClass();
374 DCHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700375 DCHECK(klass->IsReferenceClass());
376 Object* pending = obj->GetFieldObject<Object*>(Heap::GetReferencePendingNextOffset(), false);
Elliott Hughesadb460d2011-10-05 17:02:34 -0700377 Object* referent = Heap::GetReferenceReferent(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700378 if (pending == NULL && referent != NULL && !IsMarked(referent)) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700379 Object** list = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700380 if (klass->IsSoftReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700381 list = &soft_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700382 } else if (klass->IsWeakReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700383 list = &weak_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700384 } else if (klass->IsFinalizerReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700385 list = &finalizer_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700386 } else if (klass->IsPhantomReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700387 list = &phantom_reference_list_;
388 }
Brian Carlstrom0796af02011-10-12 14:31:45 -0700389 DCHECK(list != NULL) << PrettyClass(klass) << " " << std::hex << klass->GetAccessFlags();
Elliott Hughesadb460d2011-10-05 17:02:34 -0700390 Heap::EnqueuePendingReference(obj, list);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700391 }
392}
393
394// Scans the header and field references of a data object. If the
395// scanned object is a reference subclass, it is scheduled for later
Elliott Hughesadb460d2011-10-05 17:02:34 -0700396// processing.
Elliott Hughesb0663112011-10-19 18:16:37 -0700397inline void MarkSweep::ScanOther(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700398#ifndef NDEBUG
399 ++other_count_;
400#endif
Carl Shapiro69759ea2011-07-21 18:13:35 -0700401 ScanInstanceFields(obj);
Elliott Hughes352a4242011-10-31 15:15:21 -0700402 if (obj->GetClass()->IsReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700403 DelayReferenceReferent(const_cast<Object*>(obj));
404 }
405}
406
Ian Rogers5d76c432011-10-31 21:42:49 -0700407inline void MarkSweep::CheckOther(const Object* obj) {
408 CheckInstanceFields(obj);
409}
410
Carl Shapiro69759ea2011-07-21 18:13:35 -0700411// Scans an object reference. Determines the type of the reference
412// and dispatches to a specialized scanning routine.
Elliott Hughesb0663112011-10-19 18:16:37 -0700413inline void MarkSweep::ScanObject(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700414 DCHECK(obj != NULL);
415 DCHECK(obj->GetClass() != NULL);
416 DCHECK(IsMarked(obj));
417 if (obj->IsClass()) {
418 ScanClass(obj);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700419 } else if (obj->IsArrayInstance()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700420 ScanArray(obj);
421 } else {
Carl Shapiro58551df2011-07-24 03:09:51 -0700422 ScanOther(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700423 }
424}
425
Ian Rogers5d76c432011-10-31 21:42:49 -0700426// Check to see that all alloc space references are marked for the given object
427inline void MarkSweep::CheckObject(const Object* obj) {
428 DCHECK(obj != NULL);
429 DCHECK(obj->GetClass() != NULL);
430 DCHECK(IsMarked(obj));
431 if (obj->IsClass()) {
432 CheckClass(obj);
433 } else if (obj->IsArrayInstance()) {
434 CheckArray(obj);
435 } else {
436 CheckOther(obj);
437 }
438}
439
440// Scan anything that's on the mark stack.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700441void MarkSweep::ProcessMarkStack() {
Ian Rogers5d76c432011-10-31 21:42:49 -0700442 Space* alloc_space = Heap::GetAllocSpace();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700443 while (!mark_stack_->IsEmpty()) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700444 const Object* obj = mark_stack_->Pop();
Ian Rogers5d76c432011-10-31 21:42:49 -0700445 if (alloc_space->Contains(obj)) {
446 ScanObject(obj);
447 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700448 }
449}
450
451void MarkSweep::ScanDirtyObjects() {
452 ProcessMarkStack();
453}
454
Carl Shapiro69759ea2011-07-21 18:13:35 -0700455// Walks the reference list marking any references subject to the
456// reference clearing policy. References with a black referent are
457// removed from the list. References with white referents biased
458// toward saving are blackened and also removed from the list.
459void MarkSweep::PreserveSomeSoftReferences(Object** list) {
460 DCHECK(list != NULL);
461 Object* clear = NULL;
462 size_t counter = 0;
463 while (*list != NULL) {
Elliott Hughesadb460d2011-10-05 17:02:34 -0700464 Object* ref = Heap::DequeuePendingReference(list);
465 Object* referent = Heap::GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700466 if (referent == NULL) {
467 // Referent was cleared by the user during marking.
468 continue;
469 }
470 bool is_marked = IsMarked(referent);
471 if (!is_marked && ((++counter) & 1)) {
472 // Referent is white and biased toward saving, mark it.
473 MarkObject(referent);
474 is_marked = true;
475 }
476 if (!is_marked) {
477 // Referent is white, queue it for clearing.
Elliott Hughesadb460d2011-10-05 17:02:34 -0700478 Heap::EnqueuePendingReference(ref, &clear);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700479 }
480 }
481 *list = clear;
482 // Restart the mark with the newly black references added to the
483 // root set.
484 ProcessMarkStack();
485}
486
487// Unlink the reference list clearing references objects with white
488// referents. Cleared references registered to a reference queue are
489// scheduled for appending by the heap worker thread.
490void MarkSweep::ClearWhiteReferences(Object** list) {
491 DCHECK(list != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700492 while (*list != NULL) {
Elliott Hughesadb460d2011-10-05 17:02:34 -0700493 Object* ref = Heap::DequeuePendingReference(list);
494 Object* referent = Heap::GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700495 if (referent != NULL && !IsMarked(referent)) {
496 // Referent is white, clear it.
Elliott Hughesadb460d2011-10-05 17:02:34 -0700497 Heap::ClearReferenceReferent(ref);
498 if (Heap::IsEnqueuable(ref)) {
499 Heap::EnqueueReference(ref, &cleared_reference_list_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700500 }
501 }
502 }
503 DCHECK(*list == NULL);
504}
505
506// Enqueues finalizer references with white referents. White
507// referents are blackened, moved to the zombie field, and the
508// referent field is cleared.
509void MarkSweep::EnqueueFinalizerReferences(Object** list) {
510 DCHECK(list != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700511 MemberOffset zombie_offset = Heap::GetFinalizerReferenceZombieOffset();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700512 bool has_enqueued = false;
513 while (*list != NULL) {
Elliott Hughesadb460d2011-10-05 17:02:34 -0700514 Object* ref = Heap::DequeuePendingReference(list);
515 Object* referent = Heap::GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700516 if (referent != NULL && !IsMarked(referent)) {
517 MarkObject(referent);
518 // If the referent is non-null the reference must queuable.
Elliott Hughesadb460d2011-10-05 17:02:34 -0700519 DCHECK(Heap::IsEnqueuable(ref));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700520 ref->SetFieldObject(zombie_offset, referent, false);
Elliott Hughesadb460d2011-10-05 17:02:34 -0700521 Heap::ClearReferenceReferent(ref);
522 Heap::EnqueueReference(ref, &cleared_reference_list_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700523 has_enqueued = true;
524 }
525 }
526 if (has_enqueued) {
527 ProcessMarkStack();
528 }
529 DCHECK(*list == NULL);
530}
531
Carl Shapiro58551df2011-07-24 03:09:51 -0700532// Process reference class instances and schedule finalizations.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700533void MarkSweep::ProcessReferences(Object** soft_references, bool clear_soft,
534 Object** weak_references,
535 Object** finalizer_references,
536 Object** phantom_references) {
537 DCHECK(soft_references != NULL);
538 DCHECK(weak_references != NULL);
539 DCHECK(finalizer_references != NULL);
540 DCHECK(phantom_references != NULL);
541
542 // Unless we are in the zygote or required to clear soft references
543 // with white references, preserve some white referents.
544 if (clear_soft) {
545 PreserveSomeSoftReferences(soft_references);
546 }
547
548 // Clear all remaining soft and weak references with white
549 // referents.
550 ClearWhiteReferences(soft_references);
551 ClearWhiteReferences(weak_references);
552
553 // Preserve all white objects with finalize methods and schedule
554 // them for finalization.
555 EnqueueFinalizerReferences(finalizer_references);
556
557 // Clear all f-reachable soft and weak references with white
558 // referents.
559 ClearWhiteReferences(soft_references);
560 ClearWhiteReferences(weak_references);
561
562 // Clear all phantom references with white referents.
563 ClearWhiteReferences(phantom_references);
564
565 // At this point all reference lists should be empty.
566 DCHECK(*soft_references == NULL);
567 DCHECK(*weak_references == NULL);
568 DCHECK(*finalizer_references == NULL);
569 DCHECK(*phantom_references == NULL);
570}
571
Carl Shapiro69759ea2011-07-21 18:13:35 -0700572MarkSweep::~MarkSweep() {
Elliott Hughes352a4242011-10-31 15:15:21 -0700573#ifndef NDEBUG
574 if (Heap::IsVerboseHeap()) {
575 LOG(INFO) << "MarkSweep scanned classes=" << class_count_ << " arrays=" << array_count_ << " other=" << other_count_;
576 }
577#endif
Carl Shapiro58551df2011-07-24 03:09:51 -0700578 delete mark_stack_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700579 mark_bitmap_->Clear();
580}
581
582} // namespace art