blob: c6756759b2a5b504cc3e08bd92466fd1c434c420 [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;
28class Object;
29
30class MarkSweep {
31 public:
Carl Shapiro58551df2011-07-24 03:09:51 -070032 MarkSweep() :
Brian Carlstrom1f870082011-08-23 16:02:11 -070033 finger_(NULL),
34 condemned_(NULL),
35 soft_reference_list_(NULL),
36 weak_reference_list_(NULL),
37 finalizer_reference_list_(NULL),
38 phantom_reference_list_(NULL),
Elliott Hughes352a4242011-10-31 15:15:21 -070039 cleared_reference_list_(NULL),
40 class_count_(0), array_count_(0), other_count_(0) {
Carl Shapiro58551df2011-07-24 03:09:51 -070041 }
42
Carl Shapiro69759ea2011-07-21 18:13:35 -070043 ~MarkSweep();
44
Carl Shapiro58551df2011-07-24 03:09:51 -070045 // Initializes internal structures.
Jesse Wilson078f9b02011-11-18 17:51:47 -050046 void Init();
Carl Shapiro58551df2011-07-24 03:09:51 -070047
Carl Shapiro69759ea2011-07-21 18:13:35 -070048 // Marks the root set at the start of a garbage collection.
49 void MarkRoots();
50
Ian Rogers5d76c432011-10-31 21:42:49 -070051 // Marks the roots in the image space on dirty cards
52 void ScanDirtyImageRoots();
53
54 bool IsMarkStackEmpty() const {
55 return mark_stack_->IsEmpty();
56 }
57
Carl Shapiro58551df2011-07-24 03:09:51 -070058 // Builds a mark stack and recursively mark until it empties.
59 void RecursiveMark();
60
Carl Shapiro69759ea2011-07-21 18:13:35 -070061 // Remarks the root set after completing the concurrent mark.
62 void ReMarkRoots();
63
Carl Shapiro58551df2011-07-24 03:09:51 -070064 void ProcessReferences(bool clear_soft_references) {
65 ProcessReferences(&soft_reference_list_, clear_soft_references,
66 &weak_reference_list_,
67 &finalizer_reference_list_,
68 &phantom_reference_list_);
69 }
70
Carl Shapiro69759ea2011-07-21 18:13:35 -070071 // Sweeps unmarked objects to complete the garbage collection.
72 void Sweep();
73
Elliott Hughesadb460d2011-10-05 17:02:34 -070074 Object* GetClearedReferences() {
75 return cleared_reference_list_;
76 }
77
Carl Shapiro69759ea2011-07-21 18:13:35 -070078 private:
79 // Returns true if the object has its bit set in the mark bitmap.
80 bool IsMarked(const Object* object) const {
81 return mark_bitmap_->Test(object);
82 }
83
Elliott Hughesc33a32b2011-10-11 18:18:07 -070084 static bool IsMarked(const Object* object, void* arg) {
85 return reinterpret_cast<MarkSweep*>(arg)->IsMarked(object);
86 }
87
Elliott Hughescf4c6c42011-09-01 15:16:42 -070088 static void MarkObjectVisitor(const Object* root, void* arg);
Brian Carlstrom1f870082011-08-23 16:02:11 -070089
Ian Rogers5d76c432011-10-31 21:42:49 -070090 static void ScanImageRootVisitor(Object* root, void* arg);
91
Carl Shapiro69759ea2011-07-21 18:13:35 -070092 // Marks an object.
93 void MarkObject(const Object* obj);
94
95 // Yuck.
96 void MarkObject0(const Object* obj, bool check_finger);
97
Carl Shapiro58551df2011-07-24 03:09:51 -070098 static void ScanBitmapCallback(Object* obj, void* finger, void* arg);
99
Ian Rogers5d76c432011-10-31 21:42:49 -0700100 static void CheckBitmapCallback(Object* obj, void* finger, void* arg);
101
Ian Rogers30fab402012-01-23 15:43:46 -0800102 static void SweepCallback(size_t num_ptrs, Object** ptrs, void* arg);
Carl Shapiro58551df2011-07-24 03:09:51 -0700103
Ian Rogers5d76c432011-10-31 21:42:49 -0700104 void CheckReference(const Object* obj, const Object* ref, MemberOffset offset, bool is_static);
105
Carl Shapiro69759ea2011-07-21 18:13:35 -0700106 // Blackens an object.
107 void ScanObject(const Object* obj);
108
Ian Rogers5d76c432011-10-31 21:42:49 -0700109 void CheckObject(const Object* obj);
110
Carl Shapiro69759ea2011-07-21 18:13:35 -0700111 // Grays references in instance fields.
112 void ScanInstanceFields(const Object* obj);
113
Ian Rogers5d76c432011-10-31 21:42:49 -0700114 void CheckInstanceFields(const Object* obj);
115
Carl Shapiro69759ea2011-07-21 18:13:35 -0700116 // Blackens a class object.
117 void ScanClass(const Object* obj);
118
Ian Rogers5d76c432011-10-31 21:42:49 -0700119 void CheckClass(const Object* obj);
120
Carl Shapiro69759ea2011-07-21 18:13:35 -0700121 // Grays references in static fields.
122 void ScanStaticFields(const Class* klass);
123
Ian Rogers5d76c432011-10-31 21:42:49 -0700124 void CheckStaticFields(const Class* klass);
125
Brian Carlstrom4873d462011-08-21 15:23:39 -0700126 // Used by ScanInstanceFields and ScanStaticFields
127 void ScanFields(const Object* obj, uint32_t ref_offsets, bool is_static);
128
Ian Rogers5d76c432011-10-31 21:42:49 -0700129 void CheckFields(const Object* obj, uint32_t ref_offsets, bool is_static);
130
Carl Shapiro69759ea2011-07-21 18:13:35 -0700131 // Grays references in an array.
132 void ScanArray(const Object* obj);
133
Ian Rogers5d76c432011-10-31 21:42:49 -0700134 void CheckArray(const Object* obj);
135
Carl Shapiro58551df2011-07-24 03:09:51 -0700136 void ScanOther(const Object* obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700137
Ian Rogers5d76c432011-10-31 21:42:49 -0700138 void CheckOther(const Object* obj);
139
Carl Shapiro69759ea2011-07-21 18:13:35 -0700140 // Blackens objects grayed during a garbage collection.
141 void ScanDirtyObjects();
142
143 // Schedules an unmarked object for reference processing.
144 void DelayReferenceReferent(Object* reference);
145
146 // Recursively blackens objects on the mark stack.
147 void ProcessMarkStack();
148
Carl Shapiro69759ea2011-07-21 18:13:35 -0700149 void EnqueueFinalizerReferences(Object** ref);
150
151 void PreserveSomeSoftReferences(Object** ref);
152
Carl Shapiro69759ea2011-07-21 18:13:35 -0700153 void ClearWhiteReferences(Object** list);
154
Carl Shapiro58551df2011-07-24 03:09:51 -0700155 void ProcessReferences(Object** soft_references, bool clear_soft_references,
Carl Shapiro69759ea2011-07-21 18:13:35 -0700156 Object** weak_references,
157 Object** finalizer_references,
158 Object** phantom_references);
159
Carl Shapiro58551df2011-07-24 03:09:51 -0700160 void SweepSystemWeaks();
Elliott Hughes410c0c82011-09-01 17:58:25 -0700161 void SweepJniWeakGlobals();
Carl Shapiro58551df2011-07-24 03:09:51 -0700162
Carl Shapiro69759ea2011-07-21 18:13:35 -0700163 MarkStack* mark_stack_;
164
165 HeapBitmap* mark_bitmap_;
166
Carl Shapiro58551df2011-07-24 03:09:51 -0700167 HeapBitmap* live_bitmap_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700168
169 Object* finger_;
170
171 Object* condemned_;
172
173 Object* soft_reference_list_;
174
175 Object* weak_reference_list_;
176
177 Object* finalizer_reference_list_;
178
179 Object* phantom_reference_list_;
180
181 Object* cleared_reference_list_;
182
Elliott Hughes352a4242011-10-31 15:15:21 -0700183 size_t class_count_;
184 size_t array_count_;
185 size_t other_count_;
186
Elliott Hughes410c0c82011-09-01 17:58:25 -0700187 friend class InternTableEntryIsUnmarked;
188
Carl Shapiro69759ea2011-07-21 18:13:35 -0700189 DISALLOW_COPY_AND_ASSIGN(MarkSweep);
190};
191
192} // namespace art
193
194#endif // ART_SRC_MARK_SWEEP_H_