Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 17 | #include "mark_sweep.h" |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 18 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 19 | #include <climits> |
| 20 | #include <vector> |
| 21 | |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 22 | #include "class_loader.h" |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 23 | #include "dex_cache.h" |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 24 | #include "heap.h" |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 25 | #include "indirect_reference_table.h" |
| 26 | #include "intern_table.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 27 | #include "logging.h" |
| 28 | #include "macros.h" |
| 29 | #include "mark_stack.h" |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 30 | #include "monitor.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 31 | #include "object.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 32 | #include "runtime.h" |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 33 | #include "space.h" |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 34 | #include "timing_logger.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 35 | #include "thread.h" |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 36 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 37 | namespace art { |
| 38 | |
Jesse Wilson | 078f9b0 | 2011-11-18 17:51:47 -0500 | [diff] [blame] | 39 | void MarkSweep::Init() { |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 40 | mark_stack_ = MarkStack::Create(); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 41 | |
| 42 | heap_ = Runtime::Current()->GetHeap(); |
| 43 | mark_bitmap_ = heap_->GetMarkBits(); |
| 44 | live_bitmap_ = heap_->GetLiveBits(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 45 | |
| 46 | // TODO: if concurrent, clear the card table. |
| 47 | |
buzbee | 0d966cf | 2011-09-08 17:34:58 -0700 | [diff] [blame] | 48 | // TODO: if concurrent, enable card marking in compiler |
| 49 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 50 | // TODO: check that the mark bitmap is entirely clear. |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Elliott Hughes | b066311 | 2011-10-19 18:16:37 -0700 | [diff] [blame] | 53 | inline void MarkSweep::MarkObject0(const Object* obj, bool check_finger) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 54 | DCHECK(obj != NULL); |
| 55 | if (obj < condemned_) { |
| 56 | DCHECK(IsMarked(obj)); |
| 57 | return; |
| 58 | } |
| 59 | bool is_marked = mark_bitmap_->Test(obj); |
| 60 | // This object was not previously marked. |
| 61 | if (!is_marked) { |
| 62 | mark_bitmap_->Set(obj); |
| 63 | if (check_finger && obj < finger_) { |
| 64 | // The object must be pushed on to the mark stack. |
| 65 | mark_stack_->Push(obj); |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // Used to mark objects when recursing. Recursion is done by moving |
| 71 | // the finger across the bitmaps in address order and marking child |
| 72 | // objects. Any newly-marked objects whose addresses are lower than |
| 73 | // the finger won't be visited by the bitmap scan, so those objects |
| 74 | // need to be added to the mark stack. |
Elliott Hughes | b066311 | 2011-10-19 18:16:37 -0700 | [diff] [blame] | 75 | inline void MarkSweep::MarkObject(const Object* obj) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 76 | if (obj != NULL) { |
| 77 | MarkObject0(obj, true); |
| 78 | } |
| 79 | } |
| 80 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 81 | void MarkSweep::MarkObjectVisitor(const Object* root, void* arg) { |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 82 | DCHECK(root != NULL); |
| 83 | DCHECK(arg != NULL); |
| 84 | MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 85 | DCHECK(mark_sweep->finger_ == NULL); // no point to check finger if it is NULL |
| 86 | mark_sweep->MarkObject0(root, false); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 89 | // Marks all objects in the root set. |
| 90 | void MarkSweep::MarkRoots() { |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 91 | Runtime::Current()->VisitRoots(MarkObjectVisitor, this); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 94 | void MarkSweep::ScanImageRootVisitor(Object* root, void* arg) { |
| 95 | DCHECK(root != NULL); |
| 96 | DCHECK(arg != NULL); |
| 97 | MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg); |
| 98 | DCHECK(mark_sweep->finger_ == NULL); // no point to check finger if it is NULL |
| 99 | mark_sweep->MarkObject0(root, false); |
| 100 | mark_sweep->ScanObject(root); |
| 101 | } |
| 102 | |
| 103 | // Marks all objects that are in images and have been touched by the mutator |
| 104 | void MarkSweep::ScanDirtyImageRoots() { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 105 | const std::vector<Space*>& spaces = heap_->GetSpaces(); |
| 106 | CardTable* card_table = heap_->GetCardTable(); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 107 | for (size_t i = 0; i < spaces.size(); ++i) { |
| 108 | if (spaces[i]->IsImageSpace()) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 109 | byte* begin = spaces[i]->Begin(); |
| 110 | byte* end = spaces[i]->End(); |
| 111 | card_table->Scan(begin, end, ScanImageRootVisitor, this); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | void MarkSweep::CheckBitmapCallback(Object* obj, void* finger, void* arg) { |
| 117 | MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg); |
| 118 | mark_sweep->finger_ = reinterpret_cast<Object*>(finger); |
| 119 | mark_sweep->CheckObject(obj); |
| 120 | } |
| 121 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 122 | void MarkSweep::ScanBitmapCallback(Object* obj, void* finger, void* arg) { |
| 123 | MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg); |
| 124 | mark_sweep->finger_ = reinterpret_cast<Object*>(finger); |
| 125 | mark_sweep->ScanObject(obj); |
| 126 | } |
| 127 | |
| 128 | // Populates the mark stack based on the set of marked objects and |
| 129 | // recursively marks until the mark stack is emptied. |
| 130 | void MarkSweep::RecursiveMark() { |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 131 | // RecursiveMark will build the lists of known instances of the Reference classes. |
| 132 | // See DelayReferenceReferent for details. |
| 133 | CHECK(soft_reference_list_ == NULL); |
| 134 | CHECK(weak_reference_list_ == NULL); |
| 135 | CHECK(finalizer_reference_list_ == NULL); |
| 136 | CHECK(phantom_reference_list_ == NULL); |
| 137 | CHECK(cleared_reference_list_ == NULL); |
| 138 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 139 | void* arg = reinterpret_cast<void*>(this); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 140 | const std::vector<Space*>& spaces = heap_->GetSpaces(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 141 | for (size_t i = 0; i < spaces.size(); ++i) { |
TDYa127 | 540a5b7 | 2012-04-03 18:56:08 -0700 | [diff] [blame] | 142 | #if !defined(ART_USE_LLVM_COMPILER) |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 143 | #ifndef NDEBUG |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 144 | uintptr_t begin = reinterpret_cast<uintptr_t>(spaces[i]->Begin()); |
| 145 | uintptr_t end = reinterpret_cast<uintptr_t>(spaces[i]->End()); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 146 | if (!spaces[i]->IsImageSpace()) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 147 | mark_bitmap_->ScanWalk(begin, end, &MarkSweep::ScanBitmapCallback, arg); |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 148 | } else { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 149 | mark_bitmap_->ScanWalk(begin, end, &MarkSweep::CheckBitmapCallback, arg); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 150 | } |
| 151 | #else |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 152 | if (!spaces[i]->IsImageSpace()) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 153 | uintptr_t begin = reinterpret_cast<uintptr_t>(spaces[i]->Begin()); |
| 154 | uintptr_t end = reinterpret_cast<uintptr_t>(spaces[i]->End()); |
| 155 | mark_bitmap_->ScanWalk(begin, end, &MarkSweep::ScanBitmapCallback, arg); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 156 | } |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 157 | #endif |
TDYa127 | 540a5b7 | 2012-04-03 18:56:08 -0700 | [diff] [blame] | 158 | #else |
| 159 | // TODO: Implement card marking. |
| 160 | uintptr_t begin = reinterpret_cast<uintptr_t>(spaces[i]->Begin()); |
| 161 | uintptr_t end = reinterpret_cast<uintptr_t>(spaces[i]->End()); |
| 162 | mark_bitmap_->ScanWalk(begin, end, &MarkSweep::ScanBitmapCallback, arg); |
| 163 | #endif |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 164 | } |
| 165 | finger_ = reinterpret_cast<Object*>(~0); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 166 | // TODO: tune the frequency of emptying the mark stack |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 167 | ProcessMarkStack(); |
| 168 | } |
| 169 | |
| 170 | void MarkSweep::ReMarkRoots() { |
Elliott Hughes | 53b6131 | 2011-08-12 18:28:20 -0700 | [diff] [blame] | 171 | UNIMPLEMENTED(FATAL); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 174 | void MarkSweep::SweepJniWeakGlobals() { |
| 175 | JavaVMExt* vm = Runtime::Current()->GetJavaVM(); |
| 176 | MutexLock mu(vm->weak_globals_lock); |
| 177 | IndirectReferenceTable* table = &vm->weak_globals; |
| 178 | typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto |
| 179 | for (It it = table->begin(), end = table->end(); it != end; ++it) { |
| 180 | const Object** entry = *it; |
| 181 | if (!IsMarked(*entry)) { |
| 182 | *entry = kClearedJniWeakGlobal; |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 187 | void MarkSweep::SweepSystemWeaks() { |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 188 | Runtime::Current()->GetInternTable()->SweepInternTableWeaks(IsMarked, this); |
| 189 | Runtime::Current()->GetMonitorList()->SweepMonitorList(IsMarked, this); |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 190 | SweepJniWeakGlobals(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 193 | struct SweepCallbackContext { |
| 194 | Heap* heap; |
| 195 | AllocSpace* space; |
| 196 | }; |
| 197 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 198 | void MarkSweep::SweepCallback(size_t num_ptrs, Object** ptrs, void* arg) { |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 199 | // TODO: lock heap if concurrent |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 200 | size_t freed_objects = num_ptrs; |
| 201 | size_t freed_bytes = 0; |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 202 | SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg); |
| 203 | Heap* heap = context->heap; |
| 204 | AllocSpace* space = context->space; |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 205 | // Use a bulk free, that merges consecutive objects before freeing or free per object? |
| 206 | // Documentation suggests better free performance with merging, but this may be at the expensive |
| 207 | // of allocation. |
| 208 | // TODO: investigate performance |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 209 | static const bool kUseFreeList = true; |
| 210 | if (kUseFreeList) { |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 211 | for (size_t i = 0; i < num_ptrs; ++i) { |
| 212 | Object* obj = static_cast<Object*>(ptrs[i]); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 213 | freed_bytes += space->AllocationSize(obj); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 214 | heap->GetLiveBits()->Clear(obj); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 215 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 216 | // AllocSpace::FreeList clears the value in ptrs, so perform after clearing the live bit |
| 217 | space->FreeList(num_ptrs, ptrs); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 218 | } else { |
| 219 | for (size_t i = 0; i < num_ptrs; ++i) { |
| 220 | Object* obj = static_cast<Object*>(ptrs[i]); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 221 | freed_bytes += space->AllocationSize(obj); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 222 | heap->GetLiveBits()->Clear(obj); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 223 | space->Free(obj); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 224 | } |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 225 | } |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 226 | heap->RecordFreeLocked(freed_objects, freed_bytes); |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 227 | // TODO: unlock heap if concurrent |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | void MarkSweep::Sweep() { |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 231 | SweepSystemWeaks(); |
| 232 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 233 | const std::vector<Space*>& spaces = heap_->GetSpaces(); |
| 234 | SweepCallbackContext scc; |
| 235 | scc.heap = heap_; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 236 | for (size_t i = 0; i < spaces.size(); ++i) { |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 237 | if (!spaces[i]->IsImageSpace()) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 238 | uintptr_t begin = reinterpret_cast<uintptr_t>(spaces[i]->Begin()); |
| 239 | uintptr_t end = reinterpret_cast<uintptr_t>(spaces[i]->End()); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 240 | scc.space = spaces[i]->AsAllocSpace(); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 241 | HeapBitmap::SweepWalk(*live_bitmap_, *mark_bitmap_, begin, end, |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 242 | &MarkSweep::SweepCallback, reinterpret_cast<void*>(&scc)); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 247 | // Scans instance fields. |
Elliott Hughes | b066311 | 2011-10-19 18:16:37 -0700 | [diff] [blame] | 248 | inline void MarkSweep::ScanInstanceFields(const Object* obj) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 249 | DCHECK(obj != NULL); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 250 | Class* klass = obj->GetClass(); |
| 251 | DCHECK(klass != NULL); |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 252 | ScanFields(obj, klass->GetReferenceInstanceOffsets(), false); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 255 | inline void MarkSweep::CheckInstanceFields(const Object* obj) { |
| 256 | Class* klass = obj->GetClass(); |
| 257 | CheckFields(obj, klass->GetReferenceInstanceOffsets(), false); |
| 258 | } |
| 259 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 260 | // Scans static storage on a Class. |
Elliott Hughes | b066311 | 2011-10-19 18:16:37 -0700 | [diff] [blame] | 261 | inline void MarkSweep::ScanStaticFields(const Class* klass) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 262 | DCHECK(klass != NULL); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 263 | ScanFields(klass, klass->GetReferenceStaticOffsets(), true); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 266 | inline void MarkSweep::CheckStaticFields(const Class* klass) { |
| 267 | CheckFields(klass, klass->GetReferenceStaticOffsets(), true); |
| 268 | } |
| 269 | |
Elliott Hughes | b066311 | 2011-10-19 18:16:37 -0700 | [diff] [blame] | 270 | inline 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] | 271 | if (ref_offsets != CLASS_WALK_SUPER) { |
| 272 | // Found a reference offset bitmap. Mark the specified offsets. |
| 273 | while (ref_offsets != 0) { |
| 274 | size_t right_shift = CLZ(ref_offsets); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 275 | MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift); |
| 276 | const Object* ref = obj->GetFieldObject<const Object*>(byte_offset, false); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 277 | MarkObject(ref); |
| 278 | ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift); |
| 279 | } |
| 280 | } else { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 281 | // There is no reference offset bitmap. In the non-static case, |
| 282 | // walk up the class inheritance hierarchy and find reference |
| 283 | // offsets the hard way. In the static case, just consider this |
| 284 | // class. |
| 285 | for (const Class* klass = is_static ? obj->AsClass() : obj->GetClass(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 286 | klass != NULL; |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 287 | klass = is_static ? NULL : klass->GetSuperClass()) { |
| 288 | size_t num_reference_fields = (is_static |
| 289 | ? klass->NumReferenceStaticFields() |
| 290 | : klass->NumReferenceInstanceFields()); |
| 291 | for (size_t i = 0; i < num_reference_fields; ++i) { |
| 292 | Field* field = (is_static |
| 293 | ? klass->GetStaticField(i) |
| 294 | : klass->GetInstanceField(i)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 295 | MemberOffset field_offset = field->GetOffset(); |
| 296 | const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 297 | MarkObject(ref); |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 303 | inline void MarkSweep::CheckReference(const Object* obj, const Object* ref, MemberOffset offset, bool is_static) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 304 | AllocSpace* alloc_space = heap_->GetAllocSpace(); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 305 | if (alloc_space->Contains(ref)) { |
| 306 | bool is_marked = mark_bitmap_->Test(ref); |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 307 | if (!is_marked) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 308 | LOG(INFO) << *alloc_space; |
| 309 | LOG(WARNING) << (is_static ? "Static ref'" : "Instance ref'") << PrettyTypeOf(ref) |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 310 | << "' (" << reinterpret_cast<const void*>(ref) << ") in '" << PrettyTypeOf(obj) |
| 311 | << "' (" << reinterpret_cast<const void*>(obj) << ") at offset " |
| 312 | << reinterpret_cast<void*>(offset.Int32Value()) << " wasn't marked"; |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 313 | bool obj_marked = heap_->GetCardTable()->IsDirty(obj); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 314 | if (!obj_marked) { |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 315 | LOG(WARNING) << "Object '" << PrettyTypeOf(obj) << "' " |
| 316 | << "(" << reinterpret_cast<const void*>(obj) << ") contains references to " |
| 317 | << "the alloc space, but wasn't card marked"; |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | inline void MarkSweep::CheckFields(const Object* obj, uint32_t ref_offsets, bool is_static) { |
| 324 | if (ref_offsets != CLASS_WALK_SUPER) { |
| 325 | // Found a reference offset bitmap. Mark the specified offsets. |
| 326 | while (ref_offsets != 0) { |
| 327 | size_t right_shift = CLZ(ref_offsets); |
| 328 | MemberOffset field_offset = CLASS_OFFSET_FROM_CLZ(right_shift); |
| 329 | const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false); |
| 330 | CheckReference(obj, ref, field_offset, is_static); |
| 331 | ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift); |
| 332 | } |
| 333 | } else { |
| 334 | // There is no reference offset bitmap. In the non-static case, |
| 335 | // walk up the class inheritance hierarchy and find reference |
| 336 | // offsets the hard way. In the static case, just consider this |
| 337 | // class. |
| 338 | for (const Class* klass = is_static ? obj->AsClass() : obj->GetClass(); |
| 339 | klass != NULL; |
| 340 | klass = is_static ? NULL : klass->GetSuperClass()) { |
| 341 | size_t num_reference_fields = (is_static |
| 342 | ? klass->NumReferenceStaticFields() |
| 343 | : klass->NumReferenceInstanceFields()); |
| 344 | for (size_t i = 0; i < num_reference_fields; ++i) { |
| 345 | Field* field = (is_static |
| 346 | ? klass->GetStaticField(i) |
| 347 | : klass->GetInstanceField(i)); |
| 348 | MemberOffset field_offset = field->GetOffset(); |
| 349 | const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false); |
| 350 | CheckReference(obj, ref, field_offset, is_static); |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 356 | // Scans the header, static field references, and interface pointers |
| 357 | // of a class object. |
Elliott Hughes | b066311 | 2011-10-19 18:16:37 -0700 | [diff] [blame] | 358 | inline void MarkSweep::ScanClass(const Object* obj) { |
Elliott Hughes | 352a424 | 2011-10-31 15:15:21 -0700 | [diff] [blame] | 359 | #ifndef NDEBUG |
| 360 | ++class_count_; |
| 361 | #endif |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 362 | ScanInstanceFields(obj); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 363 | ScanStaticFields(obj->AsClass()); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 364 | } |
| 365 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 366 | inline void MarkSweep::CheckClass(const Object* obj) { |
| 367 | CheckInstanceFields(obj); |
| 368 | CheckStaticFields(obj->AsClass()); |
| 369 | } |
| 370 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 371 | // Scans the header of all array objects. If the array object is |
| 372 | // specialized to a reference type, scans the array data as well. |
Elliott Hughes | b066311 | 2011-10-19 18:16:37 -0700 | [diff] [blame] | 373 | inline void MarkSweep::ScanArray(const Object* obj) { |
Elliott Hughes | 352a424 | 2011-10-31 15:15:21 -0700 | [diff] [blame] | 374 | #ifndef NDEBUG |
| 375 | ++array_count_; |
| 376 | #endif |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 377 | MarkObject(obj->GetClass()); |
| 378 | if (obj->IsObjectArray()) { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 379 | const ObjectArray<Object>* array = obj->AsObjectArray<Object>(); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 380 | for (int32_t i = 0; i < array->GetLength(); ++i) { |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 381 | const Object* element = array->GetWithoutChecks(i); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 382 | MarkObject(element); |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 387 | inline void MarkSweep::CheckArray(const Object* obj) { |
| 388 | CheckReference(obj, obj->GetClass(), Object::ClassOffset(), false); |
| 389 | if (obj->IsObjectArray()) { |
| 390 | const ObjectArray<Object>* array = obj->AsObjectArray<Object>(); |
| 391 | for (int32_t i = 0; i < array->GetLength(); ++i) { |
| 392 | const Object* element = array->GetWithoutChecks(i); |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 393 | size_t width = sizeof(Object*); |
| 394 | CheckReference(obj, element, MemberOffset(i * width + |
| 395 | Array::DataOffset(width).Int32Value()), false); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 396 | } |
| 397 | } |
| 398 | } |
| 399 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 400 | // Process the "referent" field in a java.lang.ref.Reference. If the |
| 401 | // referent has not yet been marked, put it on the appropriate list in |
| 402 | // the gcHeap for later processing. |
| 403 | void MarkSweep::DelayReferenceReferent(Object* obj) { |
| 404 | DCHECK(obj != NULL); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 405 | Class* klass = obj->GetClass(); |
| 406 | DCHECK(klass != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 407 | DCHECK(klass->IsReferenceClass()); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 408 | Object* pending = obj->GetFieldObject<Object*>(heap_->GetReferencePendingNextOffset(), false); |
| 409 | Object* referent = heap_->GetReferenceReferent(obj); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 410 | if (pending == NULL && referent != NULL && !IsMarked(referent)) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 411 | Object** list = NULL; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 412 | if (klass->IsSoftReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 413 | list = &soft_reference_list_; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 414 | } else if (klass->IsWeakReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 415 | list = &weak_reference_list_; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 416 | } else if (klass->IsFinalizerReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 417 | list = &finalizer_reference_list_; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 418 | } else if (klass->IsPhantomReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 419 | list = &phantom_reference_list_; |
| 420 | } |
Brian Carlstrom | 0796af0 | 2011-10-12 14:31:45 -0700 | [diff] [blame] | 421 | DCHECK(list != NULL) << PrettyClass(klass) << " " << std::hex << klass->GetAccessFlags(); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 422 | heap_->EnqueuePendingReference(obj, list); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | |
| 426 | // Scans the header and field references of a data object. If the |
| 427 | // scanned object is a reference subclass, it is scheduled for later |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 428 | // processing. |
Elliott Hughes | b066311 | 2011-10-19 18:16:37 -0700 | [diff] [blame] | 429 | inline void MarkSweep::ScanOther(const Object* obj) { |
Elliott Hughes | 352a424 | 2011-10-31 15:15:21 -0700 | [diff] [blame] | 430 | #ifndef NDEBUG |
| 431 | ++other_count_; |
| 432 | #endif |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 433 | ScanInstanceFields(obj); |
Elliott Hughes | 352a424 | 2011-10-31 15:15:21 -0700 | [diff] [blame] | 434 | if (obj->GetClass()->IsReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 435 | DelayReferenceReferent(const_cast<Object*>(obj)); |
| 436 | } |
| 437 | } |
| 438 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 439 | inline void MarkSweep::CheckOther(const Object* obj) { |
| 440 | CheckInstanceFields(obj); |
| 441 | } |
| 442 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 443 | // Scans an object reference. Determines the type of the reference |
| 444 | // and dispatches to a specialized scanning routine. |
Elliott Hughes | b066311 | 2011-10-19 18:16:37 -0700 | [diff] [blame] | 445 | inline void MarkSweep::ScanObject(const Object* obj) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 446 | DCHECK(obj != NULL); |
| 447 | DCHECK(obj->GetClass() != NULL); |
| 448 | DCHECK(IsMarked(obj)); |
| 449 | if (obj->IsClass()) { |
| 450 | ScanClass(obj); |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 451 | } else if (obj->IsArrayInstance()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 452 | ScanArray(obj); |
| 453 | } else { |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 454 | ScanOther(obj); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 455 | } |
| 456 | } |
| 457 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 458 | // Check to see that all alloc space references are marked for the given object |
| 459 | inline void MarkSweep::CheckObject(const Object* obj) { |
| 460 | DCHECK(obj != NULL); |
| 461 | DCHECK(obj->GetClass() != NULL); |
| 462 | DCHECK(IsMarked(obj)); |
| 463 | if (obj->IsClass()) { |
| 464 | CheckClass(obj); |
| 465 | } else if (obj->IsArrayInstance()) { |
| 466 | CheckArray(obj); |
| 467 | } else { |
| 468 | CheckOther(obj); |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | // Scan anything that's on the mark stack. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 473 | void MarkSweep::ProcessMarkStack() { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 474 | Space* alloc_space = heap_->GetAllocSpace(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 475 | while (!mark_stack_->IsEmpty()) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 476 | const Object* obj = mark_stack_->Pop(); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 477 | if (alloc_space->Contains(obj)) { |
| 478 | ScanObject(obj); |
| 479 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 480 | } |
| 481 | } |
| 482 | |
| 483 | void MarkSweep::ScanDirtyObjects() { |
| 484 | ProcessMarkStack(); |
| 485 | } |
| 486 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 487 | // Walks the reference list marking any references subject to the |
| 488 | // reference clearing policy. References with a black referent are |
| 489 | // removed from the list. References with white referents biased |
| 490 | // toward saving are blackened and also removed from the list. |
| 491 | void MarkSweep::PreserveSomeSoftReferences(Object** list) { |
| 492 | DCHECK(list != NULL); |
| 493 | Object* clear = NULL; |
| 494 | size_t counter = 0; |
| 495 | while (*list != NULL) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 496 | Object* ref = heap_->DequeuePendingReference(list); |
| 497 | Object* referent = heap_->GetReferenceReferent(ref); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 498 | if (referent == NULL) { |
| 499 | // Referent was cleared by the user during marking. |
| 500 | continue; |
| 501 | } |
| 502 | bool is_marked = IsMarked(referent); |
| 503 | if (!is_marked && ((++counter) & 1)) { |
| 504 | // Referent is white and biased toward saving, mark it. |
| 505 | MarkObject(referent); |
| 506 | is_marked = true; |
| 507 | } |
| 508 | if (!is_marked) { |
| 509 | // Referent is white, queue it for clearing. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 510 | heap_->EnqueuePendingReference(ref, &clear); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 511 | } |
| 512 | } |
| 513 | *list = clear; |
| 514 | // Restart the mark with the newly black references added to the |
| 515 | // root set. |
| 516 | ProcessMarkStack(); |
| 517 | } |
| 518 | |
| 519 | // Unlink the reference list clearing references objects with white |
| 520 | // referents. Cleared references registered to a reference queue are |
| 521 | // scheduled for appending by the heap worker thread. |
| 522 | void MarkSweep::ClearWhiteReferences(Object** list) { |
| 523 | DCHECK(list != NULL); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 524 | while (*list != NULL) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 525 | Object* ref = heap_->DequeuePendingReference(list); |
| 526 | Object* referent = heap_->GetReferenceReferent(ref); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 527 | if (referent != NULL && !IsMarked(referent)) { |
| 528 | // Referent is white, clear it. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 529 | heap_->ClearReferenceReferent(ref); |
| 530 | if (heap_->IsEnqueuable(ref)) { |
| 531 | heap_->EnqueueReference(ref, &cleared_reference_list_); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 532 | } |
| 533 | } |
| 534 | } |
| 535 | DCHECK(*list == NULL); |
| 536 | } |
| 537 | |
| 538 | // Enqueues finalizer references with white referents. White |
| 539 | // referents are blackened, moved to the zombie field, and the |
| 540 | // referent field is cleared. |
| 541 | void MarkSweep::EnqueueFinalizerReferences(Object** list) { |
| 542 | DCHECK(list != NULL); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 543 | MemberOffset zombie_offset = heap_->GetFinalizerReferenceZombieOffset(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 544 | bool has_enqueued = false; |
| 545 | while (*list != NULL) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 546 | Object* ref = heap_->DequeuePendingReference(list); |
| 547 | Object* referent = heap_->GetReferenceReferent(ref); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 548 | if (referent != NULL && !IsMarked(referent)) { |
| 549 | MarkObject(referent); |
| 550 | // If the referent is non-null the reference must queuable. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 551 | DCHECK(heap_->IsEnqueuable(ref)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 552 | ref->SetFieldObject(zombie_offset, referent, false); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 553 | heap_->ClearReferenceReferent(ref); |
| 554 | heap_->EnqueueReference(ref, &cleared_reference_list_); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 555 | has_enqueued = true; |
| 556 | } |
| 557 | } |
| 558 | if (has_enqueued) { |
| 559 | ProcessMarkStack(); |
| 560 | } |
| 561 | DCHECK(*list == NULL); |
| 562 | } |
| 563 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 564 | // Process reference class instances and schedule finalizations. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 565 | void MarkSweep::ProcessReferences(Object** soft_references, bool clear_soft, |
| 566 | Object** weak_references, |
| 567 | Object** finalizer_references, |
| 568 | Object** phantom_references) { |
| 569 | DCHECK(soft_references != NULL); |
| 570 | DCHECK(weak_references != NULL); |
| 571 | DCHECK(finalizer_references != NULL); |
| 572 | DCHECK(phantom_references != NULL); |
| 573 | |
| 574 | // Unless we are in the zygote or required to clear soft references |
| 575 | // with white references, preserve some white referents. |
| 576 | if (clear_soft) { |
| 577 | PreserveSomeSoftReferences(soft_references); |
| 578 | } |
| 579 | |
| 580 | // Clear all remaining soft and weak references with white |
| 581 | // referents. |
| 582 | ClearWhiteReferences(soft_references); |
| 583 | ClearWhiteReferences(weak_references); |
| 584 | |
| 585 | // Preserve all white objects with finalize methods and schedule |
| 586 | // them for finalization. |
| 587 | EnqueueFinalizerReferences(finalizer_references); |
| 588 | |
| 589 | // Clear all f-reachable soft and weak references with white |
| 590 | // referents. |
| 591 | ClearWhiteReferences(soft_references); |
| 592 | ClearWhiteReferences(weak_references); |
| 593 | |
| 594 | // Clear all phantom references with white referents. |
| 595 | ClearWhiteReferences(phantom_references); |
| 596 | |
| 597 | // At this point all reference lists should be empty. |
| 598 | DCHECK(*soft_references == NULL); |
| 599 | DCHECK(*weak_references == NULL); |
| 600 | DCHECK(*finalizer_references == NULL); |
| 601 | DCHECK(*phantom_references == NULL); |
| 602 | } |
| 603 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 604 | MarkSweep::~MarkSweep() { |
Elliott Hughes | 352a424 | 2011-10-31 15:15:21 -0700 | [diff] [blame] | 605 | #ifndef NDEBUG |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 606 | VLOG(heap) << "MarkSweep scanned classes=" << class_count_ << " arrays=" << array_count_ << " other=" << other_count_; |
Elliott Hughes | 352a424 | 2011-10-31 15:15:21 -0700 | [diff] [blame] | 607 | #endif |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 608 | delete mark_stack_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 609 | mark_bitmap_->Clear(); |
| 610 | } |
| 611 | |
| 612 | } // namespace art |