Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1 | /* |
| 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 Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_MIRROR_ARRAY_INL_H_ |
| 18 | #define ART_RUNTIME_MIRROR_ARRAY_INL_H_ |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 19 | |
| 20 | #include "array.h" |
| 21 | |
| 22 | #include "class.h" |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 23 | #include "gc/heap-inl.h" |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 24 | #include "thread.h" |
| 25 | #include "utils.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 26 | |
| 27 | namespace art { |
| 28 | namespace mirror { |
| 29 | |
Hiroshi Yamauchi | 6e83c17 | 2014-05-01 21:25:41 -0700 | [diff] [blame^] | 30 | template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption> |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 31 | inline size_t Array::SizeOf() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 32 | // This is safe from overflow because the array was already allocated, so we know it's sane. |
Hiroshi Yamauchi | 9103c86 | 2014-04-22 13:51:07 -0700 | [diff] [blame] | 33 | size_t component_size = |
Hiroshi Yamauchi | 6e83c17 | 2014-05-01 21:25:41 -0700 | [diff] [blame^] | 34 | GetClass<kVerifyFlags, kReadBarrierOption>()->template GetComponentSize<kReadBarrierOption>(); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 35 | // 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 Yamauchi | aa866f5 | 2014-03-21 16:18:30 -0700 | [diff] [blame] | 38 | size_t header_size = DataOffset(component_size).SizeValue(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 39 | size_t data_size = component_count * component_size; |
| 40 | return header_size + data_size; |
| 41 | } |
| 42 | |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 43 | template<VerifyObjectFlags kVerifyFlags> |
| 44 | inline bool Array::CheckIsValidIndex(int32_t index) { |
| 45 | if (UNLIKELY(static_cast<uint32_t>(index) >= |
| 46 | static_cast<uint32_t>(GetLength<kVerifyFlags>()))) { |
| 47 | ThrowArrayIndexOutOfBoundsException(index); |
| 48 | return false; |
| 49 | } |
| 50 | return true; |
| 51 | } |
| 52 | |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 53 | static inline size_t ComputeArraySize(Thread* self, Class* array_class, int32_t component_count, |
| 54 | size_t component_size) |
| 55 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 56 | DCHECK(array_class != NULL); |
| 57 | DCHECK_GE(component_count, 0); |
| 58 | DCHECK(array_class->IsArrayClass()); |
| 59 | |
Hiroshi Yamauchi | aa866f5 | 2014-03-21 16:18:30 -0700 | [diff] [blame] | 60 | size_t header_size = Array::DataOffset(component_size).SizeValue(); |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 61 | size_t data_size = component_count * component_size; |
| 62 | size_t size = header_size + data_size; |
| 63 | |
| 64 | // Check for overflow and throw OutOfMemoryError if this was an unreasonable request. |
| 65 | size_t component_shift = sizeof(size_t) * 8 - 1 - CLZ(component_size); |
| 66 | if (UNLIKELY(data_size >> component_shift != size_t(component_count) || size < data_size)) { |
| 67 | self->ThrowOutOfMemoryError(StringPrintf("%s of length %d would overflow", |
| 68 | PrettyDescriptor(array_class).c_str(), |
| 69 | component_count).c_str()); |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 70 | return 0; // failure |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 71 | } |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 72 | return size; |
| 73 | } |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 74 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 75 | // Used for setting the array length in the allocation code path to ensure it is guarded by a |
| 76 | // StoreStore fence. |
Mathieu Chartier | 1febddf | 2013-11-20 12:33:14 -0800 | [diff] [blame] | 77 | class SetLengthVisitor { |
| 78 | public: |
| 79 | explicit SetLengthVisitor(int32_t length) : length_(length) { |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 80 | } |
Mathieu Chartier | 1febddf | 2013-11-20 12:33:14 -0800 | [diff] [blame] | 81 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 82 | void operator()(Object* obj, size_t usable_size) const |
| 83 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 84 | UNUSED(usable_size); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 85 | // Avoid AsArray as object is not yet in live bitmap or allocation stack. |
| 86 | Array* array = down_cast<Array*>(obj); |
| 87 | // DCHECK(array->IsArrayInstance()); |
Mathieu Chartier | 1febddf | 2013-11-20 12:33:14 -0800 | [diff] [blame] | 88 | array->SetLength(length_); |
| 89 | } |
| 90 | |
| 91 | private: |
| 92 | const int32_t length_; |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 93 | |
| 94 | DISALLOW_COPY_AND_ASSIGN(SetLengthVisitor); |
| 95 | }; |
| 96 | |
| 97 | // Similar to SetLengthVisitor, used for setting the array length to fill the usable size of an |
| 98 | // array. |
| 99 | class SetLengthToUsableSizeVisitor { |
| 100 | public: |
Ian Rogers | a55cf41 | 2014-02-27 00:31:26 -0800 | [diff] [blame] | 101 | SetLengthToUsableSizeVisitor(int32_t min_length, size_t header_size, size_t component_size) : |
| 102 | minimum_length_(min_length), header_size_(header_size), component_size_(component_size) { |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | void operator()(Object* obj, size_t usable_size) const |
| 106 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 107 | // Avoid AsArray as object is not yet in live bitmap or allocation stack. |
| 108 | Array* array = down_cast<Array*>(obj); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 109 | // DCHECK(array->IsArrayInstance()); |
Ian Rogers | a55cf41 | 2014-02-27 00:31:26 -0800 | [diff] [blame] | 110 | int32_t length = (usable_size - header_size_) / component_size_; |
| 111 | DCHECK_GE(length, minimum_length_); |
| 112 | byte* old_end = reinterpret_cast<byte*>(array->GetRawData(component_size_, minimum_length_)); |
| 113 | byte* new_end = reinterpret_cast<byte*>(array->GetRawData(component_size_, length)); |
| 114 | // Ensure space beyond original allocation is zeroed. |
| 115 | memset(old_end, 0, new_end - old_end); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 116 | array->SetLength(length); |
| 117 | } |
| 118 | |
| 119 | private: |
Ian Rogers | a55cf41 | 2014-02-27 00:31:26 -0800 | [diff] [blame] | 120 | const int32_t minimum_length_; |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 121 | const size_t header_size_; |
| 122 | const size_t component_size_; |
| 123 | |
| 124 | DISALLOW_COPY_AND_ASSIGN(SetLengthToUsableSizeVisitor); |
Mathieu Chartier | 1febddf | 2013-11-20 12:33:14 -0800 | [diff] [blame] | 125 | }; |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 126 | |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 127 | template <bool kIsInstrumented> |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 128 | inline Array* Array::Alloc(Thread* self, Class* array_class, int32_t component_count, |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 129 | size_t component_size, gc::AllocatorType allocator_type, |
| 130 | bool fill_usable) { |
| 131 | DCHECK(allocator_type != gc::kAllocatorTypeLOS); |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 132 | size_t size = ComputeArraySize(self, array_class, component_count, component_size); |
| 133 | if (UNLIKELY(size == 0)) { |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 134 | return nullptr; |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 135 | } |
| 136 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 137 | Array* result; |
| 138 | if (!fill_usable) { |
| 139 | SetLengthVisitor visitor(component_count); |
| 140 | result = down_cast<Array*>( |
| 141 | heap->AllocObjectWithAllocator<kIsInstrumented, true>(self, array_class, size, |
| 142 | allocator_type, visitor)); |
| 143 | } else { |
Hiroshi Yamauchi | aa866f5 | 2014-03-21 16:18:30 -0700 | [diff] [blame] | 144 | SetLengthToUsableSizeVisitor visitor(component_count, DataOffset(component_size).SizeValue(), |
Ian Rogers | a55cf41 | 2014-02-27 00:31:26 -0800 | [diff] [blame] | 145 | component_size); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 146 | result = down_cast<Array*>( |
| 147 | heap->AllocObjectWithAllocator<kIsInstrumented, true>(self, array_class, size, |
| 148 | allocator_type, visitor)); |
| 149 | } |
| 150 | if (kIsDebugBuild && result != nullptr && Runtime::Current()->IsStarted()) { |
Mathieu Chartier | 8580154 | 2014-02-27 18:06:26 -0800 | [diff] [blame] | 151 | array_class = result->GetClass(); // In case the array class moved. |
Ian Rogers | a55cf41 | 2014-02-27 00:31:26 -0800 | [diff] [blame] | 152 | CHECK_EQ(array_class->GetComponentSize(), component_size); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 153 | if (!fill_usable) { |
| 154 | CHECK_EQ(result->SizeOf(), size); |
| 155 | } else { |
| 156 | CHECK_GE(result->SizeOf(), size); |
| 157 | } |
| 158 | } |
| 159 | return result; |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 162 | template<class T> |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 163 | inline void PrimitiveArray<T>::VisitRoots(RootCallback* callback, void* arg) { |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 164 | if (array_class_ != nullptr) { |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 165 | callback(reinterpret_cast<mirror::Object**>(&array_class_), arg, 0, kRootStickyClass); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
Ian Rogers | 99cb4ea | 2014-03-26 22:53:56 -0700 | [diff] [blame] | 169 | template<typename T> |
| 170 | inline PrimitiveArray<T>* PrimitiveArray<T>::Alloc(Thread* self, size_t length) { |
| 171 | DCHECK(array_class_ != NULL); |
| 172 | Array* raw_array = Array::Alloc<true>(self, array_class_, length, sizeof(T), |
| 173 | Runtime::Current()->GetHeap()->GetCurrentAllocator()); |
| 174 | return down_cast<PrimitiveArray<T>*>(raw_array); |
| 175 | } |
| 176 | |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 177 | template<typename T> |
| 178 | inline T PrimitiveArray<T>::Get(int32_t i) { |
| 179 | if (!CheckIsValidIndex(i)) { |
| 180 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 181 | return T(0); |
| 182 | } |
| 183 | return GetWithoutChecks(i); |
| 184 | } |
| 185 | |
| 186 | template<typename T> |
| 187 | inline void PrimitiveArray<T>::Set(int32_t i, T value) { |
| 188 | if (Runtime::Current()->IsActiveTransaction()) { |
| 189 | Set<true>(i, value); |
| 190 | } else { |
| 191 | Set<false>(i, value); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | template<typename T> |
| 196 | template<bool kTransactionActive, bool kCheckTransaction> |
| 197 | inline void PrimitiveArray<T>::Set(int32_t i, T value) { |
| 198 | if (CheckIsValidIndex(i)) { |
| 199 | SetWithoutChecks<kTransactionActive, kCheckTransaction>(i, value); |
| 200 | } else { |
| 201 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | template<typename T> |
| 206 | template<bool kTransactionActive, bool kCheckTransaction> |
| 207 | inline void PrimitiveArray<T>::SetWithoutChecks(int32_t i, T value) { |
| 208 | if (kCheckTransaction) { |
| 209 | DCHECK_EQ(kTransactionActive, Runtime::Current()->IsActiveTransaction()); |
| 210 | } |
| 211 | if (kTransactionActive) { |
| 212 | Runtime::Current()->RecordWriteArray(this, i, GetWithoutChecks(i)); |
| 213 | } |
| 214 | DCHECK(CheckIsValidIndex(i)); |
| 215 | GetData()[i] = value; |
| 216 | } |
Ian Rogers | 99cb4ea | 2014-03-26 22:53:56 -0700 | [diff] [blame] | 217 | // Backward copy where elements are of aligned appropriately for T. Count is in T sized units. |
| 218 | // Copies are guaranteed not to tear when the sizeof T is less-than 64bit. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 219 | template<typename T> |
| 220 | static inline void ArrayBackwardCopy(T* d, const T* s, int32_t count) { |
| 221 | d += count; |
| 222 | s += count; |
| 223 | for (int32_t i = 0; i < count; ++i) { |
| 224 | d--; |
| 225 | s--; |
| 226 | *d = *s; |
| 227 | } |
| 228 | } |
| 229 | |
Ian Rogers | 99cb4ea | 2014-03-26 22:53:56 -0700 | [diff] [blame] | 230 | // Forward copy where elements are of aligned appropriately for T. Count is in T sized units. |
| 231 | // Copies are guaranteed not to tear when the sizeof T is less-than 64bit. |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 232 | template<typename T> |
Ian Rogers | 99cb4ea | 2014-03-26 22:53:56 -0700 | [diff] [blame] | 233 | static inline void ArrayForwardCopy(T* d, const T* s, int32_t count) { |
| 234 | for (int32_t i = 0; i < count; ++i) { |
| 235 | *d = *s; |
| 236 | d++; |
| 237 | s++; |
| 238 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 239 | } |
| 240 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 241 | template<class T> |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 242 | inline void PrimitiveArray<T>::Memmove(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos, |
| 243 | int32_t count) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 244 | 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. |
Ian Rogers | 99cb4ea | 2014-03-26 22:53:56 -0700 | [diff] [blame] | 258 | if (LIKELY(src != this)) { |
| 259 | // Memcpy ok for guaranteed non-overlapping distinct arrays. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 260 | Memcpy(dst_pos, src, src_pos, count); |
| 261 | } else { |
Ian Rogers | 99cb4ea | 2014-03-26 22:53:56 -0700 | [diff] [blame] | 262 | // Handle copies within the same array using the appropriate direction copy. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 263 | void* dst_raw = GetRawData(sizeof(T), dst_pos); |
| 264 | const void* src_raw = src->GetRawData(sizeof(T), src_pos); |
| 265 | if (sizeof(T) == sizeof(uint8_t)) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 266 | uint8_t* d = reinterpret_cast<uint8_t*>(dst_raw); |
| 267 | const uint8_t* s = reinterpret_cast<const uint8_t*>(src_raw); |
Ian Rogers | 99cb4ea | 2014-03-26 22:53:56 -0700 | [diff] [blame] | 268 | memmove(d, s, count); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 269 | } else { |
Ian Rogers | 99cb4ea | 2014-03-26 22:53:56 -0700 | [diff] [blame] | 270 | const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= count); |
| 271 | if (sizeof(T) == sizeof(uint16_t)) { |
| 272 | uint16_t* d = reinterpret_cast<uint16_t*>(dst_raw); |
| 273 | const uint16_t* s = reinterpret_cast<const uint16_t*>(src_raw); |
| 274 | if (copy_forward) { |
| 275 | ArrayForwardCopy<uint16_t>(d, s, count); |
| 276 | } else { |
| 277 | ArrayBackwardCopy<uint16_t>(d, s, count); |
| 278 | } |
| 279 | } else if (sizeof(T) == sizeof(uint32_t)) { |
| 280 | uint32_t* d = reinterpret_cast<uint32_t*>(dst_raw); |
| 281 | const uint32_t* s = reinterpret_cast<const uint32_t*>(src_raw); |
| 282 | if (copy_forward) { |
| 283 | ArrayForwardCopy<uint32_t>(d, s, count); |
| 284 | } else { |
| 285 | ArrayBackwardCopy<uint32_t>(d, s, count); |
| 286 | } |
| 287 | } else { |
| 288 | DCHECK_EQ(sizeof(T), sizeof(uint64_t)); |
| 289 | uint64_t* d = reinterpret_cast<uint64_t*>(dst_raw); |
| 290 | const uint64_t* s = reinterpret_cast<const uint64_t*>(src_raw); |
| 291 | if (copy_forward) { |
| 292 | ArrayForwardCopy<uint64_t>(d, s, count); |
| 293 | } else { |
| 294 | ArrayBackwardCopy<uint64_t>(d, s, count); |
| 295 | } |
| 296 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | } |
| 300 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 301 | template<class T> |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 302 | inline void PrimitiveArray<T>::Memcpy(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos, |
| 303 | int32_t count) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 304 | if (UNLIKELY(count == 0)) { |
| 305 | return; |
| 306 | } |
| 307 | DCHECK_GE(dst_pos, 0); |
| 308 | DCHECK_GE(src_pos, 0); |
| 309 | DCHECK_GT(count, 0); |
| 310 | DCHECK(src != nullptr); |
| 311 | DCHECK_LT(dst_pos, GetLength()); |
| 312 | DCHECK_LE(dst_pos, GetLength() - count); |
| 313 | DCHECK_LT(src_pos, src->GetLength()); |
| 314 | DCHECK_LE(src_pos, src->GetLength() - count); |
| 315 | |
| 316 | // Note for non-byte copies we can't rely on standard libc functions like memcpy(3) and memmove(3) |
| 317 | // in our implementation, because they may copy byte-by-byte. |
| 318 | void* dst_raw = GetRawData(sizeof(T), dst_pos); |
| 319 | const void* src_raw = src->GetRawData(sizeof(T), src_pos); |
| 320 | if (sizeof(T) == sizeof(uint8_t)) { |
| 321 | memcpy(dst_raw, src_raw, count); |
| 322 | } else if (sizeof(T) == sizeof(uint16_t)) { |
| 323 | uint16_t* d = reinterpret_cast<uint16_t*>(dst_raw); |
| 324 | const uint16_t* s = reinterpret_cast<const uint16_t*>(src_raw); |
| 325 | ArrayForwardCopy<uint16_t>(d, s, count); |
| 326 | } else if (sizeof(T) == sizeof(uint32_t)) { |
| 327 | uint32_t* d = reinterpret_cast<uint32_t*>(dst_raw); |
| 328 | const uint32_t* s = reinterpret_cast<const uint32_t*>(src_raw); |
| 329 | ArrayForwardCopy<uint32_t>(d, s, count); |
| 330 | } else { |
| 331 | DCHECK_EQ(sizeof(T), sizeof(uint64_t)); |
| 332 | uint64_t* d = reinterpret_cast<uint64_t*>(dst_raw); |
| 333 | const uint64_t* s = reinterpret_cast<const uint64_t*>(src_raw); |
| 334 | ArrayForwardCopy<uint64_t>(d, s, count); |
| 335 | } |
| 336 | } |
| 337 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 338 | } // namespace mirror |
| 339 | } // namespace art |
| 340 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 341 | #endif // ART_RUNTIME_MIRROR_ARRAY_INL_H_ |