blob: 238506e86d90f02fcf874758a6845bcf95120cee [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"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080023
24namespace art {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070025
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070026template<class T> class Handle;
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070027
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028namespace mirror {
29
30class MANAGED Array : public Object {
31 public:
Ian Rogers6fac4472014-02-25 17:01:10 -080032 // 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.
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080035 template <bool kIsInstrumented>
36 static Array* Alloc(Thread* self, Class* array_class, int32_t component_count,
Ian Rogers6fac4472014-02-25 17:01:10 -080037 size_t component_size, gc::AllocatorType allocator_type,
38 bool fill_usable = false)
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080039 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070041 static Array* CreateMultiArray(Thread* self, const Handle<Class>& element_class,
42 const Handle<IntArray>& dimensions)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080043 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
44
Hiroshi Yamauchi6e83c172014-05-01 21:25:41 -070045 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
46 ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Ian Rogersef7d42f2014-01-06 12:55:46 -080047 size_t SizeOf() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier4e305412014-02-19 10:54:44 -080048 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Ian Rogersef7d42f2014-01-06 12:55:46 -080049 int32_t GetLength() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070050 return GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Array, length_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080051 }
52
Ian Rogersef7d42f2014-01-06 12:55:46 -080053 void SetLength(int32_t length) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080054 CHECK_GE(length, 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010055 // We use non transactional version since we can't undo this write. We also disable checking
56 // since it would fail during a transaction.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070057 SetField32<false, false, kVerifyNone>(OFFSET_OF_OBJECT_MEMBER(Array, length_), length);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080058 }
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
Ian Rogersef7d42f2014-01-06 12:55:46 -080073 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);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080077 return reinterpret_cast<void*>(data);
78 }
79
Ian Rogersef7d42f2014-01-06 12:55:46 -080080 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);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080084 }
85
Sebastien Hertzabff6432014-01-27 18:01:39 +010086 // Returns true if the index is valid. If not, throws an ArrayIndexOutOfBoundsException and
87 // returns false.
Mathieu Chartier4e305412014-02-19 10:54:44 -080088 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070089 bool CheckIsValidIndex(int32_t index) ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080090
91 protected:
Ian Rogersef7d42f2014-01-06 12:55:46 -080092 void ThrowArrayStoreException(Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080093
94 private:
Ian Rogersef7d42f2014-01-06 12:55:46 -080095 void ThrowArrayIndexOutOfBoundsException(int32_t index)
Sebastien Hertzabff6432014-01-27 18:01:39 +010096 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
97
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080098 // 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
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700106template<typename T>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800107class 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
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700114 const T* GetData() const ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800115 return reinterpret_cast<const T*>(GetRawData(sizeof(T), 0));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800116 }
117
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700118 T* GetData() ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800119 return reinterpret_cast<T*>(GetRawData(sizeof(T), 0));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800120 }
121
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700122 T Get(int32_t i) ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertzabff6432014-01-27 18:01:39 +0100123
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700124 T GetWithoutChecks(int32_t i) ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertzabff6432014-01-27 18:01:39 +0100125 DCHECK(CheckIsValidIndex(i));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800126 return GetData()[i];
127 }
128
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700129 void Set(int32_t i, T value) ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100130
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>
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700134 void Set(int32_t i, T value) ALWAYS_INLINE NO_THREAD_SAFETY_ANALYSIS;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800135
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100136 // 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>
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700139 void SetWithoutChecks(int32_t i, T value) ALWAYS_INLINE NO_THREAD_SAFETY_ANALYSIS;
Sebastien Hertzabff6432014-01-27 18:01:39 +0100140
Ian Rogersef7d42f2014-01-06 12:55:46 -0800141 /*
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
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800157 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
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800168 static void VisitRoots(RootCallback* callback, void* arg)
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800169 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
170
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800171 private:
172 static Class* array_class_;
173
174 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
175};
176
177} // namespace mirror
178} // namespace art
179
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700180#endif // ART_RUNTIME_MIRROR_ARRAY_H_