blob: f20cc416bfbaebd708d45a57c0761ecd0f496a3c [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"
Carl Shapiro69759ea2011-07-21 18:13:35 -07009
10namespace art {
11
12class Class;
13class Object;
14
15class MarkSweep {
16 public:
Carl Shapiro58551df2011-07-24 03:09:51 -070017 MarkSweep() :
Brian Carlstrom1f870082011-08-23 16:02:11 -070018 finger_(NULL),
19 condemned_(NULL),
20 soft_reference_list_(NULL),
21 weak_reference_list_(NULL),
22 finalizer_reference_list_(NULL),
23 phantom_reference_list_(NULL),
24 cleared_reference_list_(NULL) {
Carl Shapiro58551df2011-07-24 03:09:51 -070025 }
26
Carl Shapiro69759ea2011-07-21 18:13:35 -070027 ~MarkSweep();
28
Carl Shapiro58551df2011-07-24 03:09:51 -070029 // Initializes internal structures.
30 bool Init();
31
Carl Shapiro69759ea2011-07-21 18:13:35 -070032 // Marks the root set at the start of a garbage collection.
33 void MarkRoots();
34
Carl Shapiro58551df2011-07-24 03:09:51 -070035 // Builds a mark stack and recursively mark until it empties.
36 void RecursiveMark();
37
Carl Shapiro69759ea2011-07-21 18:13:35 -070038 // Remarks the root set after completing the concurrent mark.
39 void ReMarkRoots();
40
Carl Shapiro58551df2011-07-24 03:09:51 -070041 void ProcessReferences(bool clear_soft_references) {
42 ProcessReferences(&soft_reference_list_, clear_soft_references,
43 &weak_reference_list_,
44 &finalizer_reference_list_,
45 &phantom_reference_list_);
46 }
47
Carl Shapiro69759ea2011-07-21 18:13:35 -070048 // Sweeps unmarked objects to complete the garbage collection.
49 void Sweep();
50
51 private:
52 // Returns true if the object has its bit set in the mark bitmap.
53 bool IsMarked(const Object* object) const {
54 return mark_bitmap_->Test(object);
55 }
56
Elliott Hughescf4c6c42011-09-01 15:16:42 -070057 static void MarkObjectVisitor(const Object* root, void* arg);
Brian Carlstrom1f870082011-08-23 16:02:11 -070058
Carl Shapiro69759ea2011-07-21 18:13:35 -070059 // Marks an object.
60 void MarkObject(const Object* obj);
61
62 // Yuck.
63 void MarkObject0(const Object* obj, bool check_finger);
64
Carl Shapiro58551df2011-07-24 03:09:51 -070065 static void ScanBitmapCallback(Object* obj, void* finger, void* arg);
66
67 static void SweepCallback(size_t num_ptrs, void** ptrs, void* arg);
68
Carl Shapiro69759ea2011-07-21 18:13:35 -070069 // Blackens an object.
70 void ScanObject(const Object* obj);
71
72 // Grays references in instance fields.
73 void ScanInstanceFields(const Object* obj);
74
75 // Blackens a class object.
76 void ScanClass(const Object* obj);
77
78 // Grays references in static fields.
79 void ScanStaticFields(const Class* klass);
80
Brian Carlstrom4873d462011-08-21 15:23:39 -070081 // Used by ScanInstanceFields and ScanStaticFields
82 void ScanFields(const Object* obj, uint32_t ref_offsets, bool is_static);
83
Carl Shapiro69759ea2011-07-21 18:13:35 -070084 // Grays interface class objects.
85 void ScanInterfaces(const Class* klass);
86
87 // Grays references in an array.
88 void ScanArray(const Object* obj);
89
Carl Shapiro58551df2011-07-24 03:09:51 -070090 void ScanOther(const Object* obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -070091
92 // Blackens objects grayed during a garbage collection.
93 void ScanDirtyObjects();
94
95 // Schedules an unmarked object for reference processing.
96 void DelayReferenceReferent(Object* reference);
97
98 // Recursively blackens objects on the mark stack.
99 void ProcessMarkStack();
100
101 // Adds a reference to the tail of a circular queue of references.
102 static void EnqueuePendingReference(Object* ref, Object** list);
103
104 // Removes the reference at the head of a circular queue of
105 // references.
106 static Object* DequeuePendingReference(Object** list);
107
108 // Sets the referent field of a reference object to null.
109 static void ClearReference(Object* reference);
110
111 // Returns true if the reference object has not yet been enqueued.
112 static bool IsEnqueuable(const Object* ref);
113
114 void EnqueueReference(Object* ref);
115
116 void EnqueueFinalizerReferences(Object** ref);
117
118 void PreserveSomeSoftReferences(Object** ref);
119
120 void EnqueueClearedReferences(Object** cleared_references);
121
122 void ClearWhiteReferences(Object** list);
123
Carl Shapiro58551df2011-07-24 03:09:51 -0700124 void ProcessReferences(Object** soft_references, bool clear_soft_references,
Carl Shapiro69759ea2011-07-21 18:13:35 -0700125 Object** weak_references,
126 Object** finalizer_references,
127 Object** phantom_references);
128
Carl Shapiro58551df2011-07-24 03:09:51 -0700129 void SweepSystemWeaks();
Elliott Hughes410c0c82011-09-01 17:58:25 -0700130 void SweepMonitorList();
131 void SweepJniWeakGlobals();
Carl Shapiro58551df2011-07-24 03:09:51 -0700132
Carl Shapiro69759ea2011-07-21 18:13:35 -0700133 MarkStack* mark_stack_;
134
135 HeapBitmap* mark_bitmap_;
136
Carl Shapiro58551df2011-07-24 03:09:51 -0700137 HeapBitmap* live_bitmap_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700138
139 Object* finger_;
140
141 Object* condemned_;
142
143 Object* soft_reference_list_;
144
145 Object* weak_reference_list_;
146
147 Object* finalizer_reference_list_;
148
149 Object* phantom_reference_list_;
150
151 Object* cleared_reference_list_;
152
Elliott Hughes410c0c82011-09-01 17:58:25 -0700153 friend class InternTableEntryIsUnmarked;
154
Carl Shapiro69759ea2011-07-21 18:13:35 -0700155 DISALLOW_COPY_AND_ASSIGN(MarkSweep);
156};
157
158} // namespace art
159
160#endif // ART_SRC_MARK_SWEEP_H_