blob: 35ea2b3d4d6f737c43f30e671aedb296b1019938 [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 Rogersef7d42f2014-01-06 12:55:46 -080030inline size_t Array::SizeOf() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031 // This is safe from overflow because the array was already allocated, so we know it's sane.
32 size_t component_size = GetClass()->GetComponentSize();
33 int32_t component_count = GetLength();
34 size_t header_size = sizeof(Object) + (component_size == sizeof(int64_t) ? 8 : 4);
35 size_t data_size = component_count * component_size;
36 return header_size + data_size;
37}
38
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070039static inline size_t ComputeArraySize(Thread* self, Class* array_class, int32_t component_count,
40 size_t component_size)
41 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070042 DCHECK(array_class != NULL);
43 DCHECK_GE(component_count, 0);
44 DCHECK(array_class->IsArrayClass());
45
46 size_t header_size = sizeof(Object) + (component_size == sizeof(int64_t) ? 8 : 4);
47 size_t data_size = component_count * component_size;
48 size_t size = header_size + data_size;
49
50 // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
51 size_t component_shift = sizeof(size_t) * 8 - 1 - CLZ(component_size);
52 if (UNLIKELY(data_size >> component_shift != size_t(component_count) || size < data_size)) {
53 self->ThrowOutOfMemoryError(StringPrintf("%s of length %d would overflow",
54 PrettyDescriptor(array_class).c_str(),
55 component_count).c_str());
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070056 return 0; // failure
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070057 }
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070058 return size;
59}
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070060
Mathieu Chartier7410f292013-11-24 13:17:35 -080061// 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 -080062class SetLengthVisitor {
63 public:
64 explicit SetLengthVisitor(int32_t length) : length_(length) {
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070065 }
Mathieu Chartier1febddf2013-11-20 12:33:14 -080066
Ian Rogersef7d42f2014-01-06 12:55:46 -080067 void operator()(Object* obj) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
68 // Avoid AsArray as object is not yet in live bitmap or allocation stack.
69 Array* array = down_cast<Array*>(obj);
70 // DCHECK(array->IsArrayInstance());
Mathieu Chartier1febddf2013-11-20 12:33:14 -080071 array->SetLength(length_);
72 }
73
74 private:
75 const int32_t length_;
76};
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070077
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080078template <bool kIsInstrumented>
Mathieu Chartier590fee92013-09-13 13:46:47 -070079inline Array* Array::Alloc(Thread* self, Class* array_class, int32_t component_count,
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080080 size_t component_size, gc::AllocatorType allocator_type) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070081 size_t size = ComputeArraySize(self, array_class, component_count, component_size);
82 if (UNLIKELY(size == 0)) {
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080083 return nullptr;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070084 }
85 gc::Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartier1febddf2013-11-20 12:33:14 -080086 SetLengthVisitor visitor(component_count);
Mathieu Chartier692fafd2013-11-29 17:24:40 -080087 DCHECK(allocator_type != gc::kAllocatorTypeLOS);
Mathieu Chartier1febddf2013-11-20 12:33:14 -080088 return down_cast<Array*>(
Mathieu Chartier692fafd2013-11-29 17:24:40 -080089 heap->AllocObjectWithAllocator<kIsInstrumented, true>(self, array_class, size,
90 allocator_type, visitor));
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070091}
92
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080093template <bool kIsInstrumented>
94inline Array* Array::Alloc(Thread* self, Class* array_class, int32_t component_count,
95 gc::AllocatorType allocator_type) {
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070096 DCHECK(array_class->IsArrayClass());
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080097 return Alloc<kIsInstrumented>(self, array_class, component_count, array_class->GetComponentSize(),
98 allocator_type);
99}
100template <bool kIsInstrumented>
101inline Array* Array::Alloc(Thread* self, Class* array_class, int32_t component_count) {
102 return Alloc<kIsInstrumented>(self, array_class, component_count,
103 Runtime::Current()->GetHeap()->GetCurrentAllocator());
104}
105
106template <bool kIsInstrumented>
107inline Array* Array::Alloc(Thread* self, Class* array_class, int32_t component_count,
108 size_t component_size) {
109 return Alloc<kIsInstrumented>(self, array_class, component_count, component_size,
110 Runtime::Current()->GetHeap()->GetCurrentAllocator());
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700111}
112
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800113template<class T>
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800114inline void PrimitiveArray<T>::VisitRoots(RootCallback* callback, void* arg) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800115 if (array_class_ != nullptr) {
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800116 array_class_ = down_cast<Class*>(callback(array_class_, arg, 0, kRootStickyClass));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800117 }
118}
119
Ian Rogersef7d42f2014-01-06 12:55:46 -0800120// Similar to memmove except elements are of aligned appropriately for T, count is in T sized units
121// copies are guaranteed not to tear when T is less-than 64bit.
122template<typename T>
123static inline void ArrayBackwardCopy(T* d, const T* s, int32_t count) {
124 d += count;
125 s += count;
126 for (int32_t i = 0; i < count; ++i) {
127 d--;
128 s--;
129 *d = *s;
130 }
131}
132
133template<class T>
134void PrimitiveArray<T>::Memmove(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos,
135 int32_t count) {
136 if (UNLIKELY(count == 0)) {
137 return;
138 }
139 DCHECK_GE(dst_pos, 0);
140 DCHECK_GE(src_pos, 0);
141 DCHECK_GT(count, 0);
142 DCHECK(src != nullptr);
143 DCHECK_LT(dst_pos, GetLength());
144 DCHECK_LE(dst_pos, GetLength() - count);
145 DCHECK_LT(src_pos, src->GetLength());
146 DCHECK_LE(src_pos, src->GetLength() - count);
147
148 // Note for non-byte copies we can't rely on standard libc functions like memcpy(3) and memmove(3)
149 // in our implementation, because they may copy byte-by-byte.
150 if (LIKELY(src != this) || (dst_pos < src_pos) || (dst_pos - src_pos >= count)) {
151 // Forward copy ok.
152 Memcpy(dst_pos, src, src_pos, count);
153 } else {
154 // Backward copy necessary.
155 void* dst_raw = GetRawData(sizeof(T), dst_pos);
156 const void* src_raw = src->GetRawData(sizeof(T), src_pos);
157 if (sizeof(T) == sizeof(uint8_t)) {
158 // TUNING: use memmove here?
159 uint8_t* d = reinterpret_cast<uint8_t*>(dst_raw);
160 const uint8_t* s = reinterpret_cast<const uint8_t*>(src_raw);
161 ArrayBackwardCopy<uint8_t>(d, s, count);
162 } else if (sizeof(T) == sizeof(uint16_t)) {
163 uint16_t* d = reinterpret_cast<uint16_t*>(dst_raw);
164 const uint16_t* s = reinterpret_cast<const uint16_t*>(src_raw);
165 ArrayBackwardCopy<uint16_t>(d, s, count);
166 } else if (sizeof(T) == sizeof(uint32_t)) {
167 uint32_t* d = reinterpret_cast<uint32_t*>(dst_raw);
168 const uint32_t* s = reinterpret_cast<const uint32_t*>(src_raw);
169 ArrayBackwardCopy<uint32_t>(d, s, count);
170 } else {
171 DCHECK_EQ(sizeof(T), sizeof(uint64_t));
172 uint64_t* d = reinterpret_cast<uint64_t*>(dst_raw);
173 const uint64_t* s = reinterpret_cast<const uint64_t*>(src_raw);
174 ArrayBackwardCopy<uint64_t>(d, s, count);
175 }
176 }
177}
178
179// Similar to memcpy except elements are of aligned appropriately for T, count is in T sized units
180// copies are guaranteed not to tear when T is less-than 64bit.
181template<typename T>
182static inline void ArrayForwardCopy(T* d, const T* s, int32_t count) {
183 for (int32_t i = 0; i < count; ++i) {
184 *d = *s;
185 d++;
186 s++;
187 }
188}
189
190
191template<class T>
192void PrimitiveArray<T>::Memcpy(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos,
193 int32_t count) {
194 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.
208 void* dst_raw = GetRawData(sizeof(T), dst_pos);
209 const void* src_raw = src->GetRawData(sizeof(T), src_pos);
210 if (sizeof(T) == sizeof(uint8_t)) {
211 memcpy(dst_raw, src_raw, 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 ArrayForwardCopy<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 ArrayForwardCopy<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 ArrayForwardCopy<uint64_t>(d, s, count);
225 }
226}
227
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800228} // namespace mirror
229} // namespace art
230
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700231#endif // ART_RUNTIME_MIRROR_ARRAY_INL_H_