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