blob: dce4b5221b68b0fca9a2f5c1a553a9fca9d7c0ec [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:
Carl Shapiro58551df2011-07-24 03:09:51 -070033 MarkSweep() :
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080034 mark_stack_(NULL),
35 heap_(NULL),
36 mark_bitmap_(NULL),
37 live_bitmap_(NULL),
Brian Carlstrom1f870082011-08-23 16:02:11 -070038 finger_(NULL),
39 condemned_(NULL),
40 soft_reference_list_(NULL),
41 weak_reference_list_(NULL),
42 finalizer_reference_list_(NULL),
43 phantom_reference_list_(NULL),
Elliott Hughes352a4242011-10-31 15:15:21 -070044 cleared_reference_list_(NULL),
45 class_count_(0), array_count_(0), other_count_(0) {
Carl Shapiro58551df2011-07-24 03:09:51 -070046 }
47
Carl Shapiro69759ea2011-07-21 18:13:35 -070048 ~MarkSweep();
49
Carl Shapiro58551df2011-07-24 03:09:51 -070050 // Initializes internal structures.
Jesse Wilson078f9b02011-11-18 17:51:47 -050051 void Init();
Carl Shapiro58551df2011-07-24 03:09:51 -070052
Carl Shapiro69759ea2011-07-21 18:13:35 -070053 // Marks the root set at the start of a garbage collection.
54 void MarkRoots();
55
Ian Rogers5d76c432011-10-31 21:42:49 -070056 // Marks the roots in the image space on dirty cards
57 void ScanDirtyImageRoots();
58
59 bool IsMarkStackEmpty() const {
60 return mark_stack_->IsEmpty();
61 }
62
Carl Shapiro58551df2011-07-24 03:09:51 -070063 // Builds a mark stack and recursively mark until it empties.
64 void RecursiveMark();
65
Carl Shapiro69759ea2011-07-21 18:13:35 -070066 // Remarks the root set after completing the concurrent mark.
67 void ReMarkRoots();
68
Carl Shapiro58551df2011-07-24 03:09:51 -070069 void ProcessReferences(bool clear_soft_references) {
70 ProcessReferences(&soft_reference_list_, clear_soft_references,
71 &weak_reference_list_,
72 &finalizer_reference_list_,
73 &phantom_reference_list_);
74 }
75
Carl Shapiro69759ea2011-07-21 18:13:35 -070076 // Sweeps unmarked objects to complete the garbage collection.
77 void Sweep();
78
Elliott Hughesadb460d2011-10-05 17:02:34 -070079 Object* GetClearedReferences() {
80 return cleared_reference_list_;
81 }
82
Carl Shapiro69759ea2011-07-21 18:13:35 -070083 private:
84 // Returns true if the object has its bit set in the mark bitmap.
85 bool IsMarked(const Object* object) const {
86 return mark_bitmap_->Test(object);
87 }
88
Elliott Hughesc33a32b2011-10-11 18:18:07 -070089 static bool IsMarked(const Object* object, void* arg) {
90 return reinterpret_cast<MarkSweep*>(arg)->IsMarked(object);
91 }
92
Elliott Hughescf4c6c42011-09-01 15:16:42 -070093 static void MarkObjectVisitor(const Object* root, void* arg);
Brian Carlstrom1f870082011-08-23 16:02:11 -070094
Ian Rogers5d76c432011-10-31 21:42:49 -070095 static void ScanImageRootVisitor(Object* root, void* arg);
96
Carl Shapiro69759ea2011-07-21 18:13:35 -070097 // Marks an object.
98 void MarkObject(const Object* obj);
99
100 // Yuck.
101 void MarkObject0(const Object* obj, bool check_finger);
102
Carl Shapiro58551df2011-07-24 03:09:51 -0700103 static void ScanBitmapCallback(Object* obj, void* finger, void* arg);
104
Ian Rogers5d76c432011-10-31 21:42:49 -0700105 static void CheckBitmapCallback(Object* obj, void* finger, void* arg);
106
Ian Rogers30fab402012-01-23 15:43:46 -0800107 static void SweepCallback(size_t num_ptrs, Object** ptrs, void* arg);
Carl Shapiro58551df2011-07-24 03:09:51 -0700108
Ian Rogers5d76c432011-10-31 21:42:49 -0700109 void CheckReference(const Object* obj, const Object* ref, MemberOffset offset, bool is_static);
110
Carl Shapiro69759ea2011-07-21 18:13:35 -0700111 // Blackens an object.
112 void ScanObject(const Object* obj);
113
Ian Rogers5d76c432011-10-31 21:42:49 -0700114 void CheckObject(const Object* obj);
115
Carl Shapiro69759ea2011-07-21 18:13:35 -0700116 // Grays references in instance fields.
117 void ScanInstanceFields(const Object* obj);
118
Ian Rogers5d76c432011-10-31 21:42:49 -0700119 void CheckInstanceFields(const Object* obj);
120
Carl Shapiro69759ea2011-07-21 18:13:35 -0700121 // Blackens a class object.
122 void ScanClass(const Object* obj);
123
Ian Rogers5d76c432011-10-31 21:42:49 -0700124 void CheckClass(const Object* obj);
125
Carl Shapiro69759ea2011-07-21 18:13:35 -0700126 // Grays references in static fields.
127 void ScanStaticFields(const Class* klass);
128
Ian Rogers5d76c432011-10-31 21:42:49 -0700129 void CheckStaticFields(const Class* klass);
130
Brian Carlstrom4873d462011-08-21 15:23:39 -0700131 // Used by ScanInstanceFields and ScanStaticFields
132 void ScanFields(const Object* obj, uint32_t ref_offsets, bool is_static);
133
Ian Rogers5d76c432011-10-31 21:42:49 -0700134 void CheckFields(const Object* obj, uint32_t ref_offsets, bool is_static);
135
Carl Shapiro69759ea2011-07-21 18:13:35 -0700136 // Grays references in an array.
137 void ScanArray(const Object* obj);
138
Ian Rogers5d76c432011-10-31 21:42:49 -0700139 void CheckArray(const Object* obj);
140
Carl Shapiro58551df2011-07-24 03:09:51 -0700141 void ScanOther(const Object* obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700142
Ian Rogers5d76c432011-10-31 21:42:49 -0700143 void CheckOther(const Object* obj);
144
Carl Shapiro69759ea2011-07-21 18:13:35 -0700145 // Blackens objects grayed during a garbage collection.
146 void ScanDirtyObjects();
147
148 // Schedules an unmarked object for reference processing.
149 void DelayReferenceReferent(Object* reference);
150
151 // Recursively blackens objects on the mark stack.
152 void ProcessMarkStack();
153
Carl Shapiro69759ea2011-07-21 18:13:35 -0700154 void EnqueueFinalizerReferences(Object** ref);
155
156 void PreserveSomeSoftReferences(Object** ref);
157
Carl Shapiro69759ea2011-07-21 18:13:35 -0700158 void ClearWhiteReferences(Object** list);
159
Carl Shapiro58551df2011-07-24 03:09:51 -0700160 void ProcessReferences(Object** soft_references, bool clear_soft_references,
Carl Shapiro69759ea2011-07-21 18:13:35 -0700161 Object** weak_references,
162 Object** finalizer_references,
163 Object** phantom_references);
164
Carl Shapiro58551df2011-07-24 03:09:51 -0700165 void SweepSystemWeaks();
Elliott Hughes410c0c82011-09-01 17:58:25 -0700166 void SweepJniWeakGlobals();
Carl Shapiro58551df2011-07-24 03:09:51 -0700167
Carl Shapiro69759ea2011-07-21 18:13:35 -0700168 MarkStack* mark_stack_;
169
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800170 Heap* heap_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700171 HeapBitmap* mark_bitmap_;
Carl Shapiro58551df2011-07-24 03:09:51 -0700172 HeapBitmap* live_bitmap_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700173
174 Object* finger_;
175
176 Object* condemned_;
177
178 Object* soft_reference_list_;
179
180 Object* weak_reference_list_;
181
182 Object* finalizer_reference_list_;
183
184 Object* phantom_reference_list_;
185
186 Object* cleared_reference_list_;
187
Elliott Hughes352a4242011-10-31 15:15:21 -0700188 size_t class_count_;
189 size_t array_count_;
190 size_t other_count_;
191
Elliott Hughes410c0c82011-09-01 17:58:25 -0700192 friend class InternTableEntryIsUnmarked;
193
Carl Shapiro69759ea2011-07-21 18:13:35 -0700194 DISALLOW_COPY_AND_ASSIGN(MarkSweep);
195};
196
197} // namespace art
198
199#endif // ART_SRC_MARK_SWEEP_H_