blob: 43ed9b6b2da2e37bdecf166f26fafb55a2331d75 [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"
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070023#include "offsets.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070024
25namespace art {
26
27class Class;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080028class Heap;
Carl Shapiro69759ea2011-07-21 18:13:35 -070029class Object;
30
31class MarkSweep {
32 public:
Mathieu Chartier5301cd22012-05-31 12:11:36 -070033 MarkSweep(MarkStack* mark_stack);
Carl Shapiro58551df2011-07-24 03:09:51 -070034
Carl Shapiro69759ea2011-07-21 18:13:35 -070035 ~MarkSweep();
36
Carl Shapiro58551df2011-07-24 03:09:51 -070037 // Initializes internal structures.
Jesse Wilson078f9b02011-11-18 17:51:47 -050038 void Init();
Carl Shapiro58551df2011-07-24 03:09:51 -070039
Carl Shapiro69759ea2011-07-21 18:13:35 -070040 // Marks the root set at the start of a garbage collection.
41 void MarkRoots();
42
Mathieu Chartier262e5ff2012-06-01 17:35:38 -070043 // Marks the roots in the image space on dirty cards.
Ian Rogers5d76c432011-10-31 21:42:49 -070044 void ScanDirtyImageRoots();
45
Mathieu Chartier262e5ff2012-06-01 17:35:38 -070046 // Verify that image roots point to only marked objects within the alloc space.
47 void VerifyImageRoots();
48
Ian Rogers5d76c432011-10-31 21:42:49 -070049 bool IsMarkStackEmpty() const {
50 return mark_stack_->IsEmpty();
51 }
52
Carl Shapiro58551df2011-07-24 03:09:51 -070053 // Builds a mark stack and recursively mark until it empties.
54 void RecursiveMark();
55
Mathieu Chartier262e5ff2012-06-01 17:35:38 -070056 // Builds a mark stack with objects on dirty cards and recursively mark
57 // until it empties.
58 void RecursiveMarkDirtyObjects();
59
Carl Shapiro69759ea2011-07-21 18:13:35 -070060 // Remarks the root set after completing the concurrent mark.
61 void ReMarkRoots();
62
Carl Shapiro58551df2011-07-24 03:09:51 -070063 void ProcessReferences(bool clear_soft_references) {
64 ProcessReferences(&soft_reference_list_, clear_soft_references,
65 &weak_reference_list_,
66 &finalizer_reference_list_,
67 &phantom_reference_list_);
68 }
69
Carl Shapiro69759ea2011-07-21 18:13:35 -070070 // Sweeps unmarked objects to complete the garbage collection.
71 void Sweep();
72
Elliott Hughesadb460d2011-10-05 17:02:34 -070073 Object* GetClearedReferences() {
74 return cleared_reference_list_;
75 }
76
Carl Shapiro69759ea2011-07-21 18:13:35 -070077 private:
78 // Returns true if the object has its bit set in the mark bitmap.
79 bool IsMarked(const Object* object) const {
80 return mark_bitmap_->Test(object);
81 }
82
Elliott Hughesc33a32b2011-10-11 18:18:07 -070083 static bool IsMarked(const Object* object, void* arg) {
84 return reinterpret_cast<MarkSweep*>(arg)->IsMarked(object);
85 }
86
Elliott Hughescf4c6c42011-09-01 15:16:42 -070087 static void MarkObjectVisitor(const Object* root, void* arg);
Brian Carlstrom1f870082011-08-23 16:02:11 -070088
Mathieu Chartier262e5ff2012-06-01 17:35:38 -070089 static void ReMarkObjectVisitor(const Object* root, void* arg);
90
Ian Rogers5d76c432011-10-31 21:42:49 -070091 static void ScanImageRootVisitor(Object* root, void* arg);
92
Mathieu Chartier262e5ff2012-06-01 17:35:38 -070093 static void ScanDirtyCardCallback(Object* obj, void* arg);
94
Carl Shapiro69759ea2011-07-21 18:13:35 -070095 // Marks an object.
96 void MarkObject(const Object* obj);
97
98 // Yuck.
99 void MarkObject0(const Object* obj, bool check_finger);
100
Carl Shapiro58551df2011-07-24 03:09:51 -0700101 static void ScanBitmapCallback(Object* obj, void* finger, void* arg);
102
Ian Rogers5d76c432011-10-31 21:42:49 -0700103 static void CheckBitmapCallback(Object* obj, void* finger, void* arg);
104
Ian Rogers30fab402012-01-23 15:43:46 -0800105 static void SweepCallback(size_t num_ptrs, Object** ptrs, void* arg);
Carl Shapiro58551df2011-07-24 03:09:51 -0700106
Ian Rogers5d76c432011-10-31 21:42:49 -0700107 void CheckReference(const Object* obj, const Object* ref, MemberOffset offset, bool is_static);
108
Carl Shapiro69759ea2011-07-21 18:13:35 -0700109 // Blackens an object.
110 void ScanObject(const Object* obj);
111
Ian Rogers5d76c432011-10-31 21:42:49 -0700112 void CheckObject(const Object* obj);
113
Carl Shapiro69759ea2011-07-21 18:13:35 -0700114 // Grays references in instance fields.
115 void ScanInstanceFields(const Object* obj);
116
Ian Rogers5d76c432011-10-31 21:42:49 -0700117 void CheckInstanceFields(const Object* obj);
118
Carl Shapiro69759ea2011-07-21 18:13:35 -0700119 // Blackens a class object.
120 void ScanClass(const Object* obj);
121
Ian Rogers5d76c432011-10-31 21:42:49 -0700122 void CheckClass(const Object* obj);
123
Carl Shapiro69759ea2011-07-21 18:13:35 -0700124 // Grays references in static fields.
125 void ScanStaticFields(const Class* klass);
126
Ian Rogers5d76c432011-10-31 21:42:49 -0700127 void CheckStaticFields(const Class* klass);
128
Brian Carlstrom4873d462011-08-21 15:23:39 -0700129 // Used by ScanInstanceFields and ScanStaticFields
130 void ScanFields(const Object* obj, uint32_t ref_offsets, bool is_static);
131
Ian Rogers5d76c432011-10-31 21:42:49 -0700132 void CheckFields(const Object* obj, uint32_t ref_offsets, bool is_static);
133
Carl Shapiro69759ea2011-07-21 18:13:35 -0700134 // Grays references in an array.
135 void ScanArray(const Object* obj);
136
Ian Rogers5d76c432011-10-31 21:42:49 -0700137 void CheckArray(const Object* obj);
138
Carl Shapiro58551df2011-07-24 03:09:51 -0700139 void ScanOther(const Object* obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700140
Ian Rogers5d76c432011-10-31 21:42:49 -0700141 void CheckOther(const Object* obj);
142
Carl Shapiro69759ea2011-07-21 18:13:35 -0700143 // Blackens objects grayed during a garbage collection.
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700144 void ScanGrayObjects();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700145
146 // Schedules an unmarked object for reference processing.
147 void DelayReferenceReferent(Object* reference);
148
149 // Recursively blackens objects on the mark stack.
150 void ProcessMarkStack();
151
Carl Shapiro69759ea2011-07-21 18:13:35 -0700152 void EnqueueFinalizerReferences(Object** ref);
153
154 void PreserveSomeSoftReferences(Object** ref);
155
Carl Shapiro69759ea2011-07-21 18:13:35 -0700156 void ClearWhiteReferences(Object** list);
157
Carl Shapiro58551df2011-07-24 03:09:51 -0700158 void ProcessReferences(Object** soft_references, bool clear_soft_references,
Carl Shapiro69759ea2011-07-21 18:13:35 -0700159 Object** weak_references,
160 Object** finalizer_references,
161 Object** phantom_references);
162
Carl Shapiro58551df2011-07-24 03:09:51 -0700163 void SweepSystemWeaks();
Elliott Hughes410c0c82011-09-01 17:58:25 -0700164 void SweepJniWeakGlobals();
Carl Shapiro58551df2011-07-24 03:09:51 -0700165
Carl Shapiro69759ea2011-07-21 18:13:35 -0700166 MarkStack* mark_stack_;
167
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800168 Heap* heap_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700169 HeapBitmap* mark_bitmap_;
Carl Shapiro58551df2011-07-24 03:09:51 -0700170 HeapBitmap* live_bitmap_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700171
172 Object* finger_;
173
174 Object* condemned_;
175
176 Object* soft_reference_list_;
177
178 Object* weak_reference_list_;
179
180 Object* finalizer_reference_list_;
181
182 Object* phantom_reference_list_;
183
184 Object* cleared_reference_list_;
185
Elliott Hughes352a4242011-10-31 15:15:21 -0700186 size_t class_count_;
187 size_t array_count_;
188 size_t other_count_;
189
Elliott Hughes410c0c82011-09-01 17:58:25 -0700190 friend class InternTableEntryIsUnmarked;
191
Carl Shapiro69759ea2011-07-21 18:13:35 -0700192 DISALLOW_COPY_AND_ASSIGN(MarkSweep);
193};
194
195} // namespace art
196
197#endif // ART_SRC_MARK_SWEEP_H_