blob: 52659464fc2a6019d198d842d10d7fa5e800265a [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"
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080021#include "gc/heap.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022
23namespace art {
24namespace mirror {
25
26class MANAGED Array : public Object {
27 public:
Mathieu Chartier590fee92013-09-13 13:46:47 -070028 // A convenience for code that doesn't know the component size, and doesn't want to have to work
29 // it out itself.
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080030 template <bool kIsInstrumented>
31 static Array* Alloc(Thread* self, Class* array_class, int32_t component_count,
32 gc::AllocatorType allocator_type)
33 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
34
35 template <bool kIsInstrumented>
36 static Array* Alloc(Thread* self, Class* array_class, int32_t component_count,
37 size_t component_size, gc::AllocatorType allocator_type)
38 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
39
40 template <bool kIsInstrumented>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080041 static Array* Alloc(Thread* self, Class* array_class, int32_t component_count)
42 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
43
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080044 template <bool kIsInstrumented>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080045 static Array* Alloc(Thread* self, Class* array_class, int32_t component_count,
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080046 size_t component_size)
47 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048
49 static Array* CreateMultiArray(Thread* self, Class* element_class, IntArray* dimensions)
50 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
51
52 size_t SizeOf() const;
53
54 int32_t GetLength() const {
55 return GetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), false);
56 }
57
58 void SetLength(int32_t length) {
59 CHECK_GE(length, 0);
60 SetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), length, false);
61 }
62
63 static MemberOffset LengthOffset() {
64 return OFFSET_OF_OBJECT_MEMBER(Array, length_);
65 }
66
67 static MemberOffset DataOffset(size_t component_size) {
68 if (component_size != sizeof(int64_t)) {
69 return OFFSET_OF_OBJECT_MEMBER(Array, first_element_);
70 } else {
71 // Align longs and doubles.
72 return MemberOffset(OFFSETOF_MEMBER(Array, first_element_) + 4);
73 }
74 }
75
76 void* GetRawData(size_t component_size) {
77 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value();
78 return reinterpret_cast<void*>(data);
79 }
80
81 const void* GetRawData(size_t component_size) const {
82 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value();
83 return reinterpret_cast<const void*>(data);
84 }
85
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080086 bool IsValidIndex(int32_t index) const
87 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz66d1aee2013-07-23 11:59:01 +020088 if (UNLIKELY(static_cast<uint32_t>(index) >= static_cast<uint32_t>(GetLength()))) {
Sebastien Hertz9897be92013-06-27 18:24:46 +020089 ThrowArrayIndexOutOfBoundsException(index);
90 return false;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080091 }
92 return true;
93 }
94
95 protected:
Sebastien Hertz9897be92013-06-27 18:24:46 +020096 void ThrowArrayIndexOutOfBoundsException(int32_t index) const
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080097 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertz9897be92013-06-27 18:24:46 +020098 void ThrowArrayStoreException(Object* object) const
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080099 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
100
101 private:
102 // 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
110template<class T>
111class 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
118 const T* GetData() const {
119 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(sizeof(T)).Int32Value();
120 return reinterpret_cast<T*>(data);
121 }
122
123 T* GetData() {
124 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(sizeof(T)).Int32Value();
125 return reinterpret_cast<T*>(data);
126 }
127
128 T Get(int32_t i) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
129 if (!IsValidIndex(i)) {
130 return T(0);
131 }
132 return GetData()[i];
133 }
134
135 void Set(int32_t i, T value) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
136 if (IsValidIndex(i)) {
137 GetData()[i] = value;
138 }
139 }
140
141 static void SetArrayClass(Class* array_class) {
142 CHECK(array_class_ == NULL);
143 CHECK(array_class != NULL);
144 array_class_ = array_class;
145 }
146
147 static void ResetArrayClass() {
148 CHECK(array_class_ != NULL);
149 array_class_ = NULL;
150 }
151
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800152 static void VisitRoots(RootVisitor* visitor, void* arg)
153 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
154
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800155 private:
156 static Class* array_class_;
157
158 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
159};
160
161} // namespace mirror
162} // namespace art
163
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700164#endif // ART_RUNTIME_MIRROR_ARRAY_H_