blob: e5e08353c51d8e77711f41c05414a104dc692028 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 Shapiro69759ea2011-07-21 18:13:35 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "mark_sweep.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070018
Carl Shapiro58551df2011-07-24 03:09:51 -070019#include <climits>
20#include <vector>
21
Elliott Hughes410c0c82011-09-01 17:58:25 -070022#include "class_loader.h"
Brian Carlstrom693267a2011-09-06 09:25:34 -070023#include "dex_cache.h"
Carl Shapiro58551df2011-07-24 03:09:51 -070024#include "heap.h"
Elliott Hughes410c0c82011-09-01 17:58:25 -070025#include "indirect_reference_table.h"
26#include "intern_table.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070027#include "logging.h"
28#include "macros.h"
29#include "mark_stack.h"
Elliott Hughesc33a32b2011-10-11 18:18:07 -070030#include "monitor.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070031#include "object.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070032#include "runtime.h"
Carl Shapiro58551df2011-07-24 03:09:51 -070033#include "space.h"
Elliott Hughes307f75d2011-10-12 18:04:40 -070034#include "timing_logger.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070035#include "thread.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070036
Carl Shapiro69759ea2011-07-21 18:13:35 -070037namespace art {
38
Jesse Wilson078f9b02011-11-18 17:51:47 -050039void MarkSweep::Init() {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070040 mark_stack_ = MarkStack::Create();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080041
42 heap_ = Runtime::Current()->GetHeap();
43 mark_bitmap_ = heap_->GetMarkBits();
44 live_bitmap_ = heap_->GetLiveBits();
Carl Shapiro58551df2011-07-24 03:09:51 -070045
46 // TODO: if concurrent, clear the card table.
47
buzbee0d966cf2011-09-08 17:34:58 -070048 // TODO: if concurrent, enable card marking in compiler
49
Carl Shapiro58551df2011-07-24 03:09:51 -070050 // TODO: check that the mark bitmap is entirely clear.
Carl Shapiro58551df2011-07-24 03:09:51 -070051}
52
Elliott Hughesb0663112011-10-19 18:16:37 -070053inline void MarkSweep::MarkObject0(const Object* obj, bool check_finger) {
Carl Shapiro69759ea2011-07-21 18:13:35 -070054 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 Hughesb0663112011-10-19 18:16:37 -070075inline void MarkSweep::MarkObject(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -070076 if (obj != NULL) {
77 MarkObject0(obj, true);
78 }
79}
80
Elliott Hughescf4c6c42011-09-01 15:16:42 -070081void MarkSweep::MarkObjectVisitor(const Object* root, void* arg) {
Brian Carlstrom1f870082011-08-23 16:02:11 -070082 DCHECK(root != NULL);
83 DCHECK(arg != NULL);
84 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
Ian Rogers5d76c432011-10-31 21:42:49 -070085 DCHECK(mark_sweep->finger_ == NULL); // no point to check finger if it is NULL
86 mark_sweep->MarkObject0(root, false);
Brian Carlstrom1f870082011-08-23 16:02:11 -070087}
88
Carl Shapiro69759ea2011-07-21 18:13:35 -070089// Marks all objects in the root set.
90void MarkSweep::MarkRoots() {
Brian Carlstrom1f870082011-08-23 16:02:11 -070091 Runtime::Current()->VisitRoots(MarkObjectVisitor, this);
Carl Shapiro69759ea2011-07-21 18:13:35 -070092}
93
Ian Rogers5d76c432011-10-31 21:42:49 -070094void 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
104void MarkSweep::ScanDirtyImageRoots() {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800105 const std::vector<Space*>& spaces = heap_->GetSpaces();
106 CardTable* card_table = heap_->GetCardTable();
Ian Rogers5d76c432011-10-31 21:42:49 -0700107 for (size_t i = 0; i < spaces.size(); ++i) {
108 if (spaces[i]->IsImageSpace()) {
Ian Rogers30fab402012-01-23 15:43:46 -0800109 byte* begin = spaces[i]->Begin();
110 byte* end = spaces[i]->End();
111 card_table->Scan(begin, end, ScanImageRootVisitor, this);
Ian Rogers5d76c432011-10-31 21:42:49 -0700112 }
113 }
114}
115
116void 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 Shapiro58551df2011-07-24 03:09:51 -0700122void 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.
130void MarkSweep::RecursiveMark() {
Brian Carlstrom1f870082011-08-23 16:02:11 -0700131 // 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 Shapiro58551df2011-07-24 03:09:51 -0700139 void* arg = reinterpret_cast<void*>(this);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800140 const std::vector<Space*>& spaces = heap_->GetSpaces();
Carl Shapiro58551df2011-07-24 03:09:51 -0700141 for (size_t i = 0; i < spaces.size(); ++i) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700142#ifndef NDEBUG
Ian Rogers30fab402012-01-23 15:43:46 -0800143 uintptr_t begin = reinterpret_cast<uintptr_t>(spaces[i]->Begin());
144 uintptr_t end = reinterpret_cast<uintptr_t>(spaces[i]->End());
Ian Rogers5d76c432011-10-31 21:42:49 -0700145 if (!spaces[i]->IsImageSpace()) {
Ian Rogers30fab402012-01-23 15:43:46 -0800146 mark_bitmap_->ScanWalk(begin, end, &MarkSweep::ScanBitmapCallback, arg);
Ian Rogers5d76c432011-10-31 21:42:49 -0700147 } else{
Ian Rogers30fab402012-01-23 15:43:46 -0800148 mark_bitmap_->ScanWalk(begin, end, &MarkSweep::CheckBitmapCallback, arg);
Ian Rogers5d76c432011-10-31 21:42:49 -0700149 }
150#else
Elliott Hughes307f75d2011-10-12 18:04:40 -0700151 if (!spaces[i]->IsImageSpace()) {
Ian Rogers30fab402012-01-23 15:43:46 -0800152 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 Shapiro58551df2011-07-24 03:09:51 -0700155 }
Ian Rogers5d76c432011-10-31 21:42:49 -0700156#endif
Carl Shapiro58551df2011-07-24 03:09:51 -0700157 }
158 finger_ = reinterpret_cast<Object*>(~0);
Ian Rogers5d76c432011-10-31 21:42:49 -0700159 // TODO: tune the frequency of emptying the mark stack
Carl Shapiro58551df2011-07-24 03:09:51 -0700160 ProcessMarkStack();
161}
162
163void MarkSweep::ReMarkRoots() {
Elliott Hughes53b61312011-08-12 18:28:20 -0700164 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700165}
166
Elliott Hughes410c0c82011-09-01 17:58:25 -0700167void 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 Hughes410c0c82011-09-01 17:58:25 -0700180void MarkSweep::SweepSystemWeaks() {
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700181 Runtime::Current()->GetInternTable()->SweepInternTableWeaks(IsMarked, this);
182 Runtime::Current()->GetMonitorList()->SweepMonitorList(IsMarked, this);
Elliott Hughes410c0c82011-09-01 17:58:25 -0700183 SweepJniWeakGlobals();
Carl Shapiro58551df2011-07-24 03:09:51 -0700184}
185
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800186struct SweepCallbackContext {
187 Heap* heap;
188 AllocSpace* space;
189};
190
Ian Rogers30fab402012-01-23 15:43:46 -0800191void MarkSweep::SweepCallback(size_t num_ptrs, Object** ptrs, void* arg) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700192 // TODO: lock heap if concurrent
Elliott Hughes307f75d2011-10-12 18:04:40 -0700193 size_t freed_objects = num_ptrs;
194 size_t freed_bytes = 0;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800195 SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
196 Heap* heap = context->heap;
197 AllocSpace* space = context->space;
Ian Rogers5d76c432011-10-31 21:42:49 -0700198 // 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 Rogers30fab402012-01-23 15:43:46 -0800202 static const bool kUseFreeList = true;
203 if (kUseFreeList) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700204 for (size_t i = 0; i < num_ptrs; ++i) {
205 Object* obj = static_cast<Object*>(ptrs[i]);
Ian Rogers30fab402012-01-23 15:43:46 -0800206 freed_bytes += space->AllocationSize(obj);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800207 heap->GetLiveBits()->Clear(obj);
Ian Rogers5d76c432011-10-31 21:42:49 -0700208 }
Ian Rogers30fab402012-01-23 15:43:46 -0800209 // AllocSpace::FreeList clears the value in ptrs, so perform after clearing the live bit
210 space->FreeList(num_ptrs, ptrs);
Ian Rogers5d76c432011-10-31 21:42:49 -0700211 } else {
212 for (size_t i = 0; i < num_ptrs; ++i) {
213 Object* obj = static_cast<Object*>(ptrs[i]);
Ian Rogers30fab402012-01-23 15:43:46 -0800214 freed_bytes += space->AllocationSize(obj);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800215 heap->GetLiveBits()->Clear(obj);
Ian Rogers30fab402012-01-23 15:43:46 -0800216 space->Free(obj);
Ian Rogers5d76c432011-10-31 21:42:49 -0700217 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700218 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800219 heap->RecordFreeLocked(freed_objects, freed_bytes);
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700220 // TODO: unlock heap if concurrent
Carl Shapiro58551df2011-07-24 03:09:51 -0700221}
222
223void MarkSweep::Sweep() {
Elliott Hughes2da50362011-10-10 16:57:08 -0700224 SweepSystemWeaks();
225
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800226 const std::vector<Space*>& spaces = heap_->GetSpaces();
227 SweepCallbackContext scc;
228 scc.heap = heap_;
Carl Shapiro58551df2011-07-24 03:09:51 -0700229 for (size_t i = 0; i < spaces.size(); ++i) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700230 if (!spaces[i]->IsImageSpace()) {
Ian Rogers30fab402012-01-23 15:43:46 -0800231 uintptr_t begin = reinterpret_cast<uintptr_t>(spaces[i]->Begin());
232 uintptr_t end = reinterpret_cast<uintptr_t>(spaces[i]->End());
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800233 scc.space = spaces[i]->AsAllocSpace();
Ian Rogers30fab402012-01-23 15:43:46 -0800234 HeapBitmap::SweepWalk(*live_bitmap_, *mark_bitmap_, begin, end,
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800235 &MarkSweep::SweepCallback, reinterpret_cast<void*>(&scc));
Carl Shapiro58551df2011-07-24 03:09:51 -0700236 }
237 }
238}
239
Carl Shapiro69759ea2011-07-21 18:13:35 -0700240// Scans instance fields.
Elliott Hughesb0663112011-10-19 18:16:37 -0700241inline void MarkSweep::ScanInstanceFields(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700242 DCHECK(obj != NULL);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700243 Class* klass = obj->GetClass();
244 DCHECK(klass != NULL);
Elliott Hughes2da50362011-10-10 16:57:08 -0700245 ScanFields(obj, klass->GetReferenceInstanceOffsets(), false);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700246}
247
Ian Rogers5d76c432011-10-31 21:42:49 -0700248inline void MarkSweep::CheckInstanceFields(const Object* obj) {
249 Class* klass = obj->GetClass();
250 CheckFields(obj, klass->GetReferenceInstanceOffsets(), false);
251}
252
Brian Carlstrom4873d462011-08-21 15:23:39 -0700253// Scans static storage on a Class.
Elliott Hughesb0663112011-10-19 18:16:37 -0700254inline void MarkSweep::ScanStaticFields(const Class* klass) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700255 DCHECK(klass != NULL);
Elliott Hughesadb460d2011-10-05 17:02:34 -0700256 ScanFields(klass, klass->GetReferenceStaticOffsets(), true);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700257}
258
Ian Rogers5d76c432011-10-31 21:42:49 -0700259inline void MarkSweep::CheckStaticFields(const Class* klass) {
260 CheckFields(klass, klass->GetReferenceStaticOffsets(), true);
261}
262
Elliott Hughesb0663112011-10-19 18:16:37 -0700263inline void MarkSweep::ScanFields(const Object* obj, uint32_t ref_offsets, bool is_static) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700264 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 Rogers0cfe1fb2011-08-26 03:29:44 -0700268 MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
269 const Object* ref = obj->GetFieldObject<const Object*>(byte_offset, false);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700270 MarkObject(ref);
271 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
272 }
273 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700274 // 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 Shapiro69759ea2011-07-21 18:13:35 -0700279 klass != NULL;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700280 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 Rogers0cfe1fb2011-08-26 03:29:44 -0700288 MemberOffset field_offset = field->GetOffset();
289 const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700290 MarkObject(ref);
291 }
292 }
293 }
294}
295
Ian Rogers5d76c432011-10-31 21:42:49 -0700296inline void MarkSweep::CheckReference(const Object* obj, const Object* ref, MemberOffset offset, bool is_static) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800297 AllocSpace* alloc_space = heap_->GetAllocSpace();
Ian Rogers5d76c432011-10-31 21:42:49 -0700298 if (alloc_space->Contains(ref)) {
299 bool is_marked = mark_bitmap_->Test(ref);
300 if(!is_marked) {
Ian Rogers30fab402012-01-23 15:43:46 -0800301 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 Hughesb3bd5f02012-03-08 21:05:27 -0800306 bool obj_marked = heap_->GetCardTable()->IsDirty(obj);
Ian Rogers5d76c432011-10-31 21:42:49 -0700307 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
315inline 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 Shapiro69759ea2011-07-21 18:13:35 -0700348// Scans the header, static field references, and interface pointers
349// of a class object.
Elliott Hughesb0663112011-10-19 18:16:37 -0700350inline void MarkSweep::ScanClass(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700351#ifndef NDEBUG
352 ++class_count_;
353#endif
Brian Carlstrom693267a2011-09-06 09:25:34 -0700354 ScanInstanceFields(obj);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700355 ScanStaticFields(obj->AsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700356}
357
Ian Rogers5d76c432011-10-31 21:42:49 -0700358inline void MarkSweep::CheckClass(const Object* obj) {
359 CheckInstanceFields(obj);
360 CheckStaticFields(obj->AsClass());
361}
362
Carl Shapiro69759ea2011-07-21 18:13:35 -0700363// 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 Hughesb0663112011-10-19 18:16:37 -0700365inline void MarkSweep::ScanArray(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700366#ifndef NDEBUG
367 ++array_count_;
368#endif
Carl Shapiro69759ea2011-07-21 18:13:35 -0700369 MarkObject(obj->GetClass());
370 if (obj->IsObjectArray()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700371 const ObjectArray<Object>* array = obj->AsObjectArray<Object>();
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700372 for (int32_t i = 0; i < array->GetLength(); ++i) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700373 const Object* element = array->GetWithoutChecks(i);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700374 MarkObject(element);
375 }
376 }
377}
378
Ian Rogers5d76c432011-10-31 21:42:49 -0700379inline 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 Rogersa15e67d2012-02-28 13:51:55 -0800385 size_t width = sizeof(Object*);
386 CheckReference(obj, element, MemberOffset(i * width +
387 Array::DataOffset(width).Int32Value()), false);
Ian Rogers5d76c432011-10-31 21:42:49 -0700388 }
389 }
390}
391
Carl Shapiro69759ea2011-07-21 18:13:35 -0700392// 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.
395void MarkSweep::DelayReferenceReferent(Object* obj) {
396 DCHECK(obj != NULL);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700397 Class* klass = obj->GetClass();
398 DCHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700399 DCHECK(klass->IsReferenceClass());
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800400 Object* pending = obj->GetFieldObject<Object*>(heap_->GetReferencePendingNextOffset(), false);
401 Object* referent = heap_->GetReferenceReferent(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700402 if (pending == NULL && referent != NULL && !IsMarked(referent)) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700403 Object** list = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700404 if (klass->IsSoftReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700405 list = &soft_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700406 } else if (klass->IsWeakReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700407 list = &weak_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700408 } else if (klass->IsFinalizerReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700409 list = &finalizer_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700410 } else if (klass->IsPhantomReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700411 list = &phantom_reference_list_;
412 }
Brian Carlstrom0796af02011-10-12 14:31:45 -0700413 DCHECK(list != NULL) << PrettyClass(klass) << " " << std::hex << klass->GetAccessFlags();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800414 heap_->EnqueuePendingReference(obj, list);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700415 }
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 Hughesadb460d2011-10-05 17:02:34 -0700420// processing.
Elliott Hughesb0663112011-10-19 18:16:37 -0700421inline void MarkSweep::ScanOther(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700422#ifndef NDEBUG
423 ++other_count_;
424#endif
Carl Shapiro69759ea2011-07-21 18:13:35 -0700425 ScanInstanceFields(obj);
Elliott Hughes352a4242011-10-31 15:15:21 -0700426 if (obj->GetClass()->IsReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700427 DelayReferenceReferent(const_cast<Object*>(obj));
428 }
429}
430
Ian Rogers5d76c432011-10-31 21:42:49 -0700431inline void MarkSweep::CheckOther(const Object* obj) {
432 CheckInstanceFields(obj);
433}
434
Carl Shapiro69759ea2011-07-21 18:13:35 -0700435// Scans an object reference. Determines the type of the reference
436// and dispatches to a specialized scanning routine.
Elliott Hughesb0663112011-10-19 18:16:37 -0700437inline void MarkSweep::ScanObject(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700438 DCHECK(obj != NULL);
439 DCHECK(obj->GetClass() != NULL);
440 DCHECK(IsMarked(obj));
441 if (obj->IsClass()) {
442 ScanClass(obj);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700443 } else if (obj->IsArrayInstance()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700444 ScanArray(obj);
445 } else {
Carl Shapiro58551df2011-07-24 03:09:51 -0700446 ScanOther(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700447 }
448}
449
Ian Rogers5d76c432011-10-31 21:42:49 -0700450// Check to see that all alloc space references are marked for the given object
451inline 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 Shapiro69759ea2011-07-21 18:13:35 -0700465void MarkSweep::ProcessMarkStack() {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800466 Space* alloc_space = heap_->GetAllocSpace();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700467 while (!mark_stack_->IsEmpty()) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700468 const Object* obj = mark_stack_->Pop();
Ian Rogers5d76c432011-10-31 21:42:49 -0700469 if (alloc_space->Contains(obj)) {
470 ScanObject(obj);
471 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700472 }
473}
474
475void MarkSweep::ScanDirtyObjects() {
476 ProcessMarkStack();
477}
478
Carl Shapiro69759ea2011-07-21 18:13:35 -0700479// 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.
483void MarkSweep::PreserveSomeSoftReferences(Object** list) {
484 DCHECK(list != NULL);
485 Object* clear = NULL;
486 size_t counter = 0;
487 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800488 Object* ref = heap_->DequeuePendingReference(list);
489 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700490 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 Hughesb3bd5f02012-03-08 21:05:27 -0800502 heap_->EnqueuePendingReference(ref, &clear);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700503 }
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.
514void MarkSweep::ClearWhiteReferences(Object** list) {
515 DCHECK(list != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700516 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800517 Object* ref = heap_->DequeuePendingReference(list);
518 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700519 if (referent != NULL && !IsMarked(referent)) {
520 // Referent is white, clear it.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800521 heap_->ClearReferenceReferent(ref);
522 if (heap_->IsEnqueuable(ref)) {
523 heap_->EnqueueReference(ref, &cleared_reference_list_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700524 }
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.
533void MarkSweep::EnqueueFinalizerReferences(Object** list) {
534 DCHECK(list != NULL);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800535 MemberOffset zombie_offset = heap_->GetFinalizerReferenceZombieOffset();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700536 bool has_enqueued = false;
537 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800538 Object* ref = heap_->DequeuePendingReference(list);
539 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700540 if (referent != NULL && !IsMarked(referent)) {
541 MarkObject(referent);
542 // If the referent is non-null the reference must queuable.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800543 DCHECK(heap_->IsEnqueuable(ref));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700544 ref->SetFieldObject(zombie_offset, referent, false);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800545 heap_->ClearReferenceReferent(ref);
546 heap_->EnqueueReference(ref, &cleared_reference_list_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700547 has_enqueued = true;
548 }
549 }
550 if (has_enqueued) {
551 ProcessMarkStack();
552 }
553 DCHECK(*list == NULL);
554}
555
Carl Shapiro58551df2011-07-24 03:09:51 -0700556// Process reference class instances and schedule finalizations.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700557void 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 Shapiro69759ea2011-07-21 18:13:35 -0700596MarkSweep::~MarkSweep() {
Elliott Hughes352a4242011-10-31 15:15:21 -0700597#ifndef NDEBUG
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800598 VLOG(heap) << "MarkSweep scanned classes=" << class_count_ << " arrays=" << array_count_ << " other=" << other_count_;
Elliott Hughes352a4242011-10-31 15:15:21 -0700599#endif
Carl Shapiro58551df2011-07-24 03:09:51 -0700600 delete mark_stack_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700601 mark_bitmap_->Clear();
602}
603
604} // namespace art