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 | |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 22 | #include "base/bit_utils.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 23 | #include "base/casts.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 24 | #include "base/logging.h" |
Mathieu Chartier | 3e0acf6 | 2015-01-08 09:41:25 -0800 | [diff] [blame] | 25 | #include "base/stringprintf.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 26 | #include "class-inl.h" |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 27 | #include "gc/heap-inl.h" |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 28 | #include "thread.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 29 | |
| 30 | namespace art { |
| 31 | namespace mirror { |
| 32 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 33 | inline uint32_t Array::ClassSize(size_t pointer_size) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 34 | uint32_t vtable_entries = Object::kVTableLength; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 35 | 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] | 36 | } |
| 37 | |
Hiroshi Yamauchi | 6e83c17 | 2014-05-01 21:25:41 -0700 | [diff] [blame] | 38 | template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption> |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 39 | inline size_t Array::SizeOf() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 40 | // 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] | 41 | size_t component_size_shift = GetClass<kVerifyFlags, kReadBarrierOption>()-> |
| 42 | template GetComponentSizeShift<kReadBarrierOption>(); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 43 | // Don't need to check this since we already check this in GetClass. |
| 44 | int32_t component_count = |
| 45 | GetLength<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>(); |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 46 | size_t header_size = DataOffset(1U << component_size_shift).SizeValue(); |
| 47 | size_t data_size = component_count << component_size_shift; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 48 | return header_size + data_size; |
| 49 | } |
| 50 | |
Ian Rogers | 7e70b00 | 2014-10-08 11:47:24 -0700 | [diff] [blame] | 51 | inline MemberOffset Array::DataOffset(size_t component_size) { |
| 52 | DCHECK(IsPowerOfTwo(component_size)) << component_size; |
| 53 | size_t data_offset = RoundUp(OFFSETOF_MEMBER(Array, first_element_), component_size); |
| 54 | DCHECK_EQ(RoundUp(data_offset, component_size), data_offset) |
| 55 | << "Array data offset isn't aligned with component size"; |
| 56 | return MemberOffset(data_offset); |
| 57 | } |
| 58 | |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 59 | template<VerifyObjectFlags kVerifyFlags> |
| 60 | inline bool Array::CheckIsValidIndex(int32_t index) { |
| 61 | if (UNLIKELY(static_cast<uint32_t>(index) >= |
| 62 | static_cast<uint32_t>(GetLength<kVerifyFlags>()))) { |
| 63 | ThrowArrayIndexOutOfBoundsException(index); |
| 64 | return false; |
| 65 | } |
| 66 | return true; |
| 67 | } |
| 68 | |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 69 | 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] | 70 | DCHECK_GE(component_count, 0); |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 71 | |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 72 | size_t component_size = 1U << component_size_shift; |
Hiroshi Yamauchi | aa866f5 | 2014-03-21 16:18:30 -0700 | [diff] [blame] | 73 | size_t header_size = Array::DataOffset(component_size).SizeValue(); |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 74 | 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] | 75 | size_t size = header_size + data_size; |
| 76 | |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 77 | // Check for size_t overflow if this was an unreasonable request |
| 78 | // but let the caller throw OutOfMemoryError. |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 79 | #ifdef __LP64__ |
| 80 | // 64-bit. No overflow as component_count is 32-bit and the maximum |
| 81 | // component size is 8. |
| 82 | DCHECK_LE((1U << component_size_shift), 8U); |
| 83 | #else |
| 84 | // 32-bit. |
| 85 | DCHECK_NE(header_size, 0U); |
| 86 | DCHECK_EQ(RoundUp(header_size, component_size), header_size); |
| 87 | // The array length limit (exclusive). |
| 88 | const size_t length_limit = (0U - header_size) >> component_size_shift; |
| 89 | if (UNLIKELY(length_limit <= static_cast<size_t>(component_count))) { |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 90 | return 0; // failure |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 91 | } |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 92 | #endif |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 93 | return size; |
| 94 | } |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 95 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 96 | // Used for setting the array length in the allocation code path to ensure it is guarded by a |
| 97 | // StoreStore fence. |
Mathieu Chartier | 1febddf | 2013-11-20 12:33:14 -0800 | [diff] [blame] | 98 | class SetLengthVisitor { |
| 99 | public: |
| 100 | explicit SetLengthVisitor(int32_t length) : length_(length) { |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 101 | } |
Mathieu Chartier | 1febddf | 2013-11-20 12:33:14 -0800 | [diff] [blame] | 102 | |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 103 | void operator()(Object* obj, size_t usable_size ATTRIBUTE_UNUSED) const |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 104 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 105 | // Avoid AsArray as object is not yet in live bitmap or allocation stack. |
| 106 | Array* array = down_cast<Array*>(obj); |
| 107 | // DCHECK(array->IsArrayInstance()); |
Mathieu Chartier | 1febddf | 2013-11-20 12:33:14 -0800 | [diff] [blame] | 108 | array->SetLength(length_); |
| 109 | } |
| 110 | |
| 111 | private: |
| 112 | const int32_t length_; |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 113 | |
| 114 | DISALLOW_COPY_AND_ASSIGN(SetLengthVisitor); |
| 115 | }; |
| 116 | |
| 117 | // Similar to SetLengthVisitor, used for setting the array length to fill the usable size of an |
| 118 | // array. |
| 119 | class SetLengthToUsableSizeVisitor { |
| 120 | public: |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 121 | SetLengthToUsableSizeVisitor(int32_t min_length, size_t header_size, |
| 122 | size_t component_size_shift) : |
| 123 | minimum_length_(min_length), header_size_(header_size), |
| 124 | component_size_shift_(component_size_shift) { |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void operator()(Object* obj, size_t usable_size) const |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 128 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 129 | // Avoid AsArray as object is not yet in live bitmap or allocation stack. |
| 130 | Array* array = down_cast<Array*>(obj); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 131 | // DCHECK(array->IsArrayInstance()); |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 132 | int32_t length = (usable_size - header_size_) >> component_size_shift_; |
Ian Rogers | a55cf41 | 2014-02-27 00:31:26 -0800 | [diff] [blame] | 133 | DCHECK_GE(length, minimum_length_); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 134 | uint8_t* old_end = reinterpret_cast<uint8_t*>(array->GetRawData(1U << component_size_shift_, |
| 135 | minimum_length_)); |
| 136 | uint8_t* new_end = reinterpret_cast<uint8_t*>(array->GetRawData(1U << component_size_shift_, |
| 137 | length)); |
Ian Rogers | a55cf41 | 2014-02-27 00:31:26 -0800 | [diff] [blame] | 138 | // Ensure space beyond original allocation is zeroed. |
| 139 | memset(old_end, 0, new_end - old_end); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 140 | array->SetLength(length); |
| 141 | } |
| 142 | |
| 143 | private: |
Ian Rogers | a55cf41 | 2014-02-27 00:31:26 -0800 | [diff] [blame] | 144 | const int32_t minimum_length_; |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 145 | const size_t header_size_; |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 146 | const size_t component_size_shift_; |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 147 | |
| 148 | DISALLOW_COPY_AND_ASSIGN(SetLengthToUsableSizeVisitor); |
Mathieu Chartier | 1febddf | 2013-11-20 12:33:14 -0800 | [diff] [blame] | 149 | }; |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 150 | |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 151 | template <bool kIsInstrumented, bool kFillUsable> |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 152 | 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] | 153 | size_t component_size_shift, gc::AllocatorType allocator_type) { |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 154 | DCHECK(allocator_type != gc::kAllocatorTypeLOS); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 155 | DCHECK(array_class != nullptr); |
| 156 | DCHECK(array_class->IsArrayClass()); |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 157 | DCHECK_EQ(array_class->GetComponentSizeShift(), component_size_shift); |
| 158 | DCHECK_EQ(array_class->GetComponentSize(), (1U << component_size_shift)); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 159 | size_t size = ComputeArraySize(component_count, component_size_shift); |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 160 | #ifdef __LP64__ |
| 161 | // 64-bit. No size_t overflow. |
| 162 | DCHECK_NE(size, 0U); |
| 163 | #else |
| 164 | // 32-bit. |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 165 | if (UNLIKELY(size == 0)) { |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 166 | self->ThrowOutOfMemoryError(StringPrintf("%s of length %d would overflow", |
| 167 | PrettyDescriptor(array_class).c_str(), |
| 168 | component_count).c_str()); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 169 | return nullptr; |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 170 | } |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 171 | #endif |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 172 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 173 | Array* result; |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 174 | if (!kFillUsable) { |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 175 | SetLengthVisitor visitor(component_count); |
| 176 | result = down_cast<Array*>( |
| 177 | heap->AllocObjectWithAllocator<kIsInstrumented, true>(self, array_class, size, |
| 178 | allocator_type, visitor)); |
| 179 | } else { |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 180 | SetLengthToUsableSizeVisitor visitor(component_count, |
| 181 | DataOffset(1U << component_size_shift).SizeValue(), |
| 182 | component_size_shift); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 183 | result = down_cast<Array*>( |
| 184 | heap->AllocObjectWithAllocator<kIsInstrumented, true>(self, array_class, size, |
| 185 | allocator_type, visitor)); |
| 186 | } |
| 187 | if (kIsDebugBuild && result != nullptr && Runtime::Current()->IsStarted()) { |
Mathieu Chartier | 8580154 | 2014-02-27 18:06:26 -0800 | [diff] [blame] | 188 | array_class = result->GetClass(); // In case the array class moved. |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 189 | CHECK_EQ(array_class->GetComponentSize(), 1U << component_size_shift); |
| 190 | if (!kFillUsable) { |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 191 | CHECK_EQ(result->SizeOf(), size); |
| 192 | } else { |
| 193 | CHECK_GE(result->SizeOf(), size); |
| 194 | } |
| 195 | } |
| 196 | return result; |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 199 | template<class T> |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 200 | inline void PrimitiveArray<T>::VisitRoots(RootVisitor* visitor) { |
| 201 | array_class_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass)); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 202 | } |
| 203 | |
Ian Rogers | 99cb4ea | 2014-03-26 22:53:56 -0700 | [diff] [blame] | 204 | template<typename T> |
| 205 | inline PrimitiveArray<T>* PrimitiveArray<T>::Alloc(Thread* self, size_t length) { |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 206 | Array* raw_array = Array::Alloc<true>(self, GetArrayClass(), length, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 207 | ComponentSizeShiftWidth(sizeof(T)), |
Ian Rogers | 99cb4ea | 2014-03-26 22:53:56 -0700 | [diff] [blame] | 208 | Runtime::Current()->GetHeap()->GetCurrentAllocator()); |
| 209 | return down_cast<PrimitiveArray<T>*>(raw_array); |
| 210 | } |
| 211 | |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 212 | template<typename T> |
| 213 | inline T PrimitiveArray<T>::Get(int32_t i) { |
| 214 | if (!CheckIsValidIndex(i)) { |
| 215 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 216 | return T(0); |
| 217 | } |
| 218 | return GetWithoutChecks(i); |
| 219 | } |
| 220 | |
| 221 | template<typename T> |
| 222 | inline void PrimitiveArray<T>::Set(int32_t i, T value) { |
| 223 | if (Runtime::Current()->IsActiveTransaction()) { |
| 224 | Set<true>(i, value); |
| 225 | } else { |
| 226 | Set<false>(i, value); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | template<typename T> |
| 231 | template<bool kTransactionActive, bool kCheckTransaction> |
| 232 | inline void PrimitiveArray<T>::Set(int32_t i, T value) { |
| 233 | if (CheckIsValidIndex(i)) { |
| 234 | SetWithoutChecks<kTransactionActive, kCheckTransaction>(i, value); |
| 235 | } else { |
| 236 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | template<typename T> |
Andreas Gampe | 3b45ef2 | 2015-05-26 21:34:09 -0700 | [diff] [blame] | 241 | template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags> |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 242 | inline void PrimitiveArray<T>::SetWithoutChecks(int32_t i, T value) { |
| 243 | if (kCheckTransaction) { |
| 244 | DCHECK_EQ(kTransactionActive, Runtime::Current()->IsActiveTransaction()); |
| 245 | } |
| 246 | if (kTransactionActive) { |
| 247 | Runtime::Current()->RecordWriteArray(this, i, GetWithoutChecks(i)); |
| 248 | } |
Andreas Gampe | 3b45ef2 | 2015-05-26 21:34:09 -0700 | [diff] [blame] | 249 | DCHECK(CheckIsValidIndex<kVerifyFlags>(i)); |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 250 | GetData()[i] = value; |
| 251 | } |
Ian Rogers | 99cb4ea | 2014-03-26 22:53:56 -0700 | [diff] [blame] | 252 | // Backward copy where elements are of aligned appropriately for T. Count is in T sized units. |
| 253 | // 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] | 254 | template<typename T> |
| 255 | static inline void ArrayBackwardCopy(T* d, const T* s, int32_t count) { |
| 256 | d += count; |
| 257 | s += count; |
| 258 | for (int32_t i = 0; i < count; ++i) { |
| 259 | d--; |
| 260 | s--; |
| 261 | *d = *s; |
| 262 | } |
| 263 | } |
| 264 | |
Ian Rogers | 99cb4ea | 2014-03-26 22:53:56 -0700 | [diff] [blame] | 265 | // Forward copy where elements are of aligned appropriately for T. Count is in T sized units. |
| 266 | // 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] | 267 | template<typename T> |
Ian Rogers | 99cb4ea | 2014-03-26 22:53:56 -0700 | [diff] [blame] | 268 | static inline void ArrayForwardCopy(T* d, const T* s, int32_t count) { |
| 269 | for (int32_t i = 0; i < count; ++i) { |
| 270 | *d = *s; |
| 271 | d++; |
| 272 | s++; |
| 273 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 274 | } |
| 275 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 276 | template<class T> |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 277 | inline void PrimitiveArray<T>::Memmove(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos, |
| 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> |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 337 | inline void PrimitiveArray<T>::Memcpy(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos, |
| 338 | int32_t count) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 339 | if (UNLIKELY(count == 0)) { |
| 340 | return; |
| 341 | } |
| 342 | DCHECK_GE(dst_pos, 0); |
| 343 | DCHECK_GE(src_pos, 0); |
| 344 | DCHECK_GT(count, 0); |
| 345 | DCHECK(src != nullptr); |
| 346 | DCHECK_LT(dst_pos, GetLength()); |
| 347 | DCHECK_LE(dst_pos, GetLength() - count); |
| 348 | DCHECK_LT(src_pos, src->GetLength()); |
| 349 | DCHECK_LE(src_pos, src->GetLength() - count); |
| 350 | |
| 351 | // Note for non-byte copies we can't rely on standard libc functions like memcpy(3) and memmove(3) |
| 352 | // in our implementation, because they may copy byte-by-byte. |
| 353 | void* dst_raw = GetRawData(sizeof(T), dst_pos); |
| 354 | const void* src_raw = src->GetRawData(sizeof(T), src_pos); |
| 355 | if (sizeof(T) == sizeof(uint8_t)) { |
| 356 | memcpy(dst_raw, src_raw, count); |
| 357 | } else if (sizeof(T) == sizeof(uint16_t)) { |
| 358 | uint16_t* d = reinterpret_cast<uint16_t*>(dst_raw); |
| 359 | const uint16_t* s = reinterpret_cast<const uint16_t*>(src_raw); |
| 360 | ArrayForwardCopy<uint16_t>(d, s, count); |
| 361 | } else if (sizeof(T) == sizeof(uint32_t)) { |
| 362 | uint32_t* d = reinterpret_cast<uint32_t*>(dst_raw); |
| 363 | const uint32_t* s = reinterpret_cast<const uint32_t*>(src_raw); |
| 364 | ArrayForwardCopy<uint32_t>(d, s, count); |
| 365 | } else { |
| 366 | DCHECK_EQ(sizeof(T), sizeof(uint64_t)); |
| 367 | uint64_t* d = reinterpret_cast<uint64_t*>(dst_raw); |
| 368 | const uint64_t* s = reinterpret_cast<const uint64_t*>(src_raw); |
| 369 | ArrayForwardCopy<uint64_t>(d, s, count); |
| 370 | } |
| 371 | } |
| 372 | |
Mathieu Chartier | dfe02f6 | 2016-02-01 20:15:11 -0800 | [diff] [blame] | 373 | template<typename T, VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption> |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 374 | inline T PointerArray::GetElementPtrSize(uint32_t idx, size_t ptr_size) { |
| 375 | // C style casts here since we sometimes have T be a pointer, or sometimes an integer |
| 376 | // (for stack traces). |
| 377 | if (ptr_size == 8) { |
Mathieu Chartier | dfe02f6 | 2016-02-01 20:15:11 -0800 | [diff] [blame] | 378 | return (T)static_cast<uintptr_t>( |
| 379 | AsLongArray<kVerifyFlags, kReadBarrierOption>()->GetWithoutChecks(idx)); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 380 | } |
| 381 | DCHECK_EQ(ptr_size, 4u); |
Mathieu Chartier | dfe02f6 | 2016-02-01 20:15:11 -0800 | [diff] [blame] | 382 | return (T)static_cast<uintptr_t>( |
| 383 | AsIntArray<kVerifyFlags, kReadBarrierOption>()->GetWithoutChecks(idx)); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Mathieu Chartier | d329a3b | 2016-01-27 15:30:10 -0800 | [diff] [blame] | 386 | template<bool kTransactionActive, bool kUnchecked> |
| 387 | inline void PointerArray::SetElementPtrSize(uint32_t idx, uint64_t element, size_t ptr_size) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 388 | if (ptr_size == 8) { |
| 389 | (kUnchecked ? down_cast<LongArray*>(static_cast<Object*>(this)) : AsLongArray())-> |
Mathieu Chartier | d329a3b | 2016-01-27 15:30:10 -0800 | [diff] [blame] | 390 | SetWithoutChecks<kTransactionActive>(idx, element); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 391 | } else { |
| 392 | DCHECK_EQ(ptr_size, 4u); |
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> |
| 400 | inline void PointerArray::SetElementPtrSize(uint32_t idx, T* element, size_t 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, |
| 408 | size_t pointer_size, |
| 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 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 419 | } // namespace mirror |
| 420 | } // namespace art |
| 421 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 422 | #endif // ART_RUNTIME_MIRROR_ARRAY_INL_H_ |