blob: 06a1c920553ed4d39a300632ed8a8751969201d7 [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
8#include "heap.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07009#include "logging.h"
10#include "macros.h"
11#include "mark_stack.h"
12#include "object.h"
Carl Shapiro58551df2011-07-24 03:09:51 -070013#include "space.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070014#include "thread.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070015
Carl Shapiro69759ea2011-07-21 18:13:35 -070016namespace art {
17
18size_t MarkSweep::reference_referent_offset_ = 0; // TODO
19size_t MarkSweep::reference_queue_offset_ = 0; // TODO
20size_t MarkSweep::reference_queueNext_offset_ = 0; // TODO
21size_t MarkSweep::reference_pendingNext_offset_ = 0; // TODO
22size_t MarkSweep::finalizer_reference_zombie_offset_ = 0; // TODO
23
Carl Shapiro58551df2011-07-24 03:09:51 -070024bool MarkSweep::Init() {
25 mark_stack_ = MarkStack::Create(Heap::GetMaximumSize());
26 if (mark_stack_ == NULL) {
27 return false;
28 }
29
30 mark_bitmap_ = Heap::GetMarkBits();
31 live_bitmap_ = Heap::GetLiveBits();
32
33 // TODO: if concurrent, clear the card table.
34
35 // TODO: check that the mark bitmap is entirely clear.
36
37 return true;
38}
39
Carl Shapiro69759ea2011-07-21 18:13:35 -070040void MarkSweep::MarkObject0(const Object* obj, bool check_finger) {
41 DCHECK(obj != NULL);
42 if (obj < condemned_) {
43 DCHECK(IsMarked(obj));
44 return;
45 }
46 bool is_marked = mark_bitmap_->Test(obj);
47 // This object was not previously marked.
48 if (!is_marked) {
49 mark_bitmap_->Set(obj);
50 if (check_finger && obj < finger_) {
51 // The object must be pushed on to the mark stack.
52 mark_stack_->Push(obj);
53 }
54 }
55}
56
57// Used to mark objects when recursing. Recursion is done by moving
58// the finger across the bitmaps in address order and marking child
59// objects. Any newly-marked objects whose addresses are lower than
60// the finger won't be visited by the bitmap scan, so those objects
61// need to be added to the mark stack.
62void MarkSweep::MarkObject(const Object* obj) {
63 if (obj != NULL) {
64 MarkObject0(obj, true);
65 }
66}
67
68// Marks all objects in the root set.
69void MarkSweep::MarkRoots() {
Elliott Hughes53b61312011-08-12 18:28:20 -070070 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -070071}
72
Carl Shapiro58551df2011-07-24 03:09:51 -070073void MarkSweep::ScanBitmapCallback(Object* obj, void* finger, void* arg) {
74 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
75 mark_sweep->finger_ = reinterpret_cast<Object*>(finger);
76 mark_sweep->ScanObject(obj);
77}
78
79// Populates the mark stack based on the set of marked objects and
80// recursively marks until the mark stack is emptied.
81void MarkSweep::RecursiveMark() {
82 void* arg = reinterpret_cast<void*>(this);
83 const std::vector<Space*>& spaces = Heap::GetSpaces();
84 for (size_t i = 0; i < spaces.size(); ++i) {
85 if (spaces[i]->IsCondemned()) {
86 uintptr_t base = reinterpret_cast<uintptr_t>(spaces[i]->GetBase());
87 uintptr_t limit = reinterpret_cast<uintptr_t>(spaces[i]->GetLimit());
88 mark_bitmap_->ScanWalk(base, limit, &MarkSweep::ScanBitmapCallback, arg);
89 }
90 }
91 finger_ = reinterpret_cast<Object*>(~0);
92 ProcessMarkStack();
93}
94
95void MarkSweep::ReMarkRoots() {
Elliott Hughes53b61312011-08-12 18:28:20 -070096 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -070097}
98
Carl Shapiro58551df2011-07-24 03:09:51 -070099void MarkSweep::SweepSystemWeaks() {
Elliott Hughes53b61312011-08-12 18:28:20 -0700100 UNIMPLEMENTED(FATAL);
Carl Shapiro58551df2011-07-24 03:09:51 -0700101}
102
103void MarkSweep::SweepCallback(size_t num_ptrs, void **ptrs, void *arg) {
104 // TODO, lock heap if concurrent
105 Space* space = static_cast<Space*>(arg);
106 for (size_t i = 0; i < num_ptrs; ++i) {
107 Object* obj = static_cast<Object*>(ptrs[i]);
108 space->Free(obj);
109 }
110 // TODO, unlock heap if concurrent
111}
112
113void MarkSweep::Sweep() {
114 const std::vector<Space*>& spaces = Heap::GetSpaces();
115 for (size_t i = 0; i < spaces.size(); ++i) {
116 if (spaces[i]->IsCondemned()) {
117 uintptr_t base = reinterpret_cast<uintptr_t>(spaces[i]->GetBase());
118 uintptr_t limit = reinterpret_cast<uintptr_t>(spaces[i]->GetLimit());
119 void* arg = static_cast<void*>(spaces[i]);
120 HeapBitmap::SweepWalk(*live_bitmap_, *mark_bitmap_, base, limit,
121 &MarkSweep::SweepCallback, arg);
122 }
123 }
124}
125
Carl Shapiro69759ea2011-07-21 18:13:35 -0700126// Scans instance fields.
127void MarkSweep::ScanInstanceFields(const Object* obj) {
128 DCHECK(obj != NULL);
129 DCHECK(obj->GetClass() != NULL);
130 uint32_t ref_offsets = obj->GetClass()->GetReferenceOffsets();
131 if (ref_offsets != CLASS_WALK_SUPER) {
132 // Found a reference offset bitmap. Mark the specified offsets.
133 while (ref_offsets != 0) {
134 size_t right_shift = CLZ(ref_offsets);
135 size_t byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
136 const Object* ref = obj->GetFieldObject(byte_offset);
137 MarkObject(ref);
138 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
139 }
140 } else {
141 // There is no reference offset bitmap for this class. Walk up
142 // the class inheritance hierarchy and find reference offsets the
143 // hard way.
144 for (Class *klass = obj->GetClass();
145 klass != NULL;
146 klass = klass->GetSuperClass()) {
147 for (size_t i = 0; i < klass->NumReferenceInstanceFields(); ++i) {
148 size_t field_offset = klass->GetInstanceField(i)->GetOffset();
149 const Object* ref = obj->GetFieldObject(field_offset);
150 MarkObject(ref);
151 }
152 }
153 }
154}
155
156// Scans the static fields of a class object.
157void MarkSweep::ScanStaticFields(const Class* klass) {
158 DCHECK(klass != NULL);
159 for (size_t i = 0; i < klass->NumStaticFields(); ++i) {
Jesse Wilson35baaab2011-08-10 16:18:03 -0400160 const Field* static_field = klass->GetStaticField(i);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700161 char ch = static_field->GetType();
162 if (ch == '[' || ch == 'L') {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700163 const Object* obj = static_field->GetObject();
164 MarkObject(obj);
165 }
166 }
167}
168
169void MarkSweep::ScanInterfaces(const Class* klass) {
170 DCHECK(klass != NULL);
171 for (size_t i = 0; i < klass->NumInterfaces(); ++i) {
172 MarkObject(klass->GetInterface(i));
173 }
174}
175
176// Scans the header, static field references, and interface pointers
177// of a class object.
178void MarkSweep::ScanClass(const Object* obj) {
179 DCHECK(obj != NULL);
180 DCHECK(obj->IsClass());
181 const Class* klass = obj->AsClass();
182 MarkObject(klass->GetClass());
183 if (klass->IsArray()) {
184 MarkObject(klass->GetComponentType());
185 }
186 if (klass->IsLoaded()) {
187 MarkObject(klass->GetSuperClass());
188 }
189 MarkObject(klass->GetClassLoader());
190 ScanInstanceFields(obj);
191 ScanStaticFields(klass);
192 // TODO: scan methods
193 // TODO: scan instance fields
194 if (klass->IsLoaded()) {
195 ScanInterfaces(klass);
196 }
197}
198
199// Scans the header of all array objects. If the array object is
200// specialized to a reference type, scans the array data as well.
201void MarkSweep::ScanArray(const Object *obj) {
202 DCHECK(obj != NULL);
203 DCHECK(obj->GetClass() != NULL);
204 MarkObject(obj->GetClass());
205 if (obj->IsObjectArray()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700206 const ObjectArray<Object>* array = obj->AsObjectArray<Object>();
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700207 for (int32_t i = 0; i < array->GetLength(); ++i) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700208 const Object* element = array->Get(i);
209 MarkObject(element);
210 }
211 }
212}
213
214void MarkSweep::EnqueuePendingReference(Object* ref, Object** list) {
215 DCHECK(ref != NULL);
216 DCHECK(list != NULL);
217 size_t offset = reference_pendingNext_offset_;
218 if (*list == NULL) {
219 ref->SetFieldObject(offset, ref);
220 *list = ref;
221 } else {
222 Object *head = (*list)->GetFieldObject(offset);
223 ref->SetFieldObject(offset, head);
224 (*list)->SetFieldObject(offset, ref);
225 }
226}
227
228Object* MarkSweep::DequeuePendingReference(Object** list) {
229 DCHECK(list != NULL);
230 DCHECK(*list != NULL);
231 size_t offset = reference_pendingNext_offset_;
232 Object* head = (*list)->GetFieldObject(offset);
233 Object* ref;
234 if (*list == head) {
235 ref = *list;
236 *list = NULL;
237 } else {
238 Object *next = head->GetFieldObject(offset);
239 (*list)->SetFieldObject(offset, next);
240 ref = head;
241 }
242 ref->SetFieldObject(offset, NULL);
243 return ref;
244}
245
246// Process the "referent" field in a java.lang.ref.Reference. If the
247// referent has not yet been marked, put it on the appropriate list in
248// the gcHeap for later processing.
249void MarkSweep::DelayReferenceReferent(Object* obj) {
250 DCHECK(obj != NULL);
251 DCHECK(obj->GetClass() != NULL);
Carl Shapiro58551df2011-07-24 03:09:51 -0700252 DCHECK(obj->IsReference());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700253 Object* pending = obj->GetFieldObject(reference_pendingNext_offset_);
254 Object* referent = obj->GetFieldObject(reference_referent_offset_);
255 if (pending == NULL && referent != NULL && !IsMarked(referent)) {
256 Object **list = NULL;
257 if (obj->IsSoftReference()) {
258 list = &soft_reference_list_;
259 } else if (obj->IsWeakReference()) {
260 list = &weak_reference_list_;
261 } else if (obj->IsFinalizerReference()) {
262 list = &finalizer_reference_list_;
263 } else if (obj->IsPhantomReference()) {
264 list = &phantom_reference_list_;
265 }
266 DCHECK(list != NULL);
267 EnqueuePendingReference(obj, list);
268 }
269}
270
271// Scans the header and field references of a data object. If the
272// scanned object is a reference subclass, it is scheduled for later
273// processing
Carl Shapiro58551df2011-07-24 03:09:51 -0700274void MarkSweep::ScanOther(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700275 DCHECK(obj != NULL);
276 DCHECK(obj->GetClass() != NULL);
277 MarkObject(obj->GetClass());
278 ScanInstanceFields(obj);
279 if (obj->IsReference()) {
280 DelayReferenceReferent(const_cast<Object*>(obj));
281 }
282}
283
284// Scans an object reference. Determines the type of the reference
285// and dispatches to a specialized scanning routine.
286void MarkSweep::ScanObject(const Object* obj) {
287 DCHECK(obj != NULL);
288 DCHECK(obj->GetClass() != NULL);
289 DCHECK(IsMarked(obj));
290 if (obj->IsClass()) {
291 ScanClass(obj);
292 } else if (obj->IsArray()) {
293 ScanArray(obj);
294 } else {
Carl Shapiro58551df2011-07-24 03:09:51 -0700295 ScanOther(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700296 }
297}
298
299// Scan anything that's on the mark stack. We can't use the bitmaps
300// anymore, so use a finger that points past the end of them.
301void MarkSweep::ProcessMarkStack() {
302 while (!mark_stack_->IsEmpty()) {
303 const Object *obj = mark_stack_->Pop();
304 ScanObject(obj);
305 }
306}
307
308void MarkSweep::ScanDirtyObjects() {
309 ProcessMarkStack();
310}
311
312void MarkSweep::ClearReference(Object* ref) {
313 DCHECK(ref != NULL);
314 ref->SetFieldObject(reference_referent_offset_, NULL);
315}
316
317bool MarkSweep::IsEnqueuable(const Object* ref) {
318 DCHECK(ref != NULL);
319 const Object* queue = ref->GetFieldObject(reference_queue_offset_);
320 const Object* queue_next = ref->GetFieldObject(reference_queueNext_offset_);
321 return (queue != NULL) && (queue_next == NULL);
322}
323
324void MarkSweep::EnqueueReference(Object* ref) {
325 DCHECK(ref != NULL);
326 CHECK(ref->GetFieldObject(reference_queue_offset_) != NULL);
327 CHECK(ref->GetFieldObject(reference_queueNext_offset_) == NULL);
328 EnqueuePendingReference(ref, &cleared_reference_list_);
329}
330
331// Walks the reference list marking any references subject to the
332// reference clearing policy. References with a black referent are
333// removed from the list. References with white referents biased
334// toward saving are blackened and also removed from the list.
335void MarkSweep::PreserveSomeSoftReferences(Object** list) {
336 DCHECK(list != NULL);
337 Object* clear = NULL;
338 size_t counter = 0;
339 while (*list != NULL) {
340 Object* ref = DequeuePendingReference(list);
341 Object* referent = ref->GetFieldObject(reference_referent_offset_);
342 if (referent == NULL) {
343 // Referent was cleared by the user during marking.
344 continue;
345 }
346 bool is_marked = IsMarked(referent);
347 if (!is_marked && ((++counter) & 1)) {
348 // Referent is white and biased toward saving, mark it.
349 MarkObject(referent);
350 is_marked = true;
351 }
352 if (!is_marked) {
353 // Referent is white, queue it for clearing.
354 EnqueuePendingReference(ref, &clear);
355 }
356 }
357 *list = clear;
358 // Restart the mark with the newly black references added to the
359 // root set.
360 ProcessMarkStack();
361}
362
363// Unlink the reference list clearing references objects with white
364// referents. Cleared references registered to a reference queue are
365// scheduled for appending by the heap worker thread.
366void MarkSweep::ClearWhiteReferences(Object** list) {
367 DCHECK(list != NULL);
368 size_t offset = reference_referent_offset_;
369 while (*list != NULL) {
370 Object *ref = DequeuePendingReference(list);
371 Object *referent = ref->GetFieldObject(offset);
372 if (referent != NULL && !IsMarked(referent)) {
373 // Referent is white, clear it.
374 ClearReference(ref);
375 if (IsEnqueuable(ref)) {
376 EnqueueReference(ref);
377 }
378 }
379 }
380 DCHECK(*list == NULL);
381}
382
383// Enqueues finalizer references with white referents. White
384// referents are blackened, moved to the zombie field, and the
385// referent field is cleared.
386void MarkSweep::EnqueueFinalizerReferences(Object** list) {
387 DCHECK(list != NULL);
388 size_t referent_offset = reference_referent_offset_;
389 size_t zombie_offset = finalizer_reference_zombie_offset_;
390 bool has_enqueued = false;
391 while (*list != NULL) {
392 Object* ref = DequeuePendingReference(list);
393 Object* referent = ref->GetFieldObject(referent_offset);
394 if (referent != NULL && !IsMarked(referent)) {
395 MarkObject(referent);
396 // If the referent is non-null the reference must queuable.
397 DCHECK(IsEnqueuable(ref));
398 ref->SetFieldObject(zombie_offset, referent);
399 ClearReference(ref);
400 EnqueueReference(ref);
401 has_enqueued = true;
402 }
403 }
404 if (has_enqueued) {
405 ProcessMarkStack();
406 }
407 DCHECK(*list == NULL);
408}
409
Carl Shapiro58551df2011-07-24 03:09:51 -0700410// Process reference class instances and schedule finalizations.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700411void MarkSweep::ProcessReferences(Object** soft_references, bool clear_soft,
412 Object** weak_references,
413 Object** finalizer_references,
414 Object** phantom_references) {
415 DCHECK(soft_references != NULL);
416 DCHECK(weak_references != NULL);
417 DCHECK(finalizer_references != NULL);
418 DCHECK(phantom_references != NULL);
419
420 // Unless we are in the zygote or required to clear soft references
421 // with white references, preserve some white referents.
422 if (clear_soft) {
423 PreserveSomeSoftReferences(soft_references);
424 }
425
426 // Clear all remaining soft and weak references with white
427 // referents.
428 ClearWhiteReferences(soft_references);
429 ClearWhiteReferences(weak_references);
430
431 // Preserve all white objects with finalize methods and schedule
432 // them for finalization.
433 EnqueueFinalizerReferences(finalizer_references);
434
435 // Clear all f-reachable soft and weak references with white
436 // referents.
437 ClearWhiteReferences(soft_references);
438 ClearWhiteReferences(weak_references);
439
440 // Clear all phantom references with white referents.
441 ClearWhiteReferences(phantom_references);
442
443 // At this point all reference lists should be empty.
444 DCHECK(*soft_references == NULL);
445 DCHECK(*weak_references == NULL);
446 DCHECK(*finalizer_references == NULL);
447 DCHECK(*phantom_references == NULL);
448}
449
450// Pushes a list of cleared references out to the managed heap.
451void MarkSweep::EnqueueClearedReferences(Object** cleared) {
452 DCHECK(cleared != NULL);
453 if (*cleared != NULL) {
454 Thread* self = Thread::Current();
455 DCHECK(self != NULL);
456 // TODO: Method *meth = gDvm.methJavaLangRefReferenceQueueAdd;
457 // DCHECK(meth != NULL);
458 // JValue unused;
459 // Object *reference = *cleared;
460 // TODO: dvmCallMethod(self, meth, NULL, &unused, reference);
Elliott Hughes53b61312011-08-12 18:28:20 -0700461 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700462 *cleared = NULL;
463 }
464}
465
466MarkSweep::~MarkSweep() {
Carl Shapiro58551df2011-07-24 03:09:51 -0700467 delete mark_stack_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700468 mark_bitmap_->Clear();
469}
470
471} // namespace art