Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 16 | |
| 17 | #ifndef ART_SRC_MARK_SWEEP_H_ |
| 18 | #define ART_SRC_MARK_SWEEP_H_ |
| 19 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 20 | #include "macros.h" |
| 21 | #include "mark_stack.h" |
Elliott Hughes | 5e71b52 | 2011-10-20 13:12:32 -0700 | [diff] [blame] | 22 | #include "heap_bitmap.h" |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 23 | #include "offsets.h" |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 24 | |
| 25 | namespace art { |
| 26 | |
| 27 | class Class; |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 28 | class Heap; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 29 | class Object; |
| 30 | |
| 31 | class MarkSweep { |
| 32 | public: |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 33 | explicit MarkSweep(MarkStack* mark_stack); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 34 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 35 | ~MarkSweep(); |
| 36 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 37 | // Initializes internal structures. |
Jesse Wilson | 078f9b0 | 2011-11-18 17:51:47 -0500 | [diff] [blame] | 38 | void Init(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 39 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 40 | // Marks the root set at the start of a garbage collection. |
| 41 | void MarkRoots(); |
| 42 | |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 43 | // Marks the roots in the image space on dirty cards. |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 44 | void ScanDirtyImageRoots(); |
| 45 | |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 46 | // Verify that image roots point to only marked objects within the alloc space. |
| 47 | void VerifyImageRoots(); |
| 48 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 49 | bool IsMarkStackEmpty() const { |
| 50 | return mark_stack_->IsEmpty(); |
| 51 | } |
| 52 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 53 | // Builds a mark stack and recursively mark until it empties. |
| 54 | void RecursiveMark(); |
| 55 | |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 56 | // Builds a mark stack with objects on dirty cards and recursively mark |
| 57 | // until it empties. |
| 58 | void RecursiveMarkDirtyObjects(); |
| 59 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 60 | // Remarks the root set after completing the concurrent mark. |
| 61 | void ReMarkRoots(); |
| 62 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 63 | void ProcessReferences(bool clear_soft_references) { |
| 64 | ProcessReferences(&soft_reference_list_, clear_soft_references, |
| 65 | &weak_reference_list_, |
| 66 | &finalizer_reference_list_, |
| 67 | &phantom_reference_list_); |
| 68 | } |
| 69 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 70 | // Sweeps unmarked objects to complete the garbage collection. |
| 71 | void Sweep(); |
| 72 | |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 73 | Object* GetClearedReferences() { |
| 74 | return cleared_reference_list_; |
| 75 | } |
| 76 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 77 | private: |
| 78 | // Returns true if the object has its bit set in the mark bitmap. |
| 79 | bool IsMarked(const Object* object) const { |
| 80 | return mark_bitmap_->Test(object); |
| 81 | } |
| 82 | |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 83 | static bool IsMarked(const Object* object, void* arg) { |
| 84 | return reinterpret_cast<MarkSweep*>(arg)->IsMarked(object); |
| 85 | } |
| 86 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 87 | static void MarkObjectVisitor(const Object* root, void* arg); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 88 | |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 89 | static void ReMarkObjectVisitor(const Object* root, void* arg); |
| 90 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 91 | static void ScanImageRootVisitor(Object* root, void* arg); |
| 92 | |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 93 | static void ScanDirtyCardCallback(Object* obj, void* arg); |
| 94 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 95 | // Marks an object. |
| 96 | void MarkObject(const Object* obj); |
| 97 | |
| 98 | // Yuck. |
| 99 | void MarkObject0(const Object* obj, bool check_finger); |
| 100 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 101 | static void ScanBitmapCallback(Object* obj, void* finger, void* arg); |
| 102 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 103 | static void CheckBitmapCallback(Object* obj, void* finger, void* arg); |
| 104 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 105 | static void SweepCallback(size_t num_ptrs, Object** ptrs, void* arg); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 106 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 107 | void CheckReference(const Object* obj, const Object* ref, MemberOffset offset, bool is_static); |
| 108 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 109 | // Blackens an object. |
| 110 | void ScanObject(const Object* obj); |
| 111 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 112 | void CheckObject(const Object* obj); |
| 113 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 114 | // Grays references in instance fields. |
| 115 | void ScanInstanceFields(const Object* obj); |
| 116 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 117 | void CheckInstanceFields(const Object* obj); |
| 118 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 119 | // Blackens a class object. |
| 120 | void ScanClass(const Object* obj); |
| 121 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 122 | void CheckClass(const Object* obj); |
| 123 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 124 | // Grays references in static fields. |
| 125 | void ScanStaticFields(const Class* klass); |
| 126 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 127 | void CheckStaticFields(const Class* klass); |
| 128 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 129 | // Used by ScanInstanceFields and ScanStaticFields |
| 130 | void ScanFields(const Object* obj, uint32_t ref_offsets, bool is_static); |
| 131 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 132 | void CheckFields(const Object* obj, uint32_t ref_offsets, bool is_static); |
| 133 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 134 | // Grays references in an array. |
| 135 | void ScanArray(const Object* obj); |
| 136 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 137 | void CheckArray(const Object* obj); |
| 138 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 139 | void ScanOther(const Object* obj); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 140 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 141 | void CheckOther(const Object* obj); |
| 142 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 143 | // Blackens objects grayed during a garbage collection. |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 144 | void ScanGrayObjects(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 145 | |
| 146 | // Schedules an unmarked object for reference processing. |
| 147 | void DelayReferenceReferent(Object* reference); |
| 148 | |
| 149 | // Recursively blackens objects on the mark stack. |
| 150 | void ProcessMarkStack(); |
| 151 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 152 | void EnqueueFinalizerReferences(Object** ref); |
| 153 | |
| 154 | void PreserveSomeSoftReferences(Object** ref); |
| 155 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 156 | void ClearWhiteReferences(Object** list); |
| 157 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 158 | void ProcessReferences(Object** soft_references, bool clear_soft_references, |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 159 | Object** weak_references, |
| 160 | Object** finalizer_references, |
| 161 | Object** phantom_references); |
| 162 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 163 | void SweepSystemWeaks(); |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 164 | void SweepJniWeakGlobals(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 165 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 166 | MarkStack* mark_stack_; |
| 167 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 168 | Heap* heap_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 169 | HeapBitmap* mark_bitmap_; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 170 | HeapBitmap* live_bitmap_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 171 | |
| 172 | Object* finger_; |
| 173 | |
| 174 | Object* condemned_; |
| 175 | |
| 176 | Object* soft_reference_list_; |
| 177 | |
| 178 | Object* weak_reference_list_; |
| 179 | |
| 180 | Object* finalizer_reference_list_; |
| 181 | |
| 182 | Object* phantom_reference_list_; |
| 183 | |
| 184 | Object* cleared_reference_list_; |
| 185 | |
Elliott Hughes | 352a424 | 2011-10-31 15:15:21 -0700 | [diff] [blame] | 186 | size_t class_count_; |
| 187 | size_t array_count_; |
| 188 | size_t other_count_; |
| 189 | |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 190 | friend class InternTableEntryIsUnmarked; |
| 191 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 192 | DISALLOW_COPY_AND_ASSIGN(MarkSweep); |
| 193 | }; |
| 194 | |
| 195 | } // namespace art |
| 196 | |
| 197 | #endif // ART_SRC_MARK_SWEEP_H_ |