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" |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 9 | #include "heap.h" |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 10 | #include "indirect_reference_table.h" |
| 11 | #include "intern_table.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 12 | #include "logging.h" |
| 13 | #include "macros.h" |
| 14 | #include "mark_stack.h" |
| 15 | #include "object.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 16 | #include "runtime.h" |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 17 | #include "space.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 18 | #include "thread.h" |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 19 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 20 | namespace art { |
| 21 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 22 | bool MarkSweep::Init() { |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 23 | mark_stack_ = MarkStack::Create(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 24 | if (mark_stack_ == NULL) { |
| 25 | return false; |
| 26 | } |
| 27 | |
| 28 | mark_bitmap_ = Heap::GetMarkBits(); |
| 29 | live_bitmap_ = Heap::GetLiveBits(); |
| 30 | |
| 31 | // TODO: if concurrent, clear the card table. |
| 32 | |
| 33 | // TODO: check that the mark bitmap is entirely clear. |
| 34 | |
| 35 | return true; |
| 36 | } |
| 37 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 38 | void MarkSweep::MarkObject0(const Object* obj, bool check_finger) { |
| 39 | DCHECK(obj != NULL); |
| 40 | if (obj < condemned_) { |
| 41 | DCHECK(IsMarked(obj)); |
| 42 | return; |
| 43 | } |
| 44 | bool is_marked = mark_bitmap_->Test(obj); |
| 45 | // This object was not previously marked. |
| 46 | if (!is_marked) { |
| 47 | mark_bitmap_->Set(obj); |
| 48 | if (check_finger && obj < finger_) { |
| 49 | // The object must be pushed on to the mark stack. |
| 50 | mark_stack_->Push(obj); |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // Used to mark objects when recursing. Recursion is done by moving |
| 56 | // the finger across the bitmaps in address order and marking child |
| 57 | // objects. Any newly-marked objects whose addresses are lower than |
| 58 | // the finger won't be visited by the bitmap scan, so those objects |
| 59 | // need to be added to the mark stack. |
| 60 | void MarkSweep::MarkObject(const Object* obj) { |
| 61 | if (obj != NULL) { |
| 62 | MarkObject0(obj, true); |
| 63 | } |
| 64 | } |
| 65 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 66 | void MarkSweep::MarkObjectVisitor(const Object* root, void* arg) { |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 67 | DCHECK(root != NULL); |
| 68 | DCHECK(arg != NULL); |
| 69 | MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg); |
| 70 | mark_sweep->MarkObject0(root, true); |
| 71 | } |
| 72 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 73 | // Marks all objects in the root set. |
| 74 | void MarkSweep::MarkRoots() { |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 75 | Runtime::Current()->VisitRoots(MarkObjectVisitor, this); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 78 | void MarkSweep::ScanBitmapCallback(Object* obj, void* finger, void* arg) { |
| 79 | MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg); |
| 80 | mark_sweep->finger_ = reinterpret_cast<Object*>(finger); |
| 81 | mark_sweep->ScanObject(obj); |
| 82 | } |
| 83 | |
| 84 | // Populates the mark stack based on the set of marked objects and |
| 85 | // recursively marks until the mark stack is emptied. |
| 86 | void MarkSweep::RecursiveMark() { |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 87 | |
| 88 | // RecursiveMark will build the lists of known instances of the Reference classes. |
| 89 | // See DelayReferenceReferent for details. |
| 90 | CHECK(soft_reference_list_ == NULL); |
| 91 | CHECK(weak_reference_list_ == NULL); |
| 92 | CHECK(finalizer_reference_list_ == NULL); |
| 93 | CHECK(phantom_reference_list_ == NULL); |
| 94 | CHECK(cleared_reference_list_ == NULL); |
| 95 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 96 | void* arg = reinterpret_cast<void*>(this); |
| 97 | const std::vector<Space*>& spaces = Heap::GetSpaces(); |
| 98 | for (size_t i = 0; i < spaces.size(); ++i) { |
| 99 | if (spaces[i]->IsCondemned()) { |
| 100 | uintptr_t base = reinterpret_cast<uintptr_t>(spaces[i]->GetBase()); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 101 | mark_bitmap_->ScanWalk(base, &MarkSweep::ScanBitmapCallback, arg); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | finger_ = reinterpret_cast<Object*>(~0); |
| 105 | ProcessMarkStack(); |
| 106 | } |
| 107 | |
| 108 | void MarkSweep::ReMarkRoots() { |
Elliott Hughes | 53b6131 | 2011-08-12 18:28:20 -0700 | [diff] [blame] | 109 | UNIMPLEMENTED(FATAL); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 112 | void MarkSweep::SweepJniWeakGlobals() { |
| 113 | JavaVMExt* vm = Runtime::Current()->GetJavaVM(); |
| 114 | MutexLock mu(vm->weak_globals_lock); |
| 115 | IndirectReferenceTable* table = &vm->weak_globals; |
| 116 | typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto |
| 117 | for (It it = table->begin(), end = table->end(); it != end; ++it) { |
| 118 | const Object** entry = *it; |
| 119 | if (!IsMarked(*entry)) { |
| 120 | *entry = kClearedJniWeakGlobal; |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | struct InternTableEntryIsUnmarked : public InternTable::Predicate { |
| 126 | InternTableEntryIsUnmarked(MarkSweep* ms) : ms_(ms) { } |
| 127 | |
| 128 | bool operator()(const String* s) const { |
| 129 | return !ms_->IsMarked(s); |
| 130 | } |
| 131 | |
| 132 | MarkSweep* ms_; |
| 133 | }; |
| 134 | |
| 135 | void MarkSweep::SweepMonitorList() { |
Elliott Hughes | 53b6131 | 2011-08-12 18:28:20 -0700 | [diff] [blame] | 136 | UNIMPLEMENTED(FATAL); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 137 | //dvmSweepMonitorList(&gDvm.monitorList, isUnmarkedObject); |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | void MarkSweep::SweepSystemWeaks() { |
| 141 | Runtime::Current()->GetInternTable()->RemoveWeakIf(InternTableEntryIsUnmarked(this)); |
| 142 | SweepMonitorList(); |
| 143 | SweepJniWeakGlobals(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | void MarkSweep::SweepCallback(size_t num_ptrs, void **ptrs, void *arg) { |
| 147 | // TODO, lock heap if concurrent |
| 148 | Space* space = static_cast<Space*>(arg); |
| 149 | for (size_t i = 0; i < num_ptrs; ++i) { |
| 150 | Object* obj = static_cast<Object*>(ptrs[i]); |
| 151 | space->Free(obj); |
| 152 | } |
| 153 | // TODO, unlock heap if concurrent |
| 154 | } |
| 155 | |
| 156 | void MarkSweep::Sweep() { |
| 157 | const std::vector<Space*>& spaces = Heap::GetSpaces(); |
| 158 | for (size_t i = 0; i < spaces.size(); ++i) { |
| 159 | if (spaces[i]->IsCondemned()) { |
| 160 | uintptr_t base = reinterpret_cast<uintptr_t>(spaces[i]->GetBase()); |
| 161 | uintptr_t limit = reinterpret_cast<uintptr_t>(spaces[i]->GetLimit()); |
| 162 | void* arg = static_cast<void*>(spaces[i]); |
| 163 | HeapBitmap::SweepWalk(*live_bitmap_, *mark_bitmap_, base, limit, |
| 164 | &MarkSweep::SweepCallback, arg); |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 169 | // Scans instance fields. |
| 170 | void MarkSweep::ScanInstanceFields(const Object* obj) { |
| 171 | DCHECK(obj != NULL); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 172 | Class* klass = obj->GetClass(); |
| 173 | DCHECK(klass != NULL); |
| 174 | ScanFields(obj, |
| 175 | klass->GetReferenceInstanceOffsets(), |
| 176 | false); |
| 177 | } |
| 178 | |
| 179 | // Scans static storage on a Class. |
| 180 | void MarkSweep::ScanStaticFields(const Class* klass) { |
| 181 | DCHECK(klass != NULL); |
| 182 | ScanFields(klass, |
| 183 | klass->GetReferenceStaticOffsets(), |
| 184 | true); |
| 185 | } |
| 186 | |
| 187 | void MarkSweep::ScanFields(const Object* obj, |
| 188 | uint32_t ref_offsets, |
| 189 | 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 | void MarkSweep::ScanInterfaces(const Class* klass) { |
| 223 | DCHECK(klass != NULL); |
| 224 | for (size_t i = 0; i < klass->NumInterfaces(); ++i) { |
| 225 | MarkObject(klass->GetInterface(i)); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | // Scans the header, static field references, and interface pointers |
| 230 | // of a class object. |
| 231 | void MarkSweep::ScanClass(const Object* obj) { |
| 232 | DCHECK(obj != NULL); |
| 233 | DCHECK(obj->IsClass()); |
| 234 | const Class* klass = obj->AsClass(); |
| 235 | MarkObject(klass->GetClass()); |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 236 | if (klass->IsArrayClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 237 | MarkObject(klass->GetComponentType()); |
| 238 | } |
| 239 | if (klass->IsLoaded()) { |
| 240 | MarkObject(klass->GetSuperClass()); |
| 241 | } |
| 242 | MarkObject(klass->GetClassLoader()); |
| 243 | ScanInstanceFields(obj); |
| 244 | ScanStaticFields(klass); |
| 245 | // TODO: scan methods |
| 246 | // TODO: scan instance fields |
| 247 | if (klass->IsLoaded()) { |
| 248 | ScanInterfaces(klass); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | // Scans the header of all array objects. If the array object is |
| 253 | // specialized to a reference type, scans the array data as well. |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 254 | void MarkSweep::ScanArray(const Object* obj) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 255 | DCHECK(obj != NULL); |
| 256 | DCHECK(obj->GetClass() != NULL); |
| 257 | MarkObject(obj->GetClass()); |
| 258 | if (obj->IsObjectArray()) { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 259 | const ObjectArray<Object>* array = obj->AsObjectArray<Object>(); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 260 | for (int32_t i = 0; i < array->GetLength(); ++i) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 261 | const Object* element = array->Get(i); |
| 262 | MarkObject(element); |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | void MarkSweep::EnqueuePendingReference(Object* ref, Object** list) { |
| 268 | DCHECK(ref != NULL); |
| 269 | DCHECK(list != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 270 | |
| 271 | MemberOffset offset = Heap::GetReferencePendingNextOffset(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 272 | if (*list == NULL) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 273 | ref->SetFieldObject(offset, ref, false); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 274 | *list = ref; |
| 275 | } else { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 276 | Object* head = (*list)->GetFieldObject<Object*>(offset, false); |
| 277 | ref->SetFieldObject(offset, head, false); |
| 278 | (*list)->SetFieldObject(offset, ref, false); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
| 282 | Object* MarkSweep::DequeuePendingReference(Object** list) { |
| 283 | DCHECK(list != NULL); |
| 284 | DCHECK(*list != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 285 | MemberOffset offset = Heap::GetReferencePendingNextOffset(); |
| 286 | Object* head = (*list)->GetFieldObject<Object*>(offset, false); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 287 | Object* ref; |
| 288 | if (*list == head) { |
| 289 | ref = *list; |
| 290 | *list = NULL; |
| 291 | } else { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 292 | Object* next = head->GetFieldObject<Object*>(offset, false); |
| 293 | (*list)->SetFieldObject(offset, next, false); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 294 | ref = head; |
| 295 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 296 | ref->SetFieldObject(offset, NULL, false); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 297 | return ref; |
| 298 | } |
| 299 | |
| 300 | // Process the "referent" field in a java.lang.ref.Reference. If the |
| 301 | // referent has not yet been marked, put it on the appropriate list in |
| 302 | // the gcHeap for later processing. |
| 303 | void MarkSweep::DelayReferenceReferent(Object* obj) { |
| 304 | DCHECK(obj != NULL); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 305 | Class* klass = obj->GetClass(); |
| 306 | DCHECK(klass != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 307 | DCHECK(klass->IsReferenceClass()); |
| 308 | Object* pending = obj->GetFieldObject<Object*>(Heap::GetReferencePendingNextOffset(), false); |
| 309 | Object* referent = obj->GetFieldObject<Object*>(Heap::GetReferenceReferentOffset(), false); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 310 | if (pending == NULL && referent != NULL && !IsMarked(referent)) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 311 | Object** list = NULL; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 312 | if (klass->IsSoftReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 313 | list = &soft_reference_list_; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 314 | } else if (klass->IsWeakReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 315 | list = &weak_reference_list_; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 316 | } else if (klass->IsFinalizerReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 317 | list = &finalizer_reference_list_; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 318 | } else if (klass->IsPhantomReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 319 | list = &phantom_reference_list_; |
| 320 | } |
| 321 | DCHECK(list != NULL); |
| 322 | EnqueuePendingReference(obj, list); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | // Scans the header and field references of a data object. If the |
| 327 | // scanned object is a reference subclass, it is scheduled for later |
| 328 | // processing |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 329 | void MarkSweep::ScanOther(const Object* obj) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 330 | DCHECK(obj != NULL); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 331 | Class* klass = obj->GetClass(); |
| 332 | DCHECK(klass != NULL); |
| 333 | MarkObject(klass); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 334 | ScanInstanceFields(obj); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 335 | if (klass->IsReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 336 | DelayReferenceReferent(const_cast<Object*>(obj)); |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | // Scans an object reference. Determines the type of the reference |
| 341 | // and dispatches to a specialized scanning routine. |
| 342 | void MarkSweep::ScanObject(const Object* obj) { |
| 343 | DCHECK(obj != NULL); |
| 344 | DCHECK(obj->GetClass() != NULL); |
| 345 | DCHECK(IsMarked(obj)); |
| 346 | if (obj->IsClass()) { |
| 347 | ScanClass(obj); |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 348 | } else if (obj->IsArrayInstance()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 349 | ScanArray(obj); |
| 350 | } else { |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 351 | ScanOther(obj); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | |
| 355 | // Scan anything that's on the mark stack. We can't use the bitmaps |
| 356 | // anymore, so use a finger that points past the end of them. |
| 357 | void MarkSweep::ProcessMarkStack() { |
| 358 | while (!mark_stack_->IsEmpty()) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 359 | const Object* obj = mark_stack_->Pop(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 360 | ScanObject(obj); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | void MarkSweep::ScanDirtyObjects() { |
| 365 | ProcessMarkStack(); |
| 366 | } |
| 367 | |
| 368 | void MarkSweep::ClearReference(Object* ref) { |
| 369 | DCHECK(ref != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 370 | ref->SetFieldObject(Heap::GetReferenceReferentOffset(), NULL, false); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | bool MarkSweep::IsEnqueuable(const Object* ref) { |
| 374 | DCHECK(ref != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 375 | const Object* queue = |
| 376 | ref->GetFieldObject<Object*>(Heap::GetReferenceQueueOffset(), false); |
| 377 | const Object* queue_next = |
| 378 | ref->GetFieldObject<Object*>(Heap::GetReferenceQueueNextOffset(), false); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 379 | return (queue != NULL) && (queue_next == NULL); |
| 380 | } |
| 381 | |
| 382 | void MarkSweep::EnqueueReference(Object* ref) { |
| 383 | DCHECK(ref != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 384 | CHECK(ref->GetFieldObject<Object*>(Heap::GetReferenceQueueOffset(), false) != NULL); |
| 385 | CHECK(ref->GetFieldObject<Object*>(Heap::GetReferenceQueueNextOffset(), false) == NULL); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 386 | EnqueuePendingReference(ref, &cleared_reference_list_); |
| 387 | } |
| 388 | |
| 389 | // Walks the reference list marking any references subject to the |
| 390 | // reference clearing policy. References with a black referent are |
| 391 | // removed from the list. References with white referents biased |
| 392 | // toward saving are blackened and also removed from the list. |
| 393 | void MarkSweep::PreserveSomeSoftReferences(Object** list) { |
| 394 | DCHECK(list != NULL); |
| 395 | Object* clear = NULL; |
| 396 | size_t counter = 0; |
| 397 | while (*list != NULL) { |
| 398 | Object* ref = DequeuePendingReference(list); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 399 | Object* referent = ref->GetFieldObject<Object*>(Heap::GetReferenceReferentOffset(), false); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 400 | if (referent == NULL) { |
| 401 | // Referent was cleared by the user during marking. |
| 402 | continue; |
| 403 | } |
| 404 | bool is_marked = IsMarked(referent); |
| 405 | if (!is_marked && ((++counter) & 1)) { |
| 406 | // Referent is white and biased toward saving, mark it. |
| 407 | MarkObject(referent); |
| 408 | is_marked = true; |
| 409 | } |
| 410 | if (!is_marked) { |
| 411 | // Referent is white, queue it for clearing. |
| 412 | EnqueuePendingReference(ref, &clear); |
| 413 | } |
| 414 | } |
| 415 | *list = clear; |
| 416 | // Restart the mark with the newly black references added to the |
| 417 | // root set. |
| 418 | ProcessMarkStack(); |
| 419 | } |
| 420 | |
| 421 | // Unlink the reference list clearing references objects with white |
| 422 | // referents. Cleared references registered to a reference queue are |
| 423 | // scheduled for appending by the heap worker thread. |
| 424 | void MarkSweep::ClearWhiteReferences(Object** list) { |
| 425 | DCHECK(list != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 426 | MemberOffset offset = Heap::GetReferenceReferentOffset(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 427 | while (*list != NULL) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 428 | Object* ref = DequeuePendingReference(list); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 429 | Object* referent = ref->GetFieldObject<Object*>(offset, false); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 430 | if (referent != NULL && !IsMarked(referent)) { |
| 431 | // Referent is white, clear it. |
| 432 | ClearReference(ref); |
| 433 | if (IsEnqueuable(ref)) { |
| 434 | EnqueueReference(ref); |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | DCHECK(*list == NULL); |
| 439 | } |
| 440 | |
| 441 | // Enqueues finalizer references with white referents. White |
| 442 | // referents are blackened, moved to the zombie field, and the |
| 443 | // referent field is cleared. |
| 444 | void MarkSweep::EnqueueFinalizerReferences(Object** list) { |
| 445 | DCHECK(list != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 446 | MemberOffset referent_offset = Heap::GetReferenceReferentOffset(); |
| 447 | MemberOffset zombie_offset = Heap::GetFinalizerReferenceZombieOffset(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 448 | bool has_enqueued = false; |
| 449 | while (*list != NULL) { |
| 450 | Object* ref = DequeuePendingReference(list); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 451 | Object* referent = ref->GetFieldObject<Object*>(referent_offset, false); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 452 | if (referent != NULL && !IsMarked(referent)) { |
| 453 | MarkObject(referent); |
| 454 | // If the referent is non-null the reference must queuable. |
| 455 | DCHECK(IsEnqueuable(ref)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 456 | ref->SetFieldObject(zombie_offset, referent, false); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 457 | ClearReference(ref); |
| 458 | EnqueueReference(ref); |
| 459 | has_enqueued = true; |
| 460 | } |
| 461 | } |
| 462 | if (has_enqueued) { |
| 463 | ProcessMarkStack(); |
| 464 | } |
| 465 | DCHECK(*list == NULL); |
| 466 | } |
| 467 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 468 | // Process reference class instances and schedule finalizations. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 469 | void MarkSweep::ProcessReferences(Object** soft_references, bool clear_soft, |
| 470 | Object** weak_references, |
| 471 | Object** finalizer_references, |
| 472 | Object** phantom_references) { |
| 473 | DCHECK(soft_references != NULL); |
| 474 | DCHECK(weak_references != NULL); |
| 475 | DCHECK(finalizer_references != NULL); |
| 476 | DCHECK(phantom_references != NULL); |
| 477 | |
| 478 | // Unless we are in the zygote or required to clear soft references |
| 479 | // with white references, preserve some white referents. |
| 480 | if (clear_soft) { |
| 481 | PreserveSomeSoftReferences(soft_references); |
| 482 | } |
| 483 | |
| 484 | // Clear all remaining soft and weak references with white |
| 485 | // referents. |
| 486 | ClearWhiteReferences(soft_references); |
| 487 | ClearWhiteReferences(weak_references); |
| 488 | |
| 489 | // Preserve all white objects with finalize methods and schedule |
| 490 | // them for finalization. |
| 491 | EnqueueFinalizerReferences(finalizer_references); |
| 492 | |
| 493 | // Clear all f-reachable soft and weak references with white |
| 494 | // referents. |
| 495 | ClearWhiteReferences(soft_references); |
| 496 | ClearWhiteReferences(weak_references); |
| 497 | |
| 498 | // Clear all phantom references with white referents. |
| 499 | ClearWhiteReferences(phantom_references); |
| 500 | |
| 501 | // At this point all reference lists should be empty. |
| 502 | DCHECK(*soft_references == NULL); |
| 503 | DCHECK(*weak_references == NULL); |
| 504 | DCHECK(*finalizer_references == NULL); |
| 505 | DCHECK(*phantom_references == NULL); |
| 506 | } |
| 507 | |
| 508 | // Pushes a list of cleared references out to the managed heap. |
| 509 | void MarkSweep::EnqueueClearedReferences(Object** cleared) { |
| 510 | DCHECK(cleared != NULL); |
| 511 | if (*cleared != NULL) { |
| 512 | Thread* self = Thread::Current(); |
| 513 | DCHECK(self != NULL); |
Elliott Hughes | f5ecf06 | 2011-09-06 17:37:59 -0700 | [diff] [blame^] | 514 | // TODO: Method* m = gDvm.methJavaLangRefReferenceQueueAdd; |
| 515 | // DCHECK(m != NULL); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 516 | // Object* reference = *cleared; |
Elliott Hughes | f5ecf06 | 2011-09-06 17:37:59 -0700 | [diff] [blame^] | 517 | // args = {reference} |
| 518 | // TODO: m->Invoke(self, NULL, args, NULL); |
Elliott Hughes | 53b6131 | 2011-08-12 18:28:20 -0700 | [diff] [blame] | 519 | UNIMPLEMENTED(FATAL); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 520 | *cleared = NULL; |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | MarkSweep::~MarkSweep() { |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 525 | delete mark_stack_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 526 | mark_bitmap_->Clear(); |
| 527 | } |
| 528 | |
| 529 | } // namespace art |