blob: 1d37775f934f35cb7a868de6da0e5342fbce9ab2 [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:
Ian Rogersa55cf412014-02-27 00:31:26 -080094 SetLengthToUsableSizeVisitor(int32_t min_length, size_t header_size, size_t component_size) :
95 minimum_length_(min_length), header_size_(header_size), component_size_(component_size) {
Ian Rogers6fac4472014-02-25 17:01:10 -080096 }
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);
Ian Rogers6fac4472014-02-25 17:01:10 -0800102 // DCHECK(array->IsArrayInstance());
Ian Rogersa55cf412014-02-27 00:31:26 -0800103 int32_t length = (usable_size - header_size_) / component_size_;
104 DCHECK_GE(length, minimum_length_);
105 byte* old_end = reinterpret_cast<byte*>(array->GetRawData(component_size_, minimum_length_));
106 byte* new_end = reinterpret_cast<byte*>(array->GetRawData(component_size_, length));
107 // Ensure space beyond original allocation is zeroed.
108 memset(old_end, 0, new_end - old_end);
Ian Rogers6fac4472014-02-25 17:01:10 -0800109 array->SetLength(length);
110 }
111
112 private:
Ian Rogersa55cf412014-02-27 00:31:26 -0800113 const int32_t minimum_length_;
Ian Rogers6fac4472014-02-25 17:01:10 -0800114 const size_t header_size_;
115 const size_t component_size_;
116
117 DISALLOW_COPY_AND_ASSIGN(SetLengthToUsableSizeVisitor);
Mathieu Chartier1febddf2013-11-20 12:33:14 -0800118};
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700119
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800120template <bool kIsInstrumented>
Mathieu Chartier590fee92013-09-13 13:46:47 -0700121inline Array* Array::Alloc(Thread* self, Class* array_class, int32_t component_count,
Ian Rogers6fac4472014-02-25 17:01:10 -0800122 size_t component_size, gc::AllocatorType allocator_type,
123 bool fill_usable) {
124 DCHECK(allocator_type != gc::kAllocatorTypeLOS);
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700125 size_t size = ComputeArraySize(self, array_class, component_count, component_size);
126 if (UNLIKELY(size == 0)) {
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800127 return nullptr;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700128 }
129 gc::Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers6fac4472014-02-25 17:01:10 -0800130 Array* result;
131 if (!fill_usable) {
132 SetLengthVisitor visitor(component_count);
133 result = down_cast<Array*>(
134 heap->AllocObjectWithAllocator<kIsInstrumented, true>(self, array_class, size,
135 allocator_type, visitor));
136 } else {
Ian Rogersa55cf412014-02-27 00:31:26 -0800137 SetLengthToUsableSizeVisitor visitor(component_count, HeaderSize(component_size),
138 component_size);
Ian Rogers6fac4472014-02-25 17:01:10 -0800139 result = down_cast<Array*>(
140 heap->AllocObjectWithAllocator<kIsInstrumented, true>(self, array_class, size,
141 allocator_type, visitor));
142 }
143 if (kIsDebugBuild && result != nullptr && Runtime::Current()->IsStarted()) {
Mathieu Chartier85801542014-02-27 18:06:26 -0800144 array_class = result->GetClass(); // In case the array class moved.
Ian Rogersa55cf412014-02-27 00:31:26 -0800145 CHECK_EQ(array_class->GetComponentSize(), component_size);
Ian Rogers6fac4472014-02-25 17:01:10 -0800146 if (!fill_usable) {
147 CHECK_EQ(result->SizeOf(), size);
148 } else {
149 CHECK_GE(result->SizeOf(), size);
150 }
151 }
152 return result;
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700153}
154
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800155template<class T>
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800156inline void PrimitiveArray<T>::VisitRoots(RootCallback* callback, void* arg) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800157 if (array_class_ != nullptr) {
Mathieu Chartier815873e2014-02-13 18:02:13 -0800158 callback(reinterpret_cast<mirror::Object**>(&array_class_), arg, 0, kRootStickyClass);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800159 }
160}
161
Ian Rogersef7d42f2014-01-06 12:55:46 -0800162// Similar to memmove except elements are of aligned appropriately for T, count is in T sized units
163// copies are guaranteed not to tear when T is less-than 64bit.
164template<typename T>
165static inline void ArrayBackwardCopy(T* d, const T* s, int32_t count) {
166 d += count;
167 s += count;
168 for (int32_t i = 0; i < count; ++i) {
169 d--;
170 s--;
171 *d = *s;
172 }
173}
174
Ian Rogers6fac4472014-02-25 17:01:10 -0800175template<typename T>
176inline PrimitiveArray<T>* PrimitiveArray<T>::Alloc(Thread* self, size_t length) {
177 DCHECK(array_class_ != NULL);
178 Array* raw_array = Array::Alloc<true>(self, array_class_, length, sizeof(T),
179 Runtime::Current()->GetHeap()->GetCurrentAllocator());
180 return down_cast<PrimitiveArray<T>*>(raw_array);
181}
182
Ian Rogersef7d42f2014-01-06 12:55:46 -0800183template<class T>
Ian Rogers6fac4472014-02-25 17:01:10 -0800184inline void PrimitiveArray<T>::Memmove(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos,
185 int32_t count) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800186 if (UNLIKELY(count == 0)) {
187 return;
188 }
189 DCHECK_GE(dst_pos, 0);
190 DCHECK_GE(src_pos, 0);
191 DCHECK_GT(count, 0);
192 DCHECK(src != nullptr);
193 DCHECK_LT(dst_pos, GetLength());
194 DCHECK_LE(dst_pos, GetLength() - count);
195 DCHECK_LT(src_pos, src->GetLength());
196 DCHECK_LE(src_pos, src->GetLength() - count);
197
198 // Note for non-byte copies we can't rely on standard libc functions like memcpy(3) and memmove(3)
199 // in our implementation, because they may copy byte-by-byte.
200 if (LIKELY(src != this) || (dst_pos < src_pos) || (dst_pos - src_pos >= count)) {
201 // Forward copy ok.
202 Memcpy(dst_pos, src, src_pos, count);
203 } else {
204 // Backward copy necessary.
205 void* dst_raw = GetRawData(sizeof(T), dst_pos);
206 const void* src_raw = src->GetRawData(sizeof(T), src_pos);
207 if (sizeof(T) == sizeof(uint8_t)) {
208 // TUNING: use memmove here?
209 uint8_t* d = reinterpret_cast<uint8_t*>(dst_raw);
210 const uint8_t* s = reinterpret_cast<const uint8_t*>(src_raw);
211 ArrayBackwardCopy<uint8_t>(d, s, count);
212 } else if (sizeof(T) == sizeof(uint16_t)) {
213 uint16_t* d = reinterpret_cast<uint16_t*>(dst_raw);
214 const uint16_t* s = reinterpret_cast<const uint16_t*>(src_raw);
215 ArrayBackwardCopy<uint16_t>(d, s, count);
216 } else if (sizeof(T) == sizeof(uint32_t)) {
217 uint32_t* d = reinterpret_cast<uint32_t*>(dst_raw);
218 const uint32_t* s = reinterpret_cast<const uint32_t*>(src_raw);
219 ArrayBackwardCopy<uint32_t>(d, s, count);
220 } else {
221 DCHECK_EQ(sizeof(T), sizeof(uint64_t));
222 uint64_t* d = reinterpret_cast<uint64_t*>(dst_raw);
223 const uint64_t* s = reinterpret_cast<const uint64_t*>(src_raw);
224 ArrayBackwardCopy<uint64_t>(d, s, count);
225 }
226 }
227}
228
229// Similar to memcpy except elements are of aligned appropriately for T, count is in T sized units
230// copies are guaranteed not to tear when T is less-than 64bit.
231template<typename T>
232static inline void ArrayForwardCopy(T* d, const T* s, int32_t count) {
233 for (int32_t i = 0; i < count; ++i) {
234 *d = *s;
235 d++;
236 s++;
237 }
238}
239
240
241template<class T>
Ian Rogers6fac4472014-02-25 17:01:10 -0800242inline void PrimitiveArray<T>::Memcpy(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos,
243 int32_t count) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800244 if (UNLIKELY(count == 0)) {
245 return;
246 }
247 DCHECK_GE(dst_pos, 0);
248 DCHECK_GE(src_pos, 0);
249 DCHECK_GT(count, 0);
250 DCHECK(src != nullptr);
251 DCHECK_LT(dst_pos, GetLength());
252 DCHECK_LE(dst_pos, GetLength() - count);
253 DCHECK_LT(src_pos, src->GetLength());
254 DCHECK_LE(src_pos, src->GetLength() - count);
255
256 // Note for non-byte copies we can't rely on standard libc functions like memcpy(3) and memmove(3)
257 // in our implementation, because they may copy byte-by-byte.
258 void* dst_raw = GetRawData(sizeof(T), dst_pos);
259 const void* src_raw = src->GetRawData(sizeof(T), src_pos);
260 if (sizeof(T) == sizeof(uint8_t)) {
261 memcpy(dst_raw, src_raw, count);
262 } else if (sizeof(T) == sizeof(uint16_t)) {
263 uint16_t* d = reinterpret_cast<uint16_t*>(dst_raw);
264 const uint16_t* s = reinterpret_cast<const uint16_t*>(src_raw);
265 ArrayForwardCopy<uint16_t>(d, s, count);
266 } else if (sizeof(T) == sizeof(uint32_t)) {
267 uint32_t* d = reinterpret_cast<uint32_t*>(dst_raw);
268 const uint32_t* s = reinterpret_cast<const uint32_t*>(src_raw);
269 ArrayForwardCopy<uint32_t>(d, s, count);
270 } else {
271 DCHECK_EQ(sizeof(T), sizeof(uint64_t));
272 uint64_t* d = reinterpret_cast<uint64_t*>(dst_raw);
273 const uint64_t* s = reinterpret_cast<const uint64_t*>(src_raw);
274 ArrayForwardCopy<uint64_t>(d, s, count);
275 }
276}
277
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800278} // namespace mirror
279} // namespace art
280
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700281#endif // ART_RUNTIME_MIRROR_ARRAY_INL_H_