blob: 7f974d0cf0ad2695afa9e2ce01293dcd065e0e88 [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
Hiroshi Yamauchi9103c862014-04-22 13:51:07 -070030template<VerifyObjectFlags kVerifyFlags, bool kDoReadBarrier>
Ian Rogersef7d42f2014-01-06 12:55:46 -080031inline size_t Array::SizeOf() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080032 // This is safe from overflow because the array was already allocated, so we know it's sane.
Hiroshi Yamauchi9103c862014-04-22 13:51:07 -070033 size_t component_size =
34 GetClass<kVerifyFlags, kDoReadBarrier>()->template GetComponentSize<kDoReadBarrier>();
Mathieu Chartier4e305412014-02-19 10:54:44 -080035 // Don't need to check this since we already check this in GetClass.
36 int32_t component_count =
37 GetLength<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>();
Hiroshi Yamauchiaa866f52014-03-21 16:18:30 -070038 size_t header_size = DataOffset(component_size).SizeValue();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039 size_t data_size = component_count * component_size;
40 return header_size + data_size;
41}
42
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070043static inline size_t ComputeArraySize(Thread* self, Class* array_class, int32_t component_count,
44 size_t component_size)
45 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070046 DCHECK(array_class != NULL);
47 DCHECK_GE(component_count, 0);
48 DCHECK(array_class->IsArrayClass());
49
Hiroshi Yamauchiaa866f52014-03-21 16:18:30 -070050 size_t header_size = Array::DataOffset(component_size).SizeValue();
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070051 size_t data_size = component_count * component_size;
52 size_t size = header_size + data_size;
53
54 // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
55 size_t component_shift = sizeof(size_t) * 8 - 1 - CLZ(component_size);
56 if (UNLIKELY(data_size >> component_shift != size_t(component_count) || size < data_size)) {
57 self->ThrowOutOfMemoryError(StringPrintf("%s of length %d would overflow",
58 PrettyDescriptor(array_class).c_str(),
59 component_count).c_str());
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070060 return 0; // failure
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070061 }
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070062 return size;
63}
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070064
Ian Rogers6fac4472014-02-25 17:01:10 -080065// Used for setting the array length in the allocation code path to ensure it is guarded by a
66// StoreStore fence.
Mathieu Chartier1febddf2013-11-20 12:33:14 -080067class SetLengthVisitor {
68 public:
69 explicit SetLengthVisitor(int32_t length) : length_(length) {
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070070 }
Mathieu Chartier1febddf2013-11-20 12:33:14 -080071
Ian Rogers6fac4472014-02-25 17:01:10 -080072 void operator()(Object* obj, size_t usable_size) const
73 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
74 UNUSED(usable_size);
Ian Rogersef7d42f2014-01-06 12:55:46 -080075 // Avoid AsArray as object is not yet in live bitmap or allocation stack.
76 Array* array = down_cast<Array*>(obj);
77 // DCHECK(array->IsArrayInstance());
Mathieu Chartier1febddf2013-11-20 12:33:14 -080078 array->SetLength(length_);
79 }
80
81 private:
82 const int32_t length_;
Ian Rogers6fac4472014-02-25 17:01:10 -080083
84 DISALLOW_COPY_AND_ASSIGN(SetLengthVisitor);
85};
86
87// Similar to SetLengthVisitor, used for setting the array length to fill the usable size of an
88// array.
89class SetLengthToUsableSizeVisitor {
90 public:
Ian Rogersa55cf412014-02-27 00:31:26 -080091 SetLengthToUsableSizeVisitor(int32_t min_length, size_t header_size, size_t component_size) :
92 minimum_length_(min_length), header_size_(header_size), component_size_(component_size) {
Ian Rogers6fac4472014-02-25 17:01:10 -080093 }
94
95 void operator()(Object* obj, size_t usable_size) const
96 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
97 // Avoid AsArray as object is not yet in live bitmap or allocation stack.
98 Array* array = down_cast<Array*>(obj);
Ian Rogers6fac4472014-02-25 17:01:10 -080099 // DCHECK(array->IsArrayInstance());
Ian Rogersa55cf412014-02-27 00:31:26 -0800100 int32_t length = (usable_size - header_size_) / component_size_;
101 DCHECK_GE(length, minimum_length_);
102 byte* old_end = reinterpret_cast<byte*>(array->GetRawData(component_size_, minimum_length_));
103 byte* new_end = reinterpret_cast<byte*>(array->GetRawData(component_size_, length));
104 // Ensure space beyond original allocation is zeroed.
105 memset(old_end, 0, new_end - old_end);
Ian Rogers6fac4472014-02-25 17:01:10 -0800106 array->SetLength(length);
107 }
108
109 private:
Ian Rogersa55cf412014-02-27 00:31:26 -0800110 const int32_t minimum_length_;
Ian Rogers6fac4472014-02-25 17:01:10 -0800111 const size_t header_size_;
112 const size_t component_size_;
113
114 DISALLOW_COPY_AND_ASSIGN(SetLengthToUsableSizeVisitor);
Mathieu Chartier1febddf2013-11-20 12:33:14 -0800115};
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700116
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800117template <bool kIsInstrumented>
Mathieu Chartier590fee92013-09-13 13:46:47 -0700118inline Array* Array::Alloc(Thread* self, Class* array_class, int32_t component_count,
Ian Rogers6fac4472014-02-25 17:01:10 -0800119 size_t component_size, gc::AllocatorType allocator_type,
120 bool fill_usable) {
121 DCHECK(allocator_type != gc::kAllocatorTypeLOS);
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700122 size_t size = ComputeArraySize(self, array_class, component_count, component_size);
123 if (UNLIKELY(size == 0)) {
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800124 return nullptr;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700125 }
126 gc::Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers6fac4472014-02-25 17:01:10 -0800127 Array* result;
128 if (!fill_usable) {
129 SetLengthVisitor visitor(component_count);
130 result = down_cast<Array*>(
131 heap->AllocObjectWithAllocator<kIsInstrumented, true>(self, array_class, size,
132 allocator_type, visitor));
133 } else {
Hiroshi Yamauchiaa866f52014-03-21 16:18:30 -0700134 SetLengthToUsableSizeVisitor visitor(component_count, DataOffset(component_size).SizeValue(),
Ian Rogersa55cf412014-02-27 00:31:26 -0800135 component_size);
Ian Rogers6fac4472014-02-25 17:01:10 -0800136 result = down_cast<Array*>(
137 heap->AllocObjectWithAllocator<kIsInstrumented, true>(self, array_class, size,
138 allocator_type, visitor));
139 }
140 if (kIsDebugBuild && result != nullptr && Runtime::Current()->IsStarted()) {
Mathieu Chartier85801542014-02-27 18:06:26 -0800141 array_class = result->GetClass(); // In case the array class moved.
Ian Rogersa55cf412014-02-27 00:31:26 -0800142 CHECK_EQ(array_class->GetComponentSize(), component_size);
Ian Rogers6fac4472014-02-25 17:01:10 -0800143 if (!fill_usable) {
144 CHECK_EQ(result->SizeOf(), size);
145 } else {
146 CHECK_GE(result->SizeOf(), size);
147 }
148 }
149 return result;
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700150}
151
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800152template<class T>
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800153inline void PrimitiveArray<T>::VisitRoots(RootCallback* callback, void* arg) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800154 if (array_class_ != nullptr) {
Mathieu Chartier815873e2014-02-13 18:02:13 -0800155 callback(reinterpret_cast<mirror::Object**>(&array_class_), arg, 0, kRootStickyClass);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800156 }
157}
158
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700159template<typename T>
160inline PrimitiveArray<T>* PrimitiveArray<T>::Alloc(Thread* self, size_t length) {
161 DCHECK(array_class_ != NULL);
162 Array* raw_array = Array::Alloc<true>(self, array_class_, length, sizeof(T),
163 Runtime::Current()->GetHeap()->GetCurrentAllocator());
164 return down_cast<PrimitiveArray<T>*>(raw_array);
165}
166
167// Backward copy where elements are of aligned appropriately for T. Count is in T sized units.
168// Copies are guaranteed not to tear when the sizeof T is less-than 64bit.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800169template<typename T>
170static inline void ArrayBackwardCopy(T* d, const T* s, int32_t count) {
171 d += count;
172 s += count;
173 for (int32_t i = 0; i < count; ++i) {
174 d--;
175 s--;
176 *d = *s;
177 }
178}
179
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700180// Forward copy where elements are of aligned appropriately for T. Count is in T sized units.
181// Copies are guaranteed not to tear when the sizeof T is less-than 64bit.
Ian Rogers6fac4472014-02-25 17:01:10 -0800182template<typename T>
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700183static inline void ArrayForwardCopy(T* d, const T* s, int32_t count) {
184 for (int32_t i = 0; i < count; ++i) {
185 *d = *s;
186 d++;
187 s++;
188 }
Ian Rogers6fac4472014-02-25 17:01:10 -0800189}
190
Ian Rogersef7d42f2014-01-06 12:55:46 -0800191template<class T>
Ian Rogers6fac4472014-02-25 17:01:10 -0800192inline void PrimitiveArray<T>::Memmove(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos,
193 int32_t count) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800194 if (UNLIKELY(count == 0)) {
195 return;
196 }
197 DCHECK_GE(dst_pos, 0);
198 DCHECK_GE(src_pos, 0);
199 DCHECK_GT(count, 0);
200 DCHECK(src != nullptr);
201 DCHECK_LT(dst_pos, GetLength());
202 DCHECK_LE(dst_pos, GetLength() - count);
203 DCHECK_LT(src_pos, src->GetLength());
204 DCHECK_LE(src_pos, src->GetLength() - count);
205
206 // Note for non-byte copies we can't rely on standard libc functions like memcpy(3) and memmove(3)
207 // in our implementation, because they may copy byte-by-byte.
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700208 if (LIKELY(src != this)) {
209 // Memcpy ok for guaranteed non-overlapping distinct arrays.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800210 Memcpy(dst_pos, src, src_pos, count);
211 } else {
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700212 // Handle copies within the same array using the appropriate direction copy.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800213 void* dst_raw = GetRawData(sizeof(T), dst_pos);
214 const void* src_raw = src->GetRawData(sizeof(T), src_pos);
215 if (sizeof(T) == sizeof(uint8_t)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800216 uint8_t* d = reinterpret_cast<uint8_t*>(dst_raw);
217 const uint8_t* s = reinterpret_cast<const uint8_t*>(src_raw);
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700218 memmove(d, s, count);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800219 } else {
Ian Rogers99cb4ea2014-03-26 22:53:56 -0700220 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= count);
221 if (sizeof(T) == sizeof(uint16_t)) {
222 uint16_t* d = reinterpret_cast<uint16_t*>(dst_raw);
223 const uint16_t* s = reinterpret_cast<const uint16_t*>(src_raw);
224 if (copy_forward) {
225 ArrayForwardCopy<uint16_t>(d, s, count);
226 } else {
227 ArrayBackwardCopy<uint16_t>(d, s, count);
228 }
229 } else if (sizeof(T) == sizeof(uint32_t)) {
230 uint32_t* d = reinterpret_cast<uint32_t*>(dst_raw);
231 const uint32_t* s = reinterpret_cast<const uint32_t*>(src_raw);
232 if (copy_forward) {
233 ArrayForwardCopy<uint32_t>(d, s, count);
234 } else {
235 ArrayBackwardCopy<uint32_t>(d, s, count);
236 }
237 } else {
238 DCHECK_EQ(sizeof(T), sizeof(uint64_t));
239 uint64_t* d = reinterpret_cast<uint64_t*>(dst_raw);
240 const uint64_t* s = reinterpret_cast<const uint64_t*>(src_raw);
241 if (copy_forward) {
242 ArrayForwardCopy<uint64_t>(d, s, count);
243 } else {
244 ArrayBackwardCopy<uint64_t>(d, s, count);
245 }
246 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800247 }
248 }
249}
250
Ian Rogersef7d42f2014-01-06 12:55:46 -0800251template<class T>
Ian Rogers6fac4472014-02-25 17:01:10 -0800252inline void PrimitiveArray<T>::Memcpy(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos,
253 int32_t count) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800254 if (UNLIKELY(count == 0)) {
255 return;
256 }
257 DCHECK_GE(dst_pos, 0);
258 DCHECK_GE(src_pos, 0);
259 DCHECK_GT(count, 0);
260 DCHECK(src != nullptr);
261 DCHECK_LT(dst_pos, GetLength());
262 DCHECK_LE(dst_pos, GetLength() - count);
263 DCHECK_LT(src_pos, src->GetLength());
264 DCHECK_LE(src_pos, src->GetLength() - count);
265
266 // Note for non-byte copies we can't rely on standard libc functions like memcpy(3) and memmove(3)
267 // in our implementation, because they may copy byte-by-byte.
268 void* dst_raw = GetRawData(sizeof(T), dst_pos);
269 const void* src_raw = src->GetRawData(sizeof(T), src_pos);
270 if (sizeof(T) == sizeof(uint8_t)) {
271 memcpy(dst_raw, src_raw, count);
272 } else if (sizeof(T) == sizeof(uint16_t)) {
273 uint16_t* d = reinterpret_cast<uint16_t*>(dst_raw);
274 const uint16_t* s = reinterpret_cast<const uint16_t*>(src_raw);
275 ArrayForwardCopy<uint16_t>(d, s, count);
276 } else if (sizeof(T) == sizeof(uint32_t)) {
277 uint32_t* d = reinterpret_cast<uint32_t*>(dst_raw);
278 const uint32_t* s = reinterpret_cast<const uint32_t*>(src_raw);
279 ArrayForwardCopy<uint32_t>(d, s, count);
280 } else {
281 DCHECK_EQ(sizeof(T), sizeof(uint64_t));
282 uint64_t* d = reinterpret_cast<uint64_t*>(dst_raw);
283 const uint64_t* s = reinterpret_cast<const uint64_t*>(src_raw);
284 ArrayForwardCopy<uint64_t>(d, s, count);
285 }
286}
287
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800288} // namespace mirror
289} // namespace art
290
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700291#endif // ART_RUNTIME_MIRROR_ARRAY_INL_H_