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" |
| 8 | #include "object_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), |
| 25 | cleared_reference_list_(NULL) { |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 26 | } |
| 27 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 28 | ~MarkSweep(); |
| 29 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 30 | // Initializes internal structures. |
| 31 | bool Init(); |
| 32 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 33 | // Marks the root set at the start of a garbage collection. |
| 34 | void MarkRoots(); |
| 35 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 36 | // Builds a mark stack and recursively mark until it empties. |
| 37 | void RecursiveMark(); |
| 38 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 39 | // Remarks the root set after completing the concurrent mark. |
| 40 | void ReMarkRoots(); |
| 41 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 42 | 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 Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 49 | // 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 Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 58 | static void MarkObjectVisitor(const Object* root, void* arg); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 59 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 60 | // Marks an object. |
| 61 | void MarkObject(const Object* obj); |
| 62 | |
| 63 | // Yuck. |
| 64 | void MarkObject0(const Object* obj, bool check_finger); |
| 65 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 66 | static void ScanBitmapCallback(Object* obj, void* finger, void* arg); |
| 67 | |
| 68 | static void SweepCallback(size_t num_ptrs, void** ptrs, void* arg); |
| 69 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 70 | // 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 Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 82 | // Used by ScanInstanceFields and ScanStaticFields |
| 83 | void ScanFields(const Object* obj, uint32_t ref_offsets, bool is_static); |
| 84 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 85 | // 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 Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 91 | void ScanOther(const Object* obj); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 92 | |
| 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 Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 125 | void ProcessReferences(Object** soft_references, bool clear_soft_references, |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 126 | Object** weak_references, |
| 127 | Object** finalizer_references, |
| 128 | Object** phantom_references); |
| 129 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 130 | void SweepSystemWeaks(); |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 131 | void SweepMonitorList(); |
| 132 | void SweepJniWeakGlobals(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 133 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 134 | MarkStack* mark_stack_; |
| 135 | |
| 136 | HeapBitmap* mark_bitmap_; |
| 137 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 138 | HeapBitmap* live_bitmap_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 139 | |
| 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 Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 154 | friend class InternTableEntryIsUnmarked; |
| 155 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 156 | DISALLOW_COPY_AND_ASSIGN(MarkSweep); |
| 157 | }; |
| 158 | |
| 159 | } // namespace art |
| 160 | |
| 161 | #endif // ART_SRC_MARK_SWEEP_H_ |