blob: 723192dc8a6140cf888bdc5c5d888c92a4b73633 [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
17#ifndef ART_SRC_MIRROR_OBJECT_INL_H_
18#define ART_SRC_MIRROR_OBJECT_INL_H_
19
20#include "object.h"
21
22#include "abstract_method.h"
23#include "atomic.h"
24#include "array.h"
25#include "field.h"
26#include "class.h"
27#include "runtime.h"
28
29namespace art {
30namespace mirror {
31
32inline Class* Object::GetClass() const {
33 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Object, klass_), false);
34}
35
36inline void Object::SetClass(Class* new_klass) {
37 // new_klass may be NULL prior to class linker initialization
38 // We don't mark the card since the class is guaranteed to be referenced from another location.
39 // Proxy classes are held live by the class loader, and other classes are roots of the class
40 // linker.
41 SetFieldPtr(OFFSET_OF_OBJECT_MEMBER(Object, klass_), new_klass, false, false);
42}
43
44inline bool Object::InstanceOf(const Class* klass) const {
45 DCHECK(klass != NULL);
46 DCHECK(GetClass() != NULL);
47 return klass->IsAssignableFrom(GetClass());
48}
49
50inline bool Object::IsClass() const {
51 Class* java_lang_Class = GetClass()->GetClass();
52 return GetClass() == java_lang_Class;
53}
54
55inline Class* Object::AsClass() {
56 DCHECK(IsClass());
57 return down_cast<Class*>(this);
58}
59
60inline const Class* Object::AsClass() const {
61 DCHECK(IsClass());
62 return down_cast<const Class*>(this);
63}
64
65inline bool Object::IsObjectArray() const {
66 return IsArrayInstance() && !GetClass()->GetComponentType()->IsPrimitive();
67}
68
69template<class T>
70inline ObjectArray<T>* Object::AsObjectArray() {
71 DCHECK(IsObjectArray());
72 return down_cast<ObjectArray<T>*>(this);
73}
74
75template<class T>
76inline const ObjectArray<T>* Object::AsObjectArray() const {
77 DCHECK(IsObjectArray());
78 return down_cast<const ObjectArray<T>*>(this);
79}
80
81inline bool Object::IsArrayInstance() const {
82 return GetClass()->IsArrayClass();
83}
84
85inline bool Object::IsField() const {
86 return GetClass()->IsFieldClass();
87}
88
89inline Field* Object::AsField() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
90 DCHECK(IsField());
91 return down_cast<Field*>(this);
92}
93
94inline const Field* Object::AsField() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
95 DCHECK(IsField());
96 return down_cast<const Field*>(this);
97}
98
99inline bool Object::IsMethod() const {
100 return GetClass()->IsMethodClass();
101}
102
103inline AbstractMethod* Object::AsMethod() {
104 DCHECK(IsMethod());
105 return down_cast<AbstractMethod*>(this);
106}
107
108inline const AbstractMethod* Object::AsMethod() const {
109 DCHECK(IsMethod());
110 return down_cast<const AbstractMethod*>(this);
111}
112
113inline bool Object::IsReferenceInstance() const {
114 return GetClass()->IsReferenceClass();
115}
116
117inline bool Object::IsWeakReferenceInstance() const {
118 return GetClass()->IsWeakReferenceClass();
119}
120
121inline bool Object::IsSoftReferenceInstance() const {
122 return GetClass()->IsSoftReferenceClass();
123}
124
125inline bool Object::IsFinalizerReferenceInstance() const {
126 return GetClass()->IsFinalizerReferenceClass();
127}
128
129inline bool Object::IsPhantomReferenceInstance() const {
130 return GetClass()->IsPhantomReferenceClass();
131}
132
133inline size_t Object::SizeOf() const {
134 size_t result;
135 if (IsArrayInstance()) {
136 result = AsArray()->SizeOf();
137 } else if (IsClass()) {
138 result = AsClass()->SizeOf();
139 } else {
140 result = GetClass()->GetObjectSize();
141 }
142 DCHECK(!IsField() || result == sizeof(Field));
143 DCHECK(!IsMethod() || result == sizeof(AbstractMethod));
144 return result;
145}
146
147inline uint64_t Object::GetField64(MemberOffset field_offset, bool is_volatile) const {
148 VerifyObject(this);
149 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset.Int32Value();
150 const int64_t* addr = reinterpret_cast<const int64_t*>(raw_addr);
151 if (UNLIKELY(is_volatile)) {
152 uint64_t result = QuasiAtomic::Read64(addr);
153 ANDROID_MEMBAR_FULL();
154 return result;
155 } else {
156 return *addr;
157 }
158}
159
160inline void Object::SetField64(MemberOffset field_offset, uint64_t new_value, bool is_volatile) {
161 VerifyObject(this);
162 byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset.Int32Value();
163 int64_t* addr = reinterpret_cast<int64_t*>(raw_addr);
164 if (UNLIKELY(is_volatile)) {
165 ANDROID_MEMBAR_STORE();
166 QuasiAtomic::Write64(addr, new_value);
167 // Post-store barrier not required due to use of atomic op or mutex.
168 } else {
169 *addr = new_value;
170 }
171}
172
173inline void Object::WriteBarrierField(const Object* dst, MemberOffset field_offset,
174 const Object* new_value) {
175 Runtime::Current()->GetHeap()->WriteBarrierField(dst, field_offset, new_value);
176}
177
178} // namespace mirror
179} // namespace art
180
181#endif // ART_SRC_MIRROR_OBJECT_INL_H_