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" |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 23 | #include "object.h" |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 24 | #include "offsets.h" |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 25 | |
| 26 | namespace art { |
| 27 | |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 28 | class CheckObjectVisitor; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 29 | class Class; |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 30 | class Heap; |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 31 | class MarkIfReachesAllocspaceVisitor; |
| 32 | class ModUnionClearCardVisitor; |
| 33 | class ModUnionVisitor; |
| 34 | class ModUnionTableBitmap; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 35 | class Object; |
| 36 | |
| 37 | class MarkSweep { |
| 38 | public: |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 39 | explicit MarkSweep(MarkStack* mark_stack); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 40 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 41 | ~MarkSweep(); |
| 42 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 43 | // Initializes internal structures. |
Jesse Wilson | 078f9b0 | 2011-11-18 17:51:47 -0500 | [diff] [blame] | 44 | void Init(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 45 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 46 | // Marks the root set at the start of a garbage collection. |
| 47 | void MarkRoots(); |
| 48 | |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 49 | // Marks the roots in the image space on dirty cards. |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 50 | void ScanDirtyImageRoots(); |
| 51 | |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 52 | // Verify that image roots point to only marked objects within the alloc space. |
| 53 | void VerifyImageRoots(); |
| 54 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 55 | bool IsMarkStackEmpty() const { |
| 56 | return mark_stack_->IsEmpty(); |
| 57 | } |
| 58 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 59 | // Builds a mark stack and recursively mark until it empties. |
| 60 | void RecursiveMark(); |
| 61 | |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 62 | // Builds a mark stack with objects on dirty cards and recursively mark |
| 63 | // until it empties. |
| 64 | void RecursiveMarkDirtyObjects(); |
| 65 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 66 | // Remarks the root set after completing the concurrent mark. |
| 67 | void ReMarkRoots(); |
| 68 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 69 | void ProcessReferences(bool clear_soft_references) { |
| 70 | ProcessReferences(&soft_reference_list_, clear_soft_references, |
| 71 | &weak_reference_list_, |
| 72 | &finalizer_reference_list_, |
| 73 | &phantom_reference_list_); |
| 74 | } |
| 75 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 76 | // Sweeps unmarked objects to complete the garbage collection. |
| 77 | void Sweep(); |
| 78 | |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 79 | Object* GetClearedReferences() { |
| 80 | return cleared_reference_list_; |
| 81 | } |
| 82 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 83 | private: |
| 84 | // Returns true if the object has its bit set in the mark bitmap. |
| 85 | bool IsMarked(const Object* object) const { |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 86 | if (current_mark_bitmap_->HasAddress(object)) { |
| 87 | return current_mark_bitmap_->Test(object); |
| 88 | } |
| 89 | return heap_->GetMarkBitmap()->Test(object); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 92 | static bool IsMarked(const Object* object, void* arg) { |
| 93 | return reinterpret_cast<MarkSweep*>(arg)->IsMarked(object); |
| 94 | } |
| 95 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 96 | static void MarkObjectVisitor(const Object* root, void* arg); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 97 | |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 98 | static void ReMarkObjectVisitor(const Object* root, void* arg); |
| 99 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 100 | static void ScanImageRootVisitor(Object* root, void* arg); |
| 101 | |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 102 | static void VerifyImageRootVisitor(Object* root, void* arg); |
| 103 | |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 104 | static void ScanDirtyCardCallback(Object* obj, void* arg); |
| 105 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 106 | // Marks an object. |
| 107 | void MarkObject(const Object* obj); |
| 108 | |
| 109 | // Yuck. |
| 110 | void MarkObject0(const Object* obj, bool check_finger); |
| 111 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 112 | static void ScanBitmapCallback(Object* obj, void* finger, void* arg); |
| 113 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 114 | static void CheckBitmapCallback(Object* obj, void* finger, void* arg); |
| 115 | |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 116 | static void CheckBitmapNoFingerCallback(Object* obj, void* arg); |
| 117 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 118 | static void SweepCallback(size_t num_ptrs, Object** ptrs, void* arg); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 119 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 120 | void CheckReference(const Object* obj, const Object* ref, MemberOffset offset, bool is_static); |
| 121 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 122 | // Blackens an object. |
| 123 | void ScanObject(const Object* obj); |
| 124 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 125 | void CheckObject(const Object* obj); |
| 126 | |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 127 | template <typename Visitor> |
| 128 | void VisitObjectReferences(const Object* obj, const Visitor& visitor) { |
| 129 | DCHECK(obj != NULL); |
| 130 | DCHECK(obj->GetClass() != NULL); |
| 131 | if (obj->IsClass()) { |
| 132 | VisitClassReferences(obj, visitor); |
| 133 | } else if (obj->IsArrayInstance()) { |
| 134 | VisitArrayReferences(obj, visitor); |
| 135 | } else { |
| 136 | VisitOtherReferences(obj, visitor); |
| 137 | } |
| 138 | } |
| 139 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 140 | // Grays references in instance fields. |
| 141 | void ScanInstanceFields(const Object* obj); |
| 142 | |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 143 | template <typename Visitor> |
| 144 | void VisitInstanceFieldsReferences(const Object* obj, const Visitor& visitor) { |
| 145 | DCHECK(obj != NULL); |
| 146 | Class* klass = obj->GetClass(); |
| 147 | DCHECK(klass != NULL); |
| 148 | VisitFieldsReferences(obj, klass->GetReferenceInstanceOffsets(), false, visitor); |
| 149 | } |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 150 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 151 | // Blackens a class object. |
| 152 | void ScanClass(const Object* obj); |
| 153 | |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 154 | template <typename Visitor> |
| 155 | void VisitClassReferences(const Object* obj, const Visitor& visitor) { |
| 156 | VisitInstanceFieldsReferences(obj, visitor); |
| 157 | VisitStaticFieldsReferences(obj->AsClass(), visitor); |
| 158 | } |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 159 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 160 | // Grays references in static fields. |
| 161 | void ScanStaticFields(const Class* klass); |
| 162 | |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 163 | template <typename Visitor> |
| 164 | void VisitStaticFieldsReferences(const Class* klass, const Visitor& visitor) { |
| 165 | DCHECK(klass != NULL); |
| 166 | VisitFieldsReferences(klass, klass->GetReferenceStaticOffsets(), true, visitor); |
| 167 | } |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 168 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 169 | // Used by ScanInstanceFields and ScanStaticFields |
| 170 | void ScanFields(const Object* obj, uint32_t ref_offsets, bool is_static); |
| 171 | |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 172 | template <typename Visitor> |
| 173 | void VisitFieldsReferences(const Object* obj, uint32_t ref_offsets, bool is_static, const Visitor& visitor) { |
| 174 | if (ref_offsets != CLASS_WALK_SUPER) { |
| 175 | // Found a reference offset bitmap. Mark the specified offsets. |
| 176 | while (ref_offsets != 0) { |
| 177 | size_t right_shift = CLZ(ref_offsets); |
| 178 | MemberOffset field_offset = CLASS_OFFSET_FROM_CLZ(right_shift); |
| 179 | const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false); |
| 180 | visitor(obj, ref, field_offset, is_static); |
| 181 | ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift); |
| 182 | } |
| 183 | } else { |
| 184 | // There is no reference offset bitmap. In the non-static case, |
| 185 | // walk up the class inheritance hierarchy and find reference |
| 186 | // offsets the hard way. In the static case, just consider this |
| 187 | // class. |
| 188 | for (const Class* klass = is_static ? obj->AsClass() : obj->GetClass(); |
| 189 | klass != NULL; |
| 190 | klass = is_static ? NULL : klass->GetSuperClass()) { |
| 191 | size_t num_reference_fields = (is_static |
| 192 | ? klass->NumReferenceStaticFields() |
| 193 | : klass->NumReferenceInstanceFields()); |
| 194 | for (size_t i = 0; i < num_reference_fields; ++i) { |
| 195 | Field* field = (is_static |
| 196 | ? klass->GetStaticField(i) |
| 197 | : klass->GetInstanceField(i)); |
| 198 | MemberOffset field_offset = field->GetOffset(); |
| 199 | const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false); |
| 200 | visitor(obj, ref, field_offset, is_static); |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | } |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 205 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 206 | // Grays references in an array. |
| 207 | void ScanArray(const Object* obj); |
| 208 | |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 209 | template <typename Visitor> |
| 210 | void VisitArrayReferences(const Object* obj, const Visitor& visitor) { |
| 211 | visitor(obj, obj->GetClass(), Object::ClassOffset(), false); |
| 212 | if (obj->IsObjectArray()) { |
| 213 | const ObjectArray<Object>* array = obj->AsObjectArray<Object>(); |
| 214 | for (int32_t i = 0; i < array->GetLength(); ++i) { |
| 215 | const Object* element = array->GetWithoutChecks(i); |
| 216 | size_t width = sizeof(Object*); |
| 217 | visitor(obj, element, MemberOffset(i * width + Array::DataOffset(width).Int32Value()), false); |
| 218 | } |
| 219 | } |
| 220 | } |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 221 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 222 | void ScanOther(const Object* obj); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 223 | |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 224 | template <typename Visitor> |
| 225 | void VisitOtherReferences(const Object* obj, const Visitor& visitor) { |
| 226 | return VisitInstanceFieldsReferences(obj, visitor); |
| 227 | } |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 228 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 229 | // Blackens objects grayed during a garbage collection. |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 230 | void ScanGrayObjects(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 231 | |
| 232 | // Schedules an unmarked object for reference processing. |
| 233 | void DelayReferenceReferent(Object* reference); |
| 234 | |
| 235 | // Recursively blackens objects on the mark stack. |
| 236 | void ProcessMarkStack(); |
| 237 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 238 | void EnqueueFinalizerReferences(Object** ref); |
| 239 | |
| 240 | void PreserveSomeSoftReferences(Object** ref); |
| 241 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 242 | void ClearWhiteReferences(Object** list); |
| 243 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 244 | void ProcessReferences(Object** soft_references, bool clear_soft_references, |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 245 | Object** weak_references, |
| 246 | Object** finalizer_references, |
| 247 | Object** phantom_references); |
| 248 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 249 | void SweepSystemWeaks(); |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 250 | void SweepJniWeakGlobals(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 251 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 252 | // Current space, we check this space first to avoid searching for the appropriate space for an object. |
| 253 | SpaceBitmap* current_mark_bitmap_; |
| 254 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 255 | MarkStack* mark_stack_; |
| 256 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 257 | Heap* heap_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 258 | |
| 259 | Object* finger_; |
| 260 | |
| 261 | Object* condemned_; |
| 262 | |
| 263 | Object* soft_reference_list_; |
| 264 | |
| 265 | Object* weak_reference_list_; |
| 266 | |
| 267 | Object* finalizer_reference_list_; |
| 268 | |
| 269 | Object* phantom_reference_list_; |
| 270 | |
| 271 | Object* cleared_reference_list_; |
| 272 | |
Elliott Hughes | 352a424 | 2011-10-31 15:15:21 -0700 | [diff] [blame] | 273 | size_t class_count_; |
| 274 | size_t array_count_; |
| 275 | size_t other_count_; |
| 276 | |
Mathieu Chartier | e6e0651 | 2012-06-26 15:00:26 -0700 | [diff] [blame^] | 277 | friend class AddIfReachesAllocSpaceVisitor; // Used by mod-union table. |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 278 | friend class CheckObjectVisitor; |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 279 | friend class InternTableEntryIsUnmarked; |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 280 | friend class MarkIfReachesAllocspaceVisitor; |
| 281 | friend class ModUnionClearCardVisitor; |
Mathieu Chartier | e6e0651 | 2012-06-26 15:00:26 -0700 | [diff] [blame^] | 282 | friend class ModUnionReferenceVisitor; |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 283 | friend class ModUnionVisitor; |
| 284 | friend class ModUnionTableBitmap; |
Mathieu Chartier | e6e0651 | 2012-06-26 15:00:26 -0700 | [diff] [blame^] | 285 | friend class ModUnionTableReferenceCache; |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 286 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 287 | DISALLOW_COPY_AND_ASSIGN(MarkSweep); |
| 288 | }; |
| 289 | |
| 290 | } // namespace art |
| 291 | |
| 292 | #endif // ART_SRC_MARK_SWEEP_H_ |