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