Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -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 | */ |
| 16 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_GC_COLLECTOR_MARK_SWEEP_INL_H_ |
| 18 | #define ART_RUNTIME_GC_COLLECTOR_MARK_SWEEP_INL_H_ |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 19 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 20 | #include "gc/collector/mark_sweep.h" |
| 21 | |
| 22 | #include "gc/heap.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 23 | #include "mirror/art_field.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 24 | #include "mirror/class.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 25 | #include "mirror/object_array.h" |
| 26 | |
| 27 | namespace art { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 28 | namespace gc { |
| 29 | namespace collector { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 30 | |
| 31 | template <typename MarkVisitor> |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 32 | inline void MarkSweep::ScanObjectVisit(mirror::Object* obj, const MarkVisitor& visitor) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 33 | DCHECK(obj != NULL); |
| 34 | if (kIsDebugBuild && !IsMarked(obj)) { |
| 35 | heap_->DumpSpaces(); |
| 36 | LOG(FATAL) << "Scanning unmarked object " << obj; |
| 37 | } |
| 38 | mirror::Class* klass = obj->GetClass(); |
| 39 | DCHECK(klass != NULL); |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 40 | if (UNLIKELY(klass->IsArrayClass())) { |
| 41 | if (kCountScannedTypes) { |
| 42 | ++array_count_; |
| 43 | } |
| 44 | if (klass->IsObjectArrayClass()) { |
| 45 | VisitObjectArrayReferences(obj->AsObjectArray<mirror::Object>(), visitor); |
| 46 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 47 | } else if (UNLIKELY(klass == mirror::Class::GetJavaLangClass())) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 48 | if (kCountScannedTypes) { |
| 49 | ++class_count_; |
| 50 | } |
| 51 | VisitClassReferences(klass, obj, visitor); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 52 | } else { |
| 53 | if (kCountScannedTypes) { |
| 54 | ++other_count_; |
| 55 | } |
| 56 | VisitOtherReferences(klass, obj, visitor); |
| 57 | if (UNLIKELY(klass->IsReferenceClass())) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 58 | DelayReferenceReferent(klass, obj); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | template <typename Visitor> |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 64 | inline void MarkSweep::VisitObjectReferences(mirror::Object* obj, const Visitor& visitor, |
| 65 | bool visit_class) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 66 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, |
| 67 | Locks::mutator_lock_) { |
| 68 | DCHECK(obj != NULL); |
| 69 | DCHECK(obj->GetClass() != NULL); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 70 | mirror::Class* klass = obj->GetClass(); |
| 71 | DCHECK(klass != NULL); |
| 72 | if (klass == mirror::Class::GetJavaLangClass()) { |
| 73 | DCHECK_EQ(klass->GetClass(), mirror::Class::GetJavaLangClass()); |
| 74 | VisitClassReferences(klass, obj, visitor); |
| 75 | } else { |
| 76 | if (klass->IsArrayClass()) { |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame^] | 77 | if (visit_class) { |
| 78 | visitor(obj, klass, mirror::Object::ClassOffset(), false); |
| 79 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 80 | if (klass->IsObjectArrayClass()) { |
| 81 | VisitObjectArrayReferences(obj->AsObjectArray<mirror::Object>(), visitor); |
| 82 | } |
| 83 | } else { |
| 84 | VisitOtherReferences(klass, obj, visitor); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | template <typename Visitor> |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 90 | inline void MarkSweep::VisitInstanceFieldsReferences(mirror::Class* klass, mirror::Object* obj, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 91 | const Visitor& visitor) |
| 92 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) { |
| 93 | DCHECK(obj != NULL); |
| 94 | DCHECK(klass != NULL); |
| 95 | VisitFieldsReferences(obj, klass->GetReferenceInstanceOffsets(), false, visitor); |
| 96 | } |
| 97 | |
| 98 | template <typename Visitor> |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 99 | inline void MarkSweep::VisitClassReferences(mirror::Class* klass, mirror::Object* obj, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 100 | const Visitor& visitor) |
| 101 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) { |
| 102 | VisitInstanceFieldsReferences(klass, obj, visitor); |
| 103 | VisitStaticFieldsReferences(obj->AsClass(), visitor); |
| 104 | } |
| 105 | |
| 106 | template <typename Visitor> |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 107 | inline void MarkSweep::VisitStaticFieldsReferences(mirror::Class* klass, const Visitor& visitor) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 108 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) { |
| 109 | DCHECK(klass != NULL); |
| 110 | VisitFieldsReferences(klass, klass->GetReferenceStaticOffsets(), true, visitor); |
| 111 | } |
| 112 | |
| 113 | template <typename Visitor> |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 114 | inline void MarkSweep::VisitFieldsReferences(mirror::Object* obj, uint32_t ref_offsets, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 115 | bool is_static, const Visitor& visitor) { |
| 116 | if (LIKELY(ref_offsets != CLASS_WALK_SUPER)) { |
| 117 | // Found a reference offset bitmap. Mark the specified offsets. |
| 118 | while (ref_offsets != 0) { |
| 119 | size_t right_shift = CLZ(ref_offsets); |
| 120 | MemberOffset field_offset = CLASS_OFFSET_FROM_CLZ(right_shift); |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 121 | mirror::Object* ref = obj->GetFieldObject<mirror::Object*>(field_offset, false); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 122 | visitor(obj, ref, field_offset, is_static); |
| 123 | ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift); |
| 124 | } |
| 125 | } else { |
| 126 | // There is no reference offset bitmap. In the non-static case, |
| 127 | // walk up the class inheritance hierarchy and find reference |
| 128 | // offsets the hard way. In the static case, just consider this |
| 129 | // class. |
| 130 | for (const mirror::Class* klass = is_static ? obj->AsClass() : obj->GetClass(); |
| 131 | klass != NULL; |
| 132 | klass = is_static ? NULL : klass->GetSuperClass()) { |
| 133 | size_t num_reference_fields = (is_static |
| 134 | ? klass->NumReferenceStaticFields() |
| 135 | : klass->NumReferenceInstanceFields()); |
| 136 | for (size_t i = 0; i < num_reference_fields; ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 137 | mirror::ArtField* field = (is_static ? klass->GetStaticField(i) |
| 138 | : klass->GetInstanceField(i)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 139 | MemberOffset field_offset = field->GetOffset(); |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 140 | mirror::Object* ref = obj->GetFieldObject<mirror::Object*>(field_offset, false); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 141 | visitor(obj, ref, field_offset, is_static); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | template <typename Visitor> |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 148 | inline void MarkSweep::VisitObjectArrayReferences(mirror::ObjectArray<mirror::Object>* array, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 149 | const Visitor& visitor) { |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 150 | const size_t length = static_cast<size_t>(array->GetLength()); |
| 151 | for (size_t i = 0; i < length; ++i) { |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 152 | mirror::Object* element = array->GetWithoutChecks(static_cast<int32_t>(i)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 153 | const size_t width = sizeof(mirror::Object*); |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 154 | MemberOffset offset(i * width + mirror::Array::DataOffset(width).Int32Value()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 155 | visitor(array, element, offset, false); |
| 156 | } |
| 157 | } |
| 158 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 159 | } // namespace collector |
| 160 | } // namespace gc |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 161 | } // namespace art |
| 162 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 163 | #endif // ART_RUNTIME_GC_COLLECTOR_MARK_SWEEP_INL_H_ |