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