blob: aaeb0ff5dbb27326ebcaf249f6fcd01fe014906f [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
17#ifndef ART_SRC_MARK_SWEEP_H_
18#define ART_SRC_MARK_SWEEP_H_
19
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070020#include "macros.h"
21#include "mark_stack.h"
Elliott Hughes5e71b522011-10-20 13:12:32 -070022#include "heap_bitmap.h"
Mathieu Chartierb43b7d42012-06-19 13:15:09 -070023#include "object.h"
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070024#include "offsets.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070025
26namespace art {
27
Mathieu Chartierb43b7d42012-06-19 13:15:09 -070028class CheckObjectVisitor;
Carl Shapiro69759ea2011-07-21 18:13:35 -070029class Class;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080030class Heap;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -070031class MarkIfReachesAllocspaceVisitor;
32class ModUnionClearCardVisitor;
33class ModUnionVisitor;
34class ModUnionTableBitmap;
Carl Shapiro69759ea2011-07-21 18:13:35 -070035class Object;
Mathieu Chartier357e9be2012-08-01 11:00:14 -070036class TimingLogger;
Carl Shapiro69759ea2011-07-21 18:13:35 -070037
38class MarkSweep {
39 public:
Elliott Hughes74847412012-06-20 18:10:21 -070040 explicit MarkSweep(MarkStack* mark_stack);
Carl Shapiro58551df2011-07-24 03:09:51 -070041
Carl Shapiro69759ea2011-07-21 18:13:35 -070042 ~MarkSweep();
43
Carl Shapiro58551df2011-07-24 03:09:51 -070044 // Initializes internal structures.
Jesse Wilson078f9b02011-11-18 17:51:47 -050045 void Init();
Carl Shapiro58551df2011-07-24 03:09:51 -070046
Carl Shapiro69759ea2011-07-21 18:13:35 -070047 // Marks the root set at the start of a garbage collection.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070048 void MarkRoots()
Ian Rogersb726dcb2012-09-05 08:57:23 -070049 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
50 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -070051
Mathieu Chartier262e5ff2012-06-01 17:35:38 -070052 // Marks the roots in the image space on dirty cards.
Ian Rogersb726dcb2012-09-05 08:57:23 -070053 void ScanDirtyImageRoots() EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Ian Rogers5d76c432011-10-31 21:42:49 -070054
Mathieu Chartier262e5ff2012-06-01 17:35:38 -070055 // Verify that image roots point to only marked objects within the alloc space.
Ian Rogersb726dcb2012-09-05 08:57:23 -070056 void VerifyImageRoots() EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartier262e5ff2012-06-01 17:35:38 -070057
Ian Rogers5d76c432011-10-31 21:42:49 -070058 bool IsMarkStackEmpty() const {
59 return mark_stack_->IsEmpty();
60 }
61
Carl Shapiro58551df2011-07-24 03:09:51 -070062 // Builds a mark stack and recursively mark until it empties.
Mathieu Chartier357e9be2012-08-01 11:00:14 -070063 void RecursiveMark(bool partial, TimingLogger& timings)
Ian Rogersb726dcb2012-09-05 08:57:23 -070064 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
65 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartiercc236d72012-07-20 10:29:05 -070066
Mathieu Chartier357e9be2012-08-01 11:00:14 -070067 // Copies mark bits from live bitmap of ZygoteSpace to mark bitmap for partial GCs.
68 void CopyMarkBits(Space* space);
Carl Shapiro58551df2011-07-24 03:09:51 -070069
Mathieu Chartier262e5ff2012-06-01 17:35:38 -070070 // Builds a mark stack with objects on dirty cards and recursively mark
71 // until it empties.
Mathieu Chartier357e9be2012-08-01 11:00:14 -070072 void RecursiveMarkDirtyObjects(bool update_finger)
Ian Rogersb726dcb2012-09-05 08:57:23 -070073 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
74 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier262e5ff2012-06-01 17:35:38 -070075
Mathieu Chartier357e9be2012-08-01 11:00:14 -070076 // Recursive mark objects on specified cards. Updates finger.
77 void RecursiveMarkCards(CardTable* card_table, const std::vector<byte*>& cards,
78 TimingLogger& timings)
Ian Rogersb726dcb2012-09-05 08:57:23 -070079 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
80 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);;
Mathieu Chartier357e9be2012-08-01 11:00:14 -070081
Carl Shapiro69759ea2011-07-21 18:13:35 -070082 // Remarks the root set after completing the concurrent mark.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070083 void ReMarkRoots()
Ian Rogersb726dcb2012-09-05 08:57:23 -070084 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
85 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -070086
Mathieu Chartiercc236d72012-07-20 10:29:05 -070087 Heap* GetHeap() {
88 return heap_;
89 }
90
Ian Rogers00f7d0e2012-07-19 15:28:27 -070091 void ProcessReferences(bool clear_soft_references)
Ian Rogersb726dcb2012-09-05 08:57:23 -070092 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
93 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Carl Shapiro58551df2011-07-24 03:09:51 -070094 ProcessReferences(&soft_reference_list_, clear_soft_references,
95 &weak_reference_list_,
96 &finalizer_reference_list_,
97 &phantom_reference_list_);
98 }
99
Carl Shapiro69759ea2011-07-21 18:13:35 -0700100 // Sweeps unmarked objects to complete the garbage collection.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700101 void Sweep(bool partial, bool swap_bitmaps)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700102 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700103
104 // Sweep only pointers within an array. WARNING: Trashes objects.
105 void SweepArray(TimingLogger& logger, MarkStack* allocation_stack_, bool swap_bitmaps)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700106 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700107
Elliott Hughesadb460d2011-10-05 17:02:34 -0700108 Object* GetClearedReferences() {
109 return cleared_reference_list_;
110 }
111
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700112 // Proxy for external access to ScanObject.
113 void ScanRoot(const Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700114 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
115 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700116
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700117 // Blackens an object.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700118 void ScanObject(const Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700119 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
120 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700121
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700122 void SetFinger(Object* new_finger) {
123 finger_ = new_finger;
124 }
125
126 void DisableFinger() {
127 SetFinger(reinterpret_cast<Object*>(~static_cast<uintptr_t>(0)));
128 }
129
130 size_t GetFreedBytes() const {
131 return freed_bytes_;
132 }
133
134 size_t GetFreedObjects() const {
135 return freed_objects_;
136 }
137
138 void SetCondemned(Object* condemned) {
139 condemned_ = condemned;
140 }
141
142 void SweepSystemWeaks(bool swap_bitmaps)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700143 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700144
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700145 static bool VerifyIsLiveCallback(const Object* obj, void* arg)
146 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
147
148 void VerifySystemWeaks()
149 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
150
151 // Verify that an object is live, either in a live bitmap or in the allocation stack.
152 void VerifyIsLive(const Object* obj)
153 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
154
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700155 template <typename Visitor>
156 static void VisitObjectReferences(const Object* obj, const Visitor& visitor)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700157 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_,
158 Locks::mutator_lock_) {
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700159 DCHECK(obj != NULL);
160 DCHECK(obj->GetClass() != NULL);
161 if (obj->IsClass()) {
162 VisitClassReferences(obj, visitor);
163 } else if (obj->IsArrayInstance()) {
164 VisitArrayReferences(obj, visitor);
165 } else {
166 VisitOtherReferences(obj, visitor);
167 }
168 }
169
Carl Shapiro69759ea2011-07-21 18:13:35 -0700170 private:
171 // Returns true if the object has its bit set in the mark bitmap.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700172 bool IsMarked(const Object* object) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700173 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700174 DCHECK(current_mark_bitmap_ != NULL);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700175 if (current_mark_bitmap_->HasAddress(object)) {
176 return current_mark_bitmap_->Test(object);
177 }
178 return heap_->GetMarkBitmap()->Test(object);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700179 }
180
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700181 static bool IsMarkedCallback(const Object* object, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700182 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700183
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700184 static bool IsLiveCallback(const Object* object, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700185 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartier46a23632012-08-07 18:44:40 -0700186
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700187 static void MarkObjectVisitor(const Object* root, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700188 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700189
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700190 static void ReMarkObjectVisitor(const Object* root, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700191 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700192
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700193 static void VerifyImageRootVisitor(Object* root, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700194 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_,
195 Locks::mutator_lock_);
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700196
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700197 static void ScanDirtyCardCallback(Object* obj, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700198 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
199 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700200
Carl Shapiro69759ea2011-07-21 18:13:35 -0700201 // Marks an object.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700202 void MarkObject(const Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700203 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700204
205 // Yuck.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700206 void MarkObject0(const Object* obj, bool check_finger)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700207 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700208
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700209 static void ScanBitmapCallback(Object* obj, void* finger, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700210 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
211 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro58551df2011-07-24 03:09:51 -0700212
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700213 static void SweepCallback(size_t num_ptrs, Object** ptrs, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700214 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Carl Shapiro58551df2011-07-24 03:09:51 -0700215
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700216 // Special sweep for zygote that just marks objects / dirties cards.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700217 static void ZygoteSweepCallback(size_t num_ptrs, Object** ptrs, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700218 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Ian Rogers5d76c432011-10-31 21:42:49 -0700219
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700220 void CheckReference(const Object* obj, const Object* ref, MemberOffset offset, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700221 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700222
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700223 void CheckObject(const Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700224 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Ian Rogers5d76c432011-10-31 21:42:49 -0700225
Carl Shapiro69759ea2011-07-21 18:13:35 -0700226 // Grays references in instance fields.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700227 void ScanInstanceFields(const Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700228 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
229 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700230
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700231 template <typename Visitor>
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700232 static void VisitInstanceFieldsReferences(const Object* obj, const Visitor& visitor)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700233 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) {
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700234 DCHECK(obj != NULL);
235 Class* klass = obj->GetClass();
236 DCHECK(klass != NULL);
237 VisitFieldsReferences(obj, klass->GetReferenceInstanceOffsets(), false, visitor);
238 }
Ian Rogers5d76c432011-10-31 21:42:49 -0700239
Carl Shapiro69759ea2011-07-21 18:13:35 -0700240 // Blackens a class object.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700241 void ScanClass(const Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700242 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
243 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700244
Carl Shapiro69759ea2011-07-21 18:13:35 -0700245
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700246 template <typename Visitor>
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700247 static void VisitClassReferences(const Object* obj, const Visitor& visitor)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700248 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) {
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700249 VisitInstanceFieldsReferences(obj, visitor);
250 VisitStaticFieldsReferences(obj->AsClass(), visitor);
251 }
Ian Rogers5d76c432011-10-31 21:42:49 -0700252
Carl Shapiro69759ea2011-07-21 18:13:35 -0700253 // Grays references in static fields.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700254 void ScanStaticFields(const Class* klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700255 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
256 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700257
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700258 template <typename Visitor>
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700259 static void VisitStaticFieldsReferences(const Class* klass, const Visitor& visitor)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700260 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) {
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700261 DCHECK(klass != NULL);
262 VisitFieldsReferences(klass, klass->GetReferenceStaticOffsets(), true, visitor);
263 }
Ian Rogers5d76c432011-10-31 21:42:49 -0700264
Brian Carlstrom4873d462011-08-21 15:23:39 -0700265 // Used by ScanInstanceFields and ScanStaticFields
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700266 void ScanFields(const Object* obj, uint32_t ref_offsets, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700267 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
268 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700269
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700270 template <typename Visitor>
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700271 static void VisitFieldsReferences(const Object* obj, uint32_t ref_offsets, bool is_static,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700272 const Visitor& visitor)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700273 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_,
274 Locks::mutator_lock_) {
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700275 if (ref_offsets != CLASS_WALK_SUPER) {
276 // Found a reference offset bitmap. Mark the specified offsets.
277 while (ref_offsets != 0) {
278 size_t right_shift = CLZ(ref_offsets);
279 MemberOffset field_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
280 const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false);
281 visitor(obj, ref, field_offset, is_static);
282 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
283 }
284 } else {
285 // There is no reference offset bitmap. In the non-static case,
286 // walk up the class inheritance hierarchy and find reference
287 // offsets the hard way. In the static case, just consider this
288 // class.
289 for (const Class* klass = is_static ? obj->AsClass() : obj->GetClass();
290 klass != NULL;
291 klass = is_static ? NULL : klass->GetSuperClass()) {
292 size_t num_reference_fields = (is_static
293 ? klass->NumReferenceStaticFields()
294 : klass->NumReferenceInstanceFields());
295 for (size_t i = 0; i < num_reference_fields; ++i) {
296 Field* field = (is_static
297 ? klass->GetStaticField(i)
298 : klass->GetInstanceField(i));
299 MemberOffset field_offset = field->GetOffset();
300 const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false);
301 visitor(obj, ref, field_offset, is_static);
302 }
303 }
304 }
305 }
Ian Rogers5d76c432011-10-31 21:42:49 -0700306
Carl Shapiro69759ea2011-07-21 18:13:35 -0700307 // Grays references in an array.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700308 void ScanArray(const Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700309 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
310 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700311
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700312 template <typename Visitor>
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700313 static void VisitArrayReferences(const Object* obj, const Visitor& visitor)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700314 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) {
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700315 visitor(obj, obj->GetClass(), Object::ClassOffset(), false);
316 if (obj->IsObjectArray()) {
317 const ObjectArray<Object>* array = obj->AsObjectArray<Object>();
318 for (int32_t i = 0; i < array->GetLength(); ++i) {
319 const Object* element = array->GetWithoutChecks(i);
320 size_t width = sizeof(Object*);
321 visitor(obj, element, MemberOffset(i * width + Array::DataOffset(width).Int32Value()), false);
322 }
323 }
324 }
Ian Rogers5d76c432011-10-31 21:42:49 -0700325
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700326 void ScanOther(const Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700327 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
328 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700329
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700330 template <typename Visitor>
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700331 static void VisitOtherReferences(const Object* obj, const Visitor& visitor)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700332 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) {
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700333 return VisitInstanceFieldsReferences(obj, visitor);
334 }
Ian Rogers5d76c432011-10-31 21:42:49 -0700335
Carl Shapiro69759ea2011-07-21 18:13:35 -0700336 // Blackens objects grayed during a garbage collection.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700337 void ScanGrayObjects(bool update_finger)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700338 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700339
340 // Schedules an unmarked object for reference processing.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700341 void DelayReferenceReferent(Object* reference)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700342 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700343
344 // Recursively blackens objects on the mark stack.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700345 void ProcessMarkStack()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700346 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
347 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700348
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700349 void EnqueueFinalizerReferences(Object** ref)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700350 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
351 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700352
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700353 void PreserveSomeSoftReferences(Object** ref)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700354 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
355 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700356
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700357 void ClearWhiteReferences(Object** list)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700358 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700359
Carl Shapiro58551df2011-07-24 03:09:51 -0700360 void ProcessReferences(Object** soft_references, bool clear_soft_references,
Carl Shapiro69759ea2011-07-21 18:13:35 -0700361 Object** weak_references,
362 Object** finalizer_references,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700363 Object** phantom_references)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700364 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
365 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700366
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700367 void SweepJniWeakGlobals(bool swap_bitmaps)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700368 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Carl Shapiro58551df2011-07-24 03:09:51 -0700369
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700370 // Current space, we check this space first to avoid searching for the appropriate space for an object.
371 SpaceBitmap* current_mark_bitmap_;
372
Carl Shapiro69759ea2011-07-21 18:13:35 -0700373 MarkStack* mark_stack_;
374
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800375 Heap* heap_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700376
377 Object* finger_;
378
379 Object* condemned_;
380
381 Object* soft_reference_list_;
382
383 Object* weak_reference_list_;
384
385 Object* finalizer_reference_list_;
386
387 Object* phantom_reference_list_;
388
389 Object* cleared_reference_list_;
390
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700391 size_t freed_bytes_;
392 size_t freed_objects_;
393
Elliott Hughes352a4242011-10-31 15:15:21 -0700394 size_t class_count_;
395 size_t array_count_;
396 size_t other_count_;
397
Mathieu Chartiere6e06512012-06-26 15:00:26 -0700398 friend class AddIfReachesAllocSpaceVisitor; // Used by mod-union table.
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700399 friend class CheckBitmapVisitor;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700400 friend class CheckObjectVisitor;
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700401 friend class CheckReferenceVisitor;
Elliott Hughes410c0c82011-09-01 17:58:25 -0700402 friend class InternTableEntryIsUnmarked;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700403 friend class MarkIfReachesAllocspaceVisitor;
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700404 friend class ModUnionCheckReferences;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700405 friend class ModUnionClearCardVisitor;
Mathieu Chartiere6e06512012-06-26 15:00:26 -0700406 friend class ModUnionReferenceVisitor;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700407 friend class ModUnionVisitor;
408 friend class ModUnionTableBitmap;
Mathieu Chartiere6e06512012-06-26 15:00:26 -0700409 friend class ModUnionTableReferenceCache;
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700410 friend class ModUnionScanImageRootVisitor;
411 friend class ScanBitmapVisitor;
412 friend class ScanImageRootVisitor;
Elliott Hughes410c0c82011-09-01 17:58:25 -0700413
Carl Shapiro69759ea2011-07-21 18:13:35 -0700414 DISALLOW_COPY_AND_ASSIGN(MarkSweep);
415};
416
417} // namespace art
418
419#endif // ART_SRC_MARK_SWEEP_H_