blob: 9c1c5dc281413f19c9c4bdfd10caace9f1f426ca [file] [log] [blame]
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001/*
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 Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_GC_COLLECTOR_MARK_SWEEP_INL_H_
18#define ART_RUNTIME_GC_COLLECTOR_MARK_SWEEP_INL_H_
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080019
Ian Rogers1d54e732013-05-02 21:10:01 -070020#include "gc/collector/mark_sweep.h"
21
22#include "gc/heap.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070023#include "mirror/art_field.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080024#include "mirror/class.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025#include "mirror/object_array.h"
26
27namespace art {
Ian Rogers1d54e732013-05-02 21:10:01 -070028namespace gc {
29namespace collector {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030
31template <typename MarkVisitor>
Mathieu Chartier11409ae2013-09-23 11:49:36 -070032inline void MarkSweep::ScanObjectVisit(mirror::Object* obj, const MarkVisitor& visitor) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033 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 Chartier94c32c52013-08-09 11:14:04 -070040 if (UNLIKELY(klass->IsArrayClass())) {
41 if (kCountScannedTypes) {
42 ++array_count_;
43 }
44 if (klass->IsObjectArrayClass()) {
45 VisitObjectArrayReferences(obj->AsObjectArray<mirror::Object>(), visitor);
46 }
Mathieu Chartier590fee92013-09-13 13:46:47 -070047 } else if (UNLIKELY(klass == mirror::Class::GetJavaLangClass())) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048 if (kCountScannedTypes) {
49 ++class_count_;
50 }
51 VisitClassReferences(klass, obj, visitor);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080052 } else {
53 if (kCountScannedTypes) {
54 ++other_count_;
55 }
56 VisitOtherReferences(klass, obj, visitor);
57 if (UNLIKELY(klass->IsReferenceClass())) {
Mathieu Chartier590fee92013-09-13 13:46:47 -070058 DelayReferenceReferent(klass, obj);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080059 }
60 }
61}
62
63template <typename Visitor>
Mathieu Chartier11409ae2013-09-23 11:49:36 -070064inline void MarkSweep::VisitObjectReferences(mirror::Object* obj, const Visitor& visitor,
65 bool visit_class)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080066 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_,
67 Locks::mutator_lock_) {
68 DCHECK(obj != NULL);
69 DCHECK(obj->GetClass() != NULL);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080070 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 Chartierc528dba2013-11-26 12:00:11 -080077 if (visit_class) {
78 visitor(obj, klass, mirror::Object::ClassOffset(), false);
79 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080080 if (klass->IsObjectArrayClass()) {
81 VisitObjectArrayReferences(obj->AsObjectArray<mirror::Object>(), visitor);
82 }
83 } else {
84 VisitOtherReferences(klass, obj, visitor);
85 }
86 }
87}
88
89template <typename Visitor>
Mathieu Chartier590fee92013-09-13 13:46:47 -070090inline void MarkSweep::VisitInstanceFieldsReferences(mirror::Class* klass, mirror::Object* obj,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080091 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
98template <typename Visitor>
Mathieu Chartier11409ae2013-09-23 11:49:36 -070099inline void MarkSweep::VisitClassReferences(mirror::Class* klass, mirror::Object* obj,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800100 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
106template <typename Visitor>
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700107inline void MarkSweep::VisitStaticFieldsReferences(mirror::Class* klass, const Visitor& visitor)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800108 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) {
109 DCHECK(klass != NULL);
110 VisitFieldsReferences(klass, klass->GetReferenceStaticOffsets(), true, visitor);
111}
112
113template <typename Visitor>
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700114inline void MarkSweep::VisitFieldsReferences(mirror::Object* obj, uint32_t ref_offsets,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800115 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 Chartier11409ae2013-09-23 11:49:36 -0700121 mirror::Object* ref = obj->GetFieldObject<mirror::Object*>(field_offset, false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800122 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 Carlstromea46f952013-07-30 01:26:50 -0700137 mirror::ArtField* field = (is_static ? klass->GetStaticField(i)
138 : klass->GetInstanceField(i));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800139 MemberOffset field_offset = field->GetOffset();
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700140 mirror::Object* ref = obj->GetFieldObject<mirror::Object*>(field_offset, false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800141 visitor(obj, ref, field_offset, is_static);
142 }
143 }
144 }
145}
146
147template <typename Visitor>
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700148inline void MarkSweep::VisitObjectArrayReferences(mirror::ObjectArray<mirror::Object>* array,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800149 const Visitor& visitor) {
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700150 const size_t length = static_cast<size_t>(array->GetLength());
151 for (size_t i = 0; i < length; ++i) {
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700152 mirror::Object* element = array->GetWithoutChecks(static_cast<int32_t>(i));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800153 const size_t width = sizeof(mirror::Object*);
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700154 MemberOffset offset(i * width + mirror::Array::DataOffset(width).Int32Value());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800155 visitor(array, element, offset, false);
156 }
157}
158
Ian Rogers1d54e732013-05-02 21:10:01 -0700159} // namespace collector
160} // namespace gc
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800161} // namespace art
162
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700163#endif // ART_RUNTIME_GC_COLLECTOR_MARK_SWEEP_INL_H_