Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
| 17 | #ifndef ART_RUNTIME_OBJ_PTR_INL_H_ |
| 18 | #define ART_RUNTIME_OBJ_PTR_INL_H_ |
| 19 | |
Andreas Gampe | 3b7dc35 | 2017-06-06 20:02:03 -0700 | [diff] [blame] | 20 | #include "base/bit_utils.h" |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 21 | #include "obj_ptr.h" |
Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 22 | #include "thread-current-inl.h" |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 23 | |
| 24 | namespace art { |
| 25 | |
Andreas Gampe | c73cb64 | 2017-02-22 10:11:30 -0800 | [diff] [blame] | 26 | template<class MirrorType> |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 27 | inline uintptr_t ObjPtr<MirrorType>::GetCurrentTrimedCookie() { |
| 28 | Thread* self = Thread::Current(); |
| 29 | if (UNLIKELY(self == nullptr)) { |
| 30 | return kCookieMask; |
| 31 | } |
| 32 | return self->GetPoisonObjectCookie() & kCookieMask; |
| 33 | } |
| 34 | |
| 35 | template<class MirrorType> |
Andreas Gampe | c73cb64 | 2017-02-22 10:11:30 -0800 | [diff] [blame] | 36 | inline bool ObjPtr<MirrorType>::IsValid() const { |
| 37 | if (!kObjPtrPoisoning || IsNull()) { |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 38 | return true; |
| 39 | } |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 40 | return GetCookie() == GetCurrentTrimedCookie(); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Andreas Gampe | c73cb64 | 2017-02-22 10:11:30 -0800 | [diff] [blame] | 43 | template<class MirrorType> |
| 44 | inline void ObjPtr<MirrorType>::AssertValid() const { |
| 45 | if (kObjPtrPoisoning) { |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 46 | CHECK(IsValid()) << "Stale object pointer " << PtrUnchecked() << " , expected cookie " |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 47 | << GetCurrentTrimedCookie() << " but got " << GetCookie(); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | |
Andreas Gampe | c73cb64 | 2017-02-22 10:11:30 -0800 | [diff] [blame] | 51 | template<class MirrorType> |
| 52 | inline uintptr_t ObjPtr<MirrorType>::Encode(MirrorType* ptr) { |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 53 | uintptr_t ref = reinterpret_cast<uintptr_t>(ptr); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 54 | DCHECK_ALIGNED(ref, kObjectAlignment); |
Andreas Gampe | c73cb64 | 2017-02-22 10:11:30 -0800 | [diff] [blame] | 55 | if (kObjPtrPoisoning && ref != 0) { |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 56 | DCHECK_LE(ref, 0xFFFFFFFFU); |
| 57 | ref >>= kObjectAlignmentShift; |
| 58 | // Put cookie in high bits. |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 59 | ref |= GetCurrentTrimedCookie() << kCookieShift; |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 60 | } |
| 61 | return ref; |
| 62 | } |
| 63 | |
Andreas Gampe | c73cb64 | 2017-02-22 10:11:30 -0800 | [diff] [blame] | 64 | template<class MirrorType> |
| 65 | inline std::ostream& operator<<(std::ostream& os, ObjPtr<MirrorType> ptr) { |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 66 | // May be used for dumping bad pointers, do not use the checked version. |
Mathieu Chartier | 1cc62e4 | 2016-10-03 18:01:28 -0700 | [diff] [blame] | 67 | return os << ptr.PtrUnchecked(); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 70 | } // namespace art |
| 71 | |
| 72 | #endif // ART_RUNTIME_OBJ_PTR_INL_H_ |