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); |
Elliott Hughes | 1e20094 | 2011-10-12 18:59:24 -0700 | [diff] [blame] | 147 | Heap::GetLiveBits()->Clear(obj); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 148 | space->Free(obj); |
| 149 | } |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 150 | Heap::RecordFreeLocked(freed_objects, freed_bytes); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 151 | // TODO, unlock heap if concurrent |
| 152 | } |
| 153 | |
| 154 | void MarkSweep::Sweep() { |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 155 | SweepSystemWeaks(); |
| 156 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 157 | const std::vector<Space*>& spaces = Heap::GetSpaces(); |
| 158 | for (size_t i = 0; i < spaces.size(); ++i) { |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 159 | if (!spaces[i]->IsImageSpace()) { |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 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); |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 174 | ScanFields(obj, klass->GetReferenceInstanceOffsets(), false); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | // Scans static storage on a Class. |
| 178 | void MarkSweep::ScanStaticFields(const Class* klass) { |
| 179 | DCHECK(klass != NULL); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 180 | ScanFields(klass, klass->GetReferenceStaticOffsets(), true); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 183 | 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] | 184 | if (ref_offsets != CLASS_WALK_SUPER) { |
| 185 | // Found a reference offset bitmap. Mark the specified offsets. |
| 186 | while (ref_offsets != 0) { |
| 187 | size_t right_shift = CLZ(ref_offsets); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 188 | MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift); |
| 189 | const Object* ref = obj->GetFieldObject<const Object*>(byte_offset, false); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 190 | MarkObject(ref); |
| 191 | ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift); |
| 192 | } |
| 193 | } else { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 194 | // There is no reference offset bitmap. In the non-static case, |
| 195 | // walk up the class inheritance hierarchy and find reference |
| 196 | // offsets the hard way. In the static case, just consider this |
| 197 | // class. |
| 198 | for (const Class* klass = is_static ? obj->AsClass() : obj->GetClass(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 199 | klass != NULL; |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 200 | klass = is_static ? NULL : klass->GetSuperClass()) { |
| 201 | size_t num_reference_fields = (is_static |
| 202 | ? klass->NumReferenceStaticFields() |
| 203 | : klass->NumReferenceInstanceFields()); |
| 204 | for (size_t i = 0; i < num_reference_fields; ++i) { |
| 205 | Field* field = (is_static |
| 206 | ? klass->GetStaticField(i) |
| 207 | : klass->GetInstanceField(i)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 208 | MemberOffset field_offset = field->GetOffset(); |
| 209 | const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 210 | MarkObject(ref); |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 216 | // Scans the header, static field references, and interface pointers |
| 217 | // of a class object. |
| 218 | void MarkSweep::ScanClass(const Object* obj) { |
| 219 | DCHECK(obj != NULL); |
| 220 | DCHECK(obj->IsClass()); |
| 221 | const Class* klass = obj->AsClass(); |
| 222 | MarkObject(klass->GetClass()); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 223 | ScanInstanceFields(obj); |
| 224 | MarkObject(klass->GetDescriptor()); |
| 225 | MarkObject(klass->GetDexCache()); |
| 226 | MarkObject(klass->GetVerifyErrorClass()); |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 227 | if (klass->IsArrayClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 228 | MarkObject(klass->GetComponentType()); |
| 229 | } |
| 230 | if (klass->IsLoaded()) { |
| 231 | MarkObject(klass->GetSuperClass()); |
| 232 | } |
| 233 | MarkObject(klass->GetClassLoader()); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 234 | if (klass->IsLoaded()) { |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 235 | MarkObject(klass->GetInterfaces()); |
| 236 | MarkObject(klass->GetDirectMethods()); |
| 237 | MarkObject(klass->GetVirtualMethods()); |
| 238 | MarkObject(klass->GetIFields()); |
| 239 | MarkObject(klass->GetSFields()); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 240 | } |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 241 | ScanStaticFields(klass); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | // Scans the header of all array objects. If the array object is |
| 245 | // specialized to a reference type, scans the array data as well. |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 246 | void MarkSweep::ScanArray(const Object* obj) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 247 | DCHECK(obj != NULL); |
| 248 | DCHECK(obj->GetClass() != NULL); |
| 249 | MarkObject(obj->GetClass()); |
| 250 | if (obj->IsObjectArray()) { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 251 | const ObjectArray<Object>* array = obj->AsObjectArray<Object>(); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 252 | for (int32_t i = 0; i < array->GetLength(); ++i) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 253 | const Object* element = array->Get(i); |
| 254 | MarkObject(element); |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 259 | // Process the "referent" field in a java.lang.ref.Reference. If the |
| 260 | // referent has not yet been marked, put it on the appropriate list in |
| 261 | // the gcHeap for later processing. |
| 262 | void MarkSweep::DelayReferenceReferent(Object* obj) { |
| 263 | DCHECK(obj != NULL); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 264 | Class* klass = obj->GetClass(); |
| 265 | DCHECK(klass != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 266 | DCHECK(klass->IsReferenceClass()); |
| 267 | Object* pending = obj->GetFieldObject<Object*>(Heap::GetReferencePendingNextOffset(), false); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 268 | Object* referent = Heap::GetReferenceReferent(obj); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 269 | if (pending == NULL && referent != NULL && !IsMarked(referent)) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 270 | Object** list = NULL; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 271 | if (klass->IsSoftReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 272 | list = &soft_reference_list_; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 273 | } else if (klass->IsWeakReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 274 | list = &weak_reference_list_; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 275 | } else if (klass->IsFinalizerReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 276 | list = &finalizer_reference_list_; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 277 | } else if (klass->IsPhantomReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 278 | list = &phantom_reference_list_; |
| 279 | } |
Brian Carlstrom | 0796af0 | 2011-10-12 14:31:45 -0700 | [diff] [blame] | 280 | DCHECK(list != NULL) << PrettyClass(klass) << " " << std::hex << klass->GetAccessFlags(); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 281 | Heap::EnqueuePendingReference(obj, list); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | |
| 285 | // Scans the header and field references of a data object. If the |
| 286 | // scanned object is a reference subclass, it is scheduled for later |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 287 | // processing. |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 288 | void MarkSweep::ScanOther(const Object* obj) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 289 | DCHECK(obj != NULL); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 290 | Class* klass = obj->GetClass(); |
| 291 | DCHECK(klass != NULL); |
| 292 | MarkObject(klass); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 293 | ScanInstanceFields(obj); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 294 | if (klass->IsReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 295 | DelayReferenceReferent(const_cast<Object*>(obj)); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | // Scans an object reference. Determines the type of the reference |
| 300 | // and dispatches to a specialized scanning routine. |
| 301 | void MarkSweep::ScanObject(const Object* obj) { |
| 302 | DCHECK(obj != NULL); |
| 303 | DCHECK(obj->GetClass() != NULL); |
| 304 | DCHECK(IsMarked(obj)); |
| 305 | if (obj->IsClass()) { |
| 306 | ScanClass(obj); |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 307 | } else if (obj->IsArrayInstance()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 308 | ScanArray(obj); |
| 309 | } else { |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 310 | ScanOther(obj); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
| 314 | // Scan anything that's on the mark stack. We can't use the bitmaps |
| 315 | // anymore, so use a finger that points past the end of them. |
| 316 | void MarkSweep::ProcessMarkStack() { |
| 317 | while (!mark_stack_->IsEmpty()) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 318 | const Object* obj = mark_stack_->Pop(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 319 | ScanObject(obj); |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | void MarkSweep::ScanDirtyObjects() { |
| 324 | ProcessMarkStack(); |
| 325 | } |
| 326 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 327 | // Walks the reference list marking any references subject to the |
| 328 | // reference clearing policy. References with a black referent are |
| 329 | // removed from the list. References with white referents biased |
| 330 | // toward saving are blackened and also removed from the list. |
| 331 | void MarkSweep::PreserveSomeSoftReferences(Object** list) { |
| 332 | DCHECK(list != NULL); |
| 333 | Object* clear = NULL; |
| 334 | size_t counter = 0; |
| 335 | while (*list != NULL) { |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 336 | Object* ref = Heap::DequeuePendingReference(list); |
| 337 | Object* referent = Heap::GetReferenceReferent(ref); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 338 | if (referent == NULL) { |
| 339 | // Referent was cleared by the user during marking. |
| 340 | continue; |
| 341 | } |
| 342 | bool is_marked = IsMarked(referent); |
| 343 | if (!is_marked && ((++counter) & 1)) { |
| 344 | // Referent is white and biased toward saving, mark it. |
| 345 | MarkObject(referent); |
| 346 | is_marked = true; |
| 347 | } |
| 348 | if (!is_marked) { |
| 349 | // Referent is white, queue it for clearing. |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 350 | Heap::EnqueuePendingReference(ref, &clear); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 351 | } |
| 352 | } |
| 353 | *list = clear; |
| 354 | // Restart the mark with the newly black references added to the |
| 355 | // root set. |
| 356 | ProcessMarkStack(); |
| 357 | } |
| 358 | |
| 359 | // Unlink the reference list clearing references objects with white |
| 360 | // referents. Cleared references registered to a reference queue are |
| 361 | // scheduled for appending by the heap worker thread. |
| 362 | void MarkSweep::ClearWhiteReferences(Object** list) { |
| 363 | DCHECK(list != NULL); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 364 | while (*list != NULL) { |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 365 | Object* ref = Heap::DequeuePendingReference(list); |
| 366 | Object* referent = Heap::GetReferenceReferent(ref); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 367 | if (referent != NULL && !IsMarked(referent)) { |
| 368 | // Referent is white, clear it. |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 369 | Heap::ClearReferenceReferent(ref); |
| 370 | if (Heap::IsEnqueuable(ref)) { |
| 371 | Heap::EnqueueReference(ref, &cleared_reference_list_); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | } |
| 375 | DCHECK(*list == NULL); |
| 376 | } |
| 377 | |
| 378 | // Enqueues finalizer references with white referents. White |
| 379 | // referents are blackened, moved to the zombie field, and the |
| 380 | // referent field is cleared. |
| 381 | void MarkSweep::EnqueueFinalizerReferences(Object** list) { |
| 382 | DCHECK(list != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 383 | MemberOffset zombie_offset = Heap::GetFinalizerReferenceZombieOffset(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 384 | bool has_enqueued = false; |
| 385 | while (*list != NULL) { |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 386 | Object* ref = Heap::DequeuePendingReference(list); |
| 387 | Object* referent = Heap::GetReferenceReferent(ref); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 388 | if (referent != NULL && !IsMarked(referent)) { |
| 389 | MarkObject(referent); |
| 390 | // If the referent is non-null the reference must queuable. |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 391 | DCHECK(Heap::IsEnqueuable(ref)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 392 | ref->SetFieldObject(zombie_offset, referent, false); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 393 | Heap::ClearReferenceReferent(ref); |
| 394 | Heap::EnqueueReference(ref, &cleared_reference_list_); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 395 | has_enqueued = true; |
| 396 | } |
| 397 | } |
| 398 | if (has_enqueued) { |
| 399 | ProcessMarkStack(); |
| 400 | } |
| 401 | DCHECK(*list == NULL); |
| 402 | } |
| 403 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 404 | // Process reference class instances and schedule finalizations. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 405 | void MarkSweep::ProcessReferences(Object** soft_references, bool clear_soft, |
| 406 | Object** weak_references, |
| 407 | Object** finalizer_references, |
| 408 | Object** phantom_references) { |
| 409 | DCHECK(soft_references != NULL); |
| 410 | DCHECK(weak_references != NULL); |
| 411 | DCHECK(finalizer_references != NULL); |
| 412 | DCHECK(phantom_references != NULL); |
| 413 | |
| 414 | // Unless we are in the zygote or required to clear soft references |
| 415 | // with white references, preserve some white referents. |
| 416 | if (clear_soft) { |
| 417 | PreserveSomeSoftReferences(soft_references); |
| 418 | } |
| 419 | |
| 420 | // Clear all remaining soft and weak references with white |
| 421 | // referents. |
| 422 | ClearWhiteReferences(soft_references); |
| 423 | ClearWhiteReferences(weak_references); |
| 424 | |
| 425 | // Preserve all white objects with finalize methods and schedule |
| 426 | // them for finalization. |
| 427 | EnqueueFinalizerReferences(finalizer_references); |
| 428 | |
| 429 | // Clear all f-reachable soft and weak references with white |
| 430 | // referents. |
| 431 | ClearWhiteReferences(soft_references); |
| 432 | ClearWhiteReferences(weak_references); |
| 433 | |
| 434 | // Clear all phantom references with white referents. |
| 435 | ClearWhiteReferences(phantom_references); |
| 436 | |
| 437 | // At this point all reference lists should be empty. |
| 438 | DCHECK(*soft_references == NULL); |
| 439 | DCHECK(*weak_references == NULL); |
| 440 | DCHECK(*finalizer_references == NULL); |
| 441 | DCHECK(*phantom_references == NULL); |
| 442 | } |
| 443 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 444 | MarkSweep::~MarkSweep() { |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 445 | delete mark_stack_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 446 | mark_bitmap_->Clear(); |
| 447 | } |
| 448 | |
| 449 | } // namespace art |