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