blob: 74b2aa734d09109382f4956034ff0d31527b39a8 [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
Mathieu Chartierd8195f12012-10-05 12:21:28 -070020#include "atomic_stack.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070021#include "macros.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:
Mathieu Chartierd8195f12012-10-05 12:21:28 -070040 explicit MarkSweep(ObjectStack* 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
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070047 // Find the default mark bitmap.
48 void FindDefaultMarkBitmap();
49
Carl Shapiro69759ea2011-07-21 18:13:35 -070050 // Marks the root set at the start of a garbage collection.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070051 void MarkRoots()
Ian Rogersb726dcb2012-09-05 08:57:23 -070052 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
53 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -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
Carl Shapiro58551df2011-07-24 03:09:51 -070058 // Builds a mark stack and recursively mark until it empties.
Mathieu Chartier357e9be2012-08-01 11:00:14 -070059 void RecursiveMark(bool partial, TimingLogger& timings)
Ian Rogersb726dcb2012-09-05 08:57:23 -070060 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
61 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartiercc236d72012-07-20 10:29:05 -070062
Mathieu Chartier357e9be2012-08-01 11:00:14 -070063 // Copies mark bits from live bitmap of ZygoteSpace to mark bitmap for partial GCs.
Mathieu Chartier2fde5332012-09-14 14:51:54 -070064 void CopyMarkBits(ContinuousSpace* space);
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070065 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
66
67 void BindLiveToMarkBitmap(ContinuousSpace* space)
68 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
69
70 void UnBindBitmaps()
71 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Carl Shapiro58551df2011-07-24 03:09:51 -070072
Mathieu Chartier262e5ff2012-06-01 17:35:38 -070073 // Builds a mark stack with objects on dirty cards and recursively mark
74 // until it empties.
Mathieu Chartier357e9be2012-08-01 11:00:14 -070075 void RecursiveMarkDirtyObjects(bool update_finger)
Ian Rogersb726dcb2012-09-05 08:57:23 -070076 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
77 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier262e5ff2012-06-01 17:35:38 -070078
Mathieu Chartier357e9be2012-08-01 11:00:14 -070079 // Recursive mark objects on specified cards. Updates finger.
80 void RecursiveMarkCards(CardTable* card_table, const std::vector<byte*>& cards,
81 TimingLogger& timings)
Ian Rogersb726dcb2012-09-05 08:57:23 -070082 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
83 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);;
Mathieu Chartier357e9be2012-08-01 11:00:14 -070084
Carl Shapiro69759ea2011-07-21 18:13:35 -070085 // Remarks the root set after completing the concurrent mark.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070086 void ReMarkRoots()
Ian Rogersb726dcb2012-09-05 08:57:23 -070087 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
88 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -070089
Mathieu Chartiercc236d72012-07-20 10:29:05 -070090 Heap* GetHeap() {
91 return heap_;
92 }
93
Ian Rogers00f7d0e2012-07-19 15:28:27 -070094 void ProcessReferences(bool clear_soft_references)
Ian Rogersb726dcb2012-09-05 08:57:23 -070095 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
96 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Carl Shapiro58551df2011-07-24 03:09:51 -070097 ProcessReferences(&soft_reference_list_, clear_soft_references,
98 &weak_reference_list_,
99 &finalizer_reference_list_,
100 &phantom_reference_list_);
101 }
102
Carl Shapiro69759ea2011-07-21 18:13:35 -0700103 // Sweeps unmarked objects to complete the garbage collection.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700104 void Sweep(bool partial, bool swap_bitmaps)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700105 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700106
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700107 // Sweeps unmarked objects to complete the garbage collection.
108 void SweepLargeObjects(bool swap_bitmaps)
109 EXCLUSIVE_LOCKS_REQUIRED(GlobalSynchronization::heap_bitmap_lock_);
110
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700111 // Sweep only pointers within an array. WARNING: Trashes objects.
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700112 void SweepArray(TimingLogger& logger, ObjectStack* allocation_stack_, bool swap_bitmaps)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700113 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700114
Elliott Hughesadb460d2011-10-05 17:02:34 -0700115 Object* GetClearedReferences() {
116 return cleared_reference_list_;
117 }
118
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700119 // Proxy for external access to ScanObject.
120 void ScanRoot(const Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700121 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
122 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700123
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700124 // Blackens an object.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700125 void ScanObject(const Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700126 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
127 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700128
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700129 void SetFinger(Object* new_finger) {
130 finger_ = new_finger;
131 }
132
133 void DisableFinger() {
134 SetFinger(reinterpret_cast<Object*>(~static_cast<uintptr_t>(0)));
135 }
136
137 size_t GetFreedBytes() const {
138 return freed_bytes_;
139 }
140
141 size_t GetFreedObjects() const {
142 return freed_objects_;
143 }
144
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700145 // Everything inside the immune range is marked.
146 void SetImmuneRange(Object* begin, Object* end) {
147 immune_begin_ = begin;
148 immune_end_ = end;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700149 }
150
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700151 void SweepSystemWeaks()
152 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
153
154 // Only sweep the weaks which are inside of an allocation stack.
155 void SweepSystemWeaksArray(ObjectStack* allocations)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700156 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700157
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700158 static bool VerifyIsLiveCallback(const Object* obj, void* arg)
159 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
160
161 void VerifySystemWeaks()
162 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
163
164 // Verify that an object is live, either in a live bitmap or in the allocation stack.
165 void VerifyIsLive(const Object* obj)
166 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
167
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700168 template <typename Visitor>
169 static void VisitObjectReferences(const Object* obj, const Visitor& visitor)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700170 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_,
171 Locks::mutator_lock_) {
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700172 DCHECK(obj != NULL);
173 DCHECK(obj->GetClass() != NULL);
174 if (obj->IsClass()) {
175 VisitClassReferences(obj, visitor);
176 } else if (obj->IsArrayInstance()) {
177 VisitArrayReferences(obj, visitor);
178 } else {
179 VisitOtherReferences(obj, visitor);
180 }
181 }
182
Carl Shapiro69759ea2011-07-21 18:13:35 -0700183 private:
184 // Returns true if the object has its bit set in the mark bitmap.
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700185 bool IsMarked(const Object* object) const;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700186
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700187 static bool IsMarkedCallback(const Object* object, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700188 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700189
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700190 static bool IsMarkedArrayCallback(const Object* object, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700191 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartier46a23632012-08-07 18:44:40 -0700192
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700193 static void MarkObjectVisitor(const Object* root, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700194 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700195
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700196 static void ReMarkObjectVisitor(const Object* root, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700197 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700198
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700199 static void VerifyImageRootVisitor(Object* root, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700200 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_,
201 Locks::mutator_lock_);
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700202
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700203 static void ScanDirtyCardCallback(Object* obj, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700204 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
205 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700206
Carl Shapiro69759ea2011-07-21 18:13:35 -0700207 // Marks an object.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700208 void MarkObject(const Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700209 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700210
211 // Yuck.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700212 void MarkObject0(const Object* obj, bool check_finger)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700213 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700214
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700215 static void ScanBitmapCallback(Object* obj, void* finger, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700216 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
217 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro58551df2011-07-24 03:09:51 -0700218
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700219 static void SweepCallback(size_t num_ptrs, Object** ptrs, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700220 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Carl Shapiro58551df2011-07-24 03:09:51 -0700221
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700222 // Special sweep for zygote that just marks objects / dirties cards.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700223 static void ZygoteSweepCallback(size_t num_ptrs, Object** ptrs, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700224 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Ian Rogers5d76c432011-10-31 21:42:49 -0700225
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700226 void CheckReference(const Object* obj, const Object* ref, MemberOffset offset, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700227 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700228
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700229 void CheckObject(const Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700230 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Ian Rogers5d76c432011-10-31 21:42:49 -0700231
Carl Shapiro69759ea2011-07-21 18:13:35 -0700232 // Grays references in instance fields.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700233 void ScanInstanceFields(const Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700234 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
235 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700236
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700237 template <typename Visitor>
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700238 static void VisitInstanceFieldsReferences(const Object* obj, const Visitor& visitor)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700239 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) {
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700240 DCHECK(obj != NULL);
241 Class* klass = obj->GetClass();
242 DCHECK(klass != NULL);
243 VisitFieldsReferences(obj, klass->GetReferenceInstanceOffsets(), false, visitor);
244 }
Ian Rogers5d76c432011-10-31 21:42:49 -0700245
Carl Shapiro69759ea2011-07-21 18:13:35 -0700246 // Blackens a class object.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700247 void ScanClass(const Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700248 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
249 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700250
Carl Shapiro69759ea2011-07-21 18:13:35 -0700251
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700252 template <typename Visitor>
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700253 static void VisitClassReferences(const Object* obj, const Visitor& visitor)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700254 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) {
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700255 VisitInstanceFieldsReferences(obj, visitor);
256 VisitStaticFieldsReferences(obj->AsClass(), visitor);
257 }
Ian Rogers5d76c432011-10-31 21:42:49 -0700258
Carl Shapiro69759ea2011-07-21 18:13:35 -0700259 // Grays references in static fields.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700260 void ScanStaticFields(const Class* klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700261 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
262 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700263
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700264 template <typename Visitor>
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700265 static void VisitStaticFieldsReferences(const Class* klass, const Visitor& visitor)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700266 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) {
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700267 DCHECK(klass != NULL);
268 VisitFieldsReferences(klass, klass->GetReferenceStaticOffsets(), true, visitor);
269 }
Ian Rogers5d76c432011-10-31 21:42:49 -0700270
Brian Carlstrom4873d462011-08-21 15:23:39 -0700271 // Used by ScanInstanceFields and ScanStaticFields
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700272 void ScanFields(const Object* obj, uint32_t ref_offsets, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700273 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
274 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700275
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700276 template <typename Visitor>
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700277 static void VisitFieldsReferences(const Object* obj, uint32_t ref_offsets, bool is_static,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700278 const Visitor& visitor)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700279 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_,
280 Locks::mutator_lock_) {
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700281 if (ref_offsets != CLASS_WALK_SUPER) {
282 // Found a reference offset bitmap. Mark the specified offsets.
283 while (ref_offsets != 0) {
284 size_t right_shift = CLZ(ref_offsets);
285 MemberOffset field_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
286 const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false);
287 visitor(obj, ref, field_offset, is_static);
288 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
289 }
290 } else {
291 // There is no reference offset bitmap. In the non-static case,
292 // walk up the class inheritance hierarchy and find reference
293 // offsets the hard way. In the static case, just consider this
294 // class.
295 for (const Class* klass = is_static ? obj->AsClass() : obj->GetClass();
296 klass != NULL;
297 klass = is_static ? NULL : klass->GetSuperClass()) {
298 size_t num_reference_fields = (is_static
299 ? klass->NumReferenceStaticFields()
300 : klass->NumReferenceInstanceFields());
301 for (size_t i = 0; i < num_reference_fields; ++i) {
302 Field* field = (is_static
303 ? klass->GetStaticField(i)
304 : klass->GetInstanceField(i));
305 MemberOffset field_offset = field->GetOffset();
306 const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false);
307 visitor(obj, ref, field_offset, is_static);
308 }
309 }
310 }
311 }
Ian Rogers5d76c432011-10-31 21:42:49 -0700312
Carl Shapiro69759ea2011-07-21 18:13:35 -0700313 // Grays references in an array.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700314 void ScanArray(const Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700315 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
316 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700317
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700318 template <typename Visitor>
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700319 static void VisitArrayReferences(const Object* obj, const Visitor& visitor)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700320 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) {
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700321 visitor(obj, obj->GetClass(), Object::ClassOffset(), false);
322 if (obj->IsObjectArray()) {
323 const ObjectArray<Object>* array = obj->AsObjectArray<Object>();
324 for (int32_t i = 0; i < array->GetLength(); ++i) {
325 const Object* element = array->GetWithoutChecks(i);
326 size_t width = sizeof(Object*);
327 visitor(obj, element, MemberOffset(i * width + Array::DataOffset(width).Int32Value()), false);
328 }
329 }
330 }
Ian Rogers5d76c432011-10-31 21:42:49 -0700331
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700332 void ScanOther(const Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700333 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
334 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700335
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700336 template <typename Visitor>
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700337 static void VisitOtherReferences(const Object* obj, const Visitor& visitor)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700338 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) {
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700339 return VisitInstanceFieldsReferences(obj, visitor);
340 }
Ian Rogers5d76c432011-10-31 21:42:49 -0700341
Carl Shapiro69759ea2011-07-21 18:13:35 -0700342 // Blackens objects grayed during a garbage collection.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700343 void ScanGrayObjects(bool update_finger)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700344 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700345
346 // Schedules an unmarked object for reference processing.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700347 void DelayReferenceReferent(Object* reference)
Ian Rogers23435d02012-09-24 11:23:12 -0700348 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700349
350 // Recursively blackens objects on the mark stack.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700351 void ProcessMarkStack()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700352 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
353 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700354
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700355 void EnqueueFinalizerReferences(Object** ref)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700356 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
357 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700358
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700359 void PreserveSomeSoftReferences(Object** ref)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700360 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
361 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700362
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700363 void ClearWhiteReferences(Object** list)
Ian Rogers23435d02012-09-24 11:23:12 -0700364 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700365
Carl Shapiro58551df2011-07-24 03:09:51 -0700366 void ProcessReferences(Object** soft_references, bool clear_soft_references,
Carl Shapiro69759ea2011-07-21 18:13:35 -0700367 Object** weak_references,
368 Object** finalizer_references,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700369 Object** phantom_references)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700370 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
371 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700372
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700373 void SweepJniWeakGlobals(Heap::IsMarkedTester is_marked, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700374 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Carl Shapiro58551df2011-07-24 03:09:51 -0700375
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700376 // Current space, we check this space first to avoid searching for the appropriate space for an object.
377 SpaceBitmap* current_mark_bitmap_;
378
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700379 ObjectStack* mark_stack_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700380
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800381 Heap* heap_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700382
383 Object* finger_;
384
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700385 // Immune range, every object inside the immune range is assumed to be marked.
386 Object* immune_begin_;
387 Object* immune_end_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700388
389 Object* soft_reference_list_;
390
391 Object* weak_reference_list_;
392
393 Object* finalizer_reference_list_;
394
395 Object* phantom_reference_list_;
396
397 Object* cleared_reference_list_;
398
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700399 size_t freed_bytes_;
400 size_t freed_objects_;
401
Elliott Hughes352a4242011-10-31 15:15:21 -0700402 size_t class_count_;
403 size_t array_count_;
404 size_t other_count_;
405
Mathieu Chartiere6e06512012-06-26 15:00:26 -0700406 friend class AddIfReachesAllocSpaceVisitor; // Used by mod-union table.
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700407 friend class CheckBitmapVisitor;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700408 friend class CheckObjectVisitor;
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700409 friend class CheckReferenceVisitor;
Elliott Hughes410c0c82011-09-01 17:58:25 -0700410 friend class InternTableEntryIsUnmarked;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700411 friend class MarkIfReachesAllocspaceVisitor;
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700412 friend class ModUnionCheckReferences;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700413 friend class ModUnionClearCardVisitor;
Mathieu Chartiere6e06512012-06-26 15:00:26 -0700414 friend class ModUnionReferenceVisitor;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700415 friend class ModUnionVisitor;
416 friend class ModUnionTableBitmap;
Mathieu Chartiere6e06512012-06-26 15:00:26 -0700417 friend class ModUnionTableReferenceCache;
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700418 friend class ModUnionScanImageRootVisitor;
419 friend class ScanBitmapVisitor;
420 friend class ScanImageRootVisitor;
Elliott Hughes410c0c82011-09-01 17:58:25 -0700421
Carl Shapiro69759ea2011-07-21 18:13:35 -0700422 DISALLOW_COPY_AND_ASSIGN(MarkSweep);
423};
424
425} // namespace art
426
427#endif // ART_SRC_MARK_SWEEP_H_