blob: 7af88d6d8698a409fd654cc49eda5ab17fcfaace [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
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070020#include "gc_root.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070021#include "gc/allocator_type.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#include "object.h"
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080023#include "object_callbacks.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:
Mingyao Yang98d1cc82014-05-15 17:02:16 -070033 // The size of a java.lang.Class representing an array.
34 static uint32_t ClassSize();
35
Ian Rogers6fac4472014-02-25 17:01:10 -080036 // 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.
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080039 template <bool kIsInstrumented>
40 static Array* Alloc(Thread* self, Class* array_class, int32_t component_count,
Ian Rogers6fac4472014-02-25 17:01:10 -080041 size_t component_size, gc::AllocatorType allocator_type,
42 bool fill_usable = false)
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080043 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080044
Mathieu Chartier0cd81352014-05-22 16:48:55 -070045 static Array* CreateMultiArray(Thread* self, Handle<Class> element_class,
46 Handle<IntArray> dimensions)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080047 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
48
Hiroshi Yamauchi6e83c172014-05-01 21:25:41 -070049 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
50 ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Ian Rogersef7d42f2014-01-06 12:55:46 -080051 size_t SizeOf() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier4e305412014-02-19 10:54:44 -080052 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Ian Rogersef7d42f2014-01-06 12:55:46 -080053 int32_t GetLength() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070054 return GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Array, length_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080055 }
56
Ian Rogersef7d42f2014-01-06 12:55:46 -080057 void SetLength(int32_t length) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080058 CHECK_GE(length, 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010059 // We use non transactional version since we can't undo this write. We also disable checking
60 // since it would fail during a transaction.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070061 SetField32<false, false, kVerifyNone>(OFFSET_OF_OBJECT_MEMBER(Array, length_), length);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080062 }
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
Ian Rogersef7d42f2014-01-06 12:55:46 -080077 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);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080081 return reinterpret_cast<void*>(data);
82 }
83
Ian Rogersef7d42f2014-01-06 12:55:46 -080084 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);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080088 }
89
Sebastien Hertzabff6432014-01-27 18:01:39 +010090 // Returns true if the index is valid. If not, throws an ArrayIndexOutOfBoundsException and
91 // returns false.
Mathieu Chartier4e305412014-02-19 10:54:44 -080092 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070093 bool CheckIsValidIndex(int32_t index) ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080094
95 protected:
Ian Rogersef7d42f2014-01-06 12:55:46 -080096 void ThrowArrayStoreException(Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080097
98 private:
Ian Rogersef7d42f2014-01-06 12:55:46 -080099 void ThrowArrayIndexOutOfBoundsException(int32_t index)
Sebastien Hertzabff6432014-01-27 18:01:39 +0100100 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
101
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800102 // 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
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700110template<typename T>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800111class 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
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700118 const T* GetData() const ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800119 return reinterpret_cast<const T*>(GetRawData(sizeof(T), 0));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800120 }
121
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700122 T* GetData() ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800123 return reinterpret_cast<T*>(GetRawData(sizeof(T), 0));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800124 }
125
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700126 T Get(int32_t i) ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertzabff6432014-01-27 18:01:39 +0100127
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700128 T GetWithoutChecks(int32_t i) ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertzabff6432014-01-27 18:01:39 +0100129 DCHECK(CheckIsValidIndex(i));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800130 return GetData()[i];
131 }
132
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700133 void Set(int32_t i, T value) ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100134
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>
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700138 void Set(int32_t i, T value) ALWAYS_INLINE NO_THREAD_SAFETY_ANALYSIS;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800139
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100140 // 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>
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700143 void SetWithoutChecks(int32_t i, T value) ALWAYS_INLINE NO_THREAD_SAFETY_ANALYSIS;
Sebastien Hertzabff6432014-01-27 18:01:39 +0100144
Ian Rogersef7d42f2014-01-06 12:55:46 -0800145 /*
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
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800161 static void SetArrayClass(Class* array_class) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700162 CHECK(array_class_.IsNull());
Ian Rogers2d10b202014-05-12 19:15:18 -0700163 CHECK(array_class != nullptr);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700164 array_class_ = GcRoot<Class>(array_class);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800165 }
166
Hiroshi Yamauchi4f1ebc22014-06-25 14:30:41 -0700167 static Class* GetArrayClass() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700168 DCHECK(!array_class_.IsNull());
169 return array_class_.Read();
Ian Rogers2d10b202014-05-12 19:15:18 -0700170 }
171
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800172 static void ResetArrayClass() {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700173 CHECK(!array_class_.IsNull());
174 array_class_ = GcRoot<Class>(nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800175 }
176
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800177 static void VisitRoots(RootCallback* callback, void* arg)
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800178 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
179
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800180 private:
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700181 static GcRoot<Class> array_class_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800182
183 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
184};
185
186} // namespace mirror
187} // namespace art
188
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700189#endif // ART_RUNTIME_MIRROR_ARRAY_H_