blob: 9078340c7c73fe86377438ee4f8821bbdd530612 [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
Jesse Wilson078f9b02011-11-18 17:51:47 -050025void MarkSweep::Init() {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070026 mark_stack_ = MarkStack::Create();
Carl Shapiro58551df2011-07-24 03:09:51 -070027 mark_bitmap_ = Heap::GetMarkBits();
28 live_bitmap_ = Heap::GetLiveBits();
29
30 // TODO: if concurrent, clear the card table.
31
buzbee0d966cf2011-09-08 17:34:58 -070032 // TODO: if concurrent, enable card marking in compiler
33
Carl Shapiro58551df2011-07-24 03:09:51 -070034 // TODO: check that the mark bitmap is entirely clear.
Carl Shapiro58551df2011-07-24 03:09:51 -070035}
36
Elliott Hughesb0663112011-10-19 18:16:37 -070037inline void MarkSweep::MarkObject0(const Object* obj, bool check_finger) {
Carl Shapiro69759ea2011-07-21 18:13:35 -070038 DCHECK(obj != NULL);
39 if (obj < condemned_) {
40 DCHECK(IsMarked(obj));
41 return;
42 }
43 bool is_marked = mark_bitmap_->Test(obj);
44 // This object was not previously marked.
45 if (!is_marked) {
46 mark_bitmap_->Set(obj);
47 if (check_finger && obj < finger_) {
48 // The object must be pushed on to the mark stack.
49 mark_stack_->Push(obj);
50 }
51 }
52}
53
54// Used to mark objects when recursing. Recursion is done by moving
55// the finger across the bitmaps in address order and marking child
56// objects. Any newly-marked objects whose addresses are lower than
57// the finger won't be visited by the bitmap scan, so those objects
58// need to be added to the mark stack.
Elliott Hughesb0663112011-10-19 18:16:37 -070059inline void MarkSweep::MarkObject(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -070060 if (obj != NULL) {
61 MarkObject0(obj, true);
62 }
63}
64
Elliott Hughescf4c6c42011-09-01 15:16:42 -070065void MarkSweep::MarkObjectVisitor(const Object* root, void* arg) {
Brian Carlstrom1f870082011-08-23 16:02:11 -070066 DCHECK(root != NULL);
67 DCHECK(arg != NULL);
68 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
Ian Rogers5d76c432011-10-31 21:42:49 -070069 DCHECK(mark_sweep->finger_ == NULL); // no point to check finger if it is NULL
70 mark_sweep->MarkObject0(root, false);
Brian Carlstrom1f870082011-08-23 16:02:11 -070071}
72
Carl Shapiro69759ea2011-07-21 18:13:35 -070073// Marks all objects in the root set.
74void MarkSweep::MarkRoots() {
Brian Carlstrom1f870082011-08-23 16:02:11 -070075 Runtime::Current()->VisitRoots(MarkObjectVisitor, this);
Carl Shapiro69759ea2011-07-21 18:13:35 -070076}
77
Ian Rogers5d76c432011-10-31 21:42:49 -070078void MarkSweep::ScanImageRootVisitor(Object* root, void* arg) {
79 DCHECK(root != NULL);
80 DCHECK(arg != NULL);
81 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
82 DCHECK(mark_sweep->finger_ == NULL); // no point to check finger if it is NULL
83 mark_sweep->MarkObject0(root, false);
84 mark_sweep->ScanObject(root);
85}
86
87// Marks all objects that are in images and have been touched by the mutator
88void MarkSweep::ScanDirtyImageRoots() {
89 const std::vector<Space*>& spaces = Heap::GetSpaces();
90 CardTable* card_table = Heap::GetCardTable();
91 for (size_t i = 0; i < spaces.size(); ++i) {
92 if (spaces[i]->IsImageSpace()) {
93 byte* base = spaces[i]->GetBase();
94 byte* limit = spaces[i]->GetLimit();
95 card_table->Scan(base, limit, ScanImageRootVisitor, this);
96 }
97 }
98}
99
100void MarkSweep::CheckBitmapCallback(Object* obj, void* finger, void* arg) {
101 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
102 mark_sweep->finger_ = reinterpret_cast<Object*>(finger);
103 mark_sweep->CheckObject(obj);
104}
105
Carl Shapiro58551df2011-07-24 03:09:51 -0700106void MarkSweep::ScanBitmapCallback(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->ScanObject(obj);
110}
111
112// Populates the mark stack based on the set of marked objects and
113// recursively marks until the mark stack is emptied.
114void MarkSweep::RecursiveMark() {
Brian Carlstrom1f870082011-08-23 16:02:11 -0700115 // RecursiveMark will build the lists of known instances of the Reference classes.
116 // See DelayReferenceReferent for details.
117 CHECK(soft_reference_list_ == NULL);
118 CHECK(weak_reference_list_ == NULL);
119 CHECK(finalizer_reference_list_ == NULL);
120 CHECK(phantom_reference_list_ == NULL);
121 CHECK(cleared_reference_list_ == NULL);
122
Carl Shapiro58551df2011-07-24 03:09:51 -0700123 void* arg = reinterpret_cast<void*>(this);
124 const std::vector<Space*>& spaces = Heap::GetSpaces();
125 for (size_t i = 0; i < spaces.size(); ++i) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700126#ifndef NDEBUG
127 uintptr_t base = reinterpret_cast<uintptr_t>(spaces[i]->GetBase());
128 uintptr_t limit = reinterpret_cast<uintptr_t>(spaces[i]->GetLimit());
129 if (!spaces[i]->IsImageSpace()) {
130 mark_bitmap_->ScanWalk(base, limit, &MarkSweep::ScanBitmapCallback, arg);
131 } else{
132 mark_bitmap_->ScanWalk(base, limit, &MarkSweep::CheckBitmapCallback, arg);
133 }
134#else
Elliott Hughes307f75d2011-10-12 18:04:40 -0700135 if (!spaces[i]->IsImageSpace()) {
Carl Shapiro58551df2011-07-24 03:09:51 -0700136 uintptr_t base = reinterpret_cast<uintptr_t>(spaces[i]->GetBase());
Ian Rogers5d76c432011-10-31 21:42:49 -0700137 uintptr_t limit = reinterpret_cast<uintptr_t>(spaces[i]->GetLimit());
138 mark_bitmap_->ScanWalk(base, limit, &MarkSweep::ScanBitmapCallback, arg);
Carl Shapiro58551df2011-07-24 03:09:51 -0700139 }
Ian Rogers5d76c432011-10-31 21:42:49 -0700140#endif
Carl Shapiro58551df2011-07-24 03:09:51 -0700141 }
142 finger_ = reinterpret_cast<Object*>(~0);
Ian Rogers5d76c432011-10-31 21:42:49 -0700143 // TODO: tune the frequency of emptying the mark stack
Carl Shapiro58551df2011-07-24 03:09:51 -0700144 ProcessMarkStack();
145}
146
147void MarkSweep::ReMarkRoots() {
Elliott Hughes53b61312011-08-12 18:28:20 -0700148 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700149}
150
Elliott Hughes410c0c82011-09-01 17:58:25 -0700151void MarkSweep::SweepJniWeakGlobals() {
152 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
153 MutexLock mu(vm->weak_globals_lock);
154 IndirectReferenceTable* table = &vm->weak_globals;
155 typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto
156 for (It it = table->begin(), end = table->end(); it != end; ++it) {
157 const Object** entry = *it;
158 if (!IsMarked(*entry)) {
159 *entry = kClearedJniWeakGlobal;
160 }
161 }
162}
163
Elliott Hughes410c0c82011-09-01 17:58:25 -0700164void MarkSweep::SweepSystemWeaks() {
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700165 Runtime::Current()->GetInternTable()->SweepInternTableWeaks(IsMarked, this);
166 Runtime::Current()->GetMonitorList()->SweepMonitorList(IsMarked, this);
Elliott Hughes410c0c82011-09-01 17:58:25 -0700167 SweepJniWeakGlobals();
Carl Shapiro58551df2011-07-24 03:09:51 -0700168}
169
Brian Carlstrom78128a62011-09-15 17:21:19 -0700170void MarkSweep::SweepCallback(size_t num_ptrs, void** ptrs, void* arg) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700171 // TODO: lock heap if concurrent
Elliott Hughes307f75d2011-10-12 18:04:40 -0700172 size_t freed_objects = num_ptrs;
173 size_t freed_bytes = 0;
Carl Shapiro58551df2011-07-24 03:09:51 -0700174 Space* space = static_cast<Space*>(arg);
Ian Rogers5d76c432011-10-31 21:42:49 -0700175 // Use a bulk free, that merges consecutive objects before freeing or free per object?
176 // Documentation suggests better free performance with merging, but this may be at the expensive
177 // of allocation.
178 // TODO: investigate performance
179 static const bool kFreeUsingMerge = true;
180 if (kFreeUsingMerge) {
181 freed_bytes = space->FreeList(num_ptrs, ptrs);
182 for (size_t i = 0; i < num_ptrs; ++i) {
183 Object* obj = static_cast<Object*>(ptrs[i]);
184 Heap::GetLiveBits()->Clear(obj);
185 }
186 } else {
187 for (size_t i = 0; i < num_ptrs; ++i) {
188 Object* obj = static_cast<Object*>(ptrs[i]);
189 Heap::GetLiveBits()->Clear(obj);
190 freed_bytes += space->Free(obj);
191 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700192 }
Elliott Hughes307f75d2011-10-12 18:04:40 -0700193 Heap::RecordFreeLocked(freed_objects, freed_bytes);
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700194 // TODO: unlock heap if concurrent
Carl Shapiro58551df2011-07-24 03:09:51 -0700195}
196
197void MarkSweep::Sweep() {
Elliott Hughes2da50362011-10-10 16:57:08 -0700198 SweepSystemWeaks();
199
Carl Shapiro58551df2011-07-24 03:09:51 -0700200 const std::vector<Space*>& spaces = Heap::GetSpaces();
201 for (size_t i = 0; i < spaces.size(); ++i) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700202 if (!spaces[i]->IsImageSpace()) {
Carl Shapiro58551df2011-07-24 03:09:51 -0700203 uintptr_t base = reinterpret_cast<uintptr_t>(spaces[i]->GetBase());
204 uintptr_t limit = reinterpret_cast<uintptr_t>(spaces[i]->GetLimit());
205 void* arg = static_cast<void*>(spaces[i]);
206 HeapBitmap::SweepWalk(*live_bitmap_, *mark_bitmap_, base, limit,
207 &MarkSweep::SweepCallback, arg);
208 }
209 }
210}
211
Carl Shapiro69759ea2011-07-21 18:13:35 -0700212// Scans instance fields.
Elliott Hughesb0663112011-10-19 18:16:37 -0700213inline void MarkSweep::ScanInstanceFields(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700214 DCHECK(obj != NULL);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700215 Class* klass = obj->GetClass();
216 DCHECK(klass != NULL);
Elliott Hughes2da50362011-10-10 16:57:08 -0700217 ScanFields(obj, klass->GetReferenceInstanceOffsets(), false);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700218}
219
Ian Rogers5d76c432011-10-31 21:42:49 -0700220inline void MarkSweep::CheckInstanceFields(const Object* obj) {
221 Class* klass = obj->GetClass();
222 CheckFields(obj, klass->GetReferenceInstanceOffsets(), false);
223}
224
Brian Carlstrom4873d462011-08-21 15:23:39 -0700225// Scans static storage on a Class.
Elliott Hughesb0663112011-10-19 18:16:37 -0700226inline void MarkSweep::ScanStaticFields(const Class* klass) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700227 DCHECK(klass != NULL);
Elliott Hughesadb460d2011-10-05 17:02:34 -0700228 ScanFields(klass, klass->GetReferenceStaticOffsets(), true);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700229}
230
Ian Rogers5d76c432011-10-31 21:42:49 -0700231inline void MarkSweep::CheckStaticFields(const Class* klass) {
232 CheckFields(klass, klass->GetReferenceStaticOffsets(), true);
233}
234
Elliott Hughesb0663112011-10-19 18:16:37 -0700235inline void MarkSweep::ScanFields(const Object* obj, uint32_t ref_offsets, bool is_static) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700236 if (ref_offsets != CLASS_WALK_SUPER) {
237 // Found a reference offset bitmap. Mark the specified offsets.
238 while (ref_offsets != 0) {
239 size_t right_shift = CLZ(ref_offsets);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700240 MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
241 const Object* ref = obj->GetFieldObject<const Object*>(byte_offset, false);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700242 MarkObject(ref);
243 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
244 }
245 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700246 // There is no reference offset bitmap. In the non-static case,
247 // walk up the class inheritance hierarchy and find reference
248 // offsets the hard way. In the static case, just consider this
249 // class.
250 for (const Class* klass = is_static ? obj->AsClass() : obj->GetClass();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700251 klass != NULL;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700252 klass = is_static ? NULL : klass->GetSuperClass()) {
253 size_t num_reference_fields = (is_static
254 ? klass->NumReferenceStaticFields()
255 : klass->NumReferenceInstanceFields());
256 for (size_t i = 0; i < num_reference_fields; ++i) {
257 Field* field = (is_static
258 ? klass->GetStaticField(i)
259 : klass->GetInstanceField(i));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700260 MemberOffset field_offset = field->GetOffset();
261 const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700262 MarkObject(ref);
263 }
264 }
265 }
266}
267
Ian Rogers5d76c432011-10-31 21:42:49 -0700268inline void MarkSweep::CheckReference(const Object* obj, const Object* ref, MemberOffset offset, bool is_static) {
269 Space* alloc_space = Heap::GetAllocSpace();
270 if (alloc_space->Contains(ref)) {
271 bool is_marked = mark_bitmap_->Test(ref);
272 if(!is_marked) {
273 LOG(INFO) << StringPrintf("Alloc space %p-%p (%s)", alloc_space->GetBase(), alloc_space->GetLimit(), alloc_space->GetName().c_str());
274 LOG(WARNING) << (is_static ? "Static ref'" : "Instance ref'") << PrettyTypeOf(ref) << "' (" << (void*)ref
275 << ") in '" << PrettyTypeOf(obj) << "' (" << (void*)obj << ") at offset "
276 << (void*)offset.Int32Value() << " wasn't marked";
277 bool obj_marked = Heap::GetCardTable()->IsDirty(obj);
278 if (!obj_marked) {
279 LOG(WARNING) << "Object '" << PrettyTypeOf(obj) << "' (" << (void*)obj
280 << ") contains references to the alloc space, but wasn't card marked";
281 }
282 }
283 }
284}
285
286inline void MarkSweep::CheckFields(const Object* obj, uint32_t ref_offsets, bool is_static) {
287 if (ref_offsets != CLASS_WALK_SUPER) {
288 // Found a reference offset bitmap. Mark the specified offsets.
289 while (ref_offsets != 0) {
290 size_t right_shift = CLZ(ref_offsets);
291 MemberOffset field_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
292 const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false);
293 CheckReference(obj, ref, field_offset, is_static);
294 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
295 }
296 } else {
297 // There is no reference offset bitmap. In the non-static case,
298 // walk up the class inheritance hierarchy and find reference
299 // offsets the hard way. In the static case, just consider this
300 // class.
301 for (const Class* klass = is_static ? obj->AsClass() : obj->GetClass();
302 klass != NULL;
303 klass = is_static ? NULL : klass->GetSuperClass()) {
304 size_t num_reference_fields = (is_static
305 ? klass->NumReferenceStaticFields()
306 : klass->NumReferenceInstanceFields());
307 for (size_t i = 0; i < num_reference_fields; ++i) {
308 Field* field = (is_static
309 ? klass->GetStaticField(i)
310 : klass->GetInstanceField(i));
311 MemberOffset field_offset = field->GetOffset();
312 const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false);
313 CheckReference(obj, ref, field_offset, is_static);
314 }
315 }
316 }
317}
318
Carl Shapiro69759ea2011-07-21 18:13:35 -0700319// Scans the header, static field references, and interface pointers
320// of a class object.
Elliott Hughesb0663112011-10-19 18:16:37 -0700321inline void MarkSweep::ScanClass(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700322#ifndef NDEBUG
323 ++class_count_;
324#endif
Brian Carlstrom693267a2011-09-06 09:25:34 -0700325 ScanInstanceFields(obj);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700326 ScanStaticFields(obj->AsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700327}
328
Ian Rogers5d76c432011-10-31 21:42:49 -0700329inline void MarkSweep::CheckClass(const Object* obj) {
330 CheckInstanceFields(obj);
331 CheckStaticFields(obj->AsClass());
332}
333
Carl Shapiro69759ea2011-07-21 18:13:35 -0700334// Scans the header of all array objects. If the array object is
335// specialized to a reference type, scans the array data as well.
Elliott Hughesb0663112011-10-19 18:16:37 -0700336inline void MarkSweep::ScanArray(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700337#ifndef NDEBUG
338 ++array_count_;
339#endif
Carl Shapiro69759ea2011-07-21 18:13:35 -0700340 MarkObject(obj->GetClass());
341 if (obj->IsObjectArray()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700342 const ObjectArray<Object>* array = obj->AsObjectArray<Object>();
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700343 for (int32_t i = 0; i < array->GetLength(); ++i) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700344 const Object* element = array->GetWithoutChecks(i);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700345 MarkObject(element);
346 }
347 }
348}
349
Ian Rogers5d76c432011-10-31 21:42:49 -0700350inline void MarkSweep::CheckArray(const Object* obj) {
351 CheckReference(obj, obj->GetClass(), Object::ClassOffset(), false);
352 if (obj->IsObjectArray()) {
353 const ObjectArray<Object>* array = obj->AsObjectArray<Object>();
354 for (int32_t i = 0; i < array->GetLength(); ++i) {
355 const Object* element = array->GetWithoutChecks(i);
356 CheckReference(obj, element, MemberOffset(i * sizeof(Object*) +
357 Array::DataOffset().Int32Value()), false);
358 }
359 }
360}
361
Carl Shapiro69759ea2011-07-21 18:13:35 -0700362// Process the "referent" field in a java.lang.ref.Reference. If the
363// referent has not yet been marked, put it on the appropriate list in
364// the gcHeap for later processing.
365void MarkSweep::DelayReferenceReferent(Object* obj) {
366 DCHECK(obj != NULL);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700367 Class* klass = obj->GetClass();
368 DCHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700369 DCHECK(klass->IsReferenceClass());
370 Object* pending = obj->GetFieldObject<Object*>(Heap::GetReferencePendingNextOffset(), false);
Elliott Hughesadb460d2011-10-05 17:02:34 -0700371 Object* referent = Heap::GetReferenceReferent(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700372 if (pending == NULL && referent != NULL && !IsMarked(referent)) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700373 Object** list = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700374 if (klass->IsSoftReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700375 list = &soft_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700376 } else if (klass->IsWeakReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700377 list = &weak_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700378 } else if (klass->IsFinalizerReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700379 list = &finalizer_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700380 } else if (klass->IsPhantomReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700381 list = &phantom_reference_list_;
382 }
Brian Carlstrom0796af02011-10-12 14:31:45 -0700383 DCHECK(list != NULL) << PrettyClass(klass) << " " << std::hex << klass->GetAccessFlags();
Elliott Hughesadb460d2011-10-05 17:02:34 -0700384 Heap::EnqueuePendingReference(obj, list);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700385 }
386}
387
388// Scans the header and field references of a data object. If the
389// scanned object is a reference subclass, it is scheduled for later
Elliott Hughesadb460d2011-10-05 17:02:34 -0700390// processing.
Elliott Hughesb0663112011-10-19 18:16:37 -0700391inline void MarkSweep::ScanOther(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700392#ifndef NDEBUG
393 ++other_count_;
394#endif
Carl Shapiro69759ea2011-07-21 18:13:35 -0700395 ScanInstanceFields(obj);
Elliott Hughes352a4242011-10-31 15:15:21 -0700396 if (obj->GetClass()->IsReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700397 DelayReferenceReferent(const_cast<Object*>(obj));
398 }
399}
400
Ian Rogers5d76c432011-10-31 21:42:49 -0700401inline void MarkSweep::CheckOther(const Object* obj) {
402 CheckInstanceFields(obj);
403}
404
Carl Shapiro69759ea2011-07-21 18:13:35 -0700405// Scans an object reference. Determines the type of the reference
406// and dispatches to a specialized scanning routine.
Elliott Hughesb0663112011-10-19 18:16:37 -0700407inline void MarkSweep::ScanObject(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700408 DCHECK(obj != NULL);
409 DCHECK(obj->GetClass() != NULL);
410 DCHECK(IsMarked(obj));
411 if (obj->IsClass()) {
412 ScanClass(obj);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700413 } else if (obj->IsArrayInstance()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700414 ScanArray(obj);
415 } else {
Carl Shapiro58551df2011-07-24 03:09:51 -0700416 ScanOther(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700417 }
418}
419
Ian Rogers5d76c432011-10-31 21:42:49 -0700420// Check to see that all alloc space references are marked for the given object
421inline void MarkSweep::CheckObject(const Object* obj) {
422 DCHECK(obj != NULL);
423 DCHECK(obj->GetClass() != NULL);
424 DCHECK(IsMarked(obj));
425 if (obj->IsClass()) {
426 CheckClass(obj);
427 } else if (obj->IsArrayInstance()) {
428 CheckArray(obj);
429 } else {
430 CheckOther(obj);
431 }
432}
433
434// Scan anything that's on the mark stack.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700435void MarkSweep::ProcessMarkStack() {
Ian Rogers5d76c432011-10-31 21:42:49 -0700436 Space* alloc_space = Heap::GetAllocSpace();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700437 while (!mark_stack_->IsEmpty()) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700438 const Object* obj = mark_stack_->Pop();
Ian Rogers5d76c432011-10-31 21:42:49 -0700439 if (alloc_space->Contains(obj)) {
440 ScanObject(obj);
441 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700442 }
443}
444
445void MarkSweep::ScanDirtyObjects() {
446 ProcessMarkStack();
447}
448
Carl Shapiro69759ea2011-07-21 18:13:35 -0700449// Walks the reference list marking any references subject to the
450// reference clearing policy. References with a black referent are
451// removed from the list. References with white referents biased
452// toward saving are blackened and also removed from the list.
453void MarkSweep::PreserveSomeSoftReferences(Object** list) {
454 DCHECK(list != NULL);
455 Object* clear = NULL;
456 size_t counter = 0;
457 while (*list != NULL) {
Elliott Hughesadb460d2011-10-05 17:02:34 -0700458 Object* ref = Heap::DequeuePendingReference(list);
459 Object* referent = Heap::GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700460 if (referent == NULL) {
461 // Referent was cleared by the user during marking.
462 continue;
463 }
464 bool is_marked = IsMarked(referent);
465 if (!is_marked && ((++counter) & 1)) {
466 // Referent is white and biased toward saving, mark it.
467 MarkObject(referent);
468 is_marked = true;
469 }
470 if (!is_marked) {
471 // Referent is white, queue it for clearing.
Elliott Hughesadb460d2011-10-05 17:02:34 -0700472 Heap::EnqueuePendingReference(ref, &clear);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700473 }
474 }
475 *list = clear;
476 // Restart the mark with the newly black references added to the
477 // root set.
478 ProcessMarkStack();
479}
480
481// Unlink the reference list clearing references objects with white
482// referents. Cleared references registered to a reference queue are
483// scheduled for appending by the heap worker thread.
484void MarkSweep::ClearWhiteReferences(Object** list) {
485 DCHECK(list != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700486 while (*list != NULL) {
Elliott Hughesadb460d2011-10-05 17:02:34 -0700487 Object* ref = Heap::DequeuePendingReference(list);
488 Object* referent = Heap::GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700489 if (referent != NULL && !IsMarked(referent)) {
490 // Referent is white, clear it.
Elliott Hughesadb460d2011-10-05 17:02:34 -0700491 Heap::ClearReferenceReferent(ref);
492 if (Heap::IsEnqueuable(ref)) {
493 Heap::EnqueueReference(ref, &cleared_reference_list_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700494 }
495 }
496 }
497 DCHECK(*list == NULL);
498}
499
500// Enqueues finalizer references with white referents. White
501// referents are blackened, moved to the zombie field, and the
502// referent field is cleared.
503void MarkSweep::EnqueueFinalizerReferences(Object** list) {
504 DCHECK(list != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700505 MemberOffset zombie_offset = Heap::GetFinalizerReferenceZombieOffset();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700506 bool has_enqueued = false;
507 while (*list != NULL) {
Elliott Hughesadb460d2011-10-05 17:02:34 -0700508 Object* ref = Heap::DequeuePendingReference(list);
509 Object* referent = Heap::GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700510 if (referent != NULL && !IsMarked(referent)) {
511 MarkObject(referent);
512 // If the referent is non-null the reference must queuable.
Elliott Hughesadb460d2011-10-05 17:02:34 -0700513 DCHECK(Heap::IsEnqueuable(ref));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700514 ref->SetFieldObject(zombie_offset, referent, false);
Elliott Hughesadb460d2011-10-05 17:02:34 -0700515 Heap::ClearReferenceReferent(ref);
516 Heap::EnqueueReference(ref, &cleared_reference_list_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700517 has_enqueued = true;
518 }
519 }
520 if (has_enqueued) {
521 ProcessMarkStack();
522 }
523 DCHECK(*list == NULL);
524}
525
Carl Shapiro58551df2011-07-24 03:09:51 -0700526// Process reference class instances and schedule finalizations.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700527void MarkSweep::ProcessReferences(Object** soft_references, bool clear_soft,
528 Object** weak_references,
529 Object** finalizer_references,
530 Object** phantom_references) {
531 DCHECK(soft_references != NULL);
532 DCHECK(weak_references != NULL);
533 DCHECK(finalizer_references != NULL);
534 DCHECK(phantom_references != NULL);
535
536 // Unless we are in the zygote or required to clear soft references
537 // with white references, preserve some white referents.
538 if (clear_soft) {
539 PreserveSomeSoftReferences(soft_references);
540 }
541
542 // Clear all remaining soft and weak references with white
543 // referents.
544 ClearWhiteReferences(soft_references);
545 ClearWhiteReferences(weak_references);
546
547 // Preserve all white objects with finalize methods and schedule
548 // them for finalization.
549 EnqueueFinalizerReferences(finalizer_references);
550
551 // Clear all f-reachable soft and weak references with white
552 // referents.
553 ClearWhiteReferences(soft_references);
554 ClearWhiteReferences(weak_references);
555
556 // Clear all phantom references with white referents.
557 ClearWhiteReferences(phantom_references);
558
559 // At this point all reference lists should be empty.
560 DCHECK(*soft_references == NULL);
561 DCHECK(*weak_references == NULL);
562 DCHECK(*finalizer_references == NULL);
563 DCHECK(*phantom_references == NULL);
564}
565
Carl Shapiro69759ea2011-07-21 18:13:35 -0700566MarkSweep::~MarkSweep() {
Elliott Hughes352a4242011-10-31 15:15:21 -0700567#ifndef NDEBUG
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800568 VLOG(heap) << "MarkSweep scanned classes=" << class_count_ << " arrays=" << array_count_ << " other=" << other_count_;
Elliott Hughes352a4242011-10-31 15:15:21 -0700569#endif
Carl Shapiro58551df2011-07-24 03:09:51 -0700570 delete mark_stack_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700571 mark_bitmap_->Clear();
572}
573
574} // namespace art