blob: fe37cabf527b9c1d127c719ce8ee7a8a853cadf0 [file] [log] [blame]
Carl Shapiro69759ea2011-07-21 18:13:35 -07001// Copyright 2011 Google Inc. All Rights Reserved.
Carl Shapiro69759ea2011-07-21 18:13:35 -07002
3#ifndef ART_SRC_MARK_SWEEP_H_
4#define ART_SRC_MARK_SWEEP_H_
5
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07006#include "macros.h"
7#include "mark_stack.h"
8#include "object_bitmap.h"
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07009#include "offsets.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070010
11namespace art {
12
13class Class;
14class Object;
15
16class MarkSweep {
17 public:
Carl Shapiro58551df2011-07-24 03:09:51 -070018 MarkSweep() :
Brian Carlstrom1f870082011-08-23 16:02:11 -070019 finger_(NULL),
20 condemned_(NULL),
21 soft_reference_list_(NULL),
22 weak_reference_list_(NULL),
23 finalizer_reference_list_(NULL),
24 phantom_reference_list_(NULL),
25 cleared_reference_list_(NULL) {
Carl Shapiro58551df2011-07-24 03:09:51 -070026 }
27
Carl Shapiro69759ea2011-07-21 18:13:35 -070028 ~MarkSweep();
29
Carl Shapiro58551df2011-07-24 03:09:51 -070030 // Initializes internal structures.
31 bool Init();
32
Carl Shapiro69759ea2011-07-21 18:13:35 -070033 // Marks the root set at the start of a garbage collection.
34 void MarkRoots();
35
Carl Shapiro58551df2011-07-24 03:09:51 -070036 // Builds a mark stack and recursively mark until it empties.
37 void RecursiveMark();
38
Carl Shapiro69759ea2011-07-21 18:13:35 -070039 // Remarks the root set after completing the concurrent mark.
40 void ReMarkRoots();
41
Carl Shapiro58551df2011-07-24 03:09:51 -070042 void ProcessReferences(bool clear_soft_references) {
43 ProcessReferences(&soft_reference_list_, clear_soft_references,
44 &weak_reference_list_,
45 &finalizer_reference_list_,
46 &phantom_reference_list_);
47 }
48
Carl Shapiro69759ea2011-07-21 18:13:35 -070049 // Sweeps unmarked objects to complete the garbage collection.
50 void Sweep();
51
52 private:
53 // Returns true if the object has its bit set in the mark bitmap.
54 bool IsMarked(const Object* object) const {
55 return mark_bitmap_->Test(object);
56 }
57
Elliott Hughescf4c6c42011-09-01 15:16:42 -070058 static void MarkObjectVisitor(const Object* root, void* arg);
Brian Carlstrom1f870082011-08-23 16:02:11 -070059
Carl Shapiro69759ea2011-07-21 18:13:35 -070060 // Marks an object.
61 void MarkObject(const Object* obj);
62
63 // Yuck.
64 void MarkObject0(const Object* obj, bool check_finger);
65
Carl Shapiro58551df2011-07-24 03:09:51 -070066 static void ScanBitmapCallback(Object* obj, void* finger, void* arg);
67
68 static void SweepCallback(size_t num_ptrs, void** ptrs, void* arg);
69
Carl Shapiro69759ea2011-07-21 18:13:35 -070070 // Blackens an object.
71 void ScanObject(const Object* obj);
72
73 // Grays references in instance fields.
74 void ScanInstanceFields(const Object* obj);
75
76 // Blackens a class object.
77 void ScanClass(const Object* obj);
78
79 // Grays references in static fields.
80 void ScanStaticFields(const Class* klass);
81
Brian Carlstrom4873d462011-08-21 15:23:39 -070082 // Used by ScanInstanceFields and ScanStaticFields
83 void ScanFields(const Object* obj, uint32_t ref_offsets, bool is_static);
84
Carl Shapiro69759ea2011-07-21 18:13:35 -070085 // Grays references in an array.
86 void ScanArray(const Object* obj);
87
Carl Shapiro58551df2011-07-24 03:09:51 -070088 void ScanOther(const Object* obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -070089
90 // Blackens objects grayed during a garbage collection.
91 void ScanDirtyObjects();
92
93 // Schedules an unmarked object for reference processing.
94 void DelayReferenceReferent(Object* reference);
95
96 // Recursively blackens objects on the mark stack.
97 void ProcessMarkStack();
98
99 // Adds a reference to the tail of a circular queue of references.
100 static void EnqueuePendingReference(Object* ref, Object** list);
101
102 // Removes the reference at the head of a circular queue of
103 // references.
104 static Object* DequeuePendingReference(Object** list);
105
106 // Sets the referent field of a reference object to null.
107 static void ClearReference(Object* reference);
108
109 // Returns true if the reference object has not yet been enqueued.
110 static bool IsEnqueuable(const Object* ref);
111
112 void EnqueueReference(Object* ref);
113
114 void EnqueueFinalizerReferences(Object** ref);
115
116 void PreserveSomeSoftReferences(Object** ref);
117
118 void EnqueueClearedReferences(Object** cleared_references);
119
120 void ClearWhiteReferences(Object** list);
121
Carl Shapiro58551df2011-07-24 03:09:51 -0700122 void ProcessReferences(Object** soft_references, bool clear_soft_references,
Carl Shapiro69759ea2011-07-21 18:13:35 -0700123 Object** weak_references,
124 Object** finalizer_references,
125 Object** phantom_references);
126
Carl Shapiro58551df2011-07-24 03:09:51 -0700127 void SweepSystemWeaks();
Elliott Hughes410c0c82011-09-01 17:58:25 -0700128 void SweepMonitorList();
129 void SweepJniWeakGlobals();
Carl Shapiro58551df2011-07-24 03:09:51 -0700130
Carl Shapiro69759ea2011-07-21 18:13:35 -0700131 MarkStack* mark_stack_;
132
133 HeapBitmap* mark_bitmap_;
134
Carl Shapiro58551df2011-07-24 03:09:51 -0700135 HeapBitmap* live_bitmap_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700136
137 Object* finger_;
138
139 Object* condemned_;
140
141 Object* soft_reference_list_;
142
143 Object* weak_reference_list_;
144
145 Object* finalizer_reference_list_;
146
147 Object* phantom_reference_list_;
148
149 Object* cleared_reference_list_;
150
Elliott Hughes410c0c82011-09-01 17:58:25 -0700151 friend class InternTableEntryIsUnmarked;
152
Carl Shapiro69759ea2011-07-21 18:13:35 -0700153 DISALLOW_COPY_AND_ASSIGN(MarkSweep);
154};
155
156} // namespace art
157
158#endif // ART_SRC_MARK_SWEEP_H_