blob: d44f75fbe1448cd1a3b9d2497d513be8b8df02d3 [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_INL_H_
18#define ART_RUNTIME_MIRROR_ARRAY_INL_H_
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080019
20#include "array.h"
21
22#include "class.h"
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070023#include "gc/heap-inl.h"
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070024#include "thread.h"
25#include "utils.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026
27namespace art {
28namespace mirror {
29
Ian Rogers6fac4472014-02-25 17:01:10 -080030static inline size_t HeaderSize(size_t component_size) {
31 return sizeof(Object) + (component_size == sizeof(int64_t) ? 8 : 4);
32}
33
Mathieu Chartier4e305412014-02-19 10:54:44 -080034template<VerifyObjectFlags kVerifyFlags>
Ian Rogersef7d42f2014-01-06 12:55:46 -080035inline size_t Array::SizeOf() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036 // This is safe from overflow because the array was already allocated, so we know it's sane.
Mathieu Chartier4e305412014-02-19 10:54:44 -080037 size_t component_size = GetClass<kVerifyFlags>()->GetComponentSize();
38 // Don't need to check this since we already check this in GetClass.
39 int32_t component_count =
40 GetLength<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>();
Ian Rogers6fac4472014-02-25 17:01:10 -080041 size_t header_size = HeaderSize(component_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080042 size_t data_size = component_count * component_size;
43 return header_size + data_size;
44}
45
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070046static inline size_t ComputeArraySize(Thread* self, Class* array_class, int32_t component_count,
47 size_t component_size)
48 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070049 DCHECK(array_class != NULL);
50 DCHECK_GE(component_count, 0);
51 DCHECK(array_class->IsArrayClass());
52
Ian Rogers6fac4472014-02-25 17:01:10 -080053 size_t header_size = HeaderSize(component_size);
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070054 size_t data_size = component_count * component_size;
55 size_t size = header_size + data_size;
56
57 // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
58 size_t component_shift = sizeof(size_t) * 8 - 1 - CLZ(component_size);
59 if (UNLIKELY(data_size >> component_shift != size_t(component_count) || size < data_size)) {
60 self->ThrowOutOfMemoryError(StringPrintf("%s of length %d would overflow",
61 PrettyDescriptor(array_class).c_str(),
62 component_count).c_str());
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070063 return 0; // failure
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070064 }
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070065 return size;
66}
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070067
Ian Rogers6fac4472014-02-25 17:01:10 -080068// Used for setting the array length in the allocation code path to ensure it is guarded by a
69// StoreStore fence.
Mathieu Chartier1febddf2013-11-20 12:33:14 -080070class SetLengthVisitor {
71 public:
72 explicit SetLengthVisitor(int32_t length) : length_(length) {
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070073 }
Mathieu Chartier1febddf2013-11-20 12:33:14 -080074
Ian Rogers6fac4472014-02-25 17:01:10 -080075 void operator()(Object* obj, size_t usable_size) const
76 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
77 UNUSED(usable_size);
Ian Rogersef7d42f2014-01-06 12:55:46 -080078 // Avoid AsArray as object is not yet in live bitmap or allocation stack.
79 Array* array = down_cast<Array*>(obj);
80 // DCHECK(array->IsArrayInstance());
Mathieu Chartier1febddf2013-11-20 12:33:14 -080081 array->SetLength(length_);
82 }
83
84 private:
85 const int32_t length_;
Ian Rogers6fac4472014-02-25 17:01:10 -080086
87 DISALLOW_COPY_AND_ASSIGN(SetLengthVisitor);
88};
89
90// Similar to SetLengthVisitor, used for setting the array length to fill the usable size of an
91// array.
92class SetLengthToUsableSizeVisitor {
93 public:
94 SetLengthToUsableSizeVisitor(size_t header_size, size_t component_size) :
95 header_size_(header_size), component_size_(component_size) {
96 }
97
98 void operator()(Object* obj, size_t usable_size) const
99 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
100 // Avoid AsArray as object is not yet in live bitmap or allocation stack.
101 Array* array = down_cast<Array*>(obj);
102 uint32_t length = (usable_size - header_size_) / component_size_;
103 // DCHECK(array->IsArrayInstance());
104 array->SetLength(length);
105 }
106
107 private:
108 const size_t header_size_;
109 const size_t component_size_;
110
111 DISALLOW_COPY_AND_ASSIGN(SetLengthToUsableSizeVisitor);
Mathieu Chartier1febddf2013-11-20 12:33:14 -0800112};
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700113
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800114template <bool kIsInstrumented>
Mathieu Chartier590fee92013-09-13 13:46:47 -0700115inline Array* Array::Alloc(Thread* self, Class* array_class, int32_t component_count,
Ian Rogers6fac4472014-02-25 17:01:10 -0800116 size_t component_size, gc::AllocatorType allocator_type,
117 bool fill_usable) {
118 DCHECK(allocator_type != gc::kAllocatorTypeLOS);
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700119 size_t size = ComputeArraySize(self, array_class, component_count, component_size);
120 if (UNLIKELY(size == 0)) {
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800121 return nullptr;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700122 }
123 gc::Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers6fac4472014-02-25 17:01:10 -0800124 Array* result;
125 if (!fill_usable) {
126 SetLengthVisitor visitor(component_count);
127 result = down_cast<Array*>(
128 heap->AllocObjectWithAllocator<kIsInstrumented, true>(self, array_class, size,
129 allocator_type, visitor));
130 } else {
131 SetLengthToUsableSizeVisitor visitor(HeaderSize(component_size), component_size);
132 result = down_cast<Array*>(
133 heap->AllocObjectWithAllocator<kIsInstrumented, true>(self, array_class, size,
134 allocator_type, visitor));
135 }
136 if (kIsDebugBuild && result != nullptr && Runtime::Current()->IsStarted()) {
137 if (!fill_usable) {
138 CHECK_EQ(result->SizeOf(), size);
139 } else {
140 CHECK_GE(result->SizeOf(), size);
141 }
142 }
143 return result;
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700144}
145
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800146template<class T>
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800147inline void PrimitiveArray<T>::VisitRoots(RootCallback* callback, void* arg) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800148 if (array_class_ != nullptr) {
Mathieu Chartier815873e2014-02-13 18:02:13 -0800149 callback(reinterpret_cast<mirror::Object**>(&array_class_), arg, 0, kRootStickyClass);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800150 }
151}
152
Ian Rogersef7d42f2014-01-06 12:55:46 -0800153// Similar to memmove except elements are of aligned appropriately for T, count is in T sized units
154// copies are guaranteed not to tear when T is less-than 64bit.
155template<typename T>
156static inline void ArrayBackwardCopy(T* d, const T* s, int32_t count) {
157 d += count;
158 s += count;
159 for (int32_t i = 0; i < count; ++i) {
160 d--;
161 s--;
162 *d = *s;
163 }
164}
165
Ian Rogers6fac4472014-02-25 17:01:10 -0800166template<typename T>
167inline PrimitiveArray<T>* PrimitiveArray<T>::Alloc(Thread* self, size_t length) {
168 DCHECK(array_class_ != NULL);
169 Array* raw_array = Array::Alloc<true>(self, array_class_, length, sizeof(T),
170 Runtime::Current()->GetHeap()->GetCurrentAllocator());
171 return down_cast<PrimitiveArray<T>*>(raw_array);
172}
173
Ian Rogersef7d42f2014-01-06 12:55:46 -0800174template<class T>
Ian Rogers6fac4472014-02-25 17:01:10 -0800175inline void PrimitiveArray<T>::Memmove(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos,
176 int32_t count) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800177 if (UNLIKELY(count == 0)) {
178 return;
179 }
180 DCHECK_GE(dst_pos, 0);
181 DCHECK_GE(src_pos, 0);
182 DCHECK_GT(count, 0);
183 DCHECK(src != nullptr);
184 DCHECK_LT(dst_pos, GetLength());
185 DCHECK_LE(dst_pos, GetLength() - count);
186 DCHECK_LT(src_pos, src->GetLength());
187 DCHECK_LE(src_pos, src->GetLength() - count);
188
189 // Note for non-byte copies we can't rely on standard libc functions like memcpy(3) and memmove(3)
190 // in our implementation, because they may copy byte-by-byte.
191 if (LIKELY(src != this) || (dst_pos < src_pos) || (dst_pos - src_pos >= count)) {
192 // Forward copy ok.
193 Memcpy(dst_pos, src, src_pos, count);
194 } else {
195 // Backward copy necessary.
196 void* dst_raw = GetRawData(sizeof(T), dst_pos);
197 const void* src_raw = src->GetRawData(sizeof(T), src_pos);
198 if (sizeof(T) == sizeof(uint8_t)) {
199 // TUNING: use memmove here?
200 uint8_t* d = reinterpret_cast<uint8_t*>(dst_raw);
201 const uint8_t* s = reinterpret_cast<const uint8_t*>(src_raw);
202 ArrayBackwardCopy<uint8_t>(d, s, count);
203 } else if (sizeof(T) == sizeof(uint16_t)) {
204 uint16_t* d = reinterpret_cast<uint16_t*>(dst_raw);
205 const uint16_t* s = reinterpret_cast<const uint16_t*>(src_raw);
206 ArrayBackwardCopy<uint16_t>(d, s, count);
207 } else if (sizeof(T) == sizeof(uint32_t)) {
208 uint32_t* d = reinterpret_cast<uint32_t*>(dst_raw);
209 const uint32_t* s = reinterpret_cast<const uint32_t*>(src_raw);
210 ArrayBackwardCopy<uint32_t>(d, s, count);
211 } else {
212 DCHECK_EQ(sizeof(T), sizeof(uint64_t));
213 uint64_t* d = reinterpret_cast<uint64_t*>(dst_raw);
214 const uint64_t* s = reinterpret_cast<const uint64_t*>(src_raw);
215 ArrayBackwardCopy<uint64_t>(d, s, count);
216 }
217 }
218}
219
220// Similar to memcpy except elements are of aligned appropriately for T, count is in T sized units
221// copies are guaranteed not to tear when T is less-than 64bit.
222template<typename T>
223static inline void ArrayForwardCopy(T* d, const T* s, int32_t count) {
224 for (int32_t i = 0; i < count; ++i) {
225 *d = *s;
226 d++;
227 s++;
228 }
229}
230
231
232template<class T>
Ian Rogers6fac4472014-02-25 17:01:10 -0800233inline void PrimitiveArray<T>::Memcpy(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos,
234 int32_t count) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800235 if (UNLIKELY(count == 0)) {
236 return;
237 }
238 DCHECK_GE(dst_pos, 0);
239 DCHECK_GE(src_pos, 0);
240 DCHECK_GT(count, 0);
241 DCHECK(src != nullptr);
242 DCHECK_LT(dst_pos, GetLength());
243 DCHECK_LE(dst_pos, GetLength() - count);
244 DCHECK_LT(src_pos, src->GetLength());
245 DCHECK_LE(src_pos, src->GetLength() - count);
246
247 // Note for non-byte copies we can't rely on standard libc functions like memcpy(3) and memmove(3)
248 // in our implementation, because they may copy byte-by-byte.
249 void* dst_raw = GetRawData(sizeof(T), dst_pos);
250 const void* src_raw = src->GetRawData(sizeof(T), src_pos);
251 if (sizeof(T) == sizeof(uint8_t)) {
252 memcpy(dst_raw, src_raw, count);
253 } else if (sizeof(T) == sizeof(uint16_t)) {
254 uint16_t* d = reinterpret_cast<uint16_t*>(dst_raw);
255 const uint16_t* s = reinterpret_cast<const uint16_t*>(src_raw);
256 ArrayForwardCopy<uint16_t>(d, s, count);
257 } else if (sizeof(T) == sizeof(uint32_t)) {
258 uint32_t* d = reinterpret_cast<uint32_t*>(dst_raw);
259 const uint32_t* s = reinterpret_cast<const uint32_t*>(src_raw);
260 ArrayForwardCopy<uint32_t>(d, s, count);
261 } else {
262 DCHECK_EQ(sizeof(T), sizeof(uint64_t));
263 uint64_t* d = reinterpret_cast<uint64_t*>(dst_raw);
264 const uint64_t* s = reinterpret_cast<const uint64_t*>(src_raw);
265 ArrayForwardCopy<uint64_t>(d, s, count);
266 }
267}
268
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800269} // namespace mirror
270} // namespace art
271
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700272#endif // ART_RUNTIME_MIRROR_ARRAY_INL_H_