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