• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #ifndef ART_RUNTIME_MIRROR_ARRAY_H_
18 #define ART_RUNTIME_MIRROR_ARRAY_H_
19 
20 #include "gc_root.h"
21 #include "gc/allocator_type.h"
22 #include "object.h"
23 #include "object_callbacks.h"
24 
25 namespace art {
26 
27 template<class T> class Handle;
28 
29 namespace mirror {
30 
31 class MANAGED Array : public Object {
32  public:
33   // The size of a java.lang.Class representing an array.
34   static uint32_t ClassSize(size_t pointer_size);
35 
36   // Allocates an array with the given properties, if kFillUsable is true the array will be of at
37   // least component_count size, however, if there's usable space at the end of the allocation the
38   // array will fill it.
39   template <bool kIsInstrumented, bool kFillUsable = false>
40   ALWAYS_INLINE static Array* Alloc(Thread* self, Class* array_class, int32_t component_count,
41                                     size_t component_size_shift, gc::AllocatorType allocator_type)
42       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
43 
44   static Array* CreateMultiArray(Thread* self, Handle<Class> element_class,
45                                  Handle<IntArray> dimensions)
46       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
47 
48   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
49            ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
50   size_t SizeOf() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
51   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
GetLength()52   ALWAYS_INLINE int32_t GetLength() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
53     return GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Array, length_));
54   }
55 
SetLength(int32_t length)56   void SetLength(int32_t length) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
57     DCHECK_GE(length, 0);
58     // We use non transactional version since we can't undo this write. We also disable checking
59     // since it would fail during a transaction.
60     SetField32<false, false, kVerifyNone>(OFFSET_OF_OBJECT_MEMBER(Array, length_), length);
61   }
62 
LengthOffset()63   static MemberOffset LengthOffset() {
64     return OFFSET_OF_OBJECT_MEMBER(Array, length_);
65   }
66 
67   static MemberOffset DataOffset(size_t component_size);
68 
GetRawData(size_t component_size,int32_t index)69   void* GetRawData(size_t component_size, int32_t index)
70       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
71     intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value() +
72         + (index * component_size);
73     return reinterpret_cast<void*>(data);
74   }
75 
GetRawData(size_t component_size,int32_t index)76   const void* GetRawData(size_t component_size, int32_t index) const {
77     intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value() +
78         + (index * component_size);
79     return reinterpret_cast<void*>(data);
80   }
81 
82   // Returns true if the index is valid. If not, throws an ArrayIndexOutOfBoundsException and
83   // returns false.
84   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
85   ALWAYS_INLINE bool CheckIsValidIndex(int32_t index) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
86 
87   Array* CopyOf(Thread* self, int32_t new_length) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
88 
89  protected:
90   void ThrowArrayStoreException(Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
91 
92  private:
93   void ThrowArrayIndexOutOfBoundsException(int32_t index)
94       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
95 
96   // The number of array elements.
97   int32_t length_;
98   // Marker for the data (used by generated code)
99   uint32_t first_element_[0];
100 
101   DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
102 };
103 
104 template<typename T>
105 class MANAGED PrimitiveArray : public Array {
106  public:
107   typedef T ElementType;
108 
109   static PrimitiveArray<T>* Alloc(Thread* self, size_t length)
110       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
111 
GetData()112   const T* GetData() const ALWAYS_INLINE  SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
113     return reinterpret_cast<const T*>(GetRawData(sizeof(T), 0));
114   }
115 
GetData()116   T* GetData() ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
117     return reinterpret_cast<T*>(GetRawData(sizeof(T), 0));
118   }
119 
120   T Get(int32_t i) ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
121 
GetWithoutChecks(int32_t i)122   T GetWithoutChecks(int32_t i) ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
123     DCHECK(CheckIsValidIndex(i));
124     return GetData()[i];
125   }
126 
127   void Set(int32_t i, T value) ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
128 
129   // TODO fix thread safety analysis broken by the use of template. This should be
130   // SHARED_LOCKS_REQUIRED(Locks::mutator_lock_).
131   template<bool kTransactionActive, bool kCheckTransaction = true>
132   void Set(int32_t i, T value) ALWAYS_INLINE NO_THREAD_SAFETY_ANALYSIS;
133 
134   // TODO fix thread safety analysis broken by the use of template. This should be
135   // SHARED_LOCKS_REQUIRED(Locks::mutator_lock_).
136   template<bool kTransactionActive, bool kCheckTransaction = true>
137   void SetWithoutChecks(int32_t i, T value) ALWAYS_INLINE NO_THREAD_SAFETY_ANALYSIS;
138 
139   /*
140    * Works like memmove(), except we guarantee not to allow tearing of array values (ie using
141    * smaller than element size copies). Arguments are assumed to be within the bounds of the array
142    * and the arrays non-null.
143    */
144   void Memmove(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos, int32_t count)
145       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
146 
147   /*
148    * Works like memcpy(), except we guarantee not to allow tearing of array values (ie using
149    * smaller than element size copies). Arguments are assumed to be within the bounds of the array
150    * and the arrays non-null.
151    */
152   void Memcpy(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos, int32_t count)
153       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
154 
SetArrayClass(Class * array_class)155   static void SetArrayClass(Class* array_class) {
156     CHECK(array_class_.IsNull());
157     CHECK(array_class != nullptr);
158     array_class_ = GcRoot<Class>(array_class);
159   }
160 
GetArrayClass()161   static Class* GetArrayClass() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
162     DCHECK(!array_class_.IsNull());
163     return array_class_.Read();
164   }
165 
ResetArrayClass()166   static void ResetArrayClass() {
167     CHECK(!array_class_.IsNull());
168     array_class_ = GcRoot<Class>(nullptr);
169   }
170 
171   static void VisitRoots(RootVisitor* visitor) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
172 
173  private:
174   static GcRoot<Class> array_class_;
175 
176   DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
177 };
178 
179 // Either an IntArray or a LongArray.
180 class PointerArray : public Array {
181  public:
182   template<typename T>
183   T GetElementPtrSize(uint32_t idx, size_t ptr_size)
184       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
185 
186   template<bool kTransactionActive = false, bool kUnchecked = false, typename T>
187   void SetElementPtrSize(uint32_t idx, T element, size_t ptr_size)
188       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
189 };
190 
191 }  // namespace mirror
192 }  // namespace art
193 
194 #endif  // ART_RUNTIME_MIRROR_ARRAY_H_
195