blob: ae13465e5989920e287121007bebeb40be14eb94 [file] [log] [blame]
Mathieu Chartiera59d9b22016-09-26 18:13:17 -07001/*
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 Gampe8764dc32019-01-07 15:20:12 -080020#include <ostream>
21
Andreas Gampe3b7dc352017-06-06 20:02:03 -070022#include "base/bit_utils.h"
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070023#include "obj_ptr.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070024#include "thread-current-inl.h"
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070025
26namespace art {
27
Andreas Gampec73cb642017-02-22 10:11:30 -080028template<class MirrorType>
Vladimir Marko4df2d802018-09-27 16:42:44 +000029inline uintptr_t ObjPtr<MirrorType>::GetCurrentTrimedCookie() {
30 Thread* self = Thread::Current();
31 if (UNLIKELY(self == nullptr)) {
32 return kCookieMask;
33 }
34 return self->GetPoisonObjectCookie() & kCookieMask;
35}
36
37template<class MirrorType>
Andreas Gampec73cb642017-02-22 10:11:30 -080038inline bool ObjPtr<MirrorType>::IsValid() const {
39 if (!kObjPtrPoisoning || IsNull()) {
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070040 return true;
41 }
Vladimir Marko4df2d802018-09-27 16:42:44 +000042 return GetCookie() == GetCurrentTrimedCookie();
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070043}
44
Andreas Gampec73cb642017-02-22 10:11:30 -080045template<class MirrorType>
46inline void ObjPtr<MirrorType>::AssertValid() const {
47 if (kObjPtrPoisoning) {
Mathieu Chartier9d156d52016-10-06 17:44:26 -070048 CHECK(IsValid()) << "Stale object pointer " << PtrUnchecked() << " , expected cookie "
Vladimir Marko4df2d802018-09-27 16:42:44 +000049 << GetCurrentTrimedCookie() << " but got " << GetCookie();
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070050 }
51}
52
Andreas Gampec73cb642017-02-22 10:11:30 -080053template<class MirrorType>
54inline uintptr_t ObjPtr<MirrorType>::Encode(MirrorType* ptr) {
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070055 uintptr_t ref = reinterpret_cast<uintptr_t>(ptr);
Mathieu Chartier0795f232016-09-27 18:43:30 -070056 DCHECK_ALIGNED(ref, kObjectAlignment);
Andreas Gampec73cb642017-02-22 10:11:30 -080057 if (kObjPtrPoisoning && ref != 0) {
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070058 DCHECK_LE(ref, 0xFFFFFFFFU);
59 ref >>= kObjectAlignmentShift;
60 // Put cookie in high bits.
Vladimir Marko4df2d802018-09-27 16:42:44 +000061 ref |= GetCurrentTrimedCookie() << kCookieShift;
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070062 }
63 return ref;
64}
65
Andreas Gampec73cb642017-02-22 10:11:30 -080066template<class MirrorType>
Vladimir Markof57f2ed2019-03-25 15:55:42 +000067template <typename Type,
68 typename /* = typename std::enable_if_t<std::is_base_of_v<MirrorType, Type>> */>
69inline ObjPtr<MirrorType>::ObjPtr(Type* ptr)
70 : reference_(Encode(static_cast<MirrorType*>(ptr))) {
71}
72
73template<class MirrorType>
74template <typename Type,
75 typename /* = typename std::enable_if_t<std::is_base_of_v<MirrorType, Type>> */>
76inline ObjPtr<MirrorType>::ObjPtr(const ObjPtr<Type>& other)
77 : reference_(other.reference_) {
78 if (kObjPtrPoisoningValidateOnCopy) {
79 AssertValid();
80 }
81}
82
83template<class MirrorType>
84template <typename Type,
85 typename /* = typename std::enable_if_t<std::is_base_of_v<MirrorType, Type>> */>
86inline ObjPtr<MirrorType>& ObjPtr<MirrorType>::operator=(const ObjPtr<Type>& other) {
87 reference_ = other.reference_;
88 if (kObjPtrPoisoningValidateOnCopy) {
89 AssertValid();
90 }
91 return *this;
92}
93
94template<class MirrorType>
95OBJPTR_INLINE ObjPtr<MirrorType>& ObjPtr<MirrorType>::operator=(MirrorType* ptr) {
96 Assign(ptr);
97 return *this;
98}
99
100template<class MirrorType>
101inline void ObjPtr<MirrorType>::Assign(MirrorType* ptr) {
102 reference_ = Encode(ptr);
103}
104
105template<class MirrorType>
106inline MirrorType* ObjPtr<MirrorType>::operator->() const {
107 return Ptr();
108}
109
110template<class MirrorType>
111inline MirrorType* ObjPtr<MirrorType>::Ptr() const {
112 AssertValid();
113 return PtrUnchecked();
114}
115
116template<class MirrorType>
117template <typename SourceType>
118inline ObjPtr<MirrorType> ObjPtr<MirrorType>::DownCast(ObjPtr<SourceType> ptr) {
119 static_assert(std::is_base_of_v<SourceType, MirrorType>,
120 "Target type must be a subtype of source type");
121 return static_cast<MirrorType*>(ptr.Ptr());
122}
123
124template<class MirrorType>
Vladimir Markod7e9bbf2019-03-28 13:18:57 +0000125template <typename SourceType>
126inline ObjPtr<MirrorType> ObjPtr<MirrorType>::DownCast(SourceType* ptr) {
127 static_assert(std::is_base_of_v<SourceType, MirrorType>,
128 "Target type must be a subtype of source type");
129 return static_cast<MirrorType*>(ptr);
130}
131
132template<class MirrorType>
Vladimir Markof57f2ed2019-03-25 15:55:42 +0000133size_t HashObjPtr::operator()(const ObjPtr<MirrorType>& ptr) const {
134 return std::hash<MirrorType*>()(ptr.Ptr());
135}
136
137template<class MirrorType1, class MirrorType2>
138inline std::enable_if_t<std::is_base_of_v<MirrorType1, MirrorType2> ||
139 std::is_base_of_v<MirrorType2, MirrorType1>, bool>
Vladimir Marko3b458902019-03-26 17:08:41 +0000140operator==(ObjPtr<MirrorType1> lhs, ObjPtr<MirrorType2> rhs) {
Vladimir Markof57f2ed2019-03-25 15:55:42 +0000141 return lhs.Ptr() == rhs.Ptr();
142}
143
144template<class MirrorType1, class MirrorType2>
145inline std::enable_if_t<std::is_base_of_v<MirrorType1, MirrorType2> ||
146 std::is_base_of_v<MirrorType2, MirrorType1>, bool>
Vladimir Marko3b458902019-03-26 17:08:41 +0000147operator==(const MirrorType1* lhs, ObjPtr<MirrorType2> rhs) {
Vladimir Markof57f2ed2019-03-25 15:55:42 +0000148 return lhs == rhs.Ptr();
149}
150
151template<class MirrorType1, class MirrorType2>
152inline std::enable_if_t<std::is_base_of_v<MirrorType1, MirrorType2> ||
153 std::is_base_of_v<MirrorType2, MirrorType1>, bool>
Vladimir Marko3b458902019-03-26 17:08:41 +0000154operator==(ObjPtr<MirrorType1> lhs, const MirrorType2* rhs) {
Vladimir Markof57f2ed2019-03-25 15:55:42 +0000155 return lhs.Ptr() == rhs;
156}
157
158template<class MirrorType1, class MirrorType2>
159inline std::enable_if_t<std::is_base_of_v<MirrorType1, MirrorType2> ||
160 std::is_base_of_v<MirrorType2, MirrorType1>, bool>
Vladimir Marko3b458902019-03-26 17:08:41 +0000161operator!=(ObjPtr<MirrorType1> lhs, ObjPtr<MirrorType2> rhs) {
Vladimir Markof57f2ed2019-03-25 15:55:42 +0000162 return !(lhs == rhs);
163}
164
165template<class MirrorType1, class MirrorType2>
166inline std::enable_if_t<std::is_base_of_v<MirrorType1, MirrorType2> ||
167 std::is_base_of_v<MirrorType2, MirrorType1>, bool>
Vladimir Marko3b458902019-03-26 17:08:41 +0000168operator!=(const MirrorType1* lhs, ObjPtr<MirrorType2> rhs) {
Vladimir Markof57f2ed2019-03-25 15:55:42 +0000169 return !(lhs == rhs);
170}
171
172template<class MirrorType1, class MirrorType2>
173inline std::enable_if_t<std::is_base_of_v<MirrorType1, MirrorType2> ||
174 std::is_base_of_v<MirrorType2, MirrorType1>, bool>
Vladimir Marko3b458902019-03-26 17:08:41 +0000175operator!=(ObjPtr<MirrorType1> lhs, const MirrorType2* rhs) {
Vladimir Markof57f2ed2019-03-25 15:55:42 +0000176 return !(lhs == rhs);
177}
178
179template<class MirrorType>
Andreas Gampec73cb642017-02-22 10:11:30 -0800180inline std::ostream& operator<<(std::ostream& os, ObjPtr<MirrorType> ptr) {
Mathieu Chartier0795f232016-09-27 18:43:30 -0700181 // May be used for dumping bad pointers, do not use the checked version.
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700182 return os << ptr.PtrUnchecked();
Mathieu Chartier0795f232016-09-27 18:43:30 -0700183}
184
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700185} // namespace art
186
187#endif // ART_RUNTIME_OBJ_PTR_INL_H_