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