blob: 64452452d9dd8db2406a8f9b05e9bafd3e5d3863 [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
Mingyao Yang98d1cc82014-05-15 17:02:16 -070030inline uint32_t Array::ClassSize() {
31 uint32_t vtable_entries = Object::kVTableLength;
32 return Class::ComputeClassSize(true, vtable_entries, 0, 0, 0);
33}
34
Hiroshi Yamauchi6e83c172014-05-01 21:25:41 -070035template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption>
Ian Rogersef7d42f2014-01-06 12:55:46 -080036inline size_t Array::SizeOf() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037 // This is safe from overflow because the array was already allocated, so we know it's sane.
Hiroshi Yamauchi9103c862014-04-22 13:51:07 -070038 size_t component_size =
Hiroshi Yamauchi6e83c172014-05-01 21:25:41 -070039 GetClass<kVerifyFlags, kReadBarrierOption>()->template GetComponentSize<kReadBarrierOption>();
Mathieu Chartier4e305412014-02-19 10:54:44 -080040 // Don't need to check this since we already check this in GetClass.
41 int32_t component_count =
42 GetLength<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>();
Hiroshi Yamauchiaa866f52014-03-21 16:18:30 -070043 size_t header_size = DataOffset(component_size).SizeValue();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080044 size_t data_size = component_count * component_size;
45 return header_size + data_size;
46}
47
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070048template<VerifyObjectFlags kVerifyFlags>
49inline bool Array::CheckIsValidIndex(int32_t index) {
50 if (UNLIKELY(static_cast<uint32_t>(index) >=
51 static_cast<uint32_t>(GetLength<kVerifyFlags>()))) {
52 ThrowArrayIndexOutOfBoundsException(index);
53 return false;
54 }
55 return true;
56}
57
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070058static inline size_t ComputeArraySize(Thread* self, Class* array_class, int32_t component_count,
59 size_t component_size)
60 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070061 DCHECK(array_class != NULL);
62 DCHECK_GE(component_count, 0);
63 DCHECK(array_class->IsArrayClass());
64
Hiroshi Yamauchiaa866f52014-03-21 16:18:30 -070065 size_t header_size = Array::DataOffset(component_size).SizeValue();
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070066 size_t data_size = component_count * component_size;
67 size_t size = header_size + data_size;
68
69 // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
70 size_t component_shift = sizeof(size_t) * 8 - 1 - CLZ(component_size);
71 if (UNLIKELY(data_size >> component_shift != size_t(component_count) || size < data_size)) {
72 self->ThrowOutOfMemoryError(StringPrintf("%s of length %d would overflow",
73 PrettyDescriptor(array_class).c_str(),
74 component_count).c_str());
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070075 return 0; // failure
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070076 }
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070077 return size;
78}
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070079
Ian Rogers6fac4472014-02-25 17:01:10 -080080// Used for setting the array length in the allocation code path to ensure it is guarded by a
81// StoreStore fence.
Mathieu Chartier1febddf2013-11-20 12:33:14 -080082class SetLengthVisitor {
83 public:
84 explicit SetLengthVisitor(int32_t length) : length_(length) {
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070085 }
Mathieu Chartier1febddf2013-11-20 12:33:14 -080086
Ian Rogers6fac4472014-02-25 17:01:10 -080087 void operator()(Object* obj, size_t usable_size) const
88 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
89 UNUSED(usable_size);
Ian Rogersef7d42f2014-01-06 12:55:46 -080090 // Avoid AsArray as object is not yet in live bitmap or allocation stack.
91 Array* array = down_cast<Array*>(obj);
92 // DCHECK(array->IsArrayInstance());
Mathieu Chartier1febddf2013-11-20 12:33:14 -080093 array->SetLength(length_);
94 }
95
96 private:
97 const int32_t length_;
Ian Rogers6fac4472014-02-25 17:01:10 -080098
99 DISALLOW_COPY_AND_ASSIGN(SetLengthVisitor);
100};
101
102// Similar to SetLengthVisitor, used for setting the array length to fill the usable size of an
103// array.
104class SetLengthToUsableSizeVisitor {
105 public:
Ian Rogersa55cf412014-02-27 00:31:26 -0800106 SetLengthToUsableSizeVisitor(int32_t min_length, size_t header_size, size_t component_size) :
107 minimum_length_(min_length), header_size_(header_size), component_size_(component_size) {
Ian Rogers6fac4472014-02-25 17:01:10 -0800108 }
109
110 void operator()(Object* obj, size_t usable_size) const
111 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
112 // Avoid AsArray as object is not yet in live bitmap or allocation stack.
113 Array* array = down_cast<Array*>(obj);
Ian Rogers6fac4472014-02-25 17:01:10 -0800114 // DCHECK(array->IsArrayInstance());
Ian Rogersa55cf412014-02-27 00:31:26 -0800115 int32_t length = (usable_size - header_size_) / component_size_;
116 DCHECK_GE(length, minimum_length_);
117 byte* old_end = reinterpret_cast<byte*>(array->GetRawData(component_size_, minimum_length_));
118 byte* new_end = reinterpret_cast<byte*>(array->GetRawData(component_size_, length));
119 // Ensure space beyond original allocation is zeroed.
120 memset(old_end, 0, new_end - old_end);
Ian Rogers6fac4472014-02-25 17:01:10 -0800121 array->SetLength(length);
122 }
123
124 private:
Ian Rogersa55cf412014-02-27 00:31:26 -0800125 const int32_t minimum_length_;
Ian Rogers6fac4472014-02-25 17:01:10 -0800126 const size_t header_size_;
127 const size_t component_size_;
128
129 DISALLOW_COPY_AND_ASSIGN(SetLengthToUsableSizeVisitor);
Mathieu Chartier1febddf2013-11-20 12:33:14 -0800130};
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700131
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800132template <bool kIsInstrumented>
Mathieu Chartier590fee92013-09-13 13:46:47 -0700133inline Array* Array::Alloc(Thread* self, Class* array_class, int32_t component_count,
Ian Rogers6fac4472014-02-25 17:01:10 -0800134 size_t component_size, gc::AllocatorType allocator_type,
135 bool fill_usable) {
136 DCHECK(allocator_type != gc::kAllocatorTypeLOS);
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700137 size_t size = ComputeArraySize(self, array_class, component_count, component_size);
138 if (UNLIKELY(size == 0)) {
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800139 return nullptr;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700140 }
141 gc::Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers6fac4472014-02-25 17:01:10 -0800142 Array* result;
143 if (!fill_usable) {
144 SetLengthVisitor visitor(component_count);
145 result = down_cast<Array*>(
146 heap->AllocObjectWithAllocator<kIsInstrumented, true>(self, array_class, size,
147 allocator_type, visitor));
148 } else {
Hiroshi Yamauchiaa866f52014-03-21 16:18:30 -0700149 SetLengthToUsableSizeVisitor visitor(component_count, DataOffset(component_size).SizeValue(),
Ian Rogersa55cf412014-02-27 00:31:26 -0800150 component_size);
Ian Rogers6fac4472014-02-25 17:01:10 -0800151 result = down_cast<Array*>(
152 heap->AllocObjectWithAllocator<kIsInstrumented, true>(self, array_class, size,
153 allocator_type, visitor));
154 }
155 if (kIsDebugBuild && result != nullptr && Runtime::Current()->IsStarted()) {
Mathieu Chartier85801542014-02-27 18:06:26 -0800156 array_class = result->GetClass(); // In case the array class moved.
Ian Rogersa55cf412014-02-27 00:31:26 -0800157 CHECK_EQ(array_class->GetComponentSize(), component_size);
Ian Rogers6fac4472014-02-25 17:01:10 -0800158 if (!fill_usable) {
159 CHECK_EQ(result->SizeOf(), size);
160 } else {
161 CHECK_GE(result->SizeOf(), size);
162 }
163 }
164 return result;
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700165}
166
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800167template<class T>
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800168inline void PrimitiveArray<T>::VisitRoots(RootCallback* callback, void* arg) {
Mathieu Chartier12f74232015-01-14 14:55:47 -0800169 array_class_.VisitRootIfNonNull(callback, arg, RootInfo(kRootStickyClass));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800170}
171
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700172template<typename T>
173inline PrimitiveArray<T>* PrimitiveArray<T>::Alloc(Thread* self, size_t length) {
Hiroshi Yamauchi4f1ebc22014-06-25 14:30:41 -0700174 Array* raw_array = Array::Alloc<true>(self, GetArrayClass(), length, sizeof(T),
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700175 Runtime::Current()->GetHeap()->GetCurrentAllocator());
176 return down_cast<PrimitiveArray<T>*>(raw_array);
177}
178
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700179template<typename T>
180inline T PrimitiveArray<T>::Get(int32_t i) {
181 if (!CheckIsValidIndex(i)) {
182 DCHECK(Thread::Current()->IsExceptionPending());
183 return T(0);
184 }
185 return GetWithoutChecks(i);
186}
187
188template<typename T>
189inline void PrimitiveArray<T>::Set(int32_t i, T value) {
190 if (Runtime::Current()->IsActiveTransaction()) {
191 Set<true>(i, value);
192 } else {
193 Set<false>(i, value);
194 }
195}
196
197template<typename T>
198template<bool kTransactionActive, bool kCheckTransaction>
199inline void PrimitiveArray<T>::Set(int32_t i, T value) {
200 if (CheckIsValidIndex(i)) {
201 SetWithoutChecks<kTransactionActive, kCheckTransaction>(i, value);
202 } else {
203 DCHECK(Thread::Current()->IsExceptionPending());
204 }
205}
206
207template<typename T>
208template<bool kTransactionActive, bool kCheckTransaction>
209inline void PrimitiveArray<T>::SetWithoutChecks(int32_t i, T value) {
210 if (kCheckTransaction) {
211 DCHECK_EQ(kTransactionActive, Runtime::Current()->IsActiveTransaction());
212 }
213 if (kTransactionActive) {
214 Runtime::Current()->RecordWriteArray(this, i, GetWithoutChecks(i));
215 }
216 DCHECK(CheckIsValidIndex(i));
217 GetData()[i] = value;
218}
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700219// Backward copy where elements are of aligned appropriately for T. Count is in T sized units.
220// Copies are guaranteed not to tear when the sizeof T is less-than 64bit.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800221template<typename T>
222static inline void ArrayBackwardCopy(T* d, const T* s, int32_t count) {
223 d += count;
224 s += count;
225 for (int32_t i = 0; i < count; ++i) {
226 d--;
227 s--;
228 *d = *s;
229 }
230}
231
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700232// Forward copy where elements are of aligned appropriately for T. Count is in T sized units.
233// Copies are guaranteed not to tear when the sizeof T is less-than 64bit.
Ian Rogers6fac4472014-02-25 17:01:10 -0800234template<typename T>
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700235static inline void ArrayForwardCopy(T* d, const T* s, int32_t count) {
236 for (int32_t i = 0; i < count; ++i) {
237 *d = *s;
238 d++;
239 s++;
240 }
Ian Rogers6fac4472014-02-25 17:01:10 -0800241}
242
Ian Rogersef7d42f2014-01-06 12:55:46 -0800243template<class T>
Ian Rogers6fac4472014-02-25 17:01:10 -0800244inline void PrimitiveArray<T>::Memmove(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos,
245 int32_t count) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800246 if (UNLIKELY(count == 0)) {
247 return;
248 }
249 DCHECK_GE(dst_pos, 0);
250 DCHECK_GE(src_pos, 0);
251 DCHECK_GT(count, 0);
252 DCHECK(src != nullptr);
253 DCHECK_LT(dst_pos, GetLength());
254 DCHECK_LE(dst_pos, GetLength() - count);
255 DCHECK_LT(src_pos, src->GetLength());
256 DCHECK_LE(src_pos, src->GetLength() - count);
257
258 // Note for non-byte copies we can't rely on standard libc functions like memcpy(3) and memmove(3)
259 // in our implementation, because they may copy byte-by-byte.
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700260 if (LIKELY(src != this)) {
261 // Memcpy ok for guaranteed non-overlapping distinct arrays.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800262 Memcpy(dst_pos, src, src_pos, count);
263 } else {
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700264 // Handle copies within the same array using the appropriate direction copy.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800265 void* dst_raw = GetRawData(sizeof(T), dst_pos);
266 const void* src_raw = src->GetRawData(sizeof(T), src_pos);
267 if (sizeof(T) == sizeof(uint8_t)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800268 uint8_t* d = reinterpret_cast<uint8_t*>(dst_raw);
269 const uint8_t* s = reinterpret_cast<const uint8_t*>(src_raw);
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700270 memmove(d, s, count);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800271 } else {
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700272 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= count);
273 if (sizeof(T) == sizeof(uint16_t)) {
274 uint16_t* d = reinterpret_cast<uint16_t*>(dst_raw);
275 const uint16_t* s = reinterpret_cast<const uint16_t*>(src_raw);
276 if (copy_forward) {
277 ArrayForwardCopy<uint16_t>(d, s, count);
278 } else {
279 ArrayBackwardCopy<uint16_t>(d, s, count);
280 }
281 } else if (sizeof(T) == sizeof(uint32_t)) {
282 uint32_t* d = reinterpret_cast<uint32_t*>(dst_raw);
283 const uint32_t* s = reinterpret_cast<const uint32_t*>(src_raw);
284 if (copy_forward) {
285 ArrayForwardCopy<uint32_t>(d, s, count);
286 } else {
287 ArrayBackwardCopy<uint32_t>(d, s, count);
288 }
289 } else {
290 DCHECK_EQ(sizeof(T), sizeof(uint64_t));
291 uint64_t* d = reinterpret_cast<uint64_t*>(dst_raw);
292 const uint64_t* s = reinterpret_cast<const uint64_t*>(src_raw);
293 if (copy_forward) {
294 ArrayForwardCopy<uint64_t>(d, s, count);
295 } else {
296 ArrayBackwardCopy<uint64_t>(d, s, count);
297 }
298 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800299 }
300 }
301}
302
Ian Rogersef7d42f2014-01-06 12:55:46 -0800303template<class T>
Ian Rogers6fac4472014-02-25 17:01:10 -0800304inline void PrimitiveArray<T>::Memcpy(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos,
305 int32_t count) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800306 if (UNLIKELY(count == 0)) {
307 return;
308 }
309 DCHECK_GE(dst_pos, 0);
310 DCHECK_GE(src_pos, 0);
311 DCHECK_GT(count, 0);
312 DCHECK(src != nullptr);
313 DCHECK_LT(dst_pos, GetLength());
314 DCHECK_LE(dst_pos, GetLength() - count);
315 DCHECK_LT(src_pos, src->GetLength());
316 DCHECK_LE(src_pos, src->GetLength() - count);
317
318 // Note for non-byte copies we can't rely on standard libc functions like memcpy(3) and memmove(3)
319 // in our implementation, because they may copy byte-by-byte.
320 void* dst_raw = GetRawData(sizeof(T), dst_pos);
321 const void* src_raw = src->GetRawData(sizeof(T), src_pos);
322 if (sizeof(T) == sizeof(uint8_t)) {
323 memcpy(dst_raw, src_raw, count);
324 } else if (sizeof(T) == sizeof(uint16_t)) {
325 uint16_t* d = reinterpret_cast<uint16_t*>(dst_raw);
326 const uint16_t* s = reinterpret_cast<const uint16_t*>(src_raw);
327 ArrayForwardCopy<uint16_t>(d, s, count);
328 } else if (sizeof(T) == sizeof(uint32_t)) {
329 uint32_t* d = reinterpret_cast<uint32_t*>(dst_raw);
330 const uint32_t* s = reinterpret_cast<const uint32_t*>(src_raw);
331 ArrayForwardCopy<uint32_t>(d, s, count);
332 } else {
333 DCHECK_EQ(sizeof(T), sizeof(uint64_t));
334 uint64_t* d = reinterpret_cast<uint64_t*>(dst_raw);
335 const uint64_t* s = reinterpret_cast<const uint64_t*>(src_raw);
336 ArrayForwardCopy<uint64_t>(d, s, count);
337 }
338}
339
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800340} // namespace mirror
341} // namespace art
342
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700343#endif // ART_RUNTIME_MIRROR_ARRAY_INL_H_