blob: 2da2ae5650025939e300d0a550e0aabb0f7a5a69 [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
29// Value type representing a pointer to a mirror::Object of type MirrorType
30// Pass kPoison as a template boolean for testing in non-debug builds.
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070031// Since the cookie is thread based, it is not safe to share an ObjPtr between threads.
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070032template<class MirrorType, bool kPoison = kIsDebugBuild>
33class ObjPtr {
34 static constexpr size_t kCookieShift =
Mathieu Chartiera058fdf2016-10-06 15:13:58 -070035 sizeof(kHeapReferenceSize) * kBitsPerByte - kObjectAlignmentShift;
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070036 static constexpr size_t kCookieBits = sizeof(uintptr_t) * kBitsPerByte - kCookieShift;
37 static constexpr uintptr_t kCookieMask = (static_cast<uintptr_t>(1u) << kCookieBits) - 1;
38
39 static_assert(kCookieBits >= kObjectAlignmentShift,
40 "must have a least kObjectAlignmentShift bits");
41
42 public:
43 ALWAYS_INLINE ObjPtr() REQUIRES_SHARED(Locks::mutator_lock_) : reference_(0u) {}
44
Andreas Gampe52edc852016-11-03 15:46:34 -070045 // Note: The following constructors allow implicit conversion. This simplifies code that uses
46 // them, e.g., for parameter passing. However, in general, implicit-conversion constructors
47 // are discouraged and detected by cpplint and clang-tidy. So mark these constructors
48 // as NOLINT (without category, as the categories are different).
49
50 ALWAYS_INLINE ObjPtr(std::nullptr_t) // NOLINT
51 REQUIRES_SHARED(Locks::mutator_lock_)
52 : reference_(0u) {}
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070053
Vladimir Marko19a4d372016-12-08 14:41:46 +000054 template <typename Type,
55 typename = typename std::enable_if<std::is_base_of<MirrorType, Type>::value>::type>
Andreas Gampe52edc852016-11-03 15:46:34 -070056 ALWAYS_INLINE ObjPtr(Type* ptr) // NOLINT
57 REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartierf8ac97f2016-10-05 15:56:52 -070058 : reference_(Encode(static_cast<MirrorType*>(ptr))) {
Mathieu Chartierf8ac97f2016-10-05 15:56:52 -070059 }
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070060
Vladimir Marko19a4d372016-12-08 14:41:46 +000061 template <typename Type,
62 typename = typename std::enable_if<std::is_base_of<MirrorType, Type>::value>::type>
Andreas Gampe52edc852016-11-03 15:46:34 -070063 ALWAYS_INLINE ObjPtr(const ObjPtr<Type, kPoison>& other) // NOLINT
64 REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartierf8ac97f2016-10-05 15:56:52 -070065 : reference_(Encode(static_cast<MirrorType*>(other.Ptr()))) {
Mathieu Chartierf8ac97f2016-10-05 15:56:52 -070066 }
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070067
Vladimir Marko19a4d372016-12-08 14:41:46 +000068 template <typename Type,
69 typename = typename std::enable_if<std::is_base_of<MirrorType, Type>::value>::type>
Mathieu Chartierf8ac97f2016-10-05 15:56:52 -070070 ALWAYS_INLINE ObjPtr& operator=(const ObjPtr<Type, kPoison>& other)
71 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier1cc62e42016-10-03 18:01:28 -070072 reference_ = Encode(static_cast<MirrorType*>(other.Ptr()));
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070073 return *this;
74 }
75
76 ALWAYS_INLINE ObjPtr& operator=(MirrorType* ptr) REQUIRES_SHARED(Locks::mutator_lock_) {
77 Assign(ptr);
78 return *this;
79 }
80
81 ALWAYS_INLINE void Assign(MirrorType* ptr) REQUIRES_SHARED(Locks::mutator_lock_) {
82 reference_ = Encode(ptr);
83 }
84
85 ALWAYS_INLINE MirrorType* operator->() const REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier1cc62e42016-10-03 18:01:28 -070086 return Ptr();
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070087 }
88
89 ALWAYS_INLINE bool IsNull() const {
90 return reference_ == 0;
91 }
92
Mathieu Chartier1cc62e42016-10-03 18:01:28 -070093 // Ptr makes sure that the object pointer is valid.
94 ALWAYS_INLINE MirrorType* Ptr() const REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070095 AssertValid();
Mathieu Chartier1cc62e42016-10-03 18:01:28 -070096 return PtrUnchecked();
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070097 }
98
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070099 ALWAYS_INLINE bool IsValid() const REQUIRES_SHARED(Locks::mutator_lock_);
100
101 ALWAYS_INLINE void AssertValid() const REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700102
103 ALWAYS_INLINE bool operator==(const ObjPtr& ptr) const REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700104 return Ptr() == ptr.Ptr();
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700105 }
106
Mathieu Chartier3398c782016-09-30 10:27:43 -0700107 template <typename PointerType>
108 ALWAYS_INLINE bool operator==(const PointerType* ptr) const
109 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700110 return Ptr() == ptr;
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700111 }
112
Mathieu Chartier3398c782016-09-30 10:27:43 -0700113 ALWAYS_INLINE bool operator==(std::nullptr_t) const {
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700114 return IsNull();
115 }
116
117 ALWAYS_INLINE bool operator!=(const ObjPtr& ptr) const REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700118 return Ptr() != ptr.Ptr();
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700119 }
120
Mathieu Chartier3398c782016-09-30 10:27:43 -0700121 template <typename PointerType>
122 ALWAYS_INLINE bool operator!=(const PointerType* ptr) const
123 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700124 return Ptr() != ptr;
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700125 }
126
Mathieu Chartier3398c782016-09-30 10:27:43 -0700127 ALWAYS_INLINE bool operator!=(std::nullptr_t) const {
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700128 return !IsNull();
129 }
130
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700131 // Ptr unchecked does not check that object pointer is valid. Do not use if you can avoid it.
132 ALWAYS_INLINE MirrorType* PtrUnchecked() const {
Mathieu Chartier0795f232016-09-27 18:43:30 -0700133 if (kPoison) {
134 return reinterpret_cast<MirrorType*>(
135 static_cast<uintptr_t>(static_cast<uint32_t>(reference_ << kObjectAlignmentShift)));
136 } else {
137 return reinterpret_cast<MirrorType*>(reference_);
138 }
139 }
140
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700141 // Static function to be friendly with null pointers.
142 template <typename SourceType>
143 static ObjPtr<MirrorType> DownCast(ObjPtr<SourceType> ptr) REQUIRES_SHARED(Locks::mutator_lock_) {
144 static_assert(std::is_base_of<SourceType, MirrorType>::value,
145 "Target type must be a subtype of source type");
146 return static_cast<MirrorType*>(ptr.Ptr());
147 }
148
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700149 private:
150 // Trim off high bits of thread local cookie.
151 ALWAYS_INLINE static uintptr_t TrimCookie(uintptr_t cookie) {
152 return cookie & kCookieMask;
153 }
154
155 ALWAYS_INLINE uintptr_t GetCookie() const {
156 return reference_ >> kCookieShift;
157 }
158
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700159 ALWAYS_INLINE static uintptr_t Encode(MirrorType* ptr) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700160 // The encoded reference and cookie.
161 uintptr_t reference_;
162};
163
Andreas Gampe52edc852016-11-03 15:46:34 -0700164static_assert(std::is_trivially_copyable<ObjPtr<void>>::value,
165 "ObjPtr should be trivially copyable");
166
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700167// Hash function for stl data structures.
168class HashObjPtr {
169 public:
170 template<class MirrorType, bool kPoison>
171 size_t operator()(const ObjPtr<MirrorType, kPoison>& ptr) const NO_THREAD_SAFETY_ANALYSIS {
172 return std::hash<MirrorType*>()(ptr.Ptr());
173 }
174};
175
Mathieu Chartier3398c782016-09-30 10:27:43 -0700176template<class MirrorType, bool kPoison, typename PointerType>
177ALWAYS_INLINE bool operator==(const PointerType* a, const ObjPtr<MirrorType, kPoison>& b)
178 REQUIRES_SHARED(Locks::mutator_lock_) {
179 return b == a;
180}
181
182template<class MirrorType, bool kPoison>
183ALWAYS_INLINE bool operator==(std::nullptr_t, const ObjPtr<MirrorType, kPoison>& b) {
184 return b == nullptr;
185}
186
187template<typename MirrorType, bool kPoison, typename PointerType>
188ALWAYS_INLINE bool operator!=(const PointerType* a, const ObjPtr<MirrorType, kPoison>& b)
189 REQUIRES_SHARED(Locks::mutator_lock_) {
190 return b != a;
191}
192
193template<class MirrorType, bool kPoison>
194ALWAYS_INLINE bool operator!=(std::nullptr_t, const ObjPtr<MirrorType, kPoison>& b) {
195 return b != nullptr;
196}
197
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700198template<class MirrorType, bool kPoison = kIsDebugBuild>
199static inline ObjPtr<MirrorType, kPoison> MakeObjPtr(MirrorType* ptr) {
200 return ObjPtr<MirrorType, kPoison>(ptr);
201}
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700202
Mathieu Chartier3398c782016-09-30 10:27:43 -0700203template<class MirrorType, bool kPoison = kIsDebugBuild>
204static inline ObjPtr<MirrorType, kPoison> MakeObjPtr(ObjPtr<MirrorType, kPoison> ptr) {
205 return ObjPtr<MirrorType, kPoison>(ptr);
206}
207
Mathieu Chartier0795f232016-09-27 18:43:30 -0700208template<class MirrorType, bool kPoison>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700209ALWAYS_INLINE std::ostream& operator<<(std::ostream& os, ObjPtr<MirrorType, kPoison> ptr);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700210
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700211} // namespace art
212
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700213#endif // ART_RUNTIME_OBJ_PTR_H_