• 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_REQUIRES(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
43 
44   static Array* CreateMultiArray(Thread* self, Handle<Class> element_class,
45                                  Handle<IntArray> dimensions)
46       SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
47 
48   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
49            ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
50   size_t SizeOf() SHARED_REQUIRES(Locks::mutator_lock_);
51   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
GetLength()52   ALWAYS_INLINE int32_t GetLength() SHARED_REQUIRES(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_REQUIRES(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_REQUIRES(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_REQUIRES(Locks::mutator_lock_);
86 
87   Array* CopyOf(Thread* self, int32_t new_length) SHARED_REQUIRES(Locks::mutator_lock_)
88       REQUIRES(!Roles::uninterruptible_);
89 
90  protected:
91   void ThrowArrayStoreException(Object* object) SHARED_REQUIRES(Locks::mutator_lock_)
92       REQUIRES(!Roles::uninterruptible_);
93 
94  private:
95   void ThrowArrayIndexOutOfBoundsException(int32_t index)
96       SHARED_REQUIRES(Locks::mutator_lock_);
97 
98   // The number of array elements.
99   int32_t length_;
100   // Marker for the data (used by generated code)
101   uint32_t first_element_[0];
102 
103   DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
104 };
105 
106 template<typename T>
107 class MANAGED PrimitiveArray : public Array {
108  public:
109   typedef T ElementType;
110 
111   static PrimitiveArray<T>* Alloc(Thread* self, size_t length)
112       SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
113 
GetData()114   const T* GetData() const ALWAYS_INLINE  SHARED_REQUIRES(Locks::mutator_lock_) {
115     return reinterpret_cast<const T*>(GetRawData(sizeof(T), 0));
116   }
117 
GetData()118   T* GetData() ALWAYS_INLINE SHARED_REQUIRES(Locks::mutator_lock_) {
119     return reinterpret_cast<T*>(GetRawData(sizeof(T), 0));
120   }
121 
122   T Get(int32_t i) ALWAYS_INLINE SHARED_REQUIRES(Locks::mutator_lock_);
123 
GetWithoutChecks(int32_t i)124   T GetWithoutChecks(int32_t i) ALWAYS_INLINE SHARED_REQUIRES(Locks::mutator_lock_) {
125     DCHECK(CheckIsValidIndex(i)) << "i=" << i << " length=" << GetLength();
126     return GetData()[i];
127   }
128 
129   void Set(int32_t i, T value) ALWAYS_INLINE SHARED_REQUIRES(Locks::mutator_lock_);
130 
131   // TODO fix thread safety analysis broken by the use of template. This should be
132   // SHARED_REQUIRES(Locks::mutator_lock_).
133   template<bool kTransactionActive, bool kCheckTransaction = true>
134   void Set(int32_t i, T value) ALWAYS_INLINE NO_THREAD_SAFETY_ANALYSIS;
135 
136   // TODO fix thread safety analysis broken by the use of template. This should be
137   // SHARED_REQUIRES(Locks::mutator_lock_).
138   template<bool kTransactionActive,
139            bool kCheckTransaction = true,
140            VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
141   void SetWithoutChecks(int32_t i, T value) ALWAYS_INLINE NO_THREAD_SAFETY_ANALYSIS;
142 
143   /*
144    * Works like memmove(), except we guarantee not to allow tearing of array values (ie using
145    * smaller than element size copies). Arguments are assumed to be within the bounds of the array
146    * and the arrays non-null.
147    */
148   void Memmove(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos, int32_t count)
149       SHARED_REQUIRES(Locks::mutator_lock_);
150 
151   /*
152    * Works like memcpy(), except we guarantee not to allow tearing of array values (ie using
153    * smaller than element size copies). Arguments are assumed to be within the bounds of the array
154    * and the arrays non-null.
155    */
156   void Memcpy(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos, int32_t count)
157       SHARED_REQUIRES(Locks::mutator_lock_);
158 
SetArrayClass(Class * array_class)159   static void SetArrayClass(Class* array_class) {
160     CHECK(array_class_.IsNull());
161     CHECK(array_class != nullptr);
162     array_class_ = GcRoot<Class>(array_class);
163   }
164 
GetArrayClass()165   static Class* GetArrayClass() SHARED_REQUIRES(Locks::mutator_lock_) {
166     DCHECK(!array_class_.IsNull());
167     return array_class_.Read();
168   }
169 
ResetArrayClass()170   static void ResetArrayClass() {
171     CHECK(!array_class_.IsNull());
172     array_class_ = GcRoot<Class>(nullptr);
173   }
174 
175   static void VisitRoots(RootVisitor* visitor) SHARED_REQUIRES(Locks::mutator_lock_);
176 
177  private:
178   static GcRoot<Class> array_class_;
179 
180   DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
181 };
182 
183 // Either an IntArray or a LongArray.
184 class PointerArray : public Array {
185  public:
186   template<typename T,
187            VerifyObjectFlags kVerifyFlags = kVerifyNone,
188            ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
189   T GetElementPtrSize(uint32_t idx, size_t ptr_size)
190       SHARED_REQUIRES(Locks::mutator_lock_);
191 
192   template<bool kTransactionActive = false, bool kUnchecked = false>
193   void SetElementPtrSize(uint32_t idx, uint64_t element, size_t ptr_size)
194       SHARED_REQUIRES(Locks::mutator_lock_);
195   template<bool kTransactionActive = false, bool kUnchecked = false, typename T>
196   void SetElementPtrSize(uint32_t idx, T* element, size_t ptr_size)
197       SHARED_REQUIRES(Locks::mutator_lock_);
198 
199   // Fixup the pointers in the dest arrays by passing our pointers through the visitor. Only copies
200   // to dest if visitor(source_ptr) != source_ptr.
201   template <VerifyObjectFlags kVerifyFlags = kVerifyNone,
202             ReadBarrierOption kReadBarrierOption = kWithReadBarrier,
203             typename Visitor>
204   void Fixup(mirror::PointerArray* dest, size_t pointer_size, const Visitor& visitor)
205       SHARED_REQUIRES(Locks::mutator_lock_);
206 };
207 
208 }  // namespace mirror
209 }  // namespace art
210 
211 #endif  // ART_RUNTIME_MIRROR_ARRAY_H_
212