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