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