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) { |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 142 | #ifndef NDEBUG |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 143 | uintptr_t begin = reinterpret_cast<uintptr_t>(spaces[i]->Begin()); |
| 144 | uintptr_t end = reinterpret_cast<uintptr_t>(spaces[i]->End()); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 145 | if (!spaces[i]->IsImageSpace()) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 146 | mark_bitmap_->ScanWalk(begin, end, &MarkSweep::ScanBitmapCallback, arg); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 147 | } else{ |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 148 | mark_bitmap_->ScanWalk(begin, end, &MarkSweep::CheckBitmapCallback, arg); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 149 | } |
| 150 | #else |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 151 | if (!spaces[i]->IsImageSpace()) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 152 | uintptr_t begin = reinterpret_cast<uintptr_t>(spaces[i]->Begin()); |
| 153 | uintptr_t end = reinterpret_cast<uintptr_t>(spaces[i]->End()); |
| 154 | mark_bitmap_->ScanWalk(begin, end, &MarkSweep::ScanBitmapCallback, arg); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 155 | } |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 156 | #endif |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 157 | } |
| 158 | finger_ = reinterpret_cast<Object*>(~0); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 159 | // TODO: tune the frequency of emptying the mark stack |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 160 | ProcessMarkStack(); |
| 161 | } |
| 162 | |
| 163 | void MarkSweep::ReMarkRoots() { |
Elliott Hughes | 53b6131 | 2011-08-12 18:28:20 -0700 | [diff] [blame] | 164 | UNIMPLEMENTED(FATAL); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 167 | void MarkSweep::SweepJniWeakGlobals() { |
| 168 | JavaVMExt* vm = Runtime::Current()->GetJavaVM(); |
| 169 | MutexLock mu(vm->weak_globals_lock); |
| 170 | IndirectReferenceTable* table = &vm->weak_globals; |
| 171 | typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto |
| 172 | for (It it = table->begin(), end = table->end(); it != end; ++it) { |
| 173 | const Object** entry = *it; |
| 174 | if (!IsMarked(*entry)) { |
| 175 | *entry = kClearedJniWeakGlobal; |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 180 | void MarkSweep::SweepSystemWeaks() { |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 181 | Runtime::Current()->GetInternTable()->SweepInternTableWeaks(IsMarked, this); |
| 182 | Runtime::Current()->GetMonitorList()->SweepMonitorList(IsMarked, this); |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 183 | SweepJniWeakGlobals(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame^] | 186 | struct SweepCallbackContext { |
| 187 | Heap* heap; |
| 188 | AllocSpace* space; |
| 189 | }; |
| 190 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 191 | void MarkSweep::SweepCallback(size_t num_ptrs, Object** ptrs, void* arg) { |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 192 | // TODO: lock heap if concurrent |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 193 | size_t freed_objects = num_ptrs; |
| 194 | size_t freed_bytes = 0; |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame^] | 195 | SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg); |
| 196 | Heap* heap = context->heap; |
| 197 | AllocSpace* space = context->space; |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 198 | // Use a bulk free, that merges consecutive objects before freeing or free per object? |
| 199 | // Documentation suggests better free performance with merging, but this may be at the expensive |
| 200 | // of allocation. |
| 201 | // TODO: investigate performance |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 202 | static const bool kUseFreeList = true; |
| 203 | if (kUseFreeList) { |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 204 | for (size_t i = 0; i < num_ptrs; ++i) { |
| 205 | Object* obj = static_cast<Object*>(ptrs[i]); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 206 | freed_bytes += space->AllocationSize(obj); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame^] | 207 | heap->GetLiveBits()->Clear(obj); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 208 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 209 | // AllocSpace::FreeList clears the value in ptrs, so perform after clearing the live bit |
| 210 | space->FreeList(num_ptrs, ptrs); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 211 | } else { |
| 212 | for (size_t i = 0; i < num_ptrs; ++i) { |
| 213 | Object* obj = static_cast<Object*>(ptrs[i]); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 214 | freed_bytes += space->AllocationSize(obj); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame^] | 215 | heap->GetLiveBits()->Clear(obj); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 216 | space->Free(obj); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 217 | } |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 218 | } |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame^] | 219 | heap->RecordFreeLocked(freed_objects, freed_bytes); |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 220 | // TODO: unlock heap if concurrent |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | void MarkSweep::Sweep() { |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 224 | SweepSystemWeaks(); |
| 225 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame^] | 226 | const std::vector<Space*>& spaces = heap_->GetSpaces(); |
| 227 | SweepCallbackContext scc; |
| 228 | scc.heap = heap_; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 229 | for (size_t i = 0; i < spaces.size(); ++i) { |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 230 | if (!spaces[i]->IsImageSpace()) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 231 | uintptr_t begin = reinterpret_cast<uintptr_t>(spaces[i]->Begin()); |
| 232 | uintptr_t end = reinterpret_cast<uintptr_t>(spaces[i]->End()); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame^] | 233 | scc.space = spaces[i]->AsAllocSpace(); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 234 | HeapBitmap::SweepWalk(*live_bitmap_, *mark_bitmap_, begin, end, |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame^] | 235 | &MarkSweep::SweepCallback, reinterpret_cast<void*>(&scc)); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | } |
| 239 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 240 | // Scans instance fields. |
Elliott Hughes | b066311 | 2011-10-19 18:16:37 -0700 | [diff] [blame] | 241 | inline void MarkSweep::ScanInstanceFields(const Object* obj) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 242 | DCHECK(obj != NULL); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 243 | Class* klass = obj->GetClass(); |
| 244 | DCHECK(klass != NULL); |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 245 | ScanFields(obj, klass->GetReferenceInstanceOffsets(), false); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 248 | inline void MarkSweep::CheckInstanceFields(const Object* obj) { |
| 249 | Class* klass = obj->GetClass(); |
| 250 | CheckFields(obj, klass->GetReferenceInstanceOffsets(), false); |
| 251 | } |
| 252 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 253 | // Scans static storage on a Class. |
Elliott Hughes | b066311 | 2011-10-19 18:16:37 -0700 | [diff] [blame] | 254 | inline void MarkSweep::ScanStaticFields(const Class* klass) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 255 | DCHECK(klass != NULL); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 256 | ScanFields(klass, klass->GetReferenceStaticOffsets(), true); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 259 | inline void MarkSweep::CheckStaticFields(const Class* klass) { |
| 260 | CheckFields(klass, klass->GetReferenceStaticOffsets(), true); |
| 261 | } |
| 262 | |
Elliott Hughes | b066311 | 2011-10-19 18:16:37 -0700 | [diff] [blame] | 263 | 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] | 264 | if (ref_offsets != CLASS_WALK_SUPER) { |
| 265 | // Found a reference offset bitmap. Mark the specified offsets. |
| 266 | while (ref_offsets != 0) { |
| 267 | size_t right_shift = CLZ(ref_offsets); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 268 | MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift); |
| 269 | const Object* ref = obj->GetFieldObject<const Object*>(byte_offset, false); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 270 | MarkObject(ref); |
| 271 | ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift); |
| 272 | } |
| 273 | } else { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 274 | // There is no reference offset bitmap. In the non-static case, |
| 275 | // walk up the class inheritance hierarchy and find reference |
| 276 | // offsets the hard way. In the static case, just consider this |
| 277 | // class. |
| 278 | for (const Class* klass = is_static ? obj->AsClass() : obj->GetClass(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 279 | klass != NULL; |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 280 | klass = is_static ? NULL : klass->GetSuperClass()) { |
| 281 | size_t num_reference_fields = (is_static |
| 282 | ? klass->NumReferenceStaticFields() |
| 283 | : klass->NumReferenceInstanceFields()); |
| 284 | for (size_t i = 0; i < num_reference_fields; ++i) { |
| 285 | Field* field = (is_static |
| 286 | ? klass->GetStaticField(i) |
| 287 | : klass->GetInstanceField(i)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 288 | MemberOffset field_offset = field->GetOffset(); |
| 289 | const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 290 | MarkObject(ref); |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 296 | 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^] | 297 | AllocSpace* alloc_space = heap_->GetAllocSpace(); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 298 | if (alloc_space->Contains(ref)) { |
| 299 | bool is_marked = mark_bitmap_->Test(ref); |
| 300 | if(!is_marked) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 301 | LOG(INFO) << *alloc_space; |
| 302 | LOG(WARNING) << (is_static ? "Static ref'" : "Instance ref'") << PrettyTypeOf(ref) |
| 303 | << "' (" << (void*)ref << ") in '" << PrettyTypeOf(obj) |
| 304 | << "' (" << (void*)obj << ") at offset " |
| 305 | << (void*)offset.Int32Value() << " wasn't marked"; |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame^] | 306 | bool obj_marked = heap_->GetCardTable()->IsDirty(obj); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 307 | if (!obj_marked) { |
| 308 | LOG(WARNING) << "Object '" << PrettyTypeOf(obj) << "' (" << (void*)obj |
| 309 | << ") contains references to the alloc space, but wasn't card marked"; |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | inline void MarkSweep::CheckFields(const Object* obj, uint32_t ref_offsets, bool is_static) { |
| 316 | if (ref_offsets != CLASS_WALK_SUPER) { |
| 317 | // Found a reference offset bitmap. Mark the specified offsets. |
| 318 | while (ref_offsets != 0) { |
| 319 | size_t right_shift = CLZ(ref_offsets); |
| 320 | MemberOffset field_offset = CLASS_OFFSET_FROM_CLZ(right_shift); |
| 321 | const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false); |
| 322 | CheckReference(obj, ref, field_offset, is_static); |
| 323 | ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift); |
| 324 | } |
| 325 | } else { |
| 326 | // There is no reference offset bitmap. In the non-static case, |
| 327 | // walk up the class inheritance hierarchy and find reference |
| 328 | // offsets the hard way. In the static case, just consider this |
| 329 | // class. |
| 330 | for (const Class* klass = is_static ? obj->AsClass() : obj->GetClass(); |
| 331 | klass != NULL; |
| 332 | klass = is_static ? NULL : klass->GetSuperClass()) { |
| 333 | size_t num_reference_fields = (is_static |
| 334 | ? klass->NumReferenceStaticFields() |
| 335 | : klass->NumReferenceInstanceFields()); |
| 336 | for (size_t i = 0; i < num_reference_fields; ++i) { |
| 337 | Field* field = (is_static |
| 338 | ? klass->GetStaticField(i) |
| 339 | : klass->GetInstanceField(i)); |
| 340 | MemberOffset field_offset = field->GetOffset(); |
| 341 | const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false); |
| 342 | CheckReference(obj, ref, field_offset, is_static); |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 348 | // Scans the header, static field references, and interface pointers |
| 349 | // of a class object. |
Elliott Hughes | b066311 | 2011-10-19 18:16:37 -0700 | [diff] [blame] | 350 | inline void MarkSweep::ScanClass(const Object* obj) { |
Elliott Hughes | 352a424 | 2011-10-31 15:15:21 -0700 | [diff] [blame] | 351 | #ifndef NDEBUG |
| 352 | ++class_count_; |
| 353 | #endif |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 354 | ScanInstanceFields(obj); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 355 | ScanStaticFields(obj->AsClass()); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 356 | } |
| 357 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 358 | inline void MarkSweep::CheckClass(const Object* obj) { |
| 359 | CheckInstanceFields(obj); |
| 360 | CheckStaticFields(obj->AsClass()); |
| 361 | } |
| 362 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 363 | // Scans the header of all array objects. If the array object is |
| 364 | // specialized to a reference type, scans the array data as well. |
Elliott Hughes | b066311 | 2011-10-19 18:16:37 -0700 | [diff] [blame] | 365 | inline void MarkSweep::ScanArray(const Object* obj) { |
Elliott Hughes | 352a424 | 2011-10-31 15:15:21 -0700 | [diff] [blame] | 366 | #ifndef NDEBUG |
| 367 | ++array_count_; |
| 368 | #endif |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 369 | MarkObject(obj->GetClass()); |
| 370 | if (obj->IsObjectArray()) { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 371 | const ObjectArray<Object>* array = obj->AsObjectArray<Object>(); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 372 | for (int32_t i = 0; i < array->GetLength(); ++i) { |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 373 | const Object* element = array->GetWithoutChecks(i); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 374 | MarkObject(element); |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 379 | inline void MarkSweep::CheckArray(const Object* obj) { |
| 380 | CheckReference(obj, obj->GetClass(), Object::ClassOffset(), false); |
| 381 | if (obj->IsObjectArray()) { |
| 382 | const ObjectArray<Object>* array = obj->AsObjectArray<Object>(); |
| 383 | for (int32_t i = 0; i < array->GetLength(); ++i) { |
| 384 | const Object* element = array->GetWithoutChecks(i); |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 385 | size_t width = sizeof(Object*); |
| 386 | CheckReference(obj, element, MemberOffset(i * width + |
| 387 | Array::DataOffset(width).Int32Value()), false); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | } |
| 391 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 392 | // Process the "referent" field in a java.lang.ref.Reference. If the |
| 393 | // referent has not yet been marked, put it on the appropriate list in |
| 394 | // the gcHeap for later processing. |
| 395 | void MarkSweep::DelayReferenceReferent(Object* obj) { |
| 396 | DCHECK(obj != NULL); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 397 | Class* klass = obj->GetClass(); |
| 398 | DCHECK(klass != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 399 | DCHECK(klass->IsReferenceClass()); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame^] | 400 | Object* pending = obj->GetFieldObject<Object*>(heap_->GetReferencePendingNextOffset(), false); |
| 401 | Object* referent = heap_->GetReferenceReferent(obj); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 402 | if (pending == NULL && referent != NULL && !IsMarked(referent)) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 403 | Object** list = NULL; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 404 | if (klass->IsSoftReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 405 | list = &soft_reference_list_; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 406 | } else if (klass->IsWeakReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 407 | list = &weak_reference_list_; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 408 | } else if (klass->IsFinalizerReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 409 | list = &finalizer_reference_list_; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 410 | } else if (klass->IsPhantomReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 411 | list = &phantom_reference_list_; |
| 412 | } |
Brian Carlstrom | 0796af0 | 2011-10-12 14:31:45 -0700 | [diff] [blame] | 413 | DCHECK(list != NULL) << PrettyClass(klass) << " " << std::hex << klass->GetAccessFlags(); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame^] | 414 | heap_->EnqueuePendingReference(obj, list); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | |
| 418 | // Scans the header and field references of a data object. If the |
| 419 | // scanned object is a reference subclass, it is scheduled for later |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 420 | // processing. |
Elliott Hughes | b066311 | 2011-10-19 18:16:37 -0700 | [diff] [blame] | 421 | inline void MarkSweep::ScanOther(const Object* obj) { |
Elliott Hughes | 352a424 | 2011-10-31 15:15:21 -0700 | [diff] [blame] | 422 | #ifndef NDEBUG |
| 423 | ++other_count_; |
| 424 | #endif |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 425 | ScanInstanceFields(obj); |
Elliott Hughes | 352a424 | 2011-10-31 15:15:21 -0700 | [diff] [blame] | 426 | if (obj->GetClass()->IsReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 427 | DelayReferenceReferent(const_cast<Object*>(obj)); |
| 428 | } |
| 429 | } |
| 430 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 431 | inline void MarkSweep::CheckOther(const Object* obj) { |
| 432 | CheckInstanceFields(obj); |
| 433 | } |
| 434 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 435 | // Scans an object reference. Determines the type of the reference |
| 436 | // and dispatches to a specialized scanning routine. |
Elliott Hughes | b066311 | 2011-10-19 18:16:37 -0700 | [diff] [blame] | 437 | inline void MarkSweep::ScanObject(const Object* obj) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 438 | DCHECK(obj != NULL); |
| 439 | DCHECK(obj->GetClass() != NULL); |
| 440 | DCHECK(IsMarked(obj)); |
| 441 | if (obj->IsClass()) { |
| 442 | ScanClass(obj); |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 443 | } else if (obj->IsArrayInstance()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 444 | ScanArray(obj); |
| 445 | } else { |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 446 | ScanOther(obj); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 450 | // Check to see that all alloc space references are marked for the given object |
| 451 | inline void MarkSweep::CheckObject(const Object* obj) { |
| 452 | DCHECK(obj != NULL); |
| 453 | DCHECK(obj->GetClass() != NULL); |
| 454 | DCHECK(IsMarked(obj)); |
| 455 | if (obj->IsClass()) { |
| 456 | CheckClass(obj); |
| 457 | } else if (obj->IsArrayInstance()) { |
| 458 | CheckArray(obj); |
| 459 | } else { |
| 460 | CheckOther(obj); |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | // Scan anything that's on the mark stack. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 465 | void MarkSweep::ProcessMarkStack() { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame^] | 466 | Space* alloc_space = heap_->GetAllocSpace(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 467 | while (!mark_stack_->IsEmpty()) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 468 | const Object* obj = mark_stack_->Pop(); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 469 | if (alloc_space->Contains(obj)) { |
| 470 | ScanObject(obj); |
| 471 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | |
| 475 | void MarkSweep::ScanDirtyObjects() { |
| 476 | ProcessMarkStack(); |
| 477 | } |
| 478 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 479 | // Walks the reference list marking any references subject to the |
| 480 | // reference clearing policy. References with a black referent are |
| 481 | // removed from the list. References with white referents biased |
| 482 | // toward saving are blackened and also removed from the list. |
| 483 | void MarkSweep::PreserveSomeSoftReferences(Object** list) { |
| 484 | DCHECK(list != NULL); |
| 485 | Object* clear = NULL; |
| 486 | size_t counter = 0; |
| 487 | while (*list != NULL) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame^] | 488 | Object* ref = heap_->DequeuePendingReference(list); |
| 489 | Object* referent = heap_->GetReferenceReferent(ref); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 490 | if (referent == NULL) { |
| 491 | // Referent was cleared by the user during marking. |
| 492 | continue; |
| 493 | } |
| 494 | bool is_marked = IsMarked(referent); |
| 495 | if (!is_marked && ((++counter) & 1)) { |
| 496 | // Referent is white and biased toward saving, mark it. |
| 497 | MarkObject(referent); |
| 498 | is_marked = true; |
| 499 | } |
| 500 | if (!is_marked) { |
| 501 | // Referent is white, queue it for clearing. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame^] | 502 | heap_->EnqueuePendingReference(ref, &clear); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | *list = clear; |
| 506 | // Restart the mark with the newly black references added to the |
| 507 | // root set. |
| 508 | ProcessMarkStack(); |
| 509 | } |
| 510 | |
| 511 | // Unlink the reference list clearing references objects with white |
| 512 | // referents. Cleared references registered to a reference queue are |
| 513 | // scheduled for appending by the heap worker thread. |
| 514 | void MarkSweep::ClearWhiteReferences(Object** list) { |
| 515 | DCHECK(list != NULL); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 516 | while (*list != NULL) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame^] | 517 | Object* ref = heap_->DequeuePendingReference(list); |
| 518 | Object* referent = heap_->GetReferenceReferent(ref); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 519 | if (referent != NULL && !IsMarked(referent)) { |
| 520 | // Referent is white, clear it. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame^] | 521 | heap_->ClearReferenceReferent(ref); |
| 522 | if (heap_->IsEnqueuable(ref)) { |
| 523 | heap_->EnqueueReference(ref, &cleared_reference_list_); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | } |
| 527 | DCHECK(*list == NULL); |
| 528 | } |
| 529 | |
| 530 | // Enqueues finalizer references with white referents. White |
| 531 | // referents are blackened, moved to the zombie field, and the |
| 532 | // referent field is cleared. |
| 533 | void MarkSweep::EnqueueFinalizerReferences(Object** list) { |
| 534 | DCHECK(list != NULL); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame^] | 535 | MemberOffset zombie_offset = heap_->GetFinalizerReferenceZombieOffset(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 536 | bool has_enqueued = false; |
| 537 | while (*list != NULL) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame^] | 538 | Object* ref = heap_->DequeuePendingReference(list); |
| 539 | Object* referent = heap_->GetReferenceReferent(ref); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 540 | if (referent != NULL && !IsMarked(referent)) { |
| 541 | MarkObject(referent); |
| 542 | // If the referent is non-null the reference must queuable. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame^] | 543 | DCHECK(heap_->IsEnqueuable(ref)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 544 | ref->SetFieldObject(zombie_offset, referent, false); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame^] | 545 | heap_->ClearReferenceReferent(ref); |
| 546 | heap_->EnqueueReference(ref, &cleared_reference_list_); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 547 | has_enqueued = true; |
| 548 | } |
| 549 | } |
| 550 | if (has_enqueued) { |
| 551 | ProcessMarkStack(); |
| 552 | } |
| 553 | DCHECK(*list == NULL); |
| 554 | } |
| 555 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 556 | // Process reference class instances and schedule finalizations. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 557 | void MarkSweep::ProcessReferences(Object** soft_references, bool clear_soft, |
| 558 | Object** weak_references, |
| 559 | Object** finalizer_references, |
| 560 | Object** phantom_references) { |
| 561 | DCHECK(soft_references != NULL); |
| 562 | DCHECK(weak_references != NULL); |
| 563 | DCHECK(finalizer_references != NULL); |
| 564 | DCHECK(phantom_references != NULL); |
| 565 | |
| 566 | // Unless we are in the zygote or required to clear soft references |
| 567 | // with white references, preserve some white referents. |
| 568 | if (clear_soft) { |
| 569 | PreserveSomeSoftReferences(soft_references); |
| 570 | } |
| 571 | |
| 572 | // Clear all remaining soft and weak references with white |
| 573 | // referents. |
| 574 | ClearWhiteReferences(soft_references); |
| 575 | ClearWhiteReferences(weak_references); |
| 576 | |
| 577 | // Preserve all white objects with finalize methods and schedule |
| 578 | // them for finalization. |
| 579 | EnqueueFinalizerReferences(finalizer_references); |
| 580 | |
| 581 | // Clear all f-reachable soft and weak references with white |
| 582 | // referents. |
| 583 | ClearWhiteReferences(soft_references); |
| 584 | ClearWhiteReferences(weak_references); |
| 585 | |
| 586 | // Clear all phantom references with white referents. |
| 587 | ClearWhiteReferences(phantom_references); |
| 588 | |
| 589 | // At this point all reference lists should be empty. |
| 590 | DCHECK(*soft_references == NULL); |
| 591 | DCHECK(*weak_references == NULL); |
| 592 | DCHECK(*finalizer_references == NULL); |
| 593 | DCHECK(*phantom_references == NULL); |
| 594 | } |
| 595 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 596 | MarkSweep::~MarkSweep() { |
Elliott Hughes | 352a424 | 2011-10-31 15:15:21 -0700 | [diff] [blame] | 597 | #ifndef NDEBUG |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 598 | 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] | 599 | #endif |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 600 | delete mark_stack_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 601 | mark_bitmap_->Clear(); |
| 602 | } |
| 603 | |
| 604 | } // namespace art |