blob: 90aaccdc4baa0b5d4c52504d921e1e5a2ebed03d [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
Mathieu Chartier4e305412014-02-19 10:54:44 -080030template<VerifyObjectFlags kVerifyFlags>
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.
Mathieu Chartier4e305412014-02-19 10:54:44 -080033 size_t component_size = GetClass<kVerifyFlags>()->GetComponentSize();
34 // Don't need to check this since we already check this in GetClass.
35 int32_t component_count =
36 GetLength<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037 size_t header_size = sizeof(Object) + (component_size == sizeof(int64_t) ? 8 : 4);
38 size_t data_size = component_count * component_size;
39 return header_size + data_size;
40}
41
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070042static inline size_t ComputeArraySize(Thread* self, Class* array_class, int32_t component_count,
43 size_t component_size)
44 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070045 DCHECK(array_class != NULL);
46 DCHECK_GE(component_count, 0);
47 DCHECK(array_class->IsArrayClass());
48
49 size_t header_size = sizeof(Object) + (component_size == sizeof(int64_t) ? 8 : 4);
50 size_t data_size = component_count * component_size;
51 size_t size = header_size + data_size;
52
53 // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
54 size_t component_shift = sizeof(size_t) * 8 - 1 - CLZ(component_size);
55 if (UNLIKELY(data_size >> component_shift != size_t(component_count) || size < data_size)) {
56 self->ThrowOutOfMemoryError(StringPrintf("%s of length %d would overflow",
57 PrettyDescriptor(array_class).c_str(),
58 component_count).c_str());
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070059 return 0; // failure
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070060 }
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070061 return size;
62}
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070063
Mathieu Chartier7410f292013-11-24 13:17:35 -080064// Used for setting the array length in the allocation code path to ensure it is guarded by a CAS.
Mathieu Chartier1febddf2013-11-20 12:33:14 -080065class SetLengthVisitor {
66 public:
67 explicit SetLengthVisitor(int32_t length) : length_(length) {
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070068 }
Mathieu Chartier1febddf2013-11-20 12:33:14 -080069
Ian Rogersef7d42f2014-01-06 12:55:46 -080070 void operator()(Object* obj) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
71 // Avoid AsArray as object is not yet in live bitmap or allocation stack.
72 Array* array = down_cast<Array*>(obj);
73 // DCHECK(array->IsArrayInstance());
Mathieu Chartier1febddf2013-11-20 12:33:14 -080074 array->SetLength(length_);
75 }
76
77 private:
78 const int32_t length_;
79};
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070080
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080081template <bool kIsInstrumented>
Mathieu Chartier590fee92013-09-13 13:46:47 -070082inline Array* Array::Alloc(Thread* self, Class* array_class, int32_t component_count,
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080083 size_t component_size, gc::AllocatorType allocator_type) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070084 size_t size = ComputeArraySize(self, array_class, component_count, component_size);
85 if (UNLIKELY(size == 0)) {
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080086 return nullptr;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070087 }
88 gc::Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartier1febddf2013-11-20 12:33:14 -080089 SetLengthVisitor visitor(component_count);
Mathieu Chartier692fafd2013-11-29 17:24:40 -080090 DCHECK(allocator_type != gc::kAllocatorTypeLOS);
Mathieu Chartier1febddf2013-11-20 12:33:14 -080091 return down_cast<Array*>(
Mathieu Chartier692fafd2013-11-29 17:24:40 -080092 heap->AllocObjectWithAllocator<kIsInstrumented, true>(self, array_class, size,
93 allocator_type, visitor));
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070094}
95
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080096template <bool kIsInstrumented>
97inline Array* Array::Alloc(Thread* self, Class* array_class, int32_t component_count,
98 gc::AllocatorType allocator_type) {
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070099 DCHECK(array_class->IsArrayClass());
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800100 return Alloc<kIsInstrumented>(self, array_class, component_count, array_class->GetComponentSize(),
101 allocator_type);
102}
103template <bool kIsInstrumented>
104inline Array* Array::Alloc(Thread* self, Class* array_class, int32_t component_count) {
105 return Alloc<kIsInstrumented>(self, array_class, component_count,
106 Runtime::Current()->GetHeap()->GetCurrentAllocator());
107}
108
109template <bool kIsInstrumented>
110inline Array* Array::Alloc(Thread* self, Class* array_class, int32_t component_count,
111 size_t component_size) {
112 return Alloc<kIsInstrumented>(self, array_class, component_count, component_size,
113 Runtime::Current()->GetHeap()->GetCurrentAllocator());
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700114}
115
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800116template<class T>
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800117inline void PrimitiveArray<T>::VisitRoots(RootCallback* callback, void* arg) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800118 if (array_class_ != nullptr) {
Mathieu Chartier815873e2014-02-13 18:02:13 -0800119 callback(reinterpret_cast<mirror::Object**>(&array_class_), arg, 0, kRootStickyClass);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800120 }
121}
122
Ian Rogersef7d42f2014-01-06 12:55:46 -0800123// Similar to memmove except elements are of aligned appropriately for T, count is in T sized units
124// copies are guaranteed not to tear when T is less-than 64bit.
125template<typename T>
126static inline void ArrayBackwardCopy(T* d, const T* s, int32_t count) {
127 d += count;
128 s += count;
129 for (int32_t i = 0; i < count; ++i) {
130 d--;
131 s--;
132 *d = *s;
133 }
134}
135
136template<class T>
137void PrimitiveArray<T>::Memmove(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos,
138 int32_t count) {
139 if (UNLIKELY(count == 0)) {
140 return;
141 }
142 DCHECK_GE(dst_pos, 0);
143 DCHECK_GE(src_pos, 0);
144 DCHECK_GT(count, 0);
145 DCHECK(src != nullptr);
146 DCHECK_LT(dst_pos, GetLength());
147 DCHECK_LE(dst_pos, GetLength() - count);
148 DCHECK_LT(src_pos, src->GetLength());
149 DCHECK_LE(src_pos, src->GetLength() - count);
150
151 // Note for non-byte copies we can't rely on standard libc functions like memcpy(3) and memmove(3)
152 // in our implementation, because they may copy byte-by-byte.
153 if (LIKELY(src != this) || (dst_pos < src_pos) || (dst_pos - src_pos >= count)) {
154 // Forward copy ok.
155 Memcpy(dst_pos, src, src_pos, count);
156 } else {
157 // Backward copy necessary.
158 void* dst_raw = GetRawData(sizeof(T), dst_pos);
159 const void* src_raw = src->GetRawData(sizeof(T), src_pos);
160 if (sizeof(T) == sizeof(uint8_t)) {
161 // TUNING: use memmove here?
162 uint8_t* d = reinterpret_cast<uint8_t*>(dst_raw);
163 const uint8_t* s = reinterpret_cast<const uint8_t*>(src_raw);
164 ArrayBackwardCopy<uint8_t>(d, s, count);
165 } else if (sizeof(T) == sizeof(uint16_t)) {
166 uint16_t* d = reinterpret_cast<uint16_t*>(dst_raw);
167 const uint16_t* s = reinterpret_cast<const uint16_t*>(src_raw);
168 ArrayBackwardCopy<uint16_t>(d, s, count);
169 } else if (sizeof(T) == sizeof(uint32_t)) {
170 uint32_t* d = reinterpret_cast<uint32_t*>(dst_raw);
171 const uint32_t* s = reinterpret_cast<const uint32_t*>(src_raw);
172 ArrayBackwardCopy<uint32_t>(d, s, count);
173 } else {
174 DCHECK_EQ(sizeof(T), sizeof(uint64_t));
175 uint64_t* d = reinterpret_cast<uint64_t*>(dst_raw);
176 const uint64_t* s = reinterpret_cast<const uint64_t*>(src_raw);
177 ArrayBackwardCopy<uint64_t>(d, s, count);
178 }
179 }
180}
181
182// Similar to memcpy except elements are of aligned appropriately for T, count is in T sized units
183// copies are guaranteed not to tear when T is less-than 64bit.
184template<typename T>
185static inline void ArrayForwardCopy(T* d, const T* s, int32_t count) {
186 for (int32_t i = 0; i < count; ++i) {
187 *d = *s;
188 d++;
189 s++;
190 }
191}
192
193
194template<class T>
195void PrimitiveArray<T>::Memcpy(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos,
196 int32_t count) {
197 if (UNLIKELY(count == 0)) {
198 return;
199 }
200 DCHECK_GE(dst_pos, 0);
201 DCHECK_GE(src_pos, 0);
202 DCHECK_GT(count, 0);
203 DCHECK(src != nullptr);
204 DCHECK_LT(dst_pos, GetLength());
205 DCHECK_LE(dst_pos, GetLength() - count);
206 DCHECK_LT(src_pos, src->GetLength());
207 DCHECK_LE(src_pos, src->GetLength() - count);
208
209 // Note for non-byte copies we can't rely on standard libc functions like memcpy(3) and memmove(3)
210 // in our implementation, because they may copy byte-by-byte.
211 void* dst_raw = GetRawData(sizeof(T), dst_pos);
212 const void* src_raw = src->GetRawData(sizeof(T), src_pos);
213 if (sizeof(T) == sizeof(uint8_t)) {
214 memcpy(dst_raw, src_raw, count);
215 } else if (sizeof(T) == sizeof(uint16_t)) {
216 uint16_t* d = reinterpret_cast<uint16_t*>(dst_raw);
217 const uint16_t* s = reinterpret_cast<const uint16_t*>(src_raw);
218 ArrayForwardCopy<uint16_t>(d, s, count);
219 } else if (sizeof(T) == sizeof(uint32_t)) {
220 uint32_t* d = reinterpret_cast<uint32_t*>(dst_raw);
221 const uint32_t* s = reinterpret_cast<const uint32_t*>(src_raw);
222 ArrayForwardCopy<uint32_t>(d, s, count);
223 } else {
224 DCHECK_EQ(sizeof(T), sizeof(uint64_t));
225 uint64_t* d = reinterpret_cast<uint64_t*>(dst_raw);
226 const uint64_t* s = reinterpret_cast<const uint64_t*>(src_raw);
227 ArrayForwardCopy<uint64_t>(d, s, count);
228 }
229}
230
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800231} // namespace mirror
232} // namespace art
233
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700234#endif // ART_RUNTIME_MIRROR_ARRAY_INL_H_