blob: 570dcaa29265cdc6361aa0276347a625962a8f9b [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
20#include "object.h"
21
22namespace art {
23namespace mirror {
24
25class MANAGED Array : public Object {
26 public:
27 // A convenience for code that doesn't know the component size,
28 // and doesn't want to have to work it out itself.
29 static Array* Alloc(Thread* self, Class* array_class, int32_t component_count)
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070030 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
31 return AllocInstrumented(self, array_class, component_count);
32 }
33 static Array* AllocUninstrumented(Thread* self, Class* array_class, int32_t component_count)
34 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
35 static Array* AllocInstrumented(Thread* self, Class* array_class, int32_t component_count)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
37
38 static Array* Alloc(Thread* self, Class* array_class, int32_t component_count,
39 size_t component_size)
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070040 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
41 return AllocInstrumented(self, array_class, component_count, component_size);
42 }
43 static Array* AllocUninstrumented(Thread* self, Class* array_class, int32_t component_count,
44 size_t component_size)
45 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
46 static Array* AllocInstrumented(Thread* self, Class* array_class, int32_t component_count,
47 size_t component_size)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
49
50 static Array* CreateMultiArray(Thread* self, Class* element_class, IntArray* dimensions)
51 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
52
53 size_t SizeOf() const;
54
55 int32_t GetLength() const {
56 return GetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), false);
57 }
58
59 void SetLength(int32_t length) {
60 CHECK_GE(length, 0);
61 SetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), length, false);
62 }
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
77 void* GetRawData(size_t component_size) {
78 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value();
79 return reinterpret_cast<void*>(data);
80 }
81
82 const void* GetRawData(size_t component_size) const {
83 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value();
84 return reinterpret_cast<const void*>(data);
85 }
86
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080087 bool IsValidIndex(int32_t index) const
88 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz66d1aee2013-07-23 11:59:01 +020089 if (UNLIKELY(static_cast<uint32_t>(index) >= static_cast<uint32_t>(GetLength()))) {
Sebastien Hertz9897be92013-06-27 18:24:46 +020090 ThrowArrayIndexOutOfBoundsException(index);
91 return false;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080092 }
93 return true;
94 }
95
96 protected:
Sebastien Hertz9897be92013-06-27 18:24:46 +020097 void ThrowArrayIndexOutOfBoundsException(int32_t index) const
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080098 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertz9897be92013-06-27 18:24:46 +020099 void ThrowArrayStoreException(Object* object) const
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800100 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
101
102 private:
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 {
120 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(sizeof(T)).Int32Value();
121 return reinterpret_cast<T*>(data);
122 }
123
124 T* GetData() {
125 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(sizeof(T)).Int32Value();
126 return reinterpret_cast<T*>(data);
127 }
128
129 T Get(int32_t i) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
130 if (!IsValidIndex(i)) {
131 return T(0);
132 }
133 return GetData()[i];
134 }
135
136 void Set(int32_t i, T value) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
137 if (IsValidIndex(i)) {
138 GetData()[i] = value;
139 }
140 }
141
142 static void SetArrayClass(Class* array_class) {
143 CHECK(array_class_ == NULL);
144 CHECK(array_class != NULL);
145 array_class_ = array_class;
146 }
147
148 static void ResetArrayClass() {
149 CHECK(array_class_ != NULL);
150 array_class_ = NULL;
151 }
152
153 private:
154 static Class* array_class_;
155
156 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
157};
158
159} // namespace mirror
160} // namespace art
161
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700162#endif // ART_RUNTIME_MIRROR_ARRAY_H_