blob: 003581a1c834683b302b4f1d92364f8b075d0775 [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_MIRROR_OBJECT_H_
18#define ART_RUNTIME_MIRROR_OBJECT_H_
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080019
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
26namespace art {
27
28class ImageWriter;
29struct ObjectOffsets;
30class Thread;
31
32namespace mirror {
33
Brian Carlstromea46f952013-07-30 01:26:50 -070034class ArtField;
35class ArtMethod;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036class Array;
37class Class;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038template<class T> class ObjectArray;
39template<class T> class PrimitiveArray;
40typedef PrimitiveArray<uint8_t> BooleanArray;
41typedef PrimitiveArray<int8_t> ByteArray;
42typedef PrimitiveArray<uint16_t> CharArray;
43typedef PrimitiveArray<double> DoubleArray;
44typedef PrimitiveArray<float> FloatArray;
45typedef PrimitiveArray<int32_t> IntArray;
46typedef PrimitiveArray<int64_t> LongArray;
47typedef PrimitiveArray<int16_t> ShortArray;
48class String;
49class 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 Rogers04d7aa92013-03-16 14:29:17 -070061const bool kCheckFieldAssignments = false;
62
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080063// C++ mirror of java.lang.Object
64class 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
Jeff Haoa3faaf42013-09-03 19:07:00 -070074 // The verifier treats all interfaces as java.lang.Object and relies on runtime checks in
75 // invoke-interface to detect incompatible interface types.
76 bool VerifierInstanceOf(const Class* klass) const
77 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
78
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080079 bool InstanceOf(const Class* klass) const
80 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
81
82 size_t SizeOf() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
83
84 Object* Clone(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
85
86 int32_t IdentityHashCode() const {
Ian Rogersa436fde2013-08-27 23:34:06 -070087#ifdef MOVING_GARBAGE_COLLECTOR
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080088 // TODO: we'll need to use the Object's internal concept of identity
Ian Rogersa436fde2013-08-27 23:34:06 -070089 UNIMPLEMENTED(FATAL);
90#endif
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080091 return reinterpret_cast<int32_t>(this);
92 }
93
94 static MemberOffset MonitorOffset() {
95 return OFFSET_OF_OBJECT_MEMBER(Object, monitor_);
96 }
97
98 volatile int32_t* GetRawLockWordAddress() {
99 byte* raw_addr = reinterpret_cast<byte*>(this) +
100 OFFSET_OF_OBJECT_MEMBER(Object, monitor_).Int32Value();
101 int32_t* word_addr = reinterpret_cast<int32_t*>(raw_addr);
102 return const_cast<volatile int32_t*>(word_addr);
103 }
104
105 uint32_t GetThinLockId();
106
Ian Rogers05f30572013-02-20 12:13:11 -0800107 void MonitorEnter(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800108 EXCLUSIVE_LOCK_FUNCTION(monitor_lock_);
109
Ian Rogers05f30572013-02-20 12:13:11 -0800110 bool MonitorExit(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800111 UNLOCK_FUNCTION(monitor_lock_);
112
Ian Rogers05f30572013-02-20 12:13:11 -0800113 void Notify(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800114
Ian Rogers05f30572013-02-20 12:13:11 -0800115 void NotifyAll(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800116
Ian Rogers05f30572013-02-20 12:13:11 -0800117 void Wait(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800118
Ian Rogers05f30572013-02-20 12:13:11 -0800119 void Wait(Thread* self, int64_t timeout, int32_t nanos) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800120
121 bool IsClass() const;
122
123 Class* AsClass();
124
125 const Class* AsClass() const;
126
127 bool IsObjectArray() const;
128
129 template<class T>
130 ObjectArray<T>* AsObjectArray();
131
132 template<class T>
133 const ObjectArray<T>* AsObjectArray() const;
134
135 bool IsArrayInstance() const;
136
137 Array* AsArray();
138
139 const Array* AsArray() const;
140
141 BooleanArray* AsBooleanArray();
142 ByteArray* AsByteArray();
143 CharArray* AsCharArray();
144 ShortArray* AsShortArray();
145 IntArray* AsIntArray();
146 LongArray* AsLongArray();
147
148 String* AsString();
149
150 Throwable* AsThrowable() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
151
Brian Carlstromea46f952013-07-30 01:26:50 -0700152 bool IsArtMethod() const;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800153
Brian Carlstromea46f952013-07-30 01:26:50 -0700154 ArtMethod* AsArtMethod();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800155
Brian Carlstromea46f952013-07-30 01:26:50 -0700156 const ArtMethod* AsArtMethod() const;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800157
Brian Carlstromea46f952013-07-30 01:26:50 -0700158 bool IsArtField() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800159
Brian Carlstromea46f952013-07-30 01:26:50 -0700160 ArtField* AsArtField() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800161
Brian Carlstromea46f952013-07-30 01:26:50 -0700162 const ArtField* AsArtField() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800163
164 bool IsReferenceInstance() const;
165
166 bool IsWeakReferenceInstance() const;
167
168 bool IsSoftReferenceInstance() const;
169
170 bool IsFinalizerReferenceInstance() const;
171
172 bool IsPhantomReferenceInstance() const;
173
174 // Accessors for Java type fields
175 template<class T>
176 T GetFieldObject(MemberOffset field_offset, bool is_volatile) const {
177 T result = reinterpret_cast<T>(GetField32(field_offset, is_volatile));
178 VerifyObject(result);
179 return result;
180 }
181
182 void SetFieldObject(MemberOffset field_offset, const Object* new_value, bool is_volatile,
183 bool this_is_valid = true) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
184 VerifyObject(new_value);
185 SetField32(field_offset, reinterpret_cast<uint32_t>(new_value), is_volatile, this_is_valid);
186 if (new_value != NULL) {
187 CheckFieldAssignment(field_offset, new_value);
188 WriteBarrierField(this, field_offset, new_value);
189 }
190 }
191
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700192 Object** GetFieldObjectAddr(MemberOffset field_offset) ALWAYS_INLINE {
193 VerifyObject(this);
194 return reinterpret_cast<Object**>(reinterpret_cast<byte*>(this) + field_offset.Int32Value());
195 }
196
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800197 uint32_t GetField32(MemberOffset field_offset, bool is_volatile) const {
198 VerifyObject(this);
199 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset.Int32Value();
200 const int32_t* word_addr = reinterpret_cast<const int32_t*>(raw_addr);
201 if (UNLIKELY(is_volatile)) {
202 return android_atomic_acquire_load(word_addr);
203 } else {
204 return *word_addr;
205 }
206 }
207
208 void SetField32(MemberOffset field_offset, uint32_t new_value, bool is_volatile,
209 bool this_is_valid = true) {
210 if (this_is_valid) {
211 VerifyObject(this);
212 }
213 byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset.Int32Value();
214 uint32_t* word_addr = reinterpret_cast<uint32_t*>(raw_addr);
215 if (UNLIKELY(is_volatile)) {
216 /*
217 * TODO: add an android_atomic_synchronization_store() function and
218 * use it in the 32-bit volatile set handlers. On some platforms we
219 * can use a fast atomic instruction and avoid the barriers.
220 */
221 ANDROID_MEMBAR_STORE();
222 *word_addr = new_value;
223 ANDROID_MEMBAR_FULL();
224 } else {
225 *word_addr = new_value;
226 }
227 }
228
229 uint64_t GetField64(MemberOffset field_offset, bool is_volatile) const;
230
231 void SetField64(MemberOffset field_offset, uint64_t new_value, bool is_volatile);
232
233 protected:
234 // Accessors for non-Java type fields
235 template<class T>
236 T GetFieldPtr(MemberOffset field_offset, bool is_volatile) const {
237 return reinterpret_cast<T>(GetField32(field_offset, is_volatile));
238 }
239
240 template<typename T>
241 void SetFieldPtr(MemberOffset field_offset, T new_value, bool is_volatile, bool this_is_valid = true) {
242 SetField32(field_offset, reinterpret_cast<uint32_t>(new_value), is_volatile, this_is_valid);
243 }
244
245 private:
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700246 static void VerifyObject(const Object* obj) ALWAYS_INLINE;
Ian Rogers04d7aa92013-03-16 14:29:17 -0700247
248 // Verify the type correctness of stores to fields.
249 void CheckFieldAssignmentImpl(MemberOffset field_offset, const Object* new_value)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800250 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers04d7aa92013-03-16 14:29:17 -0700251 void CheckFieldAssignment(MemberOffset field_offset, const Object* new_value)
252 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
253 if (kCheckFieldAssignments) {
254 CheckFieldAssignmentImpl(field_offset, new_value);
255 }
256 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800257
258 // Write barrier called post update to a reference bearing field.
259 static void WriteBarrierField(const Object* dst, MemberOffset offset, const Object* new_value);
260
261 Class* klass_;
262
263 uint32_t monitor_;
264
265 friend class art::ImageWriter;
266 friend struct art::ObjectOffsets; // for verifying offset information
267 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
268};
269
270} // namespace mirror
271} // namespace art
272
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700273#endif // ART_RUNTIME_MIRROR_OBJECT_H_