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