blob: 584a4c095bb978fb66fb2b0e14fd294b16e69d4b [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:
Mathieu Chartier590fee92013-09-13 13:46:47 -070027 // A convenience for code that doesn't know the component size, and doesn't want to have to work
28 // it out itself.
29 template <bool kIsMovable, bool kIsInstrumented>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030 static Array* Alloc(Thread* self, Class* array_class, int32_t component_count)
31 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
32
Mathieu Chartier590fee92013-09-13 13:46:47 -070033 template <bool kIsMovable, bool kIsInstrumented>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034 static Array* Alloc(Thread* self, Class* array_class, int32_t component_count,
Mathieu Chartier590fee92013-09-13 13:46:47 -070035 size_t component_size) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036
37 static Array* CreateMultiArray(Thread* self, Class* element_class, IntArray* dimensions)
38 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
39
40 size_t SizeOf() const;
41
42 int32_t GetLength() const {
43 return GetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), false);
44 }
45
46 void SetLength(int32_t length) {
47 CHECK_GE(length, 0);
48 SetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), length, false);
49 }
50
51 static MemberOffset LengthOffset() {
52 return OFFSET_OF_OBJECT_MEMBER(Array, length_);
53 }
54
55 static MemberOffset DataOffset(size_t component_size) {
56 if (component_size != sizeof(int64_t)) {
57 return OFFSET_OF_OBJECT_MEMBER(Array, first_element_);
58 } else {
59 // Align longs and doubles.
60 return MemberOffset(OFFSETOF_MEMBER(Array, first_element_) + 4);
61 }
62 }
63
64 void* GetRawData(size_t component_size) {
65 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value();
66 return reinterpret_cast<void*>(data);
67 }
68
69 const void* GetRawData(size_t component_size) const {
70 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value();
71 return reinterpret_cast<const void*>(data);
72 }
73
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080074 bool IsValidIndex(int32_t index) const
75 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz66d1aee2013-07-23 11:59:01 +020076 if (UNLIKELY(static_cast<uint32_t>(index) >= static_cast<uint32_t>(GetLength()))) {
Sebastien Hertz9897be92013-06-27 18:24:46 +020077 ThrowArrayIndexOutOfBoundsException(index);
78 return false;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080079 }
80 return true;
81 }
82
83 protected:
Sebastien Hertz9897be92013-06-27 18:24:46 +020084 void ThrowArrayIndexOutOfBoundsException(int32_t index) const
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080085 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertz9897be92013-06-27 18:24:46 +020086 void ThrowArrayStoreException(Object* object) const
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080087 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
88
89 private:
90 // The number of array elements.
91 int32_t length_;
92 // Marker for the data (used by generated code)
93 uint32_t first_element_[0];
94
95 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
96};
97
98template<class T>
99class MANAGED PrimitiveArray : public Array {
100 public:
101 typedef T ElementType;
102
103 static PrimitiveArray<T>* Alloc(Thread* self, size_t length)
104 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
105
106 const T* GetData() const {
107 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(sizeof(T)).Int32Value();
108 return reinterpret_cast<T*>(data);
109 }
110
111 T* GetData() {
112 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(sizeof(T)).Int32Value();
113 return reinterpret_cast<T*>(data);
114 }
115
116 T Get(int32_t i) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
117 if (!IsValidIndex(i)) {
118 return T(0);
119 }
120 return GetData()[i];
121 }
122
123 void Set(int32_t i, T value) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
124 if (IsValidIndex(i)) {
125 GetData()[i] = value;
126 }
127 }
128
129 static void SetArrayClass(Class* array_class) {
130 CHECK(array_class_ == NULL);
131 CHECK(array_class != NULL);
132 array_class_ = array_class;
133 }
134
135 static void ResetArrayClass() {
136 CHECK(array_class_ != NULL);
137 array_class_ = NULL;
138 }
139
140 private:
141 static Class* array_class_;
142
143 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
144};
145
146} // namespace mirror
147} // namespace art
148
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700149#endif // ART_RUNTIME_MIRROR_ARRAY_H_