blob: 25a4535f14514c45e902e8ebf708f3dcbca2ad9b [file] [log] [blame]
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001/*
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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_MIRROR_ARRAY_H_
18#define ART_RUNTIME_MIRROR_ARRAY_H_
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080019
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070020#include "gc/allocator_type.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080021#include "object.h"
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080022#include "object_callbacks.h"
Hiroshi Yamauchi4f1ebc22014-06-25 14:30:41 -070023#include "read_barrier.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080024
25namespace art {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070026
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070027template<class T> class Handle;
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070028
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029namespace mirror {
30
31class MANAGED Array : public Object {
32 public:
Ian Rogers6fac4472014-02-25 17:01:10 -080033 // Allocates an array with the given properties, if fill_usable is true the array will be of at
34 // least component_count size, however, if there's usable space at the end of the allocation the
35 // array will fill it.
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080036 template <bool kIsInstrumented>
37 static Array* Alloc(Thread* self, Class* array_class, int32_t component_count,
Ian Rogers6fac4472014-02-25 17:01:10 -080038 size_t component_size, gc::AllocatorType allocator_type,
39 bool fill_usable = false)
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080040 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080041
Mathieu Chartier0cd81352014-05-22 16:48:55 -070042 static Array* CreateMultiArray(Thread* self, Handle<Class> element_class,
43 Handle<IntArray> dimensions)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080044 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
45
Hiroshi Yamauchi6e83c172014-05-01 21:25:41 -070046 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
47 ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Ian Rogersef7d42f2014-01-06 12:55:46 -080048 size_t SizeOf() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier4e305412014-02-19 10:54:44 -080049 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Ian Rogersef7d42f2014-01-06 12:55:46 -080050 int32_t GetLength() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070051 return GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Array, length_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080052 }
53
Ian Rogersef7d42f2014-01-06 12:55:46 -080054 void SetLength(int32_t length) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080055 CHECK_GE(length, 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010056 // We use non transactional version since we can't undo this write. We also disable checking
57 // since it would fail during a transaction.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070058 SetField32<false, false, kVerifyNone>(OFFSET_OF_OBJECT_MEMBER(Array, length_), length);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080059 }
60
61 static MemberOffset LengthOffset() {
62 return OFFSET_OF_OBJECT_MEMBER(Array, length_);
63 }
64
65 static MemberOffset DataOffset(size_t component_size) {
66 if (component_size != sizeof(int64_t)) {
67 return OFFSET_OF_OBJECT_MEMBER(Array, first_element_);
68 } else {
69 // Align longs and doubles.
70 return MemberOffset(OFFSETOF_MEMBER(Array, first_element_) + 4);
71 }
72 }
73
Ian Rogersef7d42f2014-01-06 12:55:46 -080074 void* GetRawData(size_t component_size, int32_t index)
75 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
76 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value() +
77 + (index * component_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080078 return reinterpret_cast<void*>(data);
79 }
80
Ian Rogersef7d42f2014-01-06 12:55:46 -080081 const void* GetRawData(size_t component_size, int32_t index) const {
82 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value() +
83 + (index * component_size);
84 return reinterpret_cast<void*>(data);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080085 }
86
Sebastien Hertzabff6432014-01-27 18:01:39 +010087 // Returns true if the index is valid. If not, throws an ArrayIndexOutOfBoundsException and
88 // returns false.
Mathieu Chartier4e305412014-02-19 10:54:44 -080089 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070090 bool CheckIsValidIndex(int32_t index) ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080091
92 protected:
Ian Rogersef7d42f2014-01-06 12:55:46 -080093 void ThrowArrayStoreException(Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080094
95 private:
Ian Rogersef7d42f2014-01-06 12:55:46 -080096 void ThrowArrayIndexOutOfBoundsException(int32_t index)
Sebastien Hertzabff6432014-01-27 18:01:39 +010097 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
98
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080099 // The number of array elements.
100 int32_t length_;
101 // Marker for the data (used by generated code)
102 uint32_t first_element_[0];
103
104 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
105};
106
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700107template<typename T>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800108class MANAGED PrimitiveArray : public Array {
109 public:
110 typedef T ElementType;
111
112 static PrimitiveArray<T>* Alloc(Thread* self, size_t length)
113 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
114
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700115 const T* GetData() const ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800116 return reinterpret_cast<const T*>(GetRawData(sizeof(T), 0));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800117 }
118
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700119 T* GetData() ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800120 return reinterpret_cast<T*>(GetRawData(sizeof(T), 0));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800121 }
122
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700123 T Get(int32_t i) ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertzabff6432014-01-27 18:01:39 +0100124
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700125 T GetWithoutChecks(int32_t i) ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertzabff6432014-01-27 18:01:39 +0100126 DCHECK(CheckIsValidIndex(i));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800127 return GetData()[i];
128 }
129
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700130 void Set(int32_t i, T value) ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100131
132 // TODO fix thread safety analysis broken by the use of template. This should be
133 // SHARED_LOCKS_REQUIRED(Locks::mutator_lock_).
134 template<bool kTransactionActive, bool kCheckTransaction = true>
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700135 void Set(int32_t i, T value) ALWAYS_INLINE NO_THREAD_SAFETY_ANALYSIS;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800136
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100137 // TODO fix thread safety analysis broken by the use of template. This should be
138 // SHARED_LOCKS_REQUIRED(Locks::mutator_lock_).
139 template<bool kTransactionActive, bool kCheckTransaction = true>
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700140 void SetWithoutChecks(int32_t i, T value) ALWAYS_INLINE NO_THREAD_SAFETY_ANALYSIS;
Sebastien Hertzabff6432014-01-27 18:01:39 +0100141
Ian Rogersef7d42f2014-01-06 12:55:46 -0800142 /*
143 * Works like memmove(), except we guarantee not to allow tearing of array values (ie using
144 * smaller than element size copies). Arguments are assumed to be within the bounds of the array
145 * and the arrays non-null.
146 */
147 void Memmove(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos, int32_t count)
148 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
149
150 /*
151 * Works like memcpy(), except we guarantee not to allow tearing of array values (ie using
152 * smaller than element size copies). Arguments are assumed to be within the bounds of the array
153 * and the arrays non-null.
154 */
155 void Memcpy(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos, int32_t count)
156 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
157
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800158 static void SetArrayClass(Class* array_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700159 CHECK(array_class_ == nullptr);
160 CHECK(array_class != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800161 array_class_ = array_class;
162 }
163
Hiroshi Yamauchi4f1ebc22014-06-25 14:30:41 -0700164 static Class* GetArrayClass() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700165 DCHECK(array_class_ != nullptr);
Hiroshi Yamauchi4f1ebc22014-06-25 14:30:41 -0700166 return ReadBarrier::BarrierForRoot<mirror::Class, kWithReadBarrier>(
167 &array_class_);
Ian Rogers2d10b202014-05-12 19:15:18 -0700168 }
169
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800170 static void ResetArrayClass() {
Ian Rogers2d10b202014-05-12 19:15:18 -0700171 CHECK(array_class_ != nullptr);
172 array_class_ = nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800173 }
174
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800175 static void VisitRoots(RootCallback* callback, void* arg)
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800176 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
177
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800178 private:
179 static Class* array_class_;
180
181 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
182};
183
184} // namespace mirror
185} // namespace art
186
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700187#endif // ART_RUNTIME_MIRROR_ARRAY_H_