blob: 2e8041d578ed51e8012d60c507303c23a249e29e [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
Carl Shapiro69759ea2011-07-21 18:13:35 -070043void MarkSweep::MarkObject0(const Object* obj, bool check_finger) {
44 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.
65void MarkSweep::MarkObject(const Object* obj) {
66 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);
75 mark_sweep->MarkObject0(root, true);
76}
77
Carl Shapiro69759ea2011-07-21 18:13:35 -070078// Marks all objects in the root set.
79void MarkSweep::MarkRoots() {
Brian Carlstrom1f870082011-08-23 16:02:11 -070080 Runtime::Current()->VisitRoots(MarkObjectVisitor, this);
Carl Shapiro69759ea2011-07-21 18:13:35 -070081}
82
Carl Shapiro58551df2011-07-24 03:09:51 -070083void MarkSweep::ScanBitmapCallback(Object* obj, void* finger, void* arg) {
84 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
85 mark_sweep->finger_ = reinterpret_cast<Object*>(finger);
86 mark_sweep->ScanObject(obj);
87}
88
89// Populates the mark stack based on the set of marked objects and
90// recursively marks until the mark stack is emptied.
91void MarkSweep::RecursiveMark() {
Brian Carlstrom1f870082011-08-23 16:02:11 -070092 // RecursiveMark will build the lists of known instances of the Reference classes.
93 // See DelayReferenceReferent for details.
94 CHECK(soft_reference_list_ == NULL);
95 CHECK(weak_reference_list_ == NULL);
96 CHECK(finalizer_reference_list_ == NULL);
97 CHECK(phantom_reference_list_ == NULL);
98 CHECK(cleared_reference_list_ == NULL);
99
Elliott Hughes307f75d2011-10-12 18:04:40 -0700100 TimingLogger timings("MarkSweep::RecursiveMark");
Carl Shapiro58551df2011-07-24 03:09:51 -0700101 void* arg = reinterpret_cast<void*>(this);
102 const std::vector<Space*>& spaces = Heap::GetSpaces();
103 for (size_t i = 0; i < spaces.size(); ++i) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700104 if (!spaces[i]->IsImageSpace()) {
Carl Shapiro58551df2011-07-24 03:09:51 -0700105 uintptr_t base = reinterpret_cast<uintptr_t>(spaces[i]->GetBase());
Brian Carlstrom1f870082011-08-23 16:02:11 -0700106 mark_bitmap_->ScanWalk(base, &MarkSweep::ScanBitmapCallback, arg);
Carl Shapiro58551df2011-07-24 03:09:51 -0700107 }
Elliott Hughes307f75d2011-10-12 18:04:40 -0700108 timings.AddSplit(StringPrintf("ScanWalk space #%i (%s)", i, spaces[i]->GetName().c_str()));
Carl Shapiro58551df2011-07-24 03:09:51 -0700109 }
110 finger_ = reinterpret_cast<Object*>(~0);
111 ProcessMarkStack();
Elliott Hughes307f75d2011-10-12 18:04:40 -0700112 timings.AddSplit("ProcessMarkStack");
113 timings.Dump();
Carl Shapiro58551df2011-07-24 03:09:51 -0700114}
115
116void MarkSweep::ReMarkRoots() {
Elliott Hughes53b61312011-08-12 18:28:20 -0700117 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700118}
119
Elliott Hughes410c0c82011-09-01 17:58:25 -0700120void MarkSweep::SweepJniWeakGlobals() {
121 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
122 MutexLock mu(vm->weak_globals_lock);
123 IndirectReferenceTable* table = &vm->weak_globals;
124 typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto
125 for (It it = table->begin(), end = table->end(); it != end; ++it) {
126 const Object** entry = *it;
127 if (!IsMarked(*entry)) {
128 *entry = kClearedJniWeakGlobal;
129 }
130 }
131}
132
Elliott Hughes410c0c82011-09-01 17:58:25 -0700133void MarkSweep::SweepSystemWeaks() {
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700134 Runtime::Current()->GetInternTable()->SweepInternTableWeaks(IsMarked, this);
135 Runtime::Current()->GetMonitorList()->SweepMonitorList(IsMarked, this);
Elliott Hughes410c0c82011-09-01 17:58:25 -0700136 SweepJniWeakGlobals();
Carl Shapiro58551df2011-07-24 03:09:51 -0700137}
138
Brian Carlstrom78128a62011-09-15 17:21:19 -0700139void MarkSweep::SweepCallback(size_t num_ptrs, void** ptrs, void* arg) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700140 // TODO: lock heap if concurrent
Elliott Hughes307f75d2011-10-12 18:04:40 -0700141 size_t freed_objects = num_ptrs;
142 size_t freed_bytes = 0;
Carl Shapiro58551df2011-07-24 03:09:51 -0700143 Space* space = static_cast<Space*>(arg);
144 for (size_t i = 0; i < num_ptrs; ++i) {
145 Object* obj = static_cast<Object*>(ptrs[i]);
Elliott Hughes307f75d2011-10-12 18:04:40 -0700146 freed_bytes += space->AllocationSize(obj);
Elliott Hughes1e200942011-10-12 18:59:24 -0700147 Heap::GetLiveBits()->Clear(obj);
Carl Shapiro58551df2011-07-24 03:09:51 -0700148 space->Free(obj);
149 }
Elliott Hughes307f75d2011-10-12 18:04:40 -0700150 Heap::RecordFreeLocked(freed_objects, freed_bytes);
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700151 // TODO: unlock heap if concurrent
Carl Shapiro58551df2011-07-24 03:09:51 -0700152}
153
154void MarkSweep::Sweep() {
Elliott Hughes2da50362011-10-10 16:57:08 -0700155 SweepSystemWeaks();
156
Carl Shapiro58551df2011-07-24 03:09:51 -0700157 const std::vector<Space*>& spaces = Heap::GetSpaces();
158 for (size_t i = 0; i < spaces.size(); ++i) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700159 if (!spaces[i]->IsImageSpace()) {
Carl Shapiro58551df2011-07-24 03:09:51 -0700160 uintptr_t base = reinterpret_cast<uintptr_t>(spaces[i]->GetBase());
161 uintptr_t limit = reinterpret_cast<uintptr_t>(spaces[i]->GetLimit());
162 void* arg = static_cast<void*>(spaces[i]);
163 HeapBitmap::SweepWalk(*live_bitmap_, *mark_bitmap_, base, limit,
164 &MarkSweep::SweepCallback, arg);
165 }
166 }
167}
168
Carl Shapiro69759ea2011-07-21 18:13:35 -0700169// Scans instance fields.
170void MarkSweep::ScanInstanceFields(const Object* obj) {
171 DCHECK(obj != NULL);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700172 Class* klass = obj->GetClass();
173 DCHECK(klass != NULL);
Elliott Hughes2da50362011-10-10 16:57:08 -0700174 ScanFields(obj, klass->GetReferenceInstanceOffsets(), false);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700175}
176
177// Scans static storage on a Class.
178void MarkSweep::ScanStaticFields(const Class* klass) {
179 DCHECK(klass != NULL);
Elliott Hughesadb460d2011-10-05 17:02:34 -0700180 ScanFields(klass, klass->GetReferenceStaticOffsets(), true);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700181}
182
Elliott Hughes2da50362011-10-10 16:57:08 -0700183void MarkSweep::ScanFields(const Object* obj, uint32_t ref_offsets, bool is_static) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700184 if (ref_offsets != CLASS_WALK_SUPER) {
185 // Found a reference offset bitmap. Mark the specified offsets.
186 while (ref_offsets != 0) {
187 size_t right_shift = CLZ(ref_offsets);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700188 MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
189 const Object* ref = obj->GetFieldObject<const Object*>(byte_offset, false);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700190 MarkObject(ref);
191 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
192 }
193 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700194 // There is no reference offset bitmap. In the non-static case,
195 // walk up the class inheritance hierarchy and find reference
196 // offsets the hard way. In the static case, just consider this
197 // class.
198 for (const Class* klass = is_static ? obj->AsClass() : obj->GetClass();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700199 klass != NULL;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700200 klass = is_static ? NULL : klass->GetSuperClass()) {
201 size_t num_reference_fields = (is_static
202 ? klass->NumReferenceStaticFields()
203 : klass->NumReferenceInstanceFields());
204 for (size_t i = 0; i < num_reference_fields; ++i) {
205 Field* field = (is_static
206 ? klass->GetStaticField(i)
207 : klass->GetInstanceField(i));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700208 MemberOffset field_offset = field->GetOffset();
209 const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700210 MarkObject(ref);
211 }
212 }
213 }
214}
215
Carl Shapiro69759ea2011-07-21 18:13:35 -0700216// Scans the header, static field references, and interface pointers
217// of a class object.
218void MarkSweep::ScanClass(const Object* obj) {
219 DCHECK(obj != NULL);
220 DCHECK(obj->IsClass());
221 const Class* klass = obj->AsClass();
222 MarkObject(klass->GetClass());
Brian Carlstrom693267a2011-09-06 09:25:34 -0700223 ScanInstanceFields(obj);
224 MarkObject(klass->GetDescriptor());
225 MarkObject(klass->GetDexCache());
226 MarkObject(klass->GetVerifyErrorClass());
Brian Carlstromb63ec392011-08-27 17:38:27 -0700227 if (klass->IsArrayClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700228 MarkObject(klass->GetComponentType());
229 }
230 if (klass->IsLoaded()) {
231 MarkObject(klass->GetSuperClass());
232 }
233 MarkObject(klass->GetClassLoader());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700234 if (klass->IsLoaded()) {
Brian Carlstrom693267a2011-09-06 09:25:34 -0700235 MarkObject(klass->GetInterfaces());
236 MarkObject(klass->GetDirectMethods());
237 MarkObject(klass->GetVirtualMethods());
238 MarkObject(klass->GetIFields());
239 MarkObject(klass->GetSFields());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700240 }
Brian Carlstrom693267a2011-09-06 09:25:34 -0700241 ScanStaticFields(klass);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700242}
243
244// Scans the header of all array objects. If the array object is
245// specialized to a reference type, scans the array data as well.
Brian Carlstrom4873d462011-08-21 15:23:39 -0700246void MarkSweep::ScanArray(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700247 DCHECK(obj != NULL);
248 DCHECK(obj->GetClass() != NULL);
249 MarkObject(obj->GetClass());
250 if (obj->IsObjectArray()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700251 const ObjectArray<Object>* array = obj->AsObjectArray<Object>();
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700252 for (int32_t i = 0; i < array->GetLength(); ++i) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700253 const Object* element = array->Get(i);
254 MarkObject(element);
255 }
256 }
257}
258
Carl Shapiro69759ea2011-07-21 18:13:35 -0700259// Process the "referent" field in a java.lang.ref.Reference. If the
260// referent has not yet been marked, put it on the appropriate list in
261// the gcHeap for later processing.
262void MarkSweep::DelayReferenceReferent(Object* obj) {
263 DCHECK(obj != NULL);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700264 Class* klass = obj->GetClass();
265 DCHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700266 DCHECK(klass->IsReferenceClass());
267 Object* pending = obj->GetFieldObject<Object*>(Heap::GetReferencePendingNextOffset(), false);
Elliott Hughesadb460d2011-10-05 17:02:34 -0700268 Object* referent = Heap::GetReferenceReferent(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700269 if (pending == NULL && referent != NULL && !IsMarked(referent)) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700270 Object** list = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700271 if (klass->IsSoftReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700272 list = &soft_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700273 } else if (klass->IsWeakReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700274 list = &weak_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700275 } else if (klass->IsFinalizerReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700276 list = &finalizer_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700277 } else if (klass->IsPhantomReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700278 list = &phantom_reference_list_;
279 }
Brian Carlstrom0796af02011-10-12 14:31:45 -0700280 DCHECK(list != NULL) << PrettyClass(klass) << " " << std::hex << klass->GetAccessFlags();
Elliott Hughesadb460d2011-10-05 17:02:34 -0700281 Heap::EnqueuePendingReference(obj, list);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700282 }
283}
284
285// Scans the header and field references of a data object. If the
286// scanned object is a reference subclass, it is scheduled for later
Elliott Hughesadb460d2011-10-05 17:02:34 -0700287// processing.
Carl Shapiro58551df2011-07-24 03:09:51 -0700288void MarkSweep::ScanOther(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700289 DCHECK(obj != NULL);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700290 Class* klass = obj->GetClass();
291 DCHECK(klass != NULL);
292 MarkObject(klass);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700293 ScanInstanceFields(obj);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700294 if (klass->IsReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700295 DelayReferenceReferent(const_cast<Object*>(obj));
296 }
297}
298
299// Scans an object reference. Determines the type of the reference
300// and dispatches to a specialized scanning routine.
301void MarkSweep::ScanObject(const Object* obj) {
302 DCHECK(obj != NULL);
303 DCHECK(obj->GetClass() != NULL);
304 DCHECK(IsMarked(obj));
305 if (obj->IsClass()) {
306 ScanClass(obj);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700307 } else if (obj->IsArrayInstance()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700308 ScanArray(obj);
309 } else {
Carl Shapiro58551df2011-07-24 03:09:51 -0700310 ScanOther(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700311 }
312}
313
314// Scan anything that's on the mark stack. We can't use the bitmaps
315// anymore, so use a finger that points past the end of them.
316void MarkSweep::ProcessMarkStack() {
317 while (!mark_stack_->IsEmpty()) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700318 const Object* obj = mark_stack_->Pop();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700319 ScanObject(obj);
320 }
321}
322
323void MarkSweep::ScanDirtyObjects() {
324 ProcessMarkStack();
325}
326
Carl Shapiro69759ea2011-07-21 18:13:35 -0700327// Walks the reference list marking any references subject to the
328// reference clearing policy. References with a black referent are
329// removed from the list. References with white referents biased
330// toward saving are blackened and also removed from the list.
331void MarkSweep::PreserveSomeSoftReferences(Object** list) {
332 DCHECK(list != NULL);
333 Object* clear = NULL;
334 size_t counter = 0;
335 while (*list != NULL) {
Elliott Hughesadb460d2011-10-05 17:02:34 -0700336 Object* ref = Heap::DequeuePendingReference(list);
337 Object* referent = Heap::GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700338 if (referent == NULL) {
339 // Referent was cleared by the user during marking.
340 continue;
341 }
342 bool is_marked = IsMarked(referent);
343 if (!is_marked && ((++counter) & 1)) {
344 // Referent is white and biased toward saving, mark it.
345 MarkObject(referent);
346 is_marked = true;
347 }
348 if (!is_marked) {
349 // Referent is white, queue it for clearing.
Elliott Hughesadb460d2011-10-05 17:02:34 -0700350 Heap::EnqueuePendingReference(ref, &clear);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700351 }
352 }
353 *list = clear;
354 // Restart the mark with the newly black references added to the
355 // root set.
356 ProcessMarkStack();
357}
358
359// Unlink the reference list clearing references objects with white
360// referents. Cleared references registered to a reference queue are
361// scheduled for appending by the heap worker thread.
362void MarkSweep::ClearWhiteReferences(Object** list) {
363 DCHECK(list != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700364 while (*list != NULL) {
Elliott Hughesadb460d2011-10-05 17:02:34 -0700365 Object* ref = Heap::DequeuePendingReference(list);
366 Object* referent = Heap::GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700367 if (referent != NULL && !IsMarked(referent)) {
368 // Referent is white, clear it.
Elliott Hughesadb460d2011-10-05 17:02:34 -0700369 Heap::ClearReferenceReferent(ref);
370 if (Heap::IsEnqueuable(ref)) {
371 Heap::EnqueueReference(ref, &cleared_reference_list_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700372 }
373 }
374 }
375 DCHECK(*list == NULL);
376}
377
378// Enqueues finalizer references with white referents. White
379// referents are blackened, moved to the zombie field, and the
380// referent field is cleared.
381void MarkSweep::EnqueueFinalizerReferences(Object** list) {
382 DCHECK(list != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700383 MemberOffset zombie_offset = Heap::GetFinalizerReferenceZombieOffset();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700384 bool has_enqueued = false;
385 while (*list != NULL) {
Elliott Hughesadb460d2011-10-05 17:02:34 -0700386 Object* ref = Heap::DequeuePendingReference(list);
387 Object* referent = Heap::GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700388 if (referent != NULL && !IsMarked(referent)) {
389 MarkObject(referent);
390 // If the referent is non-null the reference must queuable.
Elliott Hughesadb460d2011-10-05 17:02:34 -0700391 DCHECK(Heap::IsEnqueuable(ref));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700392 ref->SetFieldObject(zombie_offset, referent, false);
Elliott Hughesadb460d2011-10-05 17:02:34 -0700393 Heap::ClearReferenceReferent(ref);
394 Heap::EnqueueReference(ref, &cleared_reference_list_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700395 has_enqueued = true;
396 }
397 }
398 if (has_enqueued) {
399 ProcessMarkStack();
400 }
401 DCHECK(*list == NULL);
402}
403
Carl Shapiro58551df2011-07-24 03:09:51 -0700404// Process reference class instances and schedule finalizations.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700405void MarkSweep::ProcessReferences(Object** soft_references, bool clear_soft,
406 Object** weak_references,
407 Object** finalizer_references,
408 Object** phantom_references) {
409 DCHECK(soft_references != NULL);
410 DCHECK(weak_references != NULL);
411 DCHECK(finalizer_references != NULL);
412 DCHECK(phantom_references != NULL);
413
414 // Unless we are in the zygote or required to clear soft references
415 // with white references, preserve some white referents.
416 if (clear_soft) {
417 PreserveSomeSoftReferences(soft_references);
418 }
419
420 // Clear all remaining soft and weak references with white
421 // referents.
422 ClearWhiteReferences(soft_references);
423 ClearWhiteReferences(weak_references);
424
425 // Preserve all white objects with finalize methods and schedule
426 // them for finalization.
427 EnqueueFinalizerReferences(finalizer_references);
428
429 // Clear all f-reachable soft and weak references with white
430 // referents.
431 ClearWhiteReferences(soft_references);
432 ClearWhiteReferences(weak_references);
433
434 // Clear all phantom references with white referents.
435 ClearWhiteReferences(phantom_references);
436
437 // At this point all reference lists should be empty.
438 DCHECK(*soft_references == NULL);
439 DCHECK(*weak_references == NULL);
440 DCHECK(*finalizer_references == NULL);
441 DCHECK(*phantom_references == NULL);
442}
443
Carl Shapiro69759ea2011-07-21 18:13:35 -0700444MarkSweep::~MarkSweep() {
Carl Shapiro58551df2011-07-24 03:09:51 -0700445 delete mark_stack_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700446 mark_bitmap_->Clear();
447}
448
449} // namespace art