blob: 841d8191cc54eccad337b35b4005325c8d82ad63 [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 interface class objects.
86 void ScanInterfaces(const Class* klass);
87
88 // Grays references in an array.
89 void ScanArray(const Object* obj);
90
Carl Shapiro58551df2011-07-24 03:09:51 -070091 void ScanOther(const Object* obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -070092
93 // Blackens objects grayed during a garbage collection.
94 void ScanDirtyObjects();
95
96 // Schedules an unmarked object for reference processing.
97 void DelayReferenceReferent(Object* reference);
98
99 // Recursively blackens objects on the mark stack.
100 void ProcessMarkStack();
101
102 // Adds a reference to the tail of a circular queue of references.
103 static void EnqueuePendingReference(Object* ref, Object** list);
104
105 // Removes the reference at the head of a circular queue of
106 // references.
107 static Object* DequeuePendingReference(Object** list);
108
109 // Sets the referent field of a reference object to null.
110 static void ClearReference(Object* reference);
111
112 // Returns true if the reference object has not yet been enqueued.
113 static bool IsEnqueuable(const Object* ref);
114
115 void EnqueueReference(Object* ref);
116
117 void EnqueueFinalizerReferences(Object** ref);
118
119 void PreserveSomeSoftReferences(Object** ref);
120
121 void EnqueueClearedReferences(Object** cleared_references);
122
123 void ClearWhiteReferences(Object** list);
124
Carl Shapiro58551df2011-07-24 03:09:51 -0700125 void ProcessReferences(Object** soft_references, bool clear_soft_references,
Carl Shapiro69759ea2011-07-21 18:13:35 -0700126 Object** weak_references,
127 Object** finalizer_references,
128 Object** phantom_references);
129
Carl Shapiro58551df2011-07-24 03:09:51 -0700130 void SweepSystemWeaks();
Elliott Hughes410c0c82011-09-01 17:58:25 -0700131 void SweepMonitorList();
132 void SweepJniWeakGlobals();
Carl Shapiro58551df2011-07-24 03:09:51 -0700133
Carl Shapiro69759ea2011-07-21 18:13:35 -0700134 MarkStack* mark_stack_;
135
136 HeapBitmap* mark_bitmap_;
137
Carl Shapiro58551df2011-07-24 03:09:51 -0700138 HeapBitmap* live_bitmap_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700139
140 Object* finger_;
141
142 Object* condemned_;
143
144 Object* soft_reference_list_;
145
146 Object* weak_reference_list_;
147
148 Object* finalizer_reference_list_;
149
150 Object* phantom_reference_list_;
151
152 Object* cleared_reference_list_;
153
Elliott Hughes410c0c82011-09-01 17:58:25 -0700154 friend class InternTableEntryIsUnmarked;
155
Carl Shapiro69759ea2011-07-21 18:13:35 -0700156 DISALLOW_COPY_AND_ASSIGN(MarkSweep);
157};
158
159} // namespace art
160
161#endif // ART_SRC_MARK_SWEEP_H_