Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1 | // 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 Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 7 | #include "macros.h" |
| 8 | #include "mark_stack.h" |
| 9 | #include "object_bitmap.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() : |
| 19 | finger_(NULL), condemned_(NULL) { |
| 20 | } |
| 21 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 22 | ~MarkSweep(); |
| 23 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 24 | // Initializes internal structures. |
| 25 | bool Init(); |
| 26 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 27 | // Marks the root set at the start of a garbage collection. |
| 28 | void MarkRoots(); |
| 29 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 30 | // Builds a mark stack and recursively mark until it empties. |
| 31 | void RecursiveMark(); |
| 32 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 33 | // Remarks the root set after completing the concurrent mark. |
| 34 | void ReMarkRoots(); |
| 35 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 36 | 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 Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 43 | // 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 Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 58 | static void ScanBitmapCallback(Object* obj, void* finger, void* arg); |
| 59 | |
| 60 | static void SweepCallback(size_t num_ptrs, void** ptrs, void* arg); |
| 61 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 62 | // 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 Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 80 | void ScanOther(const Object* obj); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 81 | |
| 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 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(); |
| 120 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 121 | MarkStack* mark_stack_; |
| 122 | |
| 123 | HeapBitmap* mark_bitmap_; |
| 124 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 125 | HeapBitmap* live_bitmap_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 126 | |
| 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_ |