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_H_ |
| 18 | #define ART_RUNTIME_MIRROR_OBJECT_H_ |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 19 | |
| 20 | #include "base/casts.h" |
| 21 | #include "base/logging.h" |
| 22 | #include "base/macros.h" |
| 23 | #include "cutils/atomic-inline.h" |
| 24 | #include "offsets.h" |
| 25 | |
| 26 | namespace art { |
| 27 | |
| 28 | class ImageWriter; |
| 29 | struct ObjectOffsets; |
| 30 | class Thread; |
| 31 | |
| 32 | namespace mirror { |
| 33 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 34 | class ArtField; |
| 35 | class ArtMethod; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 36 | class Array; |
| 37 | class Class; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 38 | template<class T> class ObjectArray; |
| 39 | template<class T> class PrimitiveArray; |
| 40 | typedef PrimitiveArray<uint8_t> BooleanArray; |
| 41 | typedef PrimitiveArray<int8_t> ByteArray; |
| 42 | typedef PrimitiveArray<uint16_t> CharArray; |
| 43 | typedef PrimitiveArray<double> DoubleArray; |
| 44 | typedef PrimitiveArray<float> FloatArray; |
| 45 | typedef PrimitiveArray<int32_t> IntArray; |
| 46 | typedef PrimitiveArray<int64_t> LongArray; |
| 47 | typedef PrimitiveArray<int16_t> ShortArray; |
| 48 | class String; |
| 49 | class Throwable; |
| 50 | |
| 51 | // Classes shared with the managed side of the world need to be packed so that they don't have |
| 52 | // extra platform specific padding. |
| 53 | #define MANAGED PACKED(4) |
| 54 | |
| 55 | // Fields within mirror objects aren't accessed directly so that the appropriate amount of |
| 56 | // handshaking is done with GC (for example, read and write barriers). This macro is used to |
| 57 | // compute an offset for the Set/Get methods defined in Object that can safely access fields. |
| 58 | #define OFFSET_OF_OBJECT_MEMBER(type, field) \ |
| 59 | MemberOffset(OFFSETOF_MEMBER(type, field)) |
| 60 | |
Ian Rogers | 04d7aa9 | 2013-03-16 14:29:17 -0700 | [diff] [blame] | 61 | const bool kCheckFieldAssignments = false; |
| 62 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 63 | // C++ mirror of java.lang.Object |
| 64 | class MANAGED Object { |
| 65 | public: |
| 66 | static MemberOffset ClassOffset() { |
| 67 | return OFFSET_OF_OBJECT_MEMBER(Object, klass_); |
| 68 | } |
| 69 | |
| 70 | Class* GetClass() const; |
| 71 | |
| 72 | void SetClass(Class* new_klass); |
| 73 | |
| 74 | bool InstanceOf(const Class* klass) const |
| 75 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 76 | |
| 77 | size_t SizeOf() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 78 | |
| 79 | Object* Clone(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 80 | |
| 81 | int32_t IdentityHashCode() const { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 82 | #ifdef MOVING_GARBAGE_COLLECTOR |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 83 | // TODO: we'll need to use the Object's internal concept of identity |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 84 | UNIMPLEMENTED(FATAL); |
| 85 | #endif |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 86 | return reinterpret_cast<int32_t>(this); |
| 87 | } |
| 88 | |
| 89 | static MemberOffset MonitorOffset() { |
| 90 | return OFFSET_OF_OBJECT_MEMBER(Object, monitor_); |
| 91 | } |
| 92 | |
| 93 | volatile int32_t* GetRawLockWordAddress() { |
| 94 | byte* raw_addr = reinterpret_cast<byte*>(this) + |
| 95 | OFFSET_OF_OBJECT_MEMBER(Object, monitor_).Int32Value(); |
| 96 | int32_t* word_addr = reinterpret_cast<int32_t*>(raw_addr); |
| 97 | return const_cast<volatile int32_t*>(word_addr); |
| 98 | } |
| 99 | |
| 100 | uint32_t GetThinLockId(); |
| 101 | |
Ian Rogers | 05f3057 | 2013-02-20 12:13:11 -0800 | [diff] [blame] | 102 | void MonitorEnter(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 103 | EXCLUSIVE_LOCK_FUNCTION(monitor_lock_); |
| 104 | |
Ian Rogers | 05f3057 | 2013-02-20 12:13:11 -0800 | [diff] [blame] | 105 | bool MonitorExit(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 106 | UNLOCK_FUNCTION(monitor_lock_); |
| 107 | |
Ian Rogers | 05f3057 | 2013-02-20 12:13:11 -0800 | [diff] [blame] | 108 | void Notify(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 109 | |
Ian Rogers | 05f3057 | 2013-02-20 12:13:11 -0800 | [diff] [blame] | 110 | void NotifyAll(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 111 | |
Ian Rogers | 05f3057 | 2013-02-20 12:13:11 -0800 | [diff] [blame] | 112 | void Wait(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 113 | |
Ian Rogers | 05f3057 | 2013-02-20 12:13:11 -0800 | [diff] [blame] | 114 | void Wait(Thread* self, int64_t timeout, int32_t nanos) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 115 | |
| 116 | bool IsClass() const; |
| 117 | |
| 118 | Class* AsClass(); |
| 119 | |
| 120 | const Class* AsClass() const; |
| 121 | |
| 122 | bool IsObjectArray() const; |
| 123 | |
| 124 | template<class T> |
| 125 | ObjectArray<T>* AsObjectArray(); |
| 126 | |
| 127 | template<class T> |
| 128 | const ObjectArray<T>* AsObjectArray() const; |
| 129 | |
| 130 | bool IsArrayInstance() const; |
| 131 | |
| 132 | Array* AsArray(); |
| 133 | |
| 134 | const Array* AsArray() const; |
| 135 | |
| 136 | BooleanArray* AsBooleanArray(); |
| 137 | ByteArray* AsByteArray(); |
| 138 | CharArray* AsCharArray(); |
| 139 | ShortArray* AsShortArray(); |
| 140 | IntArray* AsIntArray(); |
| 141 | LongArray* AsLongArray(); |
| 142 | |
| 143 | String* AsString(); |
| 144 | |
| 145 | Throwable* AsThrowable() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 146 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 147 | bool IsArtMethod() const; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 148 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 149 | ArtMethod* AsArtMethod(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 150 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 151 | const ArtMethod* AsArtMethod() const; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 152 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 153 | bool IsArtField() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 154 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 155 | ArtField* AsArtField() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 156 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 157 | const ArtField* AsArtField() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 158 | |
| 159 | bool IsReferenceInstance() const; |
| 160 | |
| 161 | bool IsWeakReferenceInstance() const; |
| 162 | |
| 163 | bool IsSoftReferenceInstance() const; |
| 164 | |
| 165 | bool IsFinalizerReferenceInstance() const; |
| 166 | |
| 167 | bool IsPhantomReferenceInstance() const; |
| 168 | |
| 169 | // Accessors for Java type fields |
| 170 | template<class T> |
| 171 | T GetFieldObject(MemberOffset field_offset, bool is_volatile) const { |
| 172 | T result = reinterpret_cast<T>(GetField32(field_offset, is_volatile)); |
| 173 | VerifyObject(result); |
| 174 | return result; |
| 175 | } |
| 176 | |
| 177 | void SetFieldObject(MemberOffset field_offset, const Object* new_value, bool is_volatile, |
| 178 | bool this_is_valid = true) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 179 | VerifyObject(new_value); |
| 180 | SetField32(field_offset, reinterpret_cast<uint32_t>(new_value), is_volatile, this_is_valid); |
| 181 | if (new_value != NULL) { |
| 182 | CheckFieldAssignment(field_offset, new_value); |
| 183 | WriteBarrierField(this, field_offset, new_value); |
| 184 | } |
| 185 | } |
| 186 | |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame^] | 187 | Object** GetFieldObjectAddr(MemberOffset field_offset) ALWAYS_INLINE { |
| 188 | VerifyObject(this); |
| 189 | return reinterpret_cast<Object**>(reinterpret_cast<byte*>(this) + field_offset.Int32Value()); |
| 190 | } |
| 191 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 192 | uint32_t GetField32(MemberOffset field_offset, bool is_volatile) const { |
| 193 | VerifyObject(this); |
| 194 | const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset.Int32Value(); |
| 195 | const int32_t* word_addr = reinterpret_cast<const int32_t*>(raw_addr); |
| 196 | if (UNLIKELY(is_volatile)) { |
| 197 | return android_atomic_acquire_load(word_addr); |
| 198 | } else { |
| 199 | return *word_addr; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | void SetField32(MemberOffset field_offset, uint32_t new_value, bool is_volatile, |
| 204 | bool this_is_valid = true) { |
| 205 | if (this_is_valid) { |
| 206 | VerifyObject(this); |
| 207 | } |
| 208 | byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset.Int32Value(); |
| 209 | uint32_t* word_addr = reinterpret_cast<uint32_t*>(raw_addr); |
| 210 | if (UNLIKELY(is_volatile)) { |
| 211 | /* |
| 212 | * TODO: add an android_atomic_synchronization_store() function and |
| 213 | * use it in the 32-bit volatile set handlers. On some platforms we |
| 214 | * can use a fast atomic instruction and avoid the barriers. |
| 215 | */ |
| 216 | ANDROID_MEMBAR_STORE(); |
| 217 | *word_addr = new_value; |
| 218 | ANDROID_MEMBAR_FULL(); |
| 219 | } else { |
| 220 | *word_addr = new_value; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | uint64_t GetField64(MemberOffset field_offset, bool is_volatile) const; |
| 225 | |
| 226 | void SetField64(MemberOffset field_offset, uint64_t new_value, bool is_volatile); |
| 227 | |
| 228 | protected: |
| 229 | // Accessors for non-Java type fields |
| 230 | template<class T> |
| 231 | T GetFieldPtr(MemberOffset field_offset, bool is_volatile) const { |
| 232 | return reinterpret_cast<T>(GetField32(field_offset, is_volatile)); |
| 233 | } |
| 234 | |
| 235 | template<typename T> |
| 236 | void SetFieldPtr(MemberOffset field_offset, T new_value, bool is_volatile, bool this_is_valid = true) { |
| 237 | SetField32(field_offset, reinterpret_cast<uint32_t>(new_value), is_volatile, this_is_valid); |
| 238 | } |
| 239 | |
| 240 | private: |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 241 | static void VerifyObject(const Object* obj) ALWAYS_INLINE; |
Ian Rogers | 04d7aa9 | 2013-03-16 14:29:17 -0700 | [diff] [blame] | 242 | |
| 243 | // Verify the type correctness of stores to fields. |
| 244 | void CheckFieldAssignmentImpl(MemberOffset field_offset, const Object* new_value) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 245 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 04d7aa9 | 2013-03-16 14:29:17 -0700 | [diff] [blame] | 246 | void CheckFieldAssignment(MemberOffset field_offset, const Object* new_value) |
| 247 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 248 | if (kCheckFieldAssignments) { |
| 249 | CheckFieldAssignmentImpl(field_offset, new_value); |
| 250 | } |
| 251 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 252 | |
| 253 | // Write barrier called post update to a reference bearing field. |
| 254 | static void WriteBarrierField(const Object* dst, MemberOffset offset, const Object* new_value); |
| 255 | |
| 256 | Class* klass_; |
| 257 | |
| 258 | uint32_t monitor_; |
| 259 | |
| 260 | friend class art::ImageWriter; |
| 261 | friend struct art::ObjectOffsets; // for verifying offset information |
| 262 | DISALLOW_IMPLICIT_CONSTRUCTORS(Object); |
| 263 | }; |
| 264 | |
| 265 | } // namespace mirror |
| 266 | } // namespace art |
| 267 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 268 | #endif // ART_RUNTIME_MIRROR_OBJECT_H_ |