blob: 570d90d78e5cb96c50b12373f71116d279d43479 [file] [log] [blame]
Carl Shapiro69759ea2011-07-21 18:13:35 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2// Author: cshapiro@google.com (Carl Shapiro)
3
4#ifndef ART_SRC_MARK_SWEEP_H_
5#define ART_SRC_MARK_SWEEP_H_
6
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07007#include "macros.h"
8#include "mark_stack.h"
9#include "object_bitmap.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() :
19 finger_(NULL), condemned_(NULL) {
20 }
21
Carl Shapiro69759ea2011-07-21 18:13:35 -070022 ~MarkSweep();
23
Carl Shapiro58551df2011-07-24 03:09:51 -070024 // Initializes internal structures.
25 bool Init();
26
Carl Shapiro69759ea2011-07-21 18:13:35 -070027 // Marks the root set at the start of a garbage collection.
28 void MarkRoots();
29
Carl Shapiro58551df2011-07-24 03:09:51 -070030 // Builds a mark stack and recursively mark until it empties.
31 void RecursiveMark();
32
Carl Shapiro69759ea2011-07-21 18:13:35 -070033 // Remarks the root set after completing the concurrent mark.
34 void ReMarkRoots();
35
Carl Shapiro58551df2011-07-24 03:09:51 -070036 void ProcessReferences(bool clear_soft_references) {
37 ProcessReferences(&soft_reference_list_, clear_soft_references,
38 &weak_reference_list_,
39 &finalizer_reference_list_,
40 &phantom_reference_list_);
41 }
42
Carl Shapiro69759ea2011-07-21 18:13:35 -070043 // Sweeps unmarked objects to complete the garbage collection.
44 void Sweep();
45
46 private:
47 // Returns true if the object has its bit set in the mark bitmap.
48 bool IsMarked(const Object* object) const {
49 return mark_bitmap_->Test(object);
50 }
51
52 // Marks an object.
53 void MarkObject(const Object* obj);
54
55 // Yuck.
56 void MarkObject0(const Object* obj, bool check_finger);
57
Carl Shapiro58551df2011-07-24 03:09:51 -070058 static void ScanBitmapCallback(Object* obj, void* finger, void* arg);
59
60 static void SweepCallback(size_t num_ptrs, void** ptrs, void* arg);
61
Carl Shapiro69759ea2011-07-21 18:13:35 -070062 // Blackens an object.
63 void ScanObject(const Object* obj);
64
65 // Grays references in instance fields.
66 void ScanInstanceFields(const Object* obj);
67
68 // Blackens a class object.
69 void ScanClass(const Object* obj);
70
71 // Grays references in static fields.
72 void ScanStaticFields(const Class* klass);
73
74 // Grays interface class objects.
75 void ScanInterfaces(const Class* klass);
76
77 // Grays references in an array.
78 void ScanArray(const Object* obj);
79
Carl Shapiro58551df2011-07-24 03:09:51 -070080 void ScanOther(const Object* obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -070081
82 // Blackens objects grayed during a garbage collection.
83 void ScanDirtyObjects();
84
85 // Schedules an unmarked object for reference processing.
86 void DelayReferenceReferent(Object* reference);
87
88 // Recursively blackens objects on the mark stack.
89 void ProcessMarkStack();
90
91 // Adds a reference to the tail of a circular queue of references.
92 static void EnqueuePendingReference(Object* ref, Object** list);
93
94 // Removes the reference at the head of a circular queue of
95 // references.
96 static Object* DequeuePendingReference(Object** list);
97
98 // Sets the referent field of a reference object to null.
99 static void ClearReference(Object* reference);
100
101 // Returns true if the reference object has not yet been enqueued.
102 static bool IsEnqueuable(const Object* ref);
103
104 void EnqueueReference(Object* ref);
105
106 void EnqueueFinalizerReferences(Object** ref);
107
108 void PreserveSomeSoftReferences(Object** ref);
109
110 void EnqueueClearedReferences(Object** cleared_references);
111
112 void ClearWhiteReferences(Object** list);
113
Carl Shapiro58551df2011-07-24 03:09:51 -0700114 void ProcessReferences(Object** soft_references, bool clear_soft_references,
Carl Shapiro69759ea2011-07-21 18:13:35 -0700115 Object** weak_references,
116 Object** finalizer_references,
117 Object** phantom_references);
118
Carl Shapiro58551df2011-07-24 03:09:51 -0700119 void SweepSystemWeaks();
120
Carl Shapiro69759ea2011-07-21 18:13:35 -0700121 MarkStack* mark_stack_;
122
123 HeapBitmap* mark_bitmap_;
124
Carl Shapiro58551df2011-07-24 03:09:51 -0700125 HeapBitmap* live_bitmap_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700126
127 Object* finger_;
128
129 Object* condemned_;
130
131 Object* soft_reference_list_;
132
133 Object* weak_reference_list_;
134
135 Object* finalizer_reference_list_;
136
137 Object* phantom_reference_list_;
138
139 Object* cleared_reference_list_;
140
141 static size_t reference_referent_offset_;
142
143 static size_t reference_queue_offset_;
144
145 static size_t reference_queueNext_offset_;
146
147 static size_t reference_pendingNext_offset_;
148
149 static size_t finalizer_reference_zombie_offset_;
150
151 DISALLOW_COPY_AND_ASSIGN(MarkSweep);
152};
153
154} // namespace art
155
156#endif // ART_SRC_MARK_SWEEP_H_