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)>(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 37 | size_t header_size = sizeof(Object) + (component_size == sizeof(int64_t) ? 8 : 4); |
| 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 | |
| 49 | size_t header_size = sizeof(Object) + (component_size == sizeof(int64_t) ? 8 : 4); |
| 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 | |
Mathieu Chartier | 7410f29 | 2013-11-24 13:17:35 -0800 | [diff] [blame] | 64 | // Used for setting the array length in the allocation code path to ensure it is guarded by a CAS. |
Mathieu Chartier | 1febddf | 2013-11-20 12:33:14 -0800 | [diff] [blame] | 65 | class SetLengthVisitor { |
| 66 | public: |
| 67 | explicit SetLengthVisitor(int32_t length) : length_(length) { |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 68 | } |
Mathieu Chartier | 1febddf | 2013-11-20 12:33:14 -0800 | [diff] [blame] | 69 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 70 | void operator()(Object* obj) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 71 | // Avoid AsArray as object is not yet in live bitmap or allocation stack. |
| 72 | Array* array = down_cast<Array*>(obj); |
| 73 | // DCHECK(array->IsArrayInstance()); |
Mathieu Chartier | 1febddf | 2013-11-20 12:33:14 -0800 | [diff] [blame] | 74 | array->SetLength(length_); |
| 75 | } |
| 76 | |
| 77 | private: |
| 78 | const int32_t length_; |
| 79 | }; |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 80 | |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 81 | template <bool kIsInstrumented> |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 82 | inline Array* Array::Alloc(Thread* self, Class* array_class, int32_t component_count, |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 83 | size_t component_size, gc::AllocatorType allocator_type) { |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 84 | size_t size = ComputeArraySize(self, array_class, component_count, component_size); |
| 85 | if (UNLIKELY(size == 0)) { |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 86 | return nullptr; |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 87 | } |
| 88 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | 1febddf | 2013-11-20 12:33:14 -0800 | [diff] [blame] | 89 | SetLengthVisitor visitor(component_count); |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 90 | DCHECK(allocator_type != gc::kAllocatorTypeLOS); |
Mathieu Chartier | 1febddf | 2013-11-20 12:33:14 -0800 | [diff] [blame] | 91 | return down_cast<Array*>( |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 92 | heap->AllocObjectWithAllocator<kIsInstrumented, true>(self, array_class, size, |
| 93 | allocator_type, visitor)); |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 96 | template <bool kIsInstrumented> |
| 97 | inline Array* Array::Alloc(Thread* self, Class* array_class, int32_t component_count, |
| 98 | gc::AllocatorType allocator_type) { |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 99 | DCHECK(array_class->IsArrayClass()); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 100 | return Alloc<kIsInstrumented>(self, array_class, component_count, array_class->GetComponentSize(), |
| 101 | allocator_type); |
| 102 | } |
| 103 | template <bool kIsInstrumented> |
| 104 | inline Array* Array::Alloc(Thread* self, Class* array_class, int32_t component_count) { |
| 105 | return Alloc<kIsInstrumented>(self, array_class, component_count, |
| 106 | Runtime::Current()->GetHeap()->GetCurrentAllocator()); |
| 107 | } |
| 108 | |
| 109 | template <bool kIsInstrumented> |
| 110 | inline Array* Array::Alloc(Thread* self, Class* array_class, int32_t component_count, |
| 111 | size_t component_size) { |
| 112 | return Alloc<kIsInstrumented>(self, array_class, component_count, component_size, |
| 113 | Runtime::Current()->GetHeap()->GetCurrentAllocator()); |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 116 | template<class T> |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 117 | inline void PrimitiveArray<T>::VisitRoots(RootCallback* callback, void* arg) { |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 118 | if (array_class_ != nullptr) { |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 119 | callback(reinterpret_cast<mirror::Object**>(&array_class_), arg, 0, kRootStickyClass); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 123 | // Similar to memmove except elements are of aligned appropriately for T, count is in T sized units |
| 124 | // copies are guaranteed not to tear when T is less-than 64bit. |
| 125 | template<typename T> |
| 126 | static inline void ArrayBackwardCopy(T* d, const T* s, int32_t count) { |
| 127 | d += count; |
| 128 | s += count; |
| 129 | for (int32_t i = 0; i < count; ++i) { |
| 130 | d--; |
| 131 | s--; |
| 132 | *d = *s; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | template<class T> |
| 137 | void PrimitiveArray<T>::Memmove(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos, |
| 138 | int32_t count) { |
| 139 | if (UNLIKELY(count == 0)) { |
| 140 | return; |
| 141 | } |
| 142 | DCHECK_GE(dst_pos, 0); |
| 143 | DCHECK_GE(src_pos, 0); |
| 144 | DCHECK_GT(count, 0); |
| 145 | DCHECK(src != nullptr); |
| 146 | DCHECK_LT(dst_pos, GetLength()); |
| 147 | DCHECK_LE(dst_pos, GetLength() - count); |
| 148 | DCHECK_LT(src_pos, src->GetLength()); |
| 149 | DCHECK_LE(src_pos, src->GetLength() - count); |
| 150 | |
| 151 | // Note for non-byte copies we can't rely on standard libc functions like memcpy(3) and memmove(3) |
| 152 | // in our implementation, because they may copy byte-by-byte. |
| 153 | if (LIKELY(src != this) || (dst_pos < src_pos) || (dst_pos - src_pos >= count)) { |
| 154 | // Forward copy ok. |
| 155 | Memcpy(dst_pos, src, src_pos, count); |
| 156 | } else { |
| 157 | // Backward copy necessary. |
| 158 | void* dst_raw = GetRawData(sizeof(T), dst_pos); |
| 159 | const void* src_raw = src->GetRawData(sizeof(T), src_pos); |
| 160 | if (sizeof(T) == sizeof(uint8_t)) { |
| 161 | // TUNING: use memmove here? |
| 162 | uint8_t* d = reinterpret_cast<uint8_t*>(dst_raw); |
| 163 | const uint8_t* s = reinterpret_cast<const uint8_t*>(src_raw); |
| 164 | ArrayBackwardCopy<uint8_t>(d, s, count); |
| 165 | } else if (sizeof(T) == sizeof(uint16_t)) { |
| 166 | uint16_t* d = reinterpret_cast<uint16_t*>(dst_raw); |
| 167 | const uint16_t* s = reinterpret_cast<const uint16_t*>(src_raw); |
| 168 | ArrayBackwardCopy<uint16_t>(d, s, count); |
| 169 | } else if (sizeof(T) == sizeof(uint32_t)) { |
| 170 | uint32_t* d = reinterpret_cast<uint32_t*>(dst_raw); |
| 171 | const uint32_t* s = reinterpret_cast<const uint32_t*>(src_raw); |
| 172 | ArrayBackwardCopy<uint32_t>(d, s, count); |
| 173 | } else { |
| 174 | DCHECK_EQ(sizeof(T), sizeof(uint64_t)); |
| 175 | uint64_t* d = reinterpret_cast<uint64_t*>(dst_raw); |
| 176 | const uint64_t* s = reinterpret_cast<const uint64_t*>(src_raw); |
| 177 | ArrayBackwardCopy<uint64_t>(d, s, count); |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | // Similar to memcpy except elements are of aligned appropriately for T, count is in T sized units |
| 183 | // copies are guaranteed not to tear when T is less-than 64bit. |
| 184 | template<typename T> |
| 185 | static inline void ArrayForwardCopy(T* d, const T* s, int32_t count) { |
| 186 | for (int32_t i = 0; i < count; ++i) { |
| 187 | *d = *s; |
| 188 | d++; |
| 189 | s++; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | |
| 194 | template<class T> |
| 195 | void PrimitiveArray<T>::Memcpy(int32_t dst_pos, PrimitiveArray<T>* src, int32_t src_pos, |
| 196 | int32_t count) { |
| 197 | if (UNLIKELY(count == 0)) { |
| 198 | return; |
| 199 | } |
| 200 | DCHECK_GE(dst_pos, 0); |
| 201 | DCHECK_GE(src_pos, 0); |
| 202 | DCHECK_GT(count, 0); |
| 203 | DCHECK(src != nullptr); |
| 204 | DCHECK_LT(dst_pos, GetLength()); |
| 205 | DCHECK_LE(dst_pos, GetLength() - count); |
| 206 | DCHECK_LT(src_pos, src->GetLength()); |
| 207 | DCHECK_LE(src_pos, src->GetLength() - count); |
| 208 | |
| 209 | // Note for non-byte copies we can't rely on standard libc functions like memcpy(3) and memmove(3) |
| 210 | // in our implementation, because they may copy byte-by-byte. |
| 211 | void* dst_raw = GetRawData(sizeof(T), dst_pos); |
| 212 | const void* src_raw = src->GetRawData(sizeof(T), src_pos); |
| 213 | if (sizeof(T) == sizeof(uint8_t)) { |
| 214 | memcpy(dst_raw, src_raw, count); |
| 215 | } else if (sizeof(T) == sizeof(uint16_t)) { |
| 216 | uint16_t* d = reinterpret_cast<uint16_t*>(dst_raw); |
| 217 | const uint16_t* s = reinterpret_cast<const uint16_t*>(src_raw); |
| 218 | ArrayForwardCopy<uint16_t>(d, s, count); |
| 219 | } else if (sizeof(T) == sizeof(uint32_t)) { |
| 220 | uint32_t* d = reinterpret_cast<uint32_t*>(dst_raw); |
| 221 | const uint32_t* s = reinterpret_cast<const uint32_t*>(src_raw); |
| 222 | ArrayForwardCopy<uint32_t>(d, s, count); |
| 223 | } else { |
| 224 | DCHECK_EQ(sizeof(T), sizeof(uint64_t)); |
| 225 | uint64_t* d = reinterpret_cast<uint64_t*>(dst_raw); |
| 226 | const uint64_t* s = reinterpret_cast<const uint64_t*>(src_raw); |
| 227 | ArrayForwardCopy<uint64_t>(d, s, count); |
| 228 | } |
| 229 | } |
| 230 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 231 | } // namespace mirror |
| 232 | } // namespace art |
| 233 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 234 | #endif // ART_RUNTIME_MIRROR_ARRAY_INL_H_ |