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