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_MIRROR_OBJECT_INL_H_ |
| 18 | #define ART_RUNTIME_MIRROR_OBJECT_INL_H_ |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 19 | |
| 20 | #include "object.h" |
| 21 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 22 | #include "art_field.h" |
| 23 | #include "art_method.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 24 | #include "atomic.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 25 | #include "array-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 26 | #include "class.h" |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 27 | #include "lock_word-inl.h" |
Ian Rogers | 05f3057 | 2013-02-20 12:13:11 -0800 | [diff] [blame] | 28 | #include "monitor.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 29 | #include "runtime.h" |
Ian Rogers | 05f3057 | 2013-02-20 12:13:11 -0800 | [diff] [blame] | 30 | #include "throwable.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 31 | |
| 32 | namespace art { |
| 33 | namespace mirror { |
| 34 | |
| 35 | inline Class* Object::GetClass() const { |
| 36 | return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Object, klass_), false); |
| 37 | } |
| 38 | |
| 39 | inline void Object::SetClass(Class* new_klass) { |
| 40 | // new_klass may be NULL prior to class linker initialization |
| 41 | // We don't mark the card since the class is guaranteed to be referenced from another location. |
| 42 | // Proxy classes are held live by the class loader, and other classes are roots of the class |
| 43 | // linker. |
| 44 | SetFieldPtr(OFFSET_OF_OBJECT_MEMBER(Object, klass_), new_klass, false, false); |
| 45 | } |
| 46 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 47 | inline LockWord Object::GetLockWord() { |
| 48 | return LockWord(GetField32(OFFSET_OF_OBJECT_MEMBER(Object, monitor_), true)); |
| 49 | } |
| 50 | |
| 51 | inline void Object::SetLockWord(LockWord new_val) { |
| 52 | SetField32(OFFSET_OF_OBJECT_MEMBER(Object, monitor_), new_val.GetValue(), true); |
| 53 | } |
| 54 | |
| 55 | inline bool Object::CasLockWord(LockWord old_val, LockWord new_val) { |
| 56 | return CasField32(OFFSET_OF_OBJECT_MEMBER(Object, monitor_), old_val.GetValue(), |
| 57 | new_val.GetValue()); |
| 58 | } |
| 59 | |
| 60 | inline uint32_t Object::GetLockOwnerThreadId() { |
| 61 | return Monitor::GetLockOwnerThreadId(this); |
Ian Rogers | 05f3057 | 2013-02-20 12:13:11 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | inline void Object::MonitorEnter(Thread* self) { |
| 65 | Monitor::MonitorEnter(self, this); |
| 66 | } |
| 67 | |
| 68 | inline bool Object::MonitorExit(Thread* self) { |
| 69 | return Monitor::MonitorExit(self, this); |
| 70 | } |
| 71 | |
| 72 | inline void Object::Notify(Thread* self) { |
| 73 | Monitor::Notify(self, this); |
| 74 | } |
| 75 | |
| 76 | inline void Object::NotifyAll(Thread* self) { |
| 77 | Monitor::NotifyAll(self, this); |
| 78 | } |
| 79 | |
| 80 | inline void Object::Wait(Thread* self) { |
| 81 | Monitor::Wait(self, this, 0, 0, true, kWaiting); |
| 82 | } |
| 83 | |
| 84 | inline void Object::Wait(Thread* self, int64_t ms, int32_t ns) { |
| 85 | Monitor::Wait(self, this, ms, ns, true, kTimedWaiting); |
| 86 | } |
| 87 | |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 88 | inline bool Object::VerifierInstanceOf(const Class* klass) const { |
| 89 | DCHECK(klass != NULL); |
| 90 | DCHECK(GetClass() != NULL); |
| 91 | return klass->IsInterface() || InstanceOf(klass); |
| 92 | } |
| 93 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 94 | inline bool Object::InstanceOf(const Class* klass) const { |
| 95 | DCHECK(klass != NULL); |
| 96 | DCHECK(GetClass() != NULL); |
| 97 | return klass->IsAssignableFrom(GetClass()); |
| 98 | } |
| 99 | |
| 100 | inline bool Object::IsClass() const { |
| 101 | Class* java_lang_Class = GetClass()->GetClass(); |
| 102 | return GetClass() == java_lang_Class; |
| 103 | } |
| 104 | |
| 105 | inline Class* Object::AsClass() { |
| 106 | DCHECK(IsClass()); |
| 107 | return down_cast<Class*>(this); |
| 108 | } |
| 109 | |
| 110 | inline const Class* Object::AsClass() const { |
| 111 | DCHECK(IsClass()); |
| 112 | return down_cast<const Class*>(this); |
| 113 | } |
| 114 | |
| 115 | inline bool Object::IsObjectArray() const { |
| 116 | return IsArrayInstance() && !GetClass()->GetComponentType()->IsPrimitive(); |
| 117 | } |
| 118 | |
| 119 | template<class T> |
| 120 | inline ObjectArray<T>* Object::AsObjectArray() { |
| 121 | DCHECK(IsObjectArray()); |
| 122 | return down_cast<ObjectArray<T>*>(this); |
| 123 | } |
| 124 | |
| 125 | template<class T> |
| 126 | inline const ObjectArray<T>* Object::AsObjectArray() const { |
| 127 | DCHECK(IsObjectArray()); |
| 128 | return down_cast<const ObjectArray<T>*>(this); |
| 129 | } |
| 130 | |
| 131 | inline bool Object::IsArrayInstance() const { |
| 132 | return GetClass()->IsArrayClass(); |
| 133 | } |
| 134 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 135 | inline bool Object::IsArtField() const { |
| 136 | return GetClass()->IsArtFieldClass(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 137 | } |
| 138 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 139 | inline ArtField* Object::AsArtField() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 140 | DCHECK(IsArtField()); |
| 141 | return down_cast<ArtField*>(this); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 142 | } |
| 143 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 144 | inline const ArtField* Object::AsArtField() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 145 | DCHECK(IsArtField()); |
| 146 | return down_cast<const ArtField*>(this); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 147 | } |
| 148 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 149 | inline bool Object::IsArtMethod() const { |
| 150 | return GetClass()->IsArtMethodClass(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 153 | inline ArtMethod* Object::AsArtMethod() { |
| 154 | DCHECK(IsArtMethod()); |
| 155 | return down_cast<ArtMethod*>(this); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 156 | } |
| 157 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 158 | inline const ArtMethod* Object::AsArtMethod() const { |
| 159 | DCHECK(IsArtMethod()); |
| 160 | return down_cast<const ArtMethod*>(this); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | inline bool Object::IsReferenceInstance() const { |
| 164 | return GetClass()->IsReferenceClass(); |
| 165 | } |
| 166 | |
Ian Rogers | 05f3057 | 2013-02-20 12:13:11 -0800 | [diff] [blame] | 167 | inline Array* Object::AsArray() { |
| 168 | DCHECK(IsArrayInstance()); |
| 169 | return down_cast<Array*>(this); |
| 170 | } |
| 171 | |
| 172 | inline const Array* Object::AsArray() const { |
| 173 | DCHECK(IsArrayInstance()); |
| 174 | return down_cast<const Array*>(this); |
| 175 | } |
| 176 | |
| 177 | inline BooleanArray* Object::AsBooleanArray() { |
| 178 | DCHECK(GetClass()->IsArrayClass()); |
| 179 | DCHECK(GetClass()->GetComponentType()->IsPrimitiveBoolean()); |
| 180 | return down_cast<BooleanArray*>(this); |
| 181 | } |
| 182 | |
| 183 | inline ByteArray* Object::AsByteArray() { |
| 184 | DCHECK(GetClass()->IsArrayClass()); |
| 185 | DCHECK(GetClass()->GetComponentType()->IsPrimitiveByte()); |
| 186 | return down_cast<ByteArray*>(this); |
| 187 | } |
| 188 | |
| 189 | inline CharArray* Object::AsCharArray() { |
| 190 | DCHECK(GetClass()->IsArrayClass()); |
| 191 | DCHECK(GetClass()->GetComponentType()->IsPrimitiveChar()); |
| 192 | return down_cast<CharArray*>(this); |
| 193 | } |
| 194 | |
| 195 | inline ShortArray* Object::AsShortArray() { |
| 196 | DCHECK(GetClass()->IsArrayClass()); |
| 197 | DCHECK(GetClass()->GetComponentType()->IsPrimitiveShort()); |
| 198 | return down_cast<ShortArray*>(this); |
| 199 | } |
| 200 | |
| 201 | inline IntArray* Object::AsIntArray() { |
| 202 | DCHECK(GetClass()->IsArrayClass()); |
| 203 | DCHECK(GetClass()->GetComponentType()->IsPrimitiveInt() || |
| 204 | GetClass()->GetComponentType()->IsPrimitiveFloat()); |
| 205 | return down_cast<IntArray*>(this); |
| 206 | } |
| 207 | |
| 208 | inline LongArray* Object::AsLongArray() { |
| 209 | DCHECK(GetClass()->IsArrayClass()); |
| 210 | DCHECK(GetClass()->GetComponentType()->IsPrimitiveLong() || |
| 211 | GetClass()->GetComponentType()->IsPrimitiveDouble()); |
| 212 | return down_cast<LongArray*>(this); |
| 213 | } |
| 214 | |
| 215 | inline String* Object::AsString() { |
| 216 | DCHECK(GetClass()->IsStringClass()); |
| 217 | return down_cast<String*>(this); |
| 218 | } |
| 219 | |
| 220 | inline Throwable* Object::AsThrowable() { |
| 221 | DCHECK(GetClass()->IsThrowableClass()); |
| 222 | return down_cast<Throwable*>(this); |
| 223 | } |
| 224 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 225 | inline bool Object::IsWeakReferenceInstance() const { |
| 226 | return GetClass()->IsWeakReferenceClass(); |
| 227 | } |
| 228 | |
| 229 | inline bool Object::IsSoftReferenceInstance() const { |
| 230 | return GetClass()->IsSoftReferenceClass(); |
| 231 | } |
| 232 | |
| 233 | inline bool Object::IsFinalizerReferenceInstance() const { |
| 234 | return GetClass()->IsFinalizerReferenceClass(); |
| 235 | } |
| 236 | |
| 237 | inline bool Object::IsPhantomReferenceInstance() const { |
| 238 | return GetClass()->IsPhantomReferenceClass(); |
| 239 | } |
| 240 | |
| 241 | inline size_t Object::SizeOf() const { |
| 242 | size_t result; |
| 243 | if (IsArrayInstance()) { |
| 244 | result = AsArray()->SizeOf(); |
| 245 | } else if (IsClass()) { |
| 246 | result = AsClass()->SizeOf(); |
| 247 | } else { |
| 248 | result = GetClass()->GetObjectSize(); |
| 249 | } |
Mathieu Chartier | 79b4f38 | 2013-10-23 15:21:37 -0700 | [diff] [blame^] | 250 | DCHECK_GE(result, sizeof(Object)) << " class=" << PrettyTypeOf(GetClass()); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 251 | DCHECK(!IsArtField() || result == sizeof(ArtField)); |
| 252 | DCHECK(!IsArtMethod() || result == sizeof(ArtMethod)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 253 | return result; |
| 254 | } |
| 255 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 256 | inline bool Object::CasField32(MemberOffset field_offset, uint32_t old_value, uint32_t new_value) { |
| 257 | VerifyObject(this); |
| 258 | byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset.Int32Value(); |
| 259 | int32_t* addr = reinterpret_cast<int32_t*>(raw_addr); |
| 260 | return android_atomic_release_cas(old_value, new_value, addr) == 0; |
| 261 | } |
| 262 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 263 | inline uint64_t Object::GetField64(MemberOffset field_offset, bool is_volatile) const { |
| 264 | VerifyObject(this); |
| 265 | const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset.Int32Value(); |
| 266 | const int64_t* addr = reinterpret_cast<const int64_t*>(raw_addr); |
| 267 | if (UNLIKELY(is_volatile)) { |
| 268 | uint64_t result = QuasiAtomic::Read64(addr); |
| 269 | ANDROID_MEMBAR_FULL(); |
| 270 | return result; |
| 271 | } else { |
| 272 | return *addr; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | inline void Object::SetField64(MemberOffset field_offset, uint64_t new_value, bool is_volatile) { |
| 277 | VerifyObject(this); |
| 278 | byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset.Int32Value(); |
| 279 | int64_t* addr = reinterpret_cast<int64_t*>(raw_addr); |
| 280 | if (UNLIKELY(is_volatile)) { |
| 281 | ANDROID_MEMBAR_STORE(); |
| 282 | QuasiAtomic::Write64(addr, new_value); |
| 283 | // Post-store barrier not required due to use of atomic op or mutex. |
| 284 | } else { |
| 285 | *addr = new_value; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | inline void Object::WriteBarrierField(const Object* dst, MemberOffset field_offset, |
| 290 | const Object* new_value) { |
| 291 | Runtime::Current()->GetHeap()->WriteBarrierField(dst, field_offset, new_value); |
| 292 | } |
| 293 | |
Ian Rogers | 04d7aa9 | 2013-03-16 14:29:17 -0700 | [diff] [blame] | 294 | inline void Object::VerifyObject(const Object* obj) { |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 295 | if (kIsDebugBuild) { |
| 296 | Runtime::Current()->GetHeap()->VerifyObject(obj); |
| 297 | } |
Ian Rogers | 04d7aa9 | 2013-03-16 14:29:17 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 300 | } // namespace mirror |
| 301 | } // namespace art |
| 302 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 303 | #endif // ART_RUNTIME_MIRROR_OBJECT_INL_H_ |