blob: 9a66983de78924f2c85ee0228f96c92981c68853 [file] [log] [blame]
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -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
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070017#ifndef ART_RUNTIME_OBJ_PTR_H_
18#define ART_RUNTIME_OBJ_PTR_H_
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070019
Mathieu Chartier0795f232016-09-27 18:43:30 -070020#include <ostream>
Mathieu Chartierf8ac97f2016-10-05 15:56:52 -070021#include <type_traits>
Mathieu Chartier0795f232016-09-27 18:43:30 -070022
Andreas Gampe52edc852016-11-03 15:46:34 -070023#include "base/macros.h"
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070024#include "base/mutex.h" // For Locks::mutator_lock_.
25#include "globals.h"
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070026
27namespace art {
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070028
Andreas Gampec73cb642017-02-22 10:11:30 -080029constexpr bool kObjPtrPoisoning = kIsDebugBuild;
30
Andreas Gampe81e89382017-10-11 21:52:42 -070031// It turns out that most of the performance overhead comes from copying. Don't validate for now.
32// This defers finding stale ObjPtr objects until they are used.
33constexpr bool kObjPtrPoisoningValidateOnCopy = false;
34
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070035// Value type representing a pointer to a mirror::Object of type MirrorType
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070036// Since the cookie is thread based, it is not safe to share an ObjPtr between threads.
Andreas Gampec73cb642017-02-22 10:11:30 -080037template<class MirrorType>
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070038class ObjPtr {
39 static constexpr size_t kCookieShift =
Mathieu Chartiera058fdf2016-10-06 15:13:58 -070040 sizeof(kHeapReferenceSize) * kBitsPerByte - kObjectAlignmentShift;
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070041 static constexpr size_t kCookieBits = sizeof(uintptr_t) * kBitsPerByte - kCookieShift;
42 static constexpr uintptr_t kCookieMask = (static_cast<uintptr_t>(1u) << kCookieBits) - 1;
43
44 static_assert(kCookieBits >= kObjectAlignmentShift,
45 "must have a least kObjectAlignmentShift bits");
46
47 public:
48 ALWAYS_INLINE ObjPtr() REQUIRES_SHARED(Locks::mutator_lock_) : reference_(0u) {}
49
Andreas Gampe52edc852016-11-03 15:46:34 -070050 // Note: The following constructors allow implicit conversion. This simplifies code that uses
51 // them, e.g., for parameter passing. However, in general, implicit-conversion constructors
52 // are discouraged and detected by cpplint and clang-tidy. So mark these constructors
53 // as NOLINT (without category, as the categories are different).
54
55 ALWAYS_INLINE ObjPtr(std::nullptr_t) // NOLINT
56 REQUIRES_SHARED(Locks::mutator_lock_)
57 : reference_(0u) {}
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070058
Vladimir Marko19a4d372016-12-08 14:41:46 +000059 template <typename Type,
60 typename = typename std::enable_if<std::is_base_of<MirrorType, Type>::value>::type>
Andreas Gampe52edc852016-11-03 15:46:34 -070061 ALWAYS_INLINE ObjPtr(Type* ptr) // NOLINT
62 REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartierf8ac97f2016-10-05 15:56:52 -070063 : reference_(Encode(static_cast<MirrorType*>(ptr))) {
Mathieu Chartierf8ac97f2016-10-05 15:56:52 -070064 }
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070065
Vladimir Marko19a4d372016-12-08 14:41:46 +000066 template <typename Type,
67 typename = typename std::enable_if<std::is_base_of<MirrorType, Type>::value>::type>
Andreas Gampec73cb642017-02-22 10:11:30 -080068 ALWAYS_INLINE ObjPtr(const ObjPtr<Type>& other) // NOLINT
Andreas Gampe52edc852016-11-03 15:46:34 -070069 REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe81e89382017-10-11 21:52:42 -070070 : reference_(kObjPtrPoisoningValidateOnCopy
71 ? Encode(static_cast<MirrorType*>(other.Ptr()))
72 : other.reference_) {
Mathieu Chartierf8ac97f2016-10-05 15:56:52 -070073 }
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070074
Vladimir Marko19a4d372016-12-08 14:41:46 +000075 template <typename Type,
76 typename = typename std::enable_if<std::is_base_of<MirrorType, Type>::value>::type>
Andreas Gampec73cb642017-02-22 10:11:30 -080077 ALWAYS_INLINE ObjPtr& operator=(const ObjPtr<Type>& other)
Mathieu Chartierf8ac97f2016-10-05 15:56:52 -070078 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe81e89382017-10-11 21:52:42 -070079 reference_ = kObjPtrPoisoningValidateOnCopy
80 ? Encode(static_cast<MirrorType*>(other.Ptr()))
81 : other.reference_;
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070082 return *this;
83 }
84
85 ALWAYS_INLINE ObjPtr& operator=(MirrorType* ptr) REQUIRES_SHARED(Locks::mutator_lock_) {
86 Assign(ptr);
87 return *this;
88 }
89
90 ALWAYS_INLINE void Assign(MirrorType* ptr) REQUIRES_SHARED(Locks::mutator_lock_) {
91 reference_ = Encode(ptr);
92 }
93
94 ALWAYS_INLINE MirrorType* operator->() const REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier1cc62e42016-10-03 18:01:28 -070095 return Ptr();
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070096 }
97
98 ALWAYS_INLINE bool IsNull() const {
99 return reference_ == 0;
100 }
101
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700102 // Ptr makes sure that the object pointer is valid.
103 ALWAYS_INLINE MirrorType* Ptr() const REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700104 AssertValid();
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700105 return PtrUnchecked();
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700106 }
107
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700108 ALWAYS_INLINE bool IsValid() const REQUIRES_SHARED(Locks::mutator_lock_);
109
110 ALWAYS_INLINE void AssertValid() const REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700111
112 ALWAYS_INLINE bool operator==(const ObjPtr& ptr) const REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700113 return Ptr() == ptr.Ptr();
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700114 }
115
Mathieu Chartier3398c782016-09-30 10:27:43 -0700116 template <typename PointerType>
117 ALWAYS_INLINE bool operator==(const PointerType* ptr) const
118 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700119 return Ptr() == ptr;
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700120 }
121
Mathieu Chartier3398c782016-09-30 10:27:43 -0700122 ALWAYS_INLINE bool operator==(std::nullptr_t) const {
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700123 return IsNull();
124 }
125
126 ALWAYS_INLINE bool operator!=(const ObjPtr& ptr) const REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700127 return Ptr() != ptr.Ptr();
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700128 }
129
Mathieu Chartier3398c782016-09-30 10:27:43 -0700130 template <typename PointerType>
131 ALWAYS_INLINE bool operator!=(const PointerType* ptr) const
132 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700133 return Ptr() != ptr;
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700134 }
135
Mathieu Chartier3398c782016-09-30 10:27:43 -0700136 ALWAYS_INLINE bool operator!=(std::nullptr_t) const {
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700137 return !IsNull();
138 }
139
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700140 // Ptr unchecked does not check that object pointer is valid. Do not use if you can avoid it.
141 ALWAYS_INLINE MirrorType* PtrUnchecked() const {
Andreas Gampec73cb642017-02-22 10:11:30 -0800142 if (kObjPtrPoisoning) {
Mathieu Chartier0795f232016-09-27 18:43:30 -0700143 return reinterpret_cast<MirrorType*>(
144 static_cast<uintptr_t>(static_cast<uint32_t>(reference_ << kObjectAlignmentShift)));
145 } else {
146 return reinterpret_cast<MirrorType*>(reference_);
147 }
148 }
149
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700150 // Static function to be friendly with null pointers.
151 template <typename SourceType>
152 static ObjPtr<MirrorType> DownCast(ObjPtr<SourceType> ptr) REQUIRES_SHARED(Locks::mutator_lock_) {
153 static_assert(std::is_base_of<SourceType, MirrorType>::value,
154 "Target type must be a subtype of source type");
155 return static_cast<MirrorType*>(ptr.Ptr());
156 }
157
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700158 private:
159 // Trim off high bits of thread local cookie.
160 ALWAYS_INLINE static uintptr_t TrimCookie(uintptr_t cookie) {
161 return cookie & kCookieMask;
162 }
163
164 ALWAYS_INLINE uintptr_t GetCookie() const {
165 return reference_ >> kCookieShift;
166 }
167
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700168 ALWAYS_INLINE static uintptr_t Encode(MirrorType* ptr) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700169 // The encoded reference and cookie.
170 uintptr_t reference_;
Andreas Gampe81e89382017-10-11 21:52:42 -0700171
172 template <class T> friend class ObjPtr; // Required for reference_ access in copy cons/operator.
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700173};
174
Andreas Gampe52edc852016-11-03 15:46:34 -0700175static_assert(std::is_trivially_copyable<ObjPtr<void>>::value,
176 "ObjPtr should be trivially copyable");
177
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700178// Hash function for stl data structures.
179class HashObjPtr {
180 public:
Andreas Gampec73cb642017-02-22 10:11:30 -0800181 template<class MirrorType>
182 size_t operator()(const ObjPtr<MirrorType>& ptr) const NO_THREAD_SAFETY_ANALYSIS {
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700183 return std::hash<MirrorType*>()(ptr.Ptr());
184 }
185};
186
Andreas Gampec73cb642017-02-22 10:11:30 -0800187template<class MirrorType, typename PointerType>
188ALWAYS_INLINE bool operator==(const PointerType* a, const ObjPtr<MirrorType>& b)
Mathieu Chartier3398c782016-09-30 10:27:43 -0700189 REQUIRES_SHARED(Locks::mutator_lock_) {
190 return b == a;
191}
192
Andreas Gampec73cb642017-02-22 10:11:30 -0800193template<class MirrorType>
194ALWAYS_INLINE bool operator==(std::nullptr_t, const ObjPtr<MirrorType>& b) {
Mathieu Chartier3398c782016-09-30 10:27:43 -0700195 return b == nullptr;
196}
197
Andreas Gampec73cb642017-02-22 10:11:30 -0800198template<typename MirrorType, typename PointerType>
199ALWAYS_INLINE bool operator!=(const PointerType* a, const ObjPtr<MirrorType>& b)
Mathieu Chartier3398c782016-09-30 10:27:43 -0700200 REQUIRES_SHARED(Locks::mutator_lock_) {
201 return b != a;
202}
203
Andreas Gampec73cb642017-02-22 10:11:30 -0800204template<class MirrorType>
205ALWAYS_INLINE bool operator!=(std::nullptr_t, const ObjPtr<MirrorType>& b) {
Mathieu Chartier3398c782016-09-30 10:27:43 -0700206 return b != nullptr;
207}
208
Andreas Gampec73cb642017-02-22 10:11:30 -0800209template<class MirrorType>
210static inline ObjPtr<MirrorType> MakeObjPtr(MirrorType* ptr) {
211 return ObjPtr<MirrorType>(ptr);
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700212}
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700213
Andreas Gampec73cb642017-02-22 10:11:30 -0800214template<class MirrorType>
215static inline ObjPtr<MirrorType> MakeObjPtr(ObjPtr<MirrorType> ptr) {
216 return ObjPtr<MirrorType>(ptr);
Mathieu Chartier3398c782016-09-30 10:27:43 -0700217}
218
Andreas Gampec73cb642017-02-22 10:11:30 -0800219template<class MirrorType>
220ALWAYS_INLINE std::ostream& operator<<(std::ostream& os, ObjPtr<MirrorType> ptr);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700221
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700222} // namespace art
223
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700224#endif // ART_RUNTIME_OBJ_PTR_H_