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. |
| 32 | bool Init(); |
| 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 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 37 | // Builds a mark stack and recursively mark until it empties. |
| 38 | void RecursiveMark(); |
| 39 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 40 | // Remarks the root set after completing the concurrent mark. |
| 41 | void ReMarkRoots(); |
| 42 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 43 | void ProcessReferences(bool clear_soft_references) { |
| 44 | ProcessReferences(&soft_reference_list_, clear_soft_references, |
| 45 | &weak_reference_list_, |
| 46 | &finalizer_reference_list_, |
| 47 | &phantom_reference_list_); |
| 48 | } |
| 49 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 50 | // Sweeps unmarked objects to complete the garbage collection. |
| 51 | void Sweep(); |
| 52 | |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 53 | Object* GetClearedReferences() { |
| 54 | return cleared_reference_list_; |
| 55 | } |
| 56 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 57 | private: |
| 58 | // Returns true if the object has its bit set in the mark bitmap. |
| 59 | bool IsMarked(const Object* object) const { |
| 60 | return mark_bitmap_->Test(object); |
| 61 | } |
| 62 | |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 63 | static bool IsMarked(const Object* object, void* arg) { |
| 64 | return reinterpret_cast<MarkSweep*>(arg)->IsMarked(object); |
| 65 | } |
| 66 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 67 | static void MarkObjectVisitor(const Object* root, void* arg); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 68 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 69 | // Marks an object. |
| 70 | void MarkObject(const Object* obj); |
| 71 | |
| 72 | // Yuck. |
| 73 | void MarkObject0(const Object* obj, bool check_finger); |
| 74 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 75 | static void ScanBitmapCallback(Object* obj, void* finger, void* arg); |
| 76 | |
| 77 | static void SweepCallback(size_t num_ptrs, void** ptrs, void* arg); |
| 78 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 79 | // Blackens an object. |
| 80 | void ScanObject(const Object* obj); |
| 81 | |
| 82 | // Grays references in instance fields. |
| 83 | void ScanInstanceFields(const Object* obj); |
| 84 | |
| 85 | // Blackens a class object. |
| 86 | void ScanClass(const Object* obj); |
| 87 | |
| 88 | // Grays references in static fields. |
| 89 | void ScanStaticFields(const Class* klass); |
| 90 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 91 | // Used by ScanInstanceFields and ScanStaticFields |
| 92 | void ScanFields(const Object* obj, uint32_t ref_offsets, bool is_static); |
| 93 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 94 | // Grays references in an array. |
| 95 | void ScanArray(const Object* obj); |
| 96 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 97 | void ScanOther(const Object* obj); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 98 | |
| 99 | // Blackens objects grayed during a garbage collection. |
| 100 | void ScanDirtyObjects(); |
| 101 | |
| 102 | // Schedules an unmarked object for reference processing. |
| 103 | void DelayReferenceReferent(Object* reference); |
| 104 | |
| 105 | // Recursively blackens objects on the mark stack. |
| 106 | void ProcessMarkStack(); |
| 107 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 108 | void EnqueueFinalizerReferences(Object** ref); |
| 109 | |
| 110 | void PreserveSomeSoftReferences(Object** ref); |
| 111 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 112 | void ClearWhiteReferences(Object** list); |
| 113 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 114 | void ProcessReferences(Object** soft_references, bool clear_soft_references, |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 115 | Object** weak_references, |
| 116 | Object** finalizer_references, |
| 117 | Object** phantom_references); |
| 118 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 119 | void SweepSystemWeaks(); |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 120 | void SweepJniWeakGlobals(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 121 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 122 | MarkStack* mark_stack_; |
| 123 | |
| 124 | HeapBitmap* mark_bitmap_; |
| 125 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 126 | HeapBitmap* live_bitmap_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 127 | |
| 128 | Object* finger_; |
| 129 | |
| 130 | Object* condemned_; |
| 131 | |
| 132 | Object* soft_reference_list_; |
| 133 | |
| 134 | Object* weak_reference_list_; |
| 135 | |
| 136 | Object* finalizer_reference_list_; |
| 137 | |
| 138 | Object* phantom_reference_list_; |
| 139 | |
| 140 | Object* cleared_reference_list_; |
| 141 | |
Elliott Hughes | 352a424 | 2011-10-31 15:15:21 -0700 | [diff] [blame^] | 142 | size_t class_count_; |
| 143 | size_t array_count_; |
| 144 | size_t other_count_; |
| 145 | |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 146 | friend class InternTableEntryIsUnmarked; |
| 147 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 148 | DISALLOW_COPY_AND_ASSIGN(MarkSweep); |
| 149 | }; |
| 150 | |
| 151 | } // namespace art |
| 152 | |
| 153 | #endif // ART_SRC_MARK_SWEEP_H_ |