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