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_OBJECT_ARRAY_INL_H_ |
| 18 | #define ART_RUNTIME_MIRROR_OBJECT_ARRAY_INL_H_ |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 19 | |
Alex Light | a9bbc08 | 2019-11-14 14:51:41 -0800 | [diff] [blame] | 20 | #include "base/globals.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 21 | #include "object_array.h" |
| 22 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 23 | #include <string> |
| 24 | |
| 25 | #include "android-base/stringprintf.h" |
| 26 | |
Ian Rogers | 7e70b00 | 2014-10-08 11:47:24 -0700 | [diff] [blame] | 27 | #include "array-inl.h" |
Andreas Gampe | c15a2f4 | 2017-04-21 12:09:39 -0700 | [diff] [blame] | 28 | #include "class.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 29 | #include "obj_ptr-inl.h" |
| 30 | #include "object-inl.h" |
Vladimir Marko | 557fece | 2019-03-26 14:29:41 +0000 | [diff] [blame] | 31 | #include "read_barrier-inl.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 32 | #include "runtime.h" |
Andreas Gampe | 52ecb65 | 2018-10-24 15:18:21 -0700 | [diff] [blame] | 33 | #include "thread-current-inl.h" |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 34 | #include "write_barrier-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 35 | |
| 36 | namespace art { |
| 37 | namespace mirror { |
| 38 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 39 | template<class T> template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption> |
Vladimir Marko | 423bebb | 2019-03-26 15:17:21 +0000 | [diff] [blame] | 40 | inline ObjPtr<T> ObjectArray<T>::Get(int32_t i) { |
Vladimir Marko | 924ad50 | 2018-09-19 09:48:04 +0100 | [diff] [blame] | 41 | if (!CheckIsValidIndex<kVerifyFlags>(i)) { |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 42 | DCHECK(Thread::Current()->IsExceptionPending()); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 43 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 44 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 45 | return GetFieldObject<T, kVerifyFlags, kReadBarrierOption>(OffsetOfElement(i)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 46 | } |
| 47 | |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 48 | template<class T> template<VerifyObjectFlags kVerifyFlags> |
Mathieu Chartier | 1a5337f | 2016-10-13 13:48:23 -0700 | [diff] [blame] | 49 | inline bool ObjectArray<T>::CheckAssignable(ObjPtr<T> object) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 50 | if (object != nullptr) { |
Vladimir Marko | c524e9e | 2019-03-26 10:54:50 +0000 | [diff] [blame] | 51 | ObjPtr<Class> element_class = GetClass<kVerifyFlags>()->GetComponentType(); |
Sebastien Hertz | 6bdd8f4 | 2013-05-17 14:44:01 +0200 | [diff] [blame] | 52 | if (UNLIKELY(!object->InstanceOf(element_class))) { |
| 53 | ThrowArrayStoreException(object); |
| 54 | return false; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 55 | } |
Sebastien Hertz | 6bdd8f4 | 2013-05-17 14:44:01 +0200 | [diff] [blame] | 56 | } |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | template<class T> |
Mathieu Chartier | 1a5337f | 2016-10-13 13:48:23 -0700 | [diff] [blame] | 61 | inline void ObjectArray<T>::Set(int32_t i, ObjPtr<T> object) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 62 | if (Runtime::Current()->IsActiveTransaction()) { |
| 63 | Set<true>(i, object); |
| 64 | } else { |
| 65 | Set<false>(i, object); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | template<class T> |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 70 | template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags> |
Mathieu Chartier | 1a5337f | 2016-10-13 13:48:23 -0700 | [diff] [blame] | 71 | inline void ObjectArray<T>::Set(int32_t i, ObjPtr<T> object) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 72 | if (CheckIsValidIndex(i) && CheckAssignable<kVerifyFlags>(object)) { |
| 73 | SetFieldObject<kTransactionActive, kCheckTransaction, kVerifyFlags>(OffsetOfElement(i), object); |
Sebastien Hertz | 6bdd8f4 | 2013-05-17 14:44:01 +0200 | [diff] [blame] | 74 | } else { |
| 75 | DCHECK(Thread::Current()->IsExceptionPending()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
| 79 | template<class T> |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 80 | template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags> |
Mathieu Chartier | 1a5337f | 2016-10-13 13:48:23 -0700 | [diff] [blame] | 81 | inline void ObjectArray<T>::SetWithoutChecks(int32_t i, ObjPtr<T> object) { |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 82 | DCHECK(CheckIsValidIndex<kVerifyFlags>(i)); |
| 83 | DCHECK(CheckAssignable<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>(object)); |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 84 | SetFieldObject<kTransactionActive, kCheckTransaction, kVerifyFlags>(OffsetOfElement(i), object); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | template<class T> |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 88 | template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags> |
Mathieu Chartier | 1a5337f | 2016-10-13 13:48:23 -0700 | [diff] [blame] | 89 | inline void ObjectArray<T>::SetWithoutChecksAndWriteBarrier(int32_t i, ObjPtr<T> object) { |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 90 | DCHECK(CheckIsValidIndex<kVerifyFlags>(i)); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 91 | // TODO: enable this check. It fails when writing the image in ImageWriter::FixupObjectArray. |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 92 | // DCHECK(CheckAssignable(object)); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 93 | SetFieldObjectWithoutWriteBarrier<kTransactionActive, kCheckTransaction, kVerifyFlags>( |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 94 | OffsetOfElement(i), object); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Hiroshi Yamauchi | e43b80e | 2016-11-14 13:42:50 -0800 | [diff] [blame] | 97 | template<class T> template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption> |
Vladimir Marko | 423bebb | 2019-03-26 15:17:21 +0000 | [diff] [blame] | 98 | inline ObjPtr<T> ObjectArray<T>::GetWithoutChecks(int32_t i) { |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 99 | DCHECK(CheckIsValidIndex(i)); |
Hiroshi Yamauchi | e43b80e | 2016-11-14 13:42:50 -0800 | [diff] [blame] | 100 | return GetFieldObject<T, kVerifyFlags, kReadBarrierOption>(OffsetOfElement(i)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | template<class T> |
Mathieu Chartier | 1a5337f | 2016-10-13 13:48:23 -0700 | [diff] [blame] | 104 | inline void ObjectArray<T>::AssignableMemmove(int32_t dst_pos, |
| 105 | ObjPtr<ObjectArray<T>> src, |
| 106 | int32_t src_pos, |
| 107 | int32_t count) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 108 | if (kIsDebugBuild) { |
| 109 | for (int i = 0; i < count; ++i) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 110 | // The get will perform the VerifyObject. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 111 | src->GetWithoutChecks(src_pos + i); |
| 112 | } |
| 113 | } |
| 114 | // Perform the memmove using int memmove then perform the write barrier. |
Roland Levillain | 33d6903 | 2015-06-18 18:20:59 +0100 | [diff] [blame] | 115 | static_assert(sizeof(HeapReference<T>) == sizeof(uint32_t), |
| 116 | "art::mirror::HeapReference<T> and uint32_t have different sizes."); |
Mathieu Chartier | fec13d4 | 2016-10-07 12:59:33 -0700 | [diff] [blame] | 117 | // TODO: Optimize this later? |
| 118 | // We can't use memmove since it does not handle read barriers and may do by per byte copying. |
| 119 | // See b/32012820. |
| 120 | const bool copy_forward = (src != this) || (dst_pos < src_pos) || (dst_pos - src_pos >= count); |
| 121 | if (copy_forward) { |
| 122 | // Forward copy. |
Hiroshi Yamauchi | e43b80e | 2016-11-14 13:42:50 -0800 | [diff] [blame] | 123 | bool baker_non_gray_case = false; |
| 124 | if (kUseReadBarrier && kUseBakerReadBarrier) { |
| 125 | uintptr_t fake_address_dependency; |
| 126 | if (!ReadBarrier::IsGray(src.Ptr(), &fake_address_dependency)) { |
| 127 | baker_non_gray_case = true; |
Hiroshi Yamauchi | 6013f77 | 2016-11-16 13:30:17 -0800 | [diff] [blame] | 128 | DCHECK_EQ(fake_address_dependency, 0U); |
Hiroshi Yamauchi | e43b80e | 2016-11-14 13:42:50 -0800 | [diff] [blame] | 129 | src.Assign(reinterpret_cast<ObjectArray<T>*>( |
| 130 | reinterpret_cast<uintptr_t>(src.Ptr()) | fake_address_dependency)); |
| 131 | for (int i = 0; i < count; ++i) { |
| 132 | // We can skip the RB here because 'src' isn't gray. |
Vladimir Marko | 423bebb | 2019-03-26 15:17:21 +0000 | [diff] [blame] | 133 | ObjPtr<T> obj = src->template GetWithoutChecks<kDefaultVerifyFlags, kWithoutReadBarrier>( |
Hiroshi Yamauchi | e43b80e | 2016-11-14 13:42:50 -0800 | [diff] [blame] | 134 | src_pos + i); |
| 135 | SetWithoutChecksAndWriteBarrier<false>(dst_pos + i, obj); |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | if (!baker_non_gray_case) { |
| 140 | for (int i = 0; i < count; ++i) { |
| 141 | // We need a RB here. ObjectArray::GetWithoutChecks() contains a RB. |
Vladimir Marko | 423bebb | 2019-03-26 15:17:21 +0000 | [diff] [blame] | 142 | ObjPtr<T> obj = src->GetWithoutChecks(src_pos + i); |
Hiroshi Yamauchi | e43b80e | 2016-11-14 13:42:50 -0800 | [diff] [blame] | 143 | SetWithoutChecksAndWriteBarrier<false>(dst_pos + i, obj); |
| 144 | } |
Hiroshi Yamauchi | 7971928 | 2014-04-10 12:46:22 -0700 | [diff] [blame] | 145 | } |
| 146 | } else { |
Mathieu Chartier | fec13d4 | 2016-10-07 12:59:33 -0700 | [diff] [blame] | 147 | // Backward copy. |
Hiroshi Yamauchi | e43b80e | 2016-11-14 13:42:50 -0800 | [diff] [blame] | 148 | bool baker_non_gray_case = false; |
| 149 | if (kUseReadBarrier && kUseBakerReadBarrier) { |
| 150 | uintptr_t fake_address_dependency; |
| 151 | if (!ReadBarrier::IsGray(src.Ptr(), &fake_address_dependency)) { |
| 152 | baker_non_gray_case = true; |
Hiroshi Yamauchi | 6013f77 | 2016-11-16 13:30:17 -0800 | [diff] [blame] | 153 | DCHECK_EQ(fake_address_dependency, 0U); |
Hiroshi Yamauchi | e43b80e | 2016-11-14 13:42:50 -0800 | [diff] [blame] | 154 | src.Assign(reinterpret_cast<ObjectArray<T>*>( |
| 155 | reinterpret_cast<uintptr_t>(src.Ptr()) | fake_address_dependency)); |
| 156 | for (int i = count - 1; i >= 0; --i) { |
| 157 | // We can skip the RB here because 'src' isn't gray. |
Vladimir Marko | 423bebb | 2019-03-26 15:17:21 +0000 | [diff] [blame] | 158 | ObjPtr<T> obj = src->template GetWithoutChecks<kDefaultVerifyFlags, kWithoutReadBarrier>( |
Hiroshi Yamauchi | e43b80e | 2016-11-14 13:42:50 -0800 | [diff] [blame] | 159 | src_pos + i); |
| 160 | SetWithoutChecksAndWriteBarrier<false>(dst_pos + i, obj); |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | if (!baker_non_gray_case) { |
| 165 | for (int i = count - 1; i >= 0; --i) { |
| 166 | // We need a RB here. ObjectArray::GetWithoutChecks() contains a RB. |
Vladimir Marko | 423bebb | 2019-03-26 15:17:21 +0000 | [diff] [blame] | 167 | ObjPtr<T> obj = src->GetWithoutChecks(src_pos + i); |
Hiroshi Yamauchi | e43b80e | 2016-11-14 13:42:50 -0800 | [diff] [blame] | 168 | SetWithoutChecksAndWriteBarrier<false>(dst_pos + i, obj); |
| 169 | } |
Mathieu Chartier | fec13d4 | 2016-10-07 12:59:33 -0700 | [diff] [blame] | 170 | } |
Hiroshi Yamauchi | 7971928 | 2014-04-10 12:46:22 -0700 | [diff] [blame] | 171 | } |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 172 | WriteBarrier::ForArrayWrite(this, dst_pos, count); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 173 | if (kIsDebugBuild) { |
| 174 | for (int i = 0; i < count; ++i) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 175 | // The get will perform the VerifyObject. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 176 | GetWithoutChecks(dst_pos + i); |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | template<class T> |
Mathieu Chartier | 1a5337f | 2016-10-13 13:48:23 -0700 | [diff] [blame] | 182 | inline void ObjectArray<T>::AssignableMemcpy(int32_t dst_pos, |
| 183 | ObjPtr<ObjectArray<T>> src, |
| 184 | int32_t src_pos, |
| 185 | int32_t count) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 186 | if (kIsDebugBuild) { |
| 187 | for (int i = 0; i < count; ++i) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 188 | // The get will perform the VerifyObject. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 189 | src->GetWithoutChecks(src_pos + i); |
| 190 | } |
| 191 | } |
| 192 | // Perform the memmove using int memcpy then perform the write barrier. |
Roland Levillain | 33d6903 | 2015-06-18 18:20:59 +0100 | [diff] [blame] | 193 | static_assert(sizeof(HeapReference<T>) == sizeof(uint32_t), |
| 194 | "art::mirror::HeapReference<T> and uint32_t have different sizes."); |
Mathieu Chartier | fec13d4 | 2016-10-07 12:59:33 -0700 | [diff] [blame] | 195 | // TODO: Optimize this later? |
| 196 | // We can't use memmove since it does not handle read barriers and may do by per byte copying. |
| 197 | // See b/32012820. |
Hiroshi Yamauchi | e43b80e | 2016-11-14 13:42:50 -0800 | [diff] [blame] | 198 | bool baker_non_gray_case = false; |
| 199 | if (kUseReadBarrier && kUseBakerReadBarrier) { |
| 200 | uintptr_t fake_address_dependency; |
| 201 | if (!ReadBarrier::IsGray(src.Ptr(), &fake_address_dependency)) { |
| 202 | baker_non_gray_case = true; |
Hiroshi Yamauchi | 6013f77 | 2016-11-16 13:30:17 -0800 | [diff] [blame] | 203 | DCHECK_EQ(fake_address_dependency, 0U); |
Hiroshi Yamauchi | e43b80e | 2016-11-14 13:42:50 -0800 | [diff] [blame] | 204 | src.Assign(reinterpret_cast<ObjectArray<T>*>( |
| 205 | reinterpret_cast<uintptr_t>(src.Ptr()) | fake_address_dependency)); |
| 206 | for (int i = 0; i < count; ++i) { |
| 207 | // We can skip the RB here because 'src' isn't gray. |
Vladimir Marko | 423bebb | 2019-03-26 15:17:21 +0000 | [diff] [blame] | 208 | ObjPtr<Object> obj = |
| 209 | src->template GetWithoutChecks<kDefaultVerifyFlags, kWithoutReadBarrier>(src_pos + i); |
Hiroshi Yamauchi | e43b80e | 2016-11-14 13:42:50 -0800 | [diff] [blame] | 210 | SetWithoutChecksAndWriteBarrier<false>(dst_pos + i, obj); |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | if (!baker_non_gray_case) { |
| 215 | for (int i = 0; i < count; ++i) { |
| 216 | // We need a RB here. ObjectArray::GetWithoutChecks() contains a RB. |
Vladimir Marko | 423bebb | 2019-03-26 15:17:21 +0000 | [diff] [blame] | 217 | ObjPtr<T> obj = src->GetWithoutChecks(src_pos + i); |
Hiroshi Yamauchi | e43b80e | 2016-11-14 13:42:50 -0800 | [diff] [blame] | 218 | SetWithoutChecksAndWriteBarrier<false>(dst_pos + i, obj); |
| 219 | } |
Hiroshi Yamauchi | 7971928 | 2014-04-10 12:46:22 -0700 | [diff] [blame] | 220 | } |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 221 | WriteBarrier::ForArrayWrite(this, dst_pos, count); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 222 | if (kIsDebugBuild) { |
| 223 | for (int i = 0; i < count; ++i) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 224 | // The get will perform the VerifyObject. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 225 | GetWithoutChecks(dst_pos + i); |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | template<class T> |
Andreas Gampe | 85a098a | 2016-03-31 13:30:53 -0700 | [diff] [blame] | 231 | template<bool kTransactionActive> |
Mathieu Chartier | 1a5337f | 2016-10-13 13:48:23 -0700 | [diff] [blame] | 232 | inline void ObjectArray<T>::AssignableCheckingMemcpy(int32_t dst_pos, |
| 233 | ObjPtr<ObjectArray<T>> src, |
| 234 | int32_t src_pos, |
| 235 | int32_t count, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 236 | bool throw_exception) { |
| 237 | DCHECK_NE(this, src) |
| 238 | << "This case should be handled with memmove that handles overlaps correctly"; |
| 239 | // We want to avoid redundant IsAssignableFrom checks where possible, so we cache a class that |
| 240 | // we know is assignable to the destination array's component type. |
Vladimir Marko | c524e9e | 2019-03-26 10:54:50 +0000 | [diff] [blame] | 241 | ObjPtr<Class> dst_class = GetClass()->GetComponentType(); |
| 242 | ObjPtr<Class> lastAssignableElementClass = dst_class; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 243 | |
Vladimir Marko | c524e9e | 2019-03-26 10:54:50 +0000 | [diff] [blame] | 244 | ObjPtr<T> o = nullptr; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 245 | int i = 0; |
Hiroshi Yamauchi | e43b80e | 2016-11-14 13:42:50 -0800 | [diff] [blame] | 246 | bool baker_non_gray_case = false; |
| 247 | if (kUseReadBarrier && kUseBakerReadBarrier) { |
| 248 | uintptr_t fake_address_dependency; |
| 249 | if (!ReadBarrier::IsGray(src.Ptr(), &fake_address_dependency)) { |
| 250 | baker_non_gray_case = true; |
Hiroshi Yamauchi | 6013f77 | 2016-11-16 13:30:17 -0800 | [diff] [blame] | 251 | DCHECK_EQ(fake_address_dependency, 0U); |
Hiroshi Yamauchi | e43b80e | 2016-11-14 13:42:50 -0800 | [diff] [blame] | 252 | src.Assign(reinterpret_cast<ObjectArray<T>*>( |
| 253 | reinterpret_cast<uintptr_t>(src.Ptr()) | fake_address_dependency)); |
| 254 | for (; i < count; ++i) { |
| 255 | // The follow get operations force the objects to be verified. |
| 256 | // We can skip the RB here because 'src' isn't gray. |
| 257 | o = src->template GetWithoutChecks<kDefaultVerifyFlags, kWithoutReadBarrier>( |
| 258 | src_pos + i); |
| 259 | if (o == nullptr) { |
| 260 | // Null is always assignable. |
| 261 | SetWithoutChecks<kTransactionActive>(dst_pos + i, nullptr); |
| 262 | } else { |
| 263 | // TODO: use the underlying class reference to avoid uncompression when not necessary. |
Vladimir Marko | c524e9e | 2019-03-26 10:54:50 +0000 | [diff] [blame] | 264 | ObjPtr<Class> o_class = o->GetClass(); |
Hiroshi Yamauchi | e43b80e | 2016-11-14 13:42:50 -0800 | [diff] [blame] | 265 | if (LIKELY(lastAssignableElementClass == o_class)) { |
| 266 | SetWithoutChecks<kTransactionActive>(dst_pos + i, o); |
| 267 | } else if (LIKELY(dst_class->IsAssignableFrom(o_class))) { |
| 268 | lastAssignableElementClass = o_class; |
| 269 | SetWithoutChecks<kTransactionActive>(dst_pos + i, o); |
| 270 | } else { |
| 271 | // Can't put this element into the array, break to perform write-barrier and throw |
| 272 | // exception. |
| 273 | break; |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | if (!baker_non_gray_case) { |
| 280 | for (; i < count; ++i) { |
| 281 | // The follow get operations force the objects to be verified. |
| 282 | // We need a RB here. ObjectArray::GetWithoutChecks() contains a RB. |
| 283 | o = src->GetWithoutChecks(src_pos + i); |
| 284 | if (o == nullptr) { |
| 285 | // Null is always assignable. |
| 286 | SetWithoutChecks<kTransactionActive>(dst_pos + i, nullptr); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 287 | } else { |
Hiroshi Yamauchi | e43b80e | 2016-11-14 13:42:50 -0800 | [diff] [blame] | 288 | // TODO: use the underlying class reference to avoid uncompression when not necessary. |
Vladimir Marko | dfc0de7 | 2019-04-01 10:57:55 +0100 | [diff] [blame] | 289 | ObjPtr<Class> o_class = o->GetClass(); |
Hiroshi Yamauchi | e43b80e | 2016-11-14 13:42:50 -0800 | [diff] [blame] | 290 | if (LIKELY(lastAssignableElementClass == o_class)) { |
| 291 | SetWithoutChecks<kTransactionActive>(dst_pos + i, o); |
| 292 | } else if (LIKELY(dst_class->IsAssignableFrom(o_class))) { |
| 293 | lastAssignableElementClass = o_class; |
| 294 | SetWithoutChecks<kTransactionActive>(dst_pos + i, o); |
| 295 | } else { |
| 296 | // Can't put this element into the array, break to perform write-barrier and throw |
| 297 | // exception. |
| 298 | break; |
| 299 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 300 | } |
| 301 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 302 | } |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 303 | WriteBarrier::ForArrayWrite(this, dst_pos, count); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 304 | if (UNLIKELY(i != count)) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 305 | std::string actualSrcType(mirror::Object::PrettyTypeOf(o)); |
| 306 | std::string dstType(PrettyTypeOf()); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 307 | Thread* self = Thread::Current(); |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 308 | std::string msg = android::base::StringPrintf( |
| 309 | "source[%d] of type %s cannot be stored in destination array of type %s", |
| 310 | src_pos + i, |
| 311 | actualSrcType.c_str(), |
| 312 | dstType.c_str()); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 313 | if (throw_exception) { |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 314 | self->ThrowNewException("Ljava/lang/ArrayStoreException;", msg.c_str()); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 315 | } else { |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 316 | LOG(FATAL) << msg; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 317 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | |
| 321 | template<class T> |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 322 | inline MemberOffset ObjectArray<T>::OffsetOfElement(int32_t i) { |
Mathieu Chartier | 1a5337f | 2016-10-13 13:48:23 -0700 | [diff] [blame] | 323 | return MemberOffset(DataOffset(kHeapReferenceSize).Int32Value() + (i * kHeapReferenceSize)); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 324 | } |
| 325 | |
Mathieu Chartier | 059ef3d | 2015-08-18 13:54:21 -0700 | [diff] [blame] | 326 | template<class T> template<typename Visitor> |
Hiroshi Yamauchi | 723e6ce | 2015-10-28 20:59:47 -0700 | [diff] [blame] | 327 | inline void ObjectArray<T>::VisitReferences(const Visitor& visitor) { |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 328 | const size_t length = static_cast<size_t>(GetLength()); |
| 329 | for (size_t i = 0; i < length; ++i) { |
| 330 | visitor(this, OffsetOfElement(i), false); |
| 331 | } |
| 332 | } |
| 333 | |
Alex Light | a9bbc08 | 2019-11-14 14:51:41 -0800 | [diff] [blame] | 334 | template <class T> |
| 335 | inline ConstObjPtrArrayIter<T> ObjectArray<T>::cbegin() const { |
| 336 | return ConstObjPtrArrayIter<T>(this, 0); |
| 337 | } |
| 338 | template <class T> |
| 339 | inline ConstObjPtrArrayIter<T> ObjectArray<T>::cend() const { |
| 340 | return ConstObjPtrArrayIter<T>(this, GetLength()); |
| 341 | } |
| 342 | template <class T> |
| 343 | inline ConstHandleArrayIter<T> ObjectArray<T>::cbegin(const Handle<ObjectArray<T>>& h_this) { |
| 344 | return ConstHandleArrayIter<T>(h_this, 0); |
| 345 | } |
| 346 | template <class T> |
| 347 | inline ConstHandleArrayIter<T> ObjectArray<T>::cend(const Handle<ObjectArray<T>>& h_this) { |
| 348 | return ConstHandleArrayIter<T>(h_this, h_this->GetLength()); |
| 349 | } |
| 350 | |
| 351 | template <class T> |
| 352 | inline ObjPtrArrayIter<T> ObjectArray<T>::begin() { |
| 353 | return ObjPtrArrayIter<T>(this, 0); |
| 354 | } |
| 355 | template <class T> |
| 356 | inline ObjPtrArrayIter<T> ObjectArray<T>::end() { |
| 357 | return ObjPtrArrayIter<T>(this, GetLength()); |
| 358 | } |
| 359 | template <class T> |
| 360 | inline HandleArrayIter<T> ObjectArray<T>::begin(Handle<ObjectArray<T>>& h_this) { |
| 361 | return HandleArrayIter<T>(h_this, 0); |
| 362 | } |
| 363 | template <class T> |
| 364 | inline HandleArrayIter<T> ObjectArray<T>::end(Handle<ObjectArray<T>>& h_this) { |
| 365 | return HandleArrayIter<T>(h_this, h_this->GetLength()); |
| 366 | } |
| 367 | |
| 368 | template<typename T, typename C> |
| 369 | inline void ArrayIter<T, C>::CheckIdx() const { |
| 370 | if (kIsDebugBuild) { |
| 371 | Locks::mutator_lock_->AssertSharedHeld(Thread::Current()); |
| 372 | } |
| 373 | DCHECK_LE(0, idx_); |
| 374 | DCHECK_LE(idx_, array_->GetLength()); |
| 375 | } |
| 376 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 377 | } // namespace mirror |
| 378 | } // namespace art |
| 379 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 380 | #endif // ART_RUNTIME_MIRROR_OBJECT_ARRAY_INL_H_ |