Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 2 | |
| 3 | #ifndef ART_SRC_MARK_SWEEP_H_ |
| 4 | #define ART_SRC_MARK_SWEEP_H_ |
| 5 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 6 | #include "macros.h" |
| 7 | #include "mark_stack.h" |
Elliott Hughes | 5e71b52 | 2011-10-20 13:12:32 -0700 | [diff] [blame] | 8 | #include "heap_bitmap.h" |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 9 | #include "offsets.h" |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 10 | |
| 11 | namespace art { |
| 12 | |
| 13 | class Class; |
| 14 | class Object; |
| 15 | |
| 16 | class MarkSweep { |
| 17 | public: |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 18 | MarkSweep() : |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 19 | 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), |
Elliott Hughes | 352a424 | 2011-10-31 15:15:21 -0700 | [diff] [blame] | 25 | cleared_reference_list_(NULL), |
| 26 | class_count_(0), array_count_(0), other_count_(0) { |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 27 | } |
| 28 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 29 | ~MarkSweep(); |
| 30 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 31 | // Initializes internal structures. |
Jesse Wilson | 078f9b0 | 2011-11-18 17:51:47 -0500 | [diff] [blame] | 32 | void Init(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 33 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 34 | // Marks the root set at the start of a garbage collection. |
| 35 | void MarkRoots(); |
| 36 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 37 | // Marks the roots in the image space on dirty cards |
| 38 | void ScanDirtyImageRoots(); |
| 39 | |
| 40 | bool IsMarkStackEmpty() const { |
| 41 | return mark_stack_->IsEmpty(); |
| 42 | } |
| 43 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 44 | // Builds a mark stack and recursively mark until it empties. |
| 45 | void RecursiveMark(); |
| 46 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 47 | // Remarks the root set after completing the concurrent mark. |
| 48 | void ReMarkRoots(); |
| 49 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 50 | void ProcessReferences(bool clear_soft_references) { |
| 51 | ProcessReferences(&soft_reference_list_, clear_soft_references, |
| 52 | &weak_reference_list_, |
| 53 | &finalizer_reference_list_, |
| 54 | &phantom_reference_list_); |
| 55 | } |
| 56 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 57 | // Sweeps unmarked objects to complete the garbage collection. |
| 58 | void Sweep(); |
| 59 | |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 60 | Object* GetClearedReferences() { |
| 61 | return cleared_reference_list_; |
| 62 | } |
| 63 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 64 | private: |
| 65 | // Returns true if the object has its bit set in the mark bitmap. |
| 66 | bool IsMarked(const Object* object) const { |
| 67 | return mark_bitmap_->Test(object); |
| 68 | } |
| 69 | |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 70 | static bool IsMarked(const Object* object, void* arg) { |
| 71 | return reinterpret_cast<MarkSweep*>(arg)->IsMarked(object); |
| 72 | } |
| 73 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 74 | static void MarkObjectVisitor(const Object* root, void* arg); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 75 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 76 | static void ScanImageRootVisitor(Object* root, void* arg); |
| 77 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 78 | // Marks an object. |
| 79 | void MarkObject(const Object* obj); |
| 80 | |
| 81 | // Yuck. |
| 82 | void MarkObject0(const Object* obj, bool check_finger); |
| 83 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 84 | static void ScanBitmapCallback(Object* obj, void* finger, void* arg); |
| 85 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 86 | static void CheckBitmapCallback(Object* obj, void* finger, void* arg); |
| 87 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 88 | static void SweepCallback(size_t num_ptrs, void** ptrs, void* arg); |
| 89 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 90 | void CheckReference(const Object* obj, const Object* ref, MemberOffset offset, bool is_static); |
| 91 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 92 | // Blackens an object. |
| 93 | void ScanObject(const Object* obj); |
| 94 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 95 | void CheckObject(const Object* obj); |
| 96 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 97 | // Grays references in instance fields. |
| 98 | void ScanInstanceFields(const Object* obj); |
| 99 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 100 | void CheckInstanceFields(const Object* obj); |
| 101 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 102 | // Blackens a class object. |
| 103 | void ScanClass(const Object* obj); |
| 104 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 105 | void CheckClass(const Object* obj); |
| 106 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 107 | // Grays references in static fields. |
| 108 | void ScanStaticFields(const Class* klass); |
| 109 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 110 | void CheckStaticFields(const Class* klass); |
| 111 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 112 | // Used by ScanInstanceFields and ScanStaticFields |
| 113 | void ScanFields(const Object* obj, uint32_t ref_offsets, bool is_static); |
| 114 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 115 | void CheckFields(const Object* obj, uint32_t ref_offsets, bool is_static); |
| 116 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 117 | // Grays references in an array. |
| 118 | void ScanArray(const Object* obj); |
| 119 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 120 | void CheckArray(const Object* obj); |
| 121 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 122 | void ScanOther(const Object* obj); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 123 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 124 | void CheckOther(const Object* obj); |
| 125 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 126 | // Blackens objects grayed during a garbage collection. |
| 127 | void ScanDirtyObjects(); |
| 128 | |
| 129 | // Schedules an unmarked object for reference processing. |
| 130 | void DelayReferenceReferent(Object* reference); |
| 131 | |
| 132 | // Recursively blackens objects on the mark stack. |
| 133 | void ProcessMarkStack(); |
| 134 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 135 | void EnqueueFinalizerReferences(Object** ref); |
| 136 | |
| 137 | void PreserveSomeSoftReferences(Object** ref); |
| 138 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 139 | void ClearWhiteReferences(Object** list); |
| 140 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 141 | void ProcessReferences(Object** soft_references, bool clear_soft_references, |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 142 | Object** weak_references, |
| 143 | Object** finalizer_references, |
| 144 | Object** phantom_references); |
| 145 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 146 | void SweepSystemWeaks(); |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 147 | void SweepJniWeakGlobals(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 148 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 149 | MarkStack* mark_stack_; |
| 150 | |
| 151 | HeapBitmap* mark_bitmap_; |
| 152 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 153 | HeapBitmap* live_bitmap_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 154 | |
| 155 | Object* finger_; |
| 156 | |
| 157 | Object* condemned_; |
| 158 | |
| 159 | Object* soft_reference_list_; |
| 160 | |
| 161 | Object* weak_reference_list_; |
| 162 | |
| 163 | Object* finalizer_reference_list_; |
| 164 | |
| 165 | Object* phantom_reference_list_; |
| 166 | |
| 167 | Object* cleared_reference_list_; |
| 168 | |
Elliott Hughes | 352a424 | 2011-10-31 15:15:21 -0700 | [diff] [blame] | 169 | size_t class_count_; |
| 170 | size_t array_count_; |
| 171 | size_t other_count_; |
| 172 | |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 173 | friend class InternTableEntryIsUnmarked; |
| 174 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 175 | DISALLOW_COPY_AND_ASSIGN(MarkSweep); |
| 176 | }; |
| 177 | |
| 178 | } // namespace art |
| 179 | |
| 180 | #endif // ART_SRC_MARK_SWEEP_H_ |