Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 2 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3 | #include "mark_sweep.h" |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 4 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 5 | #include <climits> |
| 6 | #include <vector> |
| 7 | |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 8 | #include "class_loader.h" |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 9 | #include "dex_cache.h" |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 10 | #include "heap.h" |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 11 | #include "indirect_reference_table.h" |
| 12 | #include "intern_table.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 13 | #include "logging.h" |
| 14 | #include "macros.h" |
| 15 | #include "mark_stack.h" |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame^] | 16 | #include "monitor.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 17 | #include "object.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 18 | #include "runtime.h" |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 19 | #include "space.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 20 | #include "thread.h" |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 21 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 22 | namespace art { |
| 23 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 24 | bool MarkSweep::Init() { |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 25 | mark_stack_ = MarkStack::Create(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 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 | |
buzbee | 0d966cf | 2011-09-08 17:34:58 -0700 | [diff] [blame] | 35 | // TODO: if concurrent, enable card marking in compiler |
| 36 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 37 | // TODO: check that the mark bitmap is entirely clear. |
| 38 | |
| 39 | return true; |
| 40 | } |
| 41 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 42 | void MarkSweep::MarkObject0(const Object* obj, bool check_finger) { |
| 43 | DCHECK(obj != NULL); |
| 44 | if (obj < condemned_) { |
| 45 | DCHECK(IsMarked(obj)); |
| 46 | return; |
| 47 | } |
| 48 | bool is_marked = mark_bitmap_->Test(obj); |
| 49 | // This object was not previously marked. |
| 50 | if (!is_marked) { |
| 51 | mark_bitmap_->Set(obj); |
| 52 | if (check_finger && obj < finger_) { |
| 53 | // The object must be pushed on to the mark stack. |
| 54 | mark_stack_->Push(obj); |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // Used to mark objects when recursing. Recursion is done by moving |
| 60 | // the finger across the bitmaps in address order and marking child |
| 61 | // objects. Any newly-marked objects whose addresses are lower than |
| 62 | // the finger won't be visited by the bitmap scan, so those objects |
| 63 | // need to be added to the mark stack. |
| 64 | void MarkSweep::MarkObject(const Object* obj) { |
| 65 | if (obj != NULL) { |
| 66 | MarkObject0(obj, true); |
| 67 | } |
| 68 | } |
| 69 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 70 | void MarkSweep::MarkObjectVisitor(const Object* root, void* arg) { |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 71 | DCHECK(root != NULL); |
| 72 | DCHECK(arg != NULL); |
| 73 | MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg); |
| 74 | mark_sweep->MarkObject0(root, true); |
| 75 | } |
| 76 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 77 | // Marks all objects in the root set. |
| 78 | void MarkSweep::MarkRoots() { |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 79 | Runtime::Current()->VisitRoots(MarkObjectVisitor, this); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 82 | void MarkSweep::ScanBitmapCallback(Object* obj, void* finger, void* arg) { |
| 83 | MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg); |
| 84 | mark_sweep->finger_ = reinterpret_cast<Object*>(finger); |
| 85 | mark_sweep->ScanObject(obj); |
| 86 | } |
| 87 | |
| 88 | // Populates the mark stack based on the set of marked objects and |
| 89 | // recursively marks until the mark stack is emptied. |
| 90 | void MarkSweep::RecursiveMark() { |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 91 | |
| 92 | // 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 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 100 | void* arg = reinterpret_cast<void*>(this); |
| 101 | const std::vector<Space*>& spaces = Heap::GetSpaces(); |
| 102 | for (size_t i = 0; i < spaces.size(); ++i) { |
| 103 | if (spaces[i]->IsCondemned()) { |
| 104 | uintptr_t base = reinterpret_cast<uintptr_t>(spaces[i]->GetBase()); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 105 | mark_bitmap_->ScanWalk(base, &MarkSweep::ScanBitmapCallback, arg); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | finger_ = reinterpret_cast<Object*>(~0); |
| 109 | ProcessMarkStack(); |
| 110 | } |
| 111 | |
| 112 | void MarkSweep::ReMarkRoots() { |
Elliott Hughes | 53b6131 | 2011-08-12 18:28:20 -0700 | [diff] [blame] | 113 | UNIMPLEMENTED(FATAL); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 116 | void MarkSweep::SweepJniWeakGlobals() { |
| 117 | JavaVMExt* vm = Runtime::Current()->GetJavaVM(); |
| 118 | MutexLock mu(vm->weak_globals_lock); |
| 119 | IndirectReferenceTable* table = &vm->weak_globals; |
| 120 | typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto |
| 121 | for (It it = table->begin(), end = table->end(); it != end; ++it) { |
| 122 | const Object** entry = *it; |
| 123 | if (!IsMarked(*entry)) { |
| 124 | *entry = kClearedJniWeakGlobal; |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 129 | void MarkSweep::SweepSystemWeaks() { |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame^] | 130 | Runtime::Current()->GetInternTable()->SweepInternTableWeaks(IsMarked, this); |
| 131 | Runtime::Current()->GetMonitorList()->SweepMonitorList(IsMarked, this); |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 132 | SweepJniWeakGlobals(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 135 | void MarkSweep::SweepCallback(size_t num_ptrs, void** ptrs, void* arg) { |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 136 | // TODO, lock heap if concurrent |
| 137 | Space* space = static_cast<Space*>(arg); |
| 138 | for (size_t i = 0; i < num_ptrs; ++i) { |
| 139 | Object* obj = static_cast<Object*>(ptrs[i]); |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 140 | Heap::RecordFreeLocked(space, obj); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 141 | space->Free(obj); |
| 142 | } |
| 143 | // TODO, unlock heap if concurrent |
| 144 | } |
| 145 | |
| 146 | void MarkSweep::Sweep() { |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 147 | SweepSystemWeaks(); |
| 148 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 149 | const std::vector<Space*>& spaces = Heap::GetSpaces(); |
| 150 | for (size_t i = 0; i < spaces.size(); ++i) { |
| 151 | if (spaces[i]->IsCondemned()) { |
| 152 | uintptr_t base = reinterpret_cast<uintptr_t>(spaces[i]->GetBase()); |
| 153 | uintptr_t limit = reinterpret_cast<uintptr_t>(spaces[i]->GetLimit()); |
| 154 | void* arg = static_cast<void*>(spaces[i]); |
| 155 | HeapBitmap::SweepWalk(*live_bitmap_, *mark_bitmap_, base, limit, |
| 156 | &MarkSweep::SweepCallback, arg); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 161 | // Scans instance fields. |
| 162 | void MarkSweep::ScanInstanceFields(const Object* obj) { |
| 163 | DCHECK(obj != NULL); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 164 | Class* klass = obj->GetClass(); |
| 165 | DCHECK(klass != NULL); |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 166 | ScanFields(obj, klass->GetReferenceInstanceOffsets(), false); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | // Scans static storage on a Class. |
| 170 | void MarkSweep::ScanStaticFields(const Class* klass) { |
| 171 | DCHECK(klass != NULL); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 172 | ScanFields(klass, klass->GetReferenceStaticOffsets(), true); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 175 | void MarkSweep::ScanFields(const Object* obj, uint32_t ref_offsets, bool is_static) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 176 | if (ref_offsets != CLASS_WALK_SUPER) { |
| 177 | // Found a reference offset bitmap. Mark the specified offsets. |
| 178 | while (ref_offsets != 0) { |
| 179 | size_t right_shift = CLZ(ref_offsets); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 180 | MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift); |
| 181 | const Object* ref = obj->GetFieldObject<const Object*>(byte_offset, false); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 182 | MarkObject(ref); |
| 183 | ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift); |
| 184 | } |
| 185 | } else { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 186 | // There is no reference offset bitmap. In the non-static case, |
| 187 | // walk up the class inheritance hierarchy and find reference |
| 188 | // offsets the hard way. In the static case, just consider this |
| 189 | // class. |
| 190 | for (const Class* klass = is_static ? obj->AsClass() : obj->GetClass(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 191 | klass != NULL; |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 192 | klass = is_static ? NULL : klass->GetSuperClass()) { |
| 193 | size_t num_reference_fields = (is_static |
| 194 | ? klass->NumReferenceStaticFields() |
| 195 | : klass->NumReferenceInstanceFields()); |
| 196 | for (size_t i = 0; i < num_reference_fields; ++i) { |
| 197 | Field* field = (is_static |
| 198 | ? klass->GetStaticField(i) |
| 199 | : klass->GetInstanceField(i)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 200 | MemberOffset field_offset = field->GetOffset(); |
| 201 | const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 202 | MarkObject(ref); |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 208 | // Scans the header, static field references, and interface pointers |
| 209 | // of a class object. |
| 210 | void MarkSweep::ScanClass(const Object* obj) { |
| 211 | DCHECK(obj != NULL); |
| 212 | DCHECK(obj->IsClass()); |
| 213 | const Class* klass = obj->AsClass(); |
| 214 | MarkObject(klass->GetClass()); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 215 | ScanInstanceFields(obj); |
| 216 | MarkObject(klass->GetDescriptor()); |
| 217 | MarkObject(klass->GetDexCache()); |
| 218 | MarkObject(klass->GetVerifyErrorClass()); |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 219 | if (klass->IsArrayClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 220 | MarkObject(klass->GetComponentType()); |
| 221 | } |
| 222 | if (klass->IsLoaded()) { |
| 223 | MarkObject(klass->GetSuperClass()); |
| 224 | } |
| 225 | MarkObject(klass->GetClassLoader()); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 226 | if (klass->IsLoaded()) { |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 227 | MarkObject(klass->GetInterfaces()); |
| 228 | MarkObject(klass->GetDirectMethods()); |
| 229 | MarkObject(klass->GetVirtualMethods()); |
| 230 | MarkObject(klass->GetIFields()); |
| 231 | MarkObject(klass->GetSFields()); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 232 | } |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 233 | ScanStaticFields(klass); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | // Scans the header of all array objects. If the array object is |
| 237 | // specialized to a reference type, scans the array data as well. |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 238 | void MarkSweep::ScanArray(const Object* obj) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 239 | DCHECK(obj != NULL); |
| 240 | DCHECK(obj->GetClass() != NULL); |
| 241 | MarkObject(obj->GetClass()); |
| 242 | if (obj->IsObjectArray()) { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 243 | const ObjectArray<Object>* array = obj->AsObjectArray<Object>(); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 244 | for (int32_t i = 0; i < array->GetLength(); ++i) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 245 | const Object* element = array->Get(i); |
| 246 | MarkObject(element); |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 251 | // Process the "referent" field in a java.lang.ref.Reference. If the |
| 252 | // referent has not yet been marked, put it on the appropriate list in |
| 253 | // the gcHeap for later processing. |
| 254 | void MarkSweep::DelayReferenceReferent(Object* obj) { |
| 255 | DCHECK(obj != NULL); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 256 | Class* klass = obj->GetClass(); |
| 257 | DCHECK(klass != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 258 | DCHECK(klass->IsReferenceClass()); |
| 259 | Object* pending = obj->GetFieldObject<Object*>(Heap::GetReferencePendingNextOffset(), false); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 260 | Object* referent = Heap::GetReferenceReferent(obj); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 261 | if (pending == NULL && referent != NULL && !IsMarked(referent)) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 262 | Object** list = NULL; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 263 | if (klass->IsSoftReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 264 | list = &soft_reference_list_; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 265 | } else if (klass->IsWeakReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 266 | list = &weak_reference_list_; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 267 | } else if (klass->IsFinalizerReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 268 | list = &finalizer_reference_list_; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 269 | } else if (klass->IsPhantomReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 270 | list = &phantom_reference_list_; |
| 271 | } |
| 272 | DCHECK(list != NULL); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 273 | Heap::EnqueuePendingReference(obj, list); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
| 277 | // Scans the header and field references of a data object. If the |
| 278 | // scanned object is a reference subclass, it is scheduled for later |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 279 | // processing. |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 280 | void MarkSweep::ScanOther(const Object* obj) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 281 | DCHECK(obj != NULL); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 282 | Class* klass = obj->GetClass(); |
| 283 | DCHECK(klass != NULL); |
| 284 | MarkObject(klass); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 285 | ScanInstanceFields(obj); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 286 | if (klass->IsReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 287 | DelayReferenceReferent(const_cast<Object*>(obj)); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | // Scans an object reference. Determines the type of the reference |
| 292 | // and dispatches to a specialized scanning routine. |
| 293 | void MarkSweep::ScanObject(const Object* obj) { |
| 294 | DCHECK(obj != NULL); |
| 295 | DCHECK(obj->GetClass() != NULL); |
| 296 | DCHECK(IsMarked(obj)); |
| 297 | if (obj->IsClass()) { |
| 298 | ScanClass(obj); |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 299 | } else if (obj->IsArrayInstance()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 300 | ScanArray(obj); |
| 301 | } else { |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 302 | ScanOther(obj); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | |
| 306 | // Scan anything that's on the mark stack. We can't use the bitmaps |
| 307 | // anymore, so use a finger that points past the end of them. |
| 308 | void MarkSweep::ProcessMarkStack() { |
| 309 | while (!mark_stack_->IsEmpty()) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 310 | const Object* obj = mark_stack_->Pop(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 311 | ScanObject(obj); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | void MarkSweep::ScanDirtyObjects() { |
| 316 | ProcessMarkStack(); |
| 317 | } |
| 318 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 319 | // Walks the reference list marking any references subject to the |
| 320 | // reference clearing policy. References with a black referent are |
| 321 | // removed from the list. References with white referents biased |
| 322 | // toward saving are blackened and also removed from the list. |
| 323 | void MarkSweep::PreserveSomeSoftReferences(Object** list) { |
| 324 | DCHECK(list != NULL); |
| 325 | Object* clear = NULL; |
| 326 | size_t counter = 0; |
| 327 | while (*list != NULL) { |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 328 | Object* ref = Heap::DequeuePendingReference(list); |
| 329 | Object* referent = Heap::GetReferenceReferent(ref); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 330 | if (referent == NULL) { |
| 331 | // Referent was cleared by the user during marking. |
| 332 | continue; |
| 333 | } |
| 334 | bool is_marked = IsMarked(referent); |
| 335 | if (!is_marked && ((++counter) & 1)) { |
| 336 | // Referent is white and biased toward saving, mark it. |
| 337 | MarkObject(referent); |
| 338 | is_marked = true; |
| 339 | } |
| 340 | if (!is_marked) { |
| 341 | // Referent is white, queue it for clearing. |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 342 | Heap::EnqueuePendingReference(ref, &clear); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | *list = clear; |
| 346 | // Restart the mark with the newly black references added to the |
| 347 | // root set. |
| 348 | ProcessMarkStack(); |
| 349 | } |
| 350 | |
| 351 | // Unlink the reference list clearing references objects with white |
| 352 | // referents. Cleared references registered to a reference queue are |
| 353 | // scheduled for appending by the heap worker thread. |
| 354 | void MarkSweep::ClearWhiteReferences(Object** list) { |
| 355 | DCHECK(list != NULL); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 356 | while (*list != NULL) { |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 357 | Object* ref = Heap::DequeuePendingReference(list); |
| 358 | Object* referent = Heap::GetReferenceReferent(ref); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 359 | if (referent != NULL && !IsMarked(referent)) { |
| 360 | // Referent is white, clear it. |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 361 | Heap::ClearReferenceReferent(ref); |
| 362 | if (Heap::IsEnqueuable(ref)) { |
| 363 | Heap::EnqueueReference(ref, &cleared_reference_list_); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 364 | } |
| 365 | } |
| 366 | } |
| 367 | DCHECK(*list == NULL); |
| 368 | } |
| 369 | |
| 370 | // Enqueues finalizer references with white referents. White |
| 371 | // referents are blackened, moved to the zombie field, and the |
| 372 | // referent field is cleared. |
| 373 | void MarkSweep::EnqueueFinalizerReferences(Object** list) { |
| 374 | DCHECK(list != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 375 | MemberOffset zombie_offset = Heap::GetFinalizerReferenceZombieOffset(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 376 | bool has_enqueued = false; |
| 377 | while (*list != NULL) { |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 378 | Object* ref = Heap::DequeuePendingReference(list); |
| 379 | Object* referent = Heap::GetReferenceReferent(ref); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 380 | if (referent != NULL && !IsMarked(referent)) { |
| 381 | MarkObject(referent); |
| 382 | // If the referent is non-null the reference must queuable. |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 383 | DCHECK(Heap::IsEnqueuable(ref)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 384 | ref->SetFieldObject(zombie_offset, referent, false); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 385 | Heap::ClearReferenceReferent(ref); |
| 386 | Heap::EnqueueReference(ref, &cleared_reference_list_); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 387 | has_enqueued = true; |
| 388 | } |
| 389 | } |
| 390 | if (has_enqueued) { |
| 391 | ProcessMarkStack(); |
| 392 | } |
| 393 | DCHECK(*list == NULL); |
| 394 | } |
| 395 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 396 | // Process reference class instances and schedule finalizations. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 397 | void MarkSweep::ProcessReferences(Object** soft_references, bool clear_soft, |
| 398 | Object** weak_references, |
| 399 | Object** finalizer_references, |
| 400 | Object** phantom_references) { |
| 401 | DCHECK(soft_references != NULL); |
| 402 | DCHECK(weak_references != NULL); |
| 403 | DCHECK(finalizer_references != NULL); |
| 404 | DCHECK(phantom_references != NULL); |
| 405 | |
| 406 | // Unless we are in the zygote or required to clear soft references |
| 407 | // with white references, preserve some white referents. |
| 408 | if (clear_soft) { |
| 409 | PreserveSomeSoftReferences(soft_references); |
| 410 | } |
| 411 | |
| 412 | // Clear all remaining soft and weak references with white |
| 413 | // referents. |
| 414 | ClearWhiteReferences(soft_references); |
| 415 | ClearWhiteReferences(weak_references); |
| 416 | |
| 417 | // Preserve all white objects with finalize methods and schedule |
| 418 | // them for finalization. |
| 419 | EnqueueFinalizerReferences(finalizer_references); |
| 420 | |
| 421 | // Clear all f-reachable soft and weak references with white |
| 422 | // referents. |
| 423 | ClearWhiteReferences(soft_references); |
| 424 | ClearWhiteReferences(weak_references); |
| 425 | |
| 426 | // Clear all phantom references with white referents. |
| 427 | ClearWhiteReferences(phantom_references); |
| 428 | |
| 429 | // At this point all reference lists should be empty. |
| 430 | DCHECK(*soft_references == NULL); |
| 431 | DCHECK(*weak_references == NULL); |
| 432 | DCHECK(*finalizer_references == NULL); |
| 433 | DCHECK(*phantom_references == NULL); |
| 434 | } |
| 435 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 436 | MarkSweep::~MarkSweep() { |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 437 | delete mark_stack_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 438 | mark_bitmap_->Clear(); |
| 439 | } |
| 440 | |
| 441 | } // namespace art |