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 | |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 22 | #include <android-base/logging.h> |
| 23 | #include <android-base/stringprintf.h> |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 24 | |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 25 | #include "base/bit_utils.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 26 | #include "base/casts.h" |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 27 | #include "class.h" |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 28 | #include "gc/heap-inl.h" |
Mathieu Chartier | 1a5337f | 2016-10-13 13:48:23 -0700 | [diff] [blame] | 29 | #include "obj_ptr-inl.h" |
Andreas Gampe | 88dbad3 | 2018-06-26 19:54:12 -0700 | [diff] [blame^] | 30 | #include "runtime.h" |
Andreas Gampe | 895f922 | 2017-07-05 09:53:32 -0700 | [diff] [blame] | 31 | #include "thread-current-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 32 | |
| 33 | namespace art { |
| 34 | namespace mirror { |
| 35 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 36 | inline uint32_t Array::ClassSize(PointerSize pointer_size) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 37 | uint32_t vtable_entries = Object::kVTableLength; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 38 | return Class::ComputeClassSize(true, vtable_entries, 0, 0, 0, 0, 0, pointer_size); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Hiroshi Yamauchi | 6e83c17 | 2014-05-01 21:25:41 -0700 | [diff] [blame] | 41 | template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption> |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 42 | inline size_t Array::SizeOf() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 43 | // 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] | 44 | size_t component_size_shift = GetClass<kVerifyFlags, kReadBarrierOption>()-> |
| 45 | template GetComponentSizeShift<kReadBarrierOption>(); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 46 | // Don't need to check this since we already check this in GetClass. |
| 47 | int32_t component_count = |
| 48 | GetLength<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>(); |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 49 | size_t header_size = DataOffset(1U << component_size_shift).SizeValue(); |
| 50 | size_t data_size = component_count << component_size_shift; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 51 | return header_size + data_size; |
| 52 | } |
| 53 | |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 54 | template<VerifyObjectFlags kVerifyFlags> |
| 55 | inline bool Array::CheckIsValidIndex(int32_t index) { |
| 56 | if (UNLIKELY(static_cast<uint32_t>(index) >= |
| 57 | static_cast<uint32_t>(GetLength<kVerifyFlags>()))) { |
| 58 | ThrowArrayIndexOutOfBoundsException(index); |
| 59 | return false; |
| 60 | } |
| 61 | return true; |
| 62 | } |
| 63 | |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 64 | static inline size_t ComputeArraySize(int32_t component_count, size_t component_size_shift) { |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 65 | DCHECK_GE(component_count, 0); |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 66 | |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 67 | size_t component_size = 1U << component_size_shift; |
Hiroshi Yamauchi | aa866f5 | 2014-03-21 16:18:30 -0700 | [diff] [blame] | 68 | size_t header_size = Array::DataOffset(component_size).SizeValue(); |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 69 | 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] | 70 | size_t size = header_size + data_size; |
| 71 | |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 72 | // Check for size_t overflow if this was an unreasonable request |
| 73 | // but let the caller throw OutOfMemoryError. |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 74 | #ifdef __LP64__ |
| 75 | // 64-bit. No overflow as component_count is 32-bit and the maximum |
| 76 | // component size is 8. |
| 77 | DCHECK_LE((1U << component_size_shift), 8U); |
| 78 | #else |
| 79 | // 32-bit. |
| 80 | DCHECK_NE(header_size, 0U); |
| 81 | DCHECK_EQ(RoundUp(header_size, component_size), header_size); |
| 82 | // The array length limit (exclusive). |
| 83 | const size_t length_limit = (0U - header_size) >> component_size_shift; |
| 84 | if (UNLIKELY(length_limit <= static_cast<size_t>(component_count))) { |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 85 | return 0; // failure |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 86 | } |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 87 | #endif |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 88 | return size; |
| 89 | } |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 90 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 91 | // Used for setting the array length in the allocation code path to ensure it is guarded by a |
| 92 | // StoreStore fence. |
Mathieu Chartier | 1febddf | 2013-11-20 12:33:14 -0800 | [diff] [blame] | 93 | class SetLengthVisitor { |
| 94 | public: |
| 95 | explicit SetLengthVisitor(int32_t length) : length_(length) { |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 96 | } |
Mathieu Chartier | 1febddf | 2013-11-20 12:33:14 -0800 | [diff] [blame] | 97 | |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 98 | void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 99 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 100 | // Avoid AsArray as object is not yet in live bitmap or allocation stack. |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 101 | ObjPtr<Array> array = ObjPtr<Array>::DownCast(obj); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 102 | // DCHECK(array->IsArrayInstance()); |
Mathieu Chartier | 1febddf | 2013-11-20 12:33:14 -0800 | [diff] [blame] | 103 | array->SetLength(length_); |
| 104 | } |
| 105 | |
| 106 | private: |
| 107 | const int32_t length_; |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 108 | |
| 109 | DISALLOW_COPY_AND_ASSIGN(SetLengthVisitor); |
| 110 | }; |
| 111 | |
| 112 | // Similar to SetLengthVisitor, used for setting the array length to fill the usable size of an |
| 113 | // array. |
| 114 | class SetLengthToUsableSizeVisitor { |
| 115 | public: |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 116 | SetLengthToUsableSizeVisitor(int32_t min_length, size_t header_size, |
| 117 | size_t component_size_shift) : |
| 118 | minimum_length_(min_length), header_size_(header_size), |
| 119 | component_size_shift_(component_size_shift) { |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 122 | void operator()(ObjPtr<Object> obj, size_t usable_size) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 123 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 124 | // Avoid AsArray as object is not yet in live bitmap or allocation stack. |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 125 | ObjPtr<Array> array = ObjPtr<Array>::DownCast(obj); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 126 | // DCHECK(array->IsArrayInstance()); |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 127 | int32_t length = (usable_size - header_size_) >> component_size_shift_; |
Ian Rogers | a55cf41 | 2014-02-27 00:31:26 -0800 | [diff] [blame] | 128 | DCHECK_GE(length, minimum_length_); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 129 | uint8_t* old_end = reinterpret_cast<uint8_t*>(array->GetRawData(1U << component_size_shift_, |
| 130 | minimum_length_)); |
| 131 | uint8_t* new_end = reinterpret_cast<uint8_t*>(array->GetRawData(1U << component_size_shift_, |
| 132 | length)); |
Ian Rogers | a55cf41 | 2014-02-27 00:31:26 -0800 | [diff] [blame] | 133 | // Ensure space beyond original allocation is zeroed. |
| 134 | memset(old_end, 0, new_end - old_end); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 135 | array->SetLength(length); |
| 136 | } |
| 137 | |
| 138 | private: |
Ian Rogers | a55cf41 | 2014-02-27 00:31:26 -0800 | [diff] [blame] | 139 | const int32_t minimum_length_; |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 140 | const size_t header_size_; |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 141 | const size_t component_size_shift_; |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 142 | |
| 143 | DISALLOW_COPY_AND_ASSIGN(SetLengthToUsableSizeVisitor); |
Mathieu Chartier | 1febddf | 2013-11-20 12:33:14 -0800 | [diff] [blame] | 144 | }; |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 145 | |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 146 | template <bool kIsInstrumented, bool kFillUsable> |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 147 | inline ObjPtr<Array> Array::Alloc(Thread* self, |
| 148 | ObjPtr<Class> array_class, |
| 149 | int32_t component_count, |
| 150 | size_t component_size_shift, |
| 151 | gc::AllocatorType allocator_type) { |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 152 | DCHECK(allocator_type != gc::kAllocatorTypeLOS); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 153 | DCHECK(array_class != nullptr); |
| 154 | DCHECK(array_class->IsArrayClass()); |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 155 | DCHECK_EQ(array_class->GetComponentSizeShift(), component_size_shift); |
| 156 | DCHECK_EQ(array_class->GetComponentSize(), (1U << component_size_shift)); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 157 | size_t size = ComputeArraySize(component_count, component_size_shift); |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 158 | #ifdef __LP64__ |
| 159 | // 64-bit. No size_t overflow. |
| 160 | DCHECK_NE(size, 0U); |
| 161 | #else |
| 162 | // 32-bit. |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 163 | if (UNLIKELY(size == 0)) { |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 164 | self->ThrowOutOfMemoryError(android::base::StringPrintf("%s of length %d would overflow", |
| 165 | array_class->PrettyDescriptor().c_str(), |
| 166 | component_count).c_str()); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 167 | return nullptr; |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 168 | } |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 169 | #endif |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 170 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 171 | ObjPtr<Array> result; |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 172 | if (!kFillUsable) { |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 173 | SetLengthVisitor visitor(component_count); |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 174 | result = ObjPtr<Array>::DownCast(MakeObjPtr( |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 175 | heap->AllocObjectWithAllocator<kIsInstrumented, true>(self, array_class, size, |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 176 | allocator_type, visitor))); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 177 | } else { |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 178 | SetLengthToUsableSizeVisitor visitor(component_count, |
| 179 | DataOffset(1U << component_size_shift).SizeValue(), |
| 180 | component_size_shift); |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 181 | result = ObjPtr<Array>::DownCast(MakeObjPtr( |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 182 | heap->AllocObjectWithAllocator<kIsInstrumented, true>(self, array_class, size, |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 183 | allocator_type, visitor))); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 184 | } |
| 185 | if (kIsDebugBuild && result != nullptr && Runtime::Current()->IsStarted()) { |
Mathieu Chartier | 8580154 | 2014-02-27 18:06:26 -0800 | [diff] [blame] | 186 | array_class = result->GetClass(); // In case the array class moved. |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 187 | CHECK_EQ(array_class->GetComponentSize(), 1U << component_size_shift); |
| 188 | if (!kFillUsable) { |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 189 | CHECK_EQ(result->SizeOf(), size); |
| 190 | } else { |
| 191 | CHECK_GE(result->SizeOf(), size); |
| 192 | } |
| 193 | } |
| 194 | return result; |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Ian Rogers | 99cb4ea | 2014-03-26 22:53:56 -0700 | [diff] [blame] | 197 | template<typename T> |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 198 | inline ObjPtr<PrimitiveArray<T>> PrimitiveArray<T>::AllocateAndFill(Thread* self, |
| 199 | const T* data, |
| 200 | size_t length) { |
Alex Light | 440b5d9 | 2017-01-24 15:32:25 -0800 | [diff] [blame] | 201 | StackHandleScope<1> hs(self); |
| 202 | Handle<PrimitiveArray<T>> arr(hs.NewHandle(PrimitiveArray<T>::Alloc(self, length))); |
| 203 | if (!arr.IsNull()) { |
| 204 | // Copy it in. Just skip if it's null |
| 205 | memcpy(arr->GetData(), data, sizeof(T) * length); |
| 206 | } |
| 207 | return arr.Get(); |
| 208 | } |
| 209 | |
| 210 | template<typename T> |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 211 | inline T PrimitiveArray<T>::Get(int32_t i) { |
| 212 | if (!CheckIsValidIndex(i)) { |
| 213 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 214 | return T(0); |
| 215 | } |
| 216 | return GetWithoutChecks(i); |
| 217 | } |
| 218 | |
| 219 | template<typename T> |
| 220 | inline void PrimitiveArray<T>::Set(int32_t i, T value) { |
| 221 | if (Runtime::Current()->IsActiveTransaction()) { |
| 222 | Set<true>(i, value); |
| 223 | } else { |
| 224 | Set<false>(i, value); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | template<typename T> |
| 229 | template<bool kTransactionActive, bool kCheckTransaction> |
| 230 | inline void PrimitiveArray<T>::Set(int32_t i, T value) { |
| 231 | if (CheckIsValidIndex(i)) { |
| 232 | SetWithoutChecks<kTransactionActive, kCheckTransaction>(i, value); |
| 233 | } else { |
| 234 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | template<typename T> |
Andreas Gampe | 3b45ef2 | 2015-05-26 21:34:09 -0700 | [diff] [blame] | 239 | template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags> |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 240 | inline void PrimitiveArray<T>::SetWithoutChecks(int32_t i, T value) { |
| 241 | if (kCheckTransaction) { |
| 242 | DCHECK_EQ(kTransactionActive, Runtime::Current()->IsActiveTransaction()); |
| 243 | } |
| 244 | if (kTransactionActive) { |
| 245 | Runtime::Current()->RecordWriteArray(this, i, GetWithoutChecks(i)); |
| 246 | } |
Andreas Gampe | 3b45ef2 | 2015-05-26 21:34:09 -0700 | [diff] [blame] | 247 | DCHECK(CheckIsValidIndex<kVerifyFlags>(i)); |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 248 | GetData()[i] = value; |
| 249 | } |
Ian Rogers | 99cb4ea | 2014-03-26 22:53:56 -0700 | [diff] [blame] | 250 | // Backward copy where elements are of aligned appropriately for T. Count is in T sized units. |
| 251 | // 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] | 252 | template<typename T> |
| 253 | static inline void ArrayBackwardCopy(T* d, const T* s, int32_t count) { |
| 254 | d += count; |
| 255 | s += count; |
| 256 | for (int32_t i = 0; i < count; ++i) { |
| 257 | d--; |
| 258 | s--; |
| 259 | *d = *s; |
| 260 | } |
| 261 | } |
| 262 | |
Ian Rogers | 99cb4ea | 2014-03-26 22:53:56 -0700 | [diff] [blame] | 263 | // Forward copy where elements are of aligned appropriately for T. Count is in T sized units. |
| 264 | // 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] | 265 | template<typename T> |
Ian Rogers | 99cb4ea | 2014-03-26 22:53:56 -0700 | [diff] [blame] | 266 | static inline void ArrayForwardCopy(T* d, const T* s, int32_t count) { |
| 267 | for (int32_t i = 0; i < count; ++i) { |
| 268 | *d = *s; |
| 269 | d++; |
| 270 | s++; |
| 271 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 272 | } |
| 273 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 274 | template<class T> |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 275 | inline void PrimitiveArray<T>::Memmove(int32_t dst_pos, |
| 276 | ObjPtr<PrimitiveArray<T>> src, |
| 277 | int32_t src_pos, |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 278 | int32_t count) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 279 | if (UNLIKELY(count == 0)) { |
| 280 | return; |
| 281 | } |
| 282 | DCHECK_GE(dst_pos, 0); |
| 283 | DCHECK_GE(src_pos, 0); |
| 284 | DCHECK_GT(count, 0); |
| 285 | DCHECK(src != nullptr); |
| 286 | DCHECK_LT(dst_pos, GetLength()); |
| 287 | DCHECK_LE(dst_pos, GetLength() - count); |
| 288 | DCHECK_LT(src_pos, src->GetLength()); |
| 289 | DCHECK_LE(src_pos, src->GetLength() - count); |
| 290 | |
| 291 | // Note for non-byte copies we can't rely on standard libc functions like memcpy(3) and memmove(3) |
| 292 | // in our implementation, because they may copy byte-by-byte. |
Ian Rogers | 99cb4ea | 2014-03-26 22:53:56 -0700 | [diff] [blame] | 293 | if (LIKELY(src != this)) { |
| 294 | // Memcpy ok for guaranteed non-overlapping distinct arrays. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 295 | Memcpy(dst_pos, src, src_pos, count); |
| 296 | } else { |
Ian Rogers | 99cb4ea | 2014-03-26 22:53:56 -0700 | [diff] [blame] | 297 | // Handle copies within the same array using the appropriate direction copy. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 298 | void* dst_raw = GetRawData(sizeof(T), dst_pos); |
| 299 | const void* src_raw = src->GetRawData(sizeof(T), src_pos); |
| 300 | if (sizeof(T) == sizeof(uint8_t)) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 301 | uint8_t* d = reinterpret_cast<uint8_t*>(dst_raw); |
| 302 | const uint8_t* s = reinterpret_cast<const uint8_t*>(src_raw); |
Ian Rogers | 99cb4ea | 2014-03-26 22:53:56 -0700 | [diff] [blame] | 303 | memmove(d, s, count); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 304 | } else { |
Ian Rogers | 99cb4ea | 2014-03-26 22:53:56 -0700 | [diff] [blame] | 305 | const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= count); |
| 306 | if (sizeof(T) == sizeof(uint16_t)) { |
| 307 | uint16_t* d = reinterpret_cast<uint16_t*>(dst_raw); |
| 308 | const uint16_t* s = reinterpret_cast<const uint16_t*>(src_raw); |
| 309 | if (copy_forward) { |
| 310 | ArrayForwardCopy<uint16_t>(d, s, count); |
| 311 | } else { |
| 312 | ArrayBackwardCopy<uint16_t>(d, s, count); |
| 313 | } |
| 314 | } else if (sizeof(T) == sizeof(uint32_t)) { |
| 315 | uint32_t* d = reinterpret_cast<uint32_t*>(dst_raw); |
| 316 | const uint32_t* s = reinterpret_cast<const uint32_t*>(src_raw); |
| 317 | if (copy_forward) { |
| 318 | ArrayForwardCopy<uint32_t>(d, s, count); |
| 319 | } else { |
| 320 | ArrayBackwardCopy<uint32_t>(d, s, count); |
| 321 | } |
| 322 | } else { |
| 323 | DCHECK_EQ(sizeof(T), sizeof(uint64_t)); |
| 324 | uint64_t* d = reinterpret_cast<uint64_t*>(dst_raw); |
| 325 | const uint64_t* s = reinterpret_cast<const uint64_t*>(src_raw); |
| 326 | if (copy_forward) { |
| 327 | ArrayForwardCopy<uint64_t>(d, s, count); |
| 328 | } else { |
| 329 | ArrayBackwardCopy<uint64_t>(d, s, count); |
| 330 | } |
| 331 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | } |
| 335 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 336 | template<class T> |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 337 | inline void PrimitiveArray<T>::Memcpy(int32_t dst_pos, |
| 338 | ObjPtr<PrimitiveArray<T>> src, |
| 339 | int32_t src_pos, |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 340 | int32_t count) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 341 | if (UNLIKELY(count == 0)) { |
| 342 | return; |
| 343 | } |
| 344 | DCHECK_GE(dst_pos, 0); |
| 345 | DCHECK_GE(src_pos, 0); |
| 346 | DCHECK_GT(count, 0); |
| 347 | DCHECK(src != nullptr); |
| 348 | DCHECK_LT(dst_pos, GetLength()); |
| 349 | DCHECK_LE(dst_pos, GetLength() - count); |
| 350 | DCHECK_LT(src_pos, src->GetLength()); |
| 351 | DCHECK_LE(src_pos, src->GetLength() - count); |
| 352 | |
| 353 | // Note for non-byte copies we can't rely on standard libc functions like memcpy(3) and memmove(3) |
| 354 | // in our implementation, because they may copy byte-by-byte. |
| 355 | void* dst_raw = GetRawData(sizeof(T), dst_pos); |
| 356 | const void* src_raw = src->GetRawData(sizeof(T), src_pos); |
| 357 | if (sizeof(T) == sizeof(uint8_t)) { |
| 358 | memcpy(dst_raw, src_raw, count); |
| 359 | } else if (sizeof(T) == sizeof(uint16_t)) { |
| 360 | uint16_t* d = reinterpret_cast<uint16_t*>(dst_raw); |
| 361 | const uint16_t* s = reinterpret_cast<const uint16_t*>(src_raw); |
| 362 | ArrayForwardCopy<uint16_t>(d, s, count); |
| 363 | } else if (sizeof(T) == sizeof(uint32_t)) { |
| 364 | uint32_t* d = reinterpret_cast<uint32_t*>(dst_raw); |
| 365 | const uint32_t* s = reinterpret_cast<const uint32_t*>(src_raw); |
| 366 | ArrayForwardCopy<uint32_t>(d, s, count); |
| 367 | } else { |
| 368 | DCHECK_EQ(sizeof(T), sizeof(uint64_t)); |
| 369 | uint64_t* d = reinterpret_cast<uint64_t*>(dst_raw); |
| 370 | const uint64_t* s = reinterpret_cast<const uint64_t*>(src_raw); |
| 371 | ArrayForwardCopy<uint64_t>(d, s, count); |
| 372 | } |
| 373 | } |
| 374 | |
Mathieu Chartier | dfe02f6 | 2016-02-01 20:15:11 -0800 | [diff] [blame] | 375 | template<typename T, VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption> |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 376 | inline T PointerArray::GetElementPtrSize(uint32_t idx, PointerSize ptr_size) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 377 | // C style casts here since we sometimes have T be a pointer, or sometimes an integer |
| 378 | // (for stack traces). |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 379 | if (ptr_size == PointerSize::k64) { |
Mathieu Chartier | dfe02f6 | 2016-02-01 20:15:11 -0800 | [diff] [blame] | 380 | return (T)static_cast<uintptr_t>( |
| 381 | AsLongArray<kVerifyFlags, kReadBarrierOption>()->GetWithoutChecks(idx)); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 382 | } |
Colin Cross | 32f5388 | 2017-03-15 15:25:24 -0700 | [diff] [blame] | 383 | return (T)static_cast<uintptr_t>(static_cast<uint32_t>( |
| 384 | AsIntArray<kVerifyFlags, kReadBarrierOption>()->GetWithoutChecks(idx))); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Mathieu Chartier | d329a3b | 2016-01-27 15:30:10 -0800 | [diff] [blame] | 387 | template<bool kTransactionActive, bool kUnchecked> |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 388 | inline void PointerArray::SetElementPtrSize(uint32_t idx, uint64_t element, PointerSize ptr_size) { |
| 389 | if (ptr_size == PointerSize::k64) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 390 | (kUnchecked ? down_cast<LongArray*>(static_cast<Object*>(this)) : AsLongArray())-> |
Mathieu Chartier | d329a3b | 2016-01-27 15:30:10 -0800 | [diff] [blame] | 391 | SetWithoutChecks<kTransactionActive>(idx, element); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 392 | } else { |
Mathieu Chartier | d329a3b | 2016-01-27 15:30:10 -0800 | [diff] [blame] | 393 | DCHECK_LE(element, static_cast<uint64_t>(0xFFFFFFFFu)); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 394 | (kUnchecked ? down_cast<IntArray*>(static_cast<Object*>(this)) : AsIntArray()) |
Mathieu Chartier | d329a3b | 2016-01-27 15:30:10 -0800 | [diff] [blame] | 395 | ->SetWithoutChecks<kTransactionActive>(idx, static_cast<uint32_t>(element)); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 396 | } |
| 397 | } |
| 398 | |
Mathieu Chartier | d329a3b | 2016-01-27 15:30:10 -0800 | [diff] [blame] | 399 | template<bool kTransactionActive, bool kUnchecked, typename T> |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 400 | inline void PointerArray::SetElementPtrSize(uint32_t idx, T* element, PointerSize ptr_size) { |
Mathieu Chartier | 1bbfab6 | 2016-01-27 16:37:19 -0800 | [diff] [blame] | 401 | SetElementPtrSize<kTransactionActive, kUnchecked>(idx, |
| 402 | reinterpret_cast<uintptr_t>(element), |
| 403 | ptr_size); |
Mathieu Chartier | d329a3b | 2016-01-27 15:30:10 -0800 | [diff] [blame] | 404 | } |
| 405 | |
Mathieu Chartier | dfe02f6 | 2016-02-01 20:15:11 -0800 | [diff] [blame] | 406 | template <VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption, typename Visitor> |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 407 | inline void PointerArray::Fixup(mirror::PointerArray* dest, |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 408 | PointerSize pointer_size, |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 409 | const Visitor& visitor) { |
| 410 | for (size_t i = 0, count = GetLength(); i < count; ++i) { |
Mathieu Chartier | dfe02f6 | 2016-02-01 20:15:11 -0800 | [diff] [blame] | 411 | void* ptr = GetElementPtrSize<void*, kVerifyFlags, kReadBarrierOption>(i, pointer_size); |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 412 | void* new_ptr = visitor(ptr); |
| 413 | if (ptr != new_ptr) { |
| 414 | dest->SetElementPtrSize<false, true>(i, new_ptr, pointer_size); |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 | |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 419 | template<bool kUnchecked> |
| 420 | void PointerArray::Memcpy(int32_t dst_pos, |
| 421 | ObjPtr<PointerArray> src, |
| 422 | int32_t src_pos, |
| 423 | int32_t count, |
| 424 | PointerSize ptr_size) { |
| 425 | DCHECK(!Runtime::Current()->IsActiveTransaction()); |
| 426 | DCHECK(!src.IsNull()); |
| 427 | if (ptr_size == PointerSize::k64) { |
| 428 | LongArray* l_this = (kUnchecked ? down_cast<LongArray*>(static_cast<Object*>(this)) |
| 429 | : AsLongArray()); |
| 430 | LongArray* l_src = (kUnchecked ? down_cast<LongArray*>(static_cast<Object*>(src.Ptr())) |
| 431 | : src->AsLongArray()); |
| 432 | l_this->Memcpy(dst_pos, l_src, src_pos, count); |
| 433 | } else { |
| 434 | IntArray* i_this = (kUnchecked ? down_cast<IntArray*>(static_cast<Object*>(this)) |
| 435 | : AsIntArray()); |
| 436 | IntArray* i_src = (kUnchecked ? down_cast<IntArray*>(static_cast<Object*>(src.Ptr())) |
| 437 | : src->AsIntArray()); |
| 438 | i_this->Memcpy(dst_pos, i_src, src_pos, count); |
| 439 | } |
| 440 | } |
| 441 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 442 | } // namespace mirror |
| 443 | } // namespace art |
| 444 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 445 | #endif // ART_RUNTIME_MIRROR_ARRAY_INL_H_ |