blob: cc1dcde9613296d230fb50b1da8e15134acbe917 [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()) {
Ian Rogers30fab402012-01-23 15:43:46 -080093 byte* begin = spaces[i]->Begin();
94 byte* end = spaces[i]->End();
95 card_table->Scan(begin, end, ScanImageRootVisitor, this);
Ian Rogers5d76c432011-10-31 21:42:49 -070096 }
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
Ian Rogers30fab402012-01-23 15:43:46 -0800127 uintptr_t begin = reinterpret_cast<uintptr_t>(spaces[i]->Begin());
128 uintptr_t end = reinterpret_cast<uintptr_t>(spaces[i]->End());
Ian Rogers5d76c432011-10-31 21:42:49 -0700129 if (!spaces[i]->IsImageSpace()) {
Ian Rogers30fab402012-01-23 15:43:46 -0800130 mark_bitmap_->ScanWalk(begin, end, &MarkSweep::ScanBitmapCallback, arg);
Ian Rogers5d76c432011-10-31 21:42:49 -0700131 } else{
Ian Rogers30fab402012-01-23 15:43:46 -0800132 mark_bitmap_->ScanWalk(begin, end, &MarkSweep::CheckBitmapCallback, arg);
Ian Rogers5d76c432011-10-31 21:42:49 -0700133 }
134#else
Elliott Hughes307f75d2011-10-12 18:04:40 -0700135 if (!spaces[i]->IsImageSpace()) {
Ian Rogers30fab402012-01-23 15:43:46 -0800136 uintptr_t begin = reinterpret_cast<uintptr_t>(spaces[i]->Begin());
137 uintptr_t end = reinterpret_cast<uintptr_t>(spaces[i]->End());
138 mark_bitmap_->ScanWalk(begin, end, &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
Ian Rogers30fab402012-01-23 15:43:46 -0800170void MarkSweep::SweepCallback(size_t num_ptrs, Object** 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;
Ian Rogers30fab402012-01-23 15:43:46 -0800174 AllocSpace* space = static_cast<AllocSpace*>(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
Ian Rogers30fab402012-01-23 15:43:46 -0800179 static const bool kUseFreeList = true;
180 if (kUseFreeList) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700181 for (size_t i = 0; i < num_ptrs; ++i) {
182 Object* obj = static_cast<Object*>(ptrs[i]);
Ian Rogers30fab402012-01-23 15:43:46 -0800183 freed_bytes += space->AllocationSize(obj);
Ian Rogers5d76c432011-10-31 21:42:49 -0700184 Heap::GetLiveBits()->Clear(obj);
185 }
Ian Rogers30fab402012-01-23 15:43:46 -0800186 // AllocSpace::FreeList clears the value in ptrs, so perform after clearing the live bit
187 space->FreeList(num_ptrs, ptrs);
Ian Rogers5d76c432011-10-31 21:42:49 -0700188 } else {
189 for (size_t i = 0; i < num_ptrs; ++i) {
190 Object* obj = static_cast<Object*>(ptrs[i]);
Ian Rogers30fab402012-01-23 15:43:46 -0800191 freed_bytes += space->AllocationSize(obj);
Ian Rogers5d76c432011-10-31 21:42:49 -0700192 Heap::GetLiveBits()->Clear(obj);
Ian Rogers30fab402012-01-23 15:43:46 -0800193 space->Free(obj);
Ian Rogers5d76c432011-10-31 21:42:49 -0700194 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700195 }
Elliott Hughes307f75d2011-10-12 18:04:40 -0700196 Heap::RecordFreeLocked(freed_objects, freed_bytes);
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700197 // TODO: unlock heap if concurrent
Carl Shapiro58551df2011-07-24 03:09:51 -0700198}
199
200void MarkSweep::Sweep() {
Elliott Hughes2da50362011-10-10 16:57:08 -0700201 SweepSystemWeaks();
202
Carl Shapiro58551df2011-07-24 03:09:51 -0700203 const std::vector<Space*>& spaces = Heap::GetSpaces();
204 for (size_t i = 0; i < spaces.size(); ++i) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700205 if (!spaces[i]->IsImageSpace()) {
Ian Rogers30fab402012-01-23 15:43:46 -0800206 uintptr_t begin = reinterpret_cast<uintptr_t>(spaces[i]->Begin());
207 uintptr_t end = reinterpret_cast<uintptr_t>(spaces[i]->End());
Carl Shapiro58551df2011-07-24 03:09:51 -0700208 void* arg = static_cast<void*>(spaces[i]);
Ian Rogers30fab402012-01-23 15:43:46 -0800209 HeapBitmap::SweepWalk(*live_bitmap_, *mark_bitmap_, begin, end,
Carl Shapiro58551df2011-07-24 03:09:51 -0700210 &MarkSweep::SweepCallback, arg);
211 }
212 }
213}
214
Carl Shapiro69759ea2011-07-21 18:13:35 -0700215// Scans instance fields.
Elliott Hughesb0663112011-10-19 18:16:37 -0700216inline void MarkSweep::ScanInstanceFields(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700217 DCHECK(obj != NULL);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700218 Class* klass = obj->GetClass();
219 DCHECK(klass != NULL);
Elliott Hughes2da50362011-10-10 16:57:08 -0700220 ScanFields(obj, klass->GetReferenceInstanceOffsets(), false);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700221}
222
Ian Rogers5d76c432011-10-31 21:42:49 -0700223inline void MarkSweep::CheckInstanceFields(const Object* obj) {
224 Class* klass = obj->GetClass();
225 CheckFields(obj, klass->GetReferenceInstanceOffsets(), false);
226}
227
Brian Carlstrom4873d462011-08-21 15:23:39 -0700228// Scans static storage on a Class.
Elliott Hughesb0663112011-10-19 18:16:37 -0700229inline void MarkSweep::ScanStaticFields(const Class* klass) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700230 DCHECK(klass != NULL);
Elliott Hughesadb460d2011-10-05 17:02:34 -0700231 ScanFields(klass, klass->GetReferenceStaticOffsets(), true);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700232}
233
Ian Rogers5d76c432011-10-31 21:42:49 -0700234inline void MarkSweep::CheckStaticFields(const Class* klass) {
235 CheckFields(klass, klass->GetReferenceStaticOffsets(), true);
236}
237
Elliott Hughesb0663112011-10-19 18:16:37 -0700238inline void MarkSweep::ScanFields(const Object* obj, uint32_t ref_offsets, bool is_static) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700239 if (ref_offsets != CLASS_WALK_SUPER) {
240 // Found a reference offset bitmap. Mark the specified offsets.
241 while (ref_offsets != 0) {
242 size_t right_shift = CLZ(ref_offsets);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700243 MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
244 const Object* ref = obj->GetFieldObject<const Object*>(byte_offset, false);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700245 MarkObject(ref);
246 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
247 }
248 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700249 // There is no reference offset bitmap. In the non-static case,
250 // walk up the class inheritance hierarchy and find reference
251 // offsets the hard way. In the static case, just consider this
252 // class.
253 for (const Class* klass = is_static ? obj->AsClass() : obj->GetClass();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700254 klass != NULL;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700255 klass = is_static ? NULL : klass->GetSuperClass()) {
256 size_t num_reference_fields = (is_static
257 ? klass->NumReferenceStaticFields()
258 : klass->NumReferenceInstanceFields());
259 for (size_t i = 0; i < num_reference_fields; ++i) {
260 Field* field = (is_static
261 ? klass->GetStaticField(i)
262 : klass->GetInstanceField(i));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700263 MemberOffset field_offset = field->GetOffset();
264 const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700265 MarkObject(ref);
266 }
267 }
268 }
269}
270
Ian Rogers5d76c432011-10-31 21:42:49 -0700271inline void MarkSweep::CheckReference(const Object* obj, const Object* ref, MemberOffset offset, bool is_static) {
Ian Rogers30fab402012-01-23 15:43:46 -0800272 AllocSpace* alloc_space = Heap::GetAllocSpace();
Ian Rogers5d76c432011-10-31 21:42:49 -0700273 if (alloc_space->Contains(ref)) {
274 bool is_marked = mark_bitmap_->Test(ref);
275 if(!is_marked) {
Ian Rogers30fab402012-01-23 15:43:46 -0800276 LOG(INFO) << *alloc_space;
277 LOG(WARNING) << (is_static ? "Static ref'" : "Instance ref'") << PrettyTypeOf(ref)
278 << "' (" << (void*)ref << ") in '" << PrettyTypeOf(obj)
279 << "' (" << (void*)obj << ") at offset "
280 << (void*)offset.Int32Value() << " wasn't marked";
Ian Rogers5d76c432011-10-31 21:42:49 -0700281 bool obj_marked = Heap::GetCardTable()->IsDirty(obj);
282 if (!obj_marked) {
283 LOG(WARNING) << "Object '" << PrettyTypeOf(obj) << "' (" << (void*)obj
284 << ") contains references to the alloc space, but wasn't card marked";
285 }
286 }
287 }
288}
289
290inline void MarkSweep::CheckFields(const Object* obj, uint32_t ref_offsets, bool is_static) {
291 if (ref_offsets != CLASS_WALK_SUPER) {
292 // Found a reference offset bitmap. Mark the specified offsets.
293 while (ref_offsets != 0) {
294 size_t right_shift = CLZ(ref_offsets);
295 MemberOffset field_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
296 const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false);
297 CheckReference(obj, ref, field_offset, is_static);
298 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
299 }
300 } else {
301 // There is no reference offset bitmap. In the non-static case,
302 // walk up the class inheritance hierarchy and find reference
303 // offsets the hard way. In the static case, just consider this
304 // class.
305 for (const Class* klass = is_static ? obj->AsClass() : obj->GetClass();
306 klass != NULL;
307 klass = is_static ? NULL : klass->GetSuperClass()) {
308 size_t num_reference_fields = (is_static
309 ? klass->NumReferenceStaticFields()
310 : klass->NumReferenceInstanceFields());
311 for (size_t i = 0; i < num_reference_fields; ++i) {
312 Field* field = (is_static
313 ? klass->GetStaticField(i)
314 : klass->GetInstanceField(i));
315 MemberOffset field_offset = field->GetOffset();
316 const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false);
317 CheckReference(obj, ref, field_offset, is_static);
318 }
319 }
320 }
321}
322
Carl Shapiro69759ea2011-07-21 18:13:35 -0700323// Scans the header, static field references, and interface pointers
324// of a class object.
Elliott Hughesb0663112011-10-19 18:16:37 -0700325inline void MarkSweep::ScanClass(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700326#ifndef NDEBUG
327 ++class_count_;
328#endif
Brian Carlstrom693267a2011-09-06 09:25:34 -0700329 ScanInstanceFields(obj);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700330 ScanStaticFields(obj->AsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700331}
332
Ian Rogers5d76c432011-10-31 21:42:49 -0700333inline void MarkSweep::CheckClass(const Object* obj) {
334 CheckInstanceFields(obj);
335 CheckStaticFields(obj->AsClass());
336}
337
Carl Shapiro69759ea2011-07-21 18:13:35 -0700338// Scans the header of all array objects. If the array object is
339// specialized to a reference type, scans the array data as well.
Elliott Hughesb0663112011-10-19 18:16:37 -0700340inline void MarkSweep::ScanArray(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700341#ifndef NDEBUG
342 ++array_count_;
343#endif
Carl Shapiro69759ea2011-07-21 18:13:35 -0700344 MarkObject(obj->GetClass());
345 if (obj->IsObjectArray()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700346 const ObjectArray<Object>* array = obj->AsObjectArray<Object>();
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700347 for (int32_t i = 0; i < array->GetLength(); ++i) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700348 const Object* element = array->GetWithoutChecks(i);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700349 MarkObject(element);
350 }
351 }
352}
353
Ian Rogers5d76c432011-10-31 21:42:49 -0700354inline void MarkSweep::CheckArray(const Object* obj) {
355 CheckReference(obj, obj->GetClass(), Object::ClassOffset(), false);
356 if (obj->IsObjectArray()) {
357 const ObjectArray<Object>* array = obj->AsObjectArray<Object>();
358 for (int32_t i = 0; i < array->GetLength(); ++i) {
359 const Object* element = array->GetWithoutChecks(i);
360 CheckReference(obj, element, MemberOffset(i * sizeof(Object*) +
361 Array::DataOffset().Int32Value()), false);
362 }
363 }
364}
365
Carl Shapiro69759ea2011-07-21 18:13:35 -0700366// Process the "referent" field in a java.lang.ref.Reference. If the
367// referent has not yet been marked, put it on the appropriate list in
368// the gcHeap for later processing.
369void MarkSweep::DelayReferenceReferent(Object* obj) {
370 DCHECK(obj != NULL);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700371 Class* klass = obj->GetClass();
372 DCHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700373 DCHECK(klass->IsReferenceClass());
374 Object* pending = obj->GetFieldObject<Object*>(Heap::GetReferencePendingNextOffset(), false);
Elliott Hughesadb460d2011-10-05 17:02:34 -0700375 Object* referent = Heap::GetReferenceReferent(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700376 if (pending == NULL && referent != NULL && !IsMarked(referent)) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700377 Object** list = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700378 if (klass->IsSoftReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700379 list = &soft_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700380 } else if (klass->IsWeakReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700381 list = &weak_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700382 } else if (klass->IsFinalizerReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700383 list = &finalizer_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700384 } else if (klass->IsPhantomReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700385 list = &phantom_reference_list_;
386 }
Brian Carlstrom0796af02011-10-12 14:31:45 -0700387 DCHECK(list != NULL) << PrettyClass(klass) << " " << std::hex << klass->GetAccessFlags();
Elliott Hughesadb460d2011-10-05 17:02:34 -0700388 Heap::EnqueuePendingReference(obj, list);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700389 }
390}
391
392// Scans the header and field references of a data object. If the
393// scanned object is a reference subclass, it is scheduled for later
Elliott Hughesadb460d2011-10-05 17:02:34 -0700394// processing.
Elliott Hughesb0663112011-10-19 18:16:37 -0700395inline void MarkSweep::ScanOther(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700396#ifndef NDEBUG
397 ++other_count_;
398#endif
Carl Shapiro69759ea2011-07-21 18:13:35 -0700399 ScanInstanceFields(obj);
Elliott Hughes352a4242011-10-31 15:15:21 -0700400 if (obj->GetClass()->IsReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700401 DelayReferenceReferent(const_cast<Object*>(obj));
402 }
403}
404
Ian Rogers5d76c432011-10-31 21:42:49 -0700405inline void MarkSweep::CheckOther(const Object* obj) {
406 CheckInstanceFields(obj);
407}
408
Carl Shapiro69759ea2011-07-21 18:13:35 -0700409// Scans an object reference. Determines the type of the reference
410// and dispatches to a specialized scanning routine.
Elliott Hughesb0663112011-10-19 18:16:37 -0700411inline void MarkSweep::ScanObject(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700412 DCHECK(obj != NULL);
413 DCHECK(obj->GetClass() != NULL);
414 DCHECK(IsMarked(obj));
415 if (obj->IsClass()) {
416 ScanClass(obj);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700417 } else if (obj->IsArrayInstance()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700418 ScanArray(obj);
419 } else {
Carl Shapiro58551df2011-07-24 03:09:51 -0700420 ScanOther(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700421 }
422}
423
Ian Rogers5d76c432011-10-31 21:42:49 -0700424// Check to see that all alloc space references are marked for the given object
425inline void MarkSweep::CheckObject(const Object* obj) {
426 DCHECK(obj != NULL);
427 DCHECK(obj->GetClass() != NULL);
428 DCHECK(IsMarked(obj));
429 if (obj->IsClass()) {
430 CheckClass(obj);
431 } else if (obj->IsArrayInstance()) {
432 CheckArray(obj);
433 } else {
434 CheckOther(obj);
435 }
436}
437
438// Scan anything that's on the mark stack.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700439void MarkSweep::ProcessMarkStack() {
Ian Rogers5d76c432011-10-31 21:42:49 -0700440 Space* alloc_space = Heap::GetAllocSpace();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700441 while (!mark_stack_->IsEmpty()) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700442 const Object* obj = mark_stack_->Pop();
Ian Rogers5d76c432011-10-31 21:42:49 -0700443 if (alloc_space->Contains(obj)) {
444 ScanObject(obj);
445 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700446 }
447}
448
449void MarkSweep::ScanDirtyObjects() {
450 ProcessMarkStack();
451}
452
Carl Shapiro69759ea2011-07-21 18:13:35 -0700453// Walks the reference list marking any references subject to the
454// reference clearing policy. References with a black referent are
455// removed from the list. References with white referents biased
456// toward saving are blackened and also removed from the list.
457void MarkSweep::PreserveSomeSoftReferences(Object** list) {
458 DCHECK(list != NULL);
459 Object* clear = NULL;
460 size_t counter = 0;
461 while (*list != NULL) {
Elliott Hughesadb460d2011-10-05 17:02:34 -0700462 Object* ref = Heap::DequeuePendingReference(list);
463 Object* referent = Heap::GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700464 if (referent == NULL) {
465 // Referent was cleared by the user during marking.
466 continue;
467 }
468 bool is_marked = IsMarked(referent);
469 if (!is_marked && ((++counter) & 1)) {
470 // Referent is white and biased toward saving, mark it.
471 MarkObject(referent);
472 is_marked = true;
473 }
474 if (!is_marked) {
475 // Referent is white, queue it for clearing.
Elliott Hughesadb460d2011-10-05 17:02:34 -0700476 Heap::EnqueuePendingReference(ref, &clear);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700477 }
478 }
479 *list = clear;
480 // Restart the mark with the newly black references added to the
481 // root set.
482 ProcessMarkStack();
483}
484
485// Unlink the reference list clearing references objects with white
486// referents. Cleared references registered to a reference queue are
487// scheduled for appending by the heap worker thread.
488void MarkSweep::ClearWhiteReferences(Object** list) {
489 DCHECK(list != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700490 while (*list != NULL) {
Elliott Hughesadb460d2011-10-05 17:02:34 -0700491 Object* ref = Heap::DequeuePendingReference(list);
492 Object* referent = Heap::GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700493 if (referent != NULL && !IsMarked(referent)) {
494 // Referent is white, clear it.
Elliott Hughesadb460d2011-10-05 17:02:34 -0700495 Heap::ClearReferenceReferent(ref);
496 if (Heap::IsEnqueuable(ref)) {
497 Heap::EnqueueReference(ref, &cleared_reference_list_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700498 }
499 }
500 }
501 DCHECK(*list == NULL);
502}
503
504// Enqueues finalizer references with white referents. White
505// referents are blackened, moved to the zombie field, and the
506// referent field is cleared.
507void MarkSweep::EnqueueFinalizerReferences(Object** list) {
508 DCHECK(list != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700509 MemberOffset zombie_offset = Heap::GetFinalizerReferenceZombieOffset();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700510 bool has_enqueued = false;
511 while (*list != NULL) {
Elliott Hughesadb460d2011-10-05 17:02:34 -0700512 Object* ref = Heap::DequeuePendingReference(list);
513 Object* referent = Heap::GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700514 if (referent != NULL && !IsMarked(referent)) {
515 MarkObject(referent);
516 // If the referent is non-null the reference must queuable.
Elliott Hughesadb460d2011-10-05 17:02:34 -0700517 DCHECK(Heap::IsEnqueuable(ref));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700518 ref->SetFieldObject(zombie_offset, referent, false);
Elliott Hughesadb460d2011-10-05 17:02:34 -0700519 Heap::ClearReferenceReferent(ref);
520 Heap::EnqueueReference(ref, &cleared_reference_list_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700521 has_enqueued = true;
522 }
523 }
524 if (has_enqueued) {
525 ProcessMarkStack();
526 }
527 DCHECK(*list == NULL);
528}
529
Carl Shapiro58551df2011-07-24 03:09:51 -0700530// Process reference class instances and schedule finalizations.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700531void MarkSweep::ProcessReferences(Object** soft_references, bool clear_soft,
532 Object** weak_references,
533 Object** finalizer_references,
534 Object** phantom_references) {
535 DCHECK(soft_references != NULL);
536 DCHECK(weak_references != NULL);
537 DCHECK(finalizer_references != NULL);
538 DCHECK(phantom_references != NULL);
539
540 // Unless we are in the zygote or required to clear soft references
541 // with white references, preserve some white referents.
542 if (clear_soft) {
543 PreserveSomeSoftReferences(soft_references);
544 }
545
546 // Clear all remaining soft and weak references with white
547 // referents.
548 ClearWhiteReferences(soft_references);
549 ClearWhiteReferences(weak_references);
550
551 // Preserve all white objects with finalize methods and schedule
552 // them for finalization.
553 EnqueueFinalizerReferences(finalizer_references);
554
555 // Clear all f-reachable soft and weak references with white
556 // referents.
557 ClearWhiteReferences(soft_references);
558 ClearWhiteReferences(weak_references);
559
560 // Clear all phantom references with white referents.
561 ClearWhiteReferences(phantom_references);
562
563 // At this point all reference lists should be empty.
564 DCHECK(*soft_references == NULL);
565 DCHECK(*weak_references == NULL);
566 DCHECK(*finalizer_references == NULL);
567 DCHECK(*phantom_references == NULL);
568}
569
Carl Shapiro69759ea2011-07-21 18:13:35 -0700570MarkSweep::~MarkSweep() {
Elliott Hughes352a4242011-10-31 15:15:21 -0700571#ifndef NDEBUG
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800572 VLOG(heap) << "MarkSweep scanned classes=" << class_count_ << " arrays=" << array_count_ << " other=" << other_count_;
Elliott Hughes352a4242011-10-31 15:15:21 -0700573#endif
Carl Shapiro58551df2011-07-24 03:09:51 -0700574 delete mark_stack_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700575 mark_bitmap_->Clear();
576}
577
578} // namespace art