blob: 33c0aeb152639f0153e1f9a5313474ad9f9176e9 [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
17#ifndef ART_SRC_MIRROR_ARRAY_H_
18#define ART_SRC_MIRROR_ARRAY_H_
19
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)
30 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
31
32 static Array* Alloc(Thread* self, Class* array_class, int32_t component_count,
33 size_t component_size)
34 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
35
36 static Array* CreateMultiArray(Thread* self, Class* element_class, IntArray* dimensions)
37 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
38
39 size_t SizeOf() const;
40
41 int32_t GetLength() const {
42 return GetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), false);
43 }
44
45 void SetLength(int32_t length) {
46 CHECK_GE(length, 0);
47 SetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), length, false);
48 }
49
50 static MemberOffset LengthOffset() {
51 return OFFSET_OF_OBJECT_MEMBER(Array, length_);
52 }
53
54 static MemberOffset DataOffset(size_t component_size) {
55 if (component_size != sizeof(int64_t)) {
56 return OFFSET_OF_OBJECT_MEMBER(Array, first_element_);
57 } else {
58 // Align longs and doubles.
59 return MemberOffset(OFFSETOF_MEMBER(Array, first_element_) + 4);
60 }
61 }
62
63 void* GetRawData(size_t component_size) {
64 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value();
65 return reinterpret_cast<void*>(data);
66 }
67
68 const void* GetRawData(size_t component_size) const {
69 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value();
70 return reinterpret_cast<const void*>(data);
71 }
72
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080073 bool IsValidIndex(int32_t index) const
74 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
75 if (UNLIKELY(index < 0 || index >= GetLength())) {
76 return ThrowArrayIndexOutOfBoundsException(index);
77 }
78 return true;
79 }
80
81 protected:
82 bool ThrowArrayIndexOutOfBoundsException(int32_t index) const
83 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
84 bool ThrowArrayStoreException(Object* object) const
85 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
86
87 private:
88 // The number of array elements.
89 int32_t length_;
90 // Marker for the data (used by generated code)
91 uint32_t first_element_[0];
92
93 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
94};
95
96template<class T>
97class MANAGED PrimitiveArray : public Array {
98 public:
99 typedef T ElementType;
100
101 static PrimitiveArray<T>* Alloc(Thread* self, size_t length)
102 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
103
104 const T* GetData() const {
105 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(sizeof(T)).Int32Value();
106 return reinterpret_cast<T*>(data);
107 }
108
109 T* GetData() {
110 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(sizeof(T)).Int32Value();
111 return reinterpret_cast<T*>(data);
112 }
113
114 T Get(int32_t i) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
115 if (!IsValidIndex(i)) {
116 return T(0);
117 }
118 return GetData()[i];
119 }
120
121 void Set(int32_t i, T value) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
122 if (IsValidIndex(i)) {
123 GetData()[i] = value;
124 }
125 }
126
127 static void SetArrayClass(Class* array_class) {
128 CHECK(array_class_ == NULL);
129 CHECK(array_class != NULL);
130 array_class_ = array_class;
131 }
132
133 static void ResetArrayClass() {
134 CHECK(array_class_ != NULL);
135 array_class_ = NULL;
136 }
137
138 private:
139 static Class* array_class_;
140
141 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
142};
143
144} // namespace mirror
145} // namespace art
146
147#endif // ART_SRC_MIRROR_ARRAY_H_