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