blob: d24c6fbd2c7e5f1230bc8a82073599422775c9cb [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
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070054 template <typename Type>
Andreas Gampe52edc852016-11-03 15:46:34 -070055 ALWAYS_INLINE ObjPtr(Type* ptr) // NOLINT
56 REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartierf8ac97f2016-10-05 15:56:52 -070057 : reference_(Encode(static_cast<MirrorType*>(ptr))) {
58 static_assert(std::is_base_of<MirrorType, Type>::value,
59 "Input type must be a subtype of the ObjPtr type");
60 }
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070061
Mathieu Chartier0795f232016-09-27 18:43:30 -070062 template <typename 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()))) {
66 static_assert(std::is_base_of<MirrorType, Type>::value,
67 "Input type must be a subtype of the ObjPtr type");
68 }
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070069
Mathieu Chartier0795f232016-09-27 18:43:30 -070070 template <typename Type>
Mathieu Chartierf8ac97f2016-10-05 15:56:52 -070071 ALWAYS_INLINE ObjPtr& operator=(const ObjPtr<Type, kPoison>& other)
72 REQUIRES_SHARED(Locks::mutator_lock_) {
73 static_assert(std::is_base_of<MirrorType, Type>::value,
74 "Input type must be a subtype of the ObjPtr type");
Mathieu Chartier1cc62e42016-10-03 18:01:28 -070075 reference_ = Encode(static_cast<MirrorType*>(other.Ptr()));
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070076 return *this;
77 }
78
79 ALWAYS_INLINE ObjPtr& operator=(MirrorType* ptr) REQUIRES_SHARED(Locks::mutator_lock_) {
80 Assign(ptr);
81 return *this;
82 }
83
84 ALWAYS_INLINE void Assign(MirrorType* ptr) REQUIRES_SHARED(Locks::mutator_lock_) {
85 reference_ = Encode(ptr);
86 }
87
88 ALWAYS_INLINE MirrorType* operator->() const REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier1cc62e42016-10-03 18:01:28 -070089 return Ptr();
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070090 }
91
92 ALWAYS_INLINE bool IsNull() const {
93 return reference_ == 0;
94 }
95
Mathieu Chartier1cc62e42016-10-03 18:01:28 -070096 // Ptr makes sure that the object pointer is valid.
97 ALWAYS_INLINE MirrorType* Ptr() const REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070098 AssertValid();
Mathieu Chartier1cc62e42016-10-03 18:01:28 -070099 return PtrUnchecked();
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700100 }
101
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700102 ALWAYS_INLINE bool IsValid() const REQUIRES_SHARED(Locks::mutator_lock_);
103
104 ALWAYS_INLINE void AssertValid() const REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700105
106 ALWAYS_INLINE bool operator==(const ObjPtr& ptr) const REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700107 return Ptr() == ptr.Ptr();
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700108 }
109
Mathieu Chartier3398c782016-09-30 10:27:43 -0700110 template <typename PointerType>
111 ALWAYS_INLINE bool operator==(const PointerType* ptr) const
112 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700113 return Ptr() == ptr;
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700114 }
115
Mathieu Chartier3398c782016-09-30 10:27:43 -0700116 ALWAYS_INLINE bool operator==(std::nullptr_t) const {
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700117 return IsNull();
118 }
119
120 ALWAYS_INLINE bool operator!=(const ObjPtr& ptr) const REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700121 return Ptr() != ptr.Ptr();
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700122 }
123
Mathieu Chartier3398c782016-09-30 10:27:43 -0700124 template <typename PointerType>
125 ALWAYS_INLINE bool operator!=(const PointerType* ptr) const
126 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700127 return Ptr() != ptr;
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700128 }
129
Mathieu Chartier3398c782016-09-30 10:27:43 -0700130 ALWAYS_INLINE bool operator!=(std::nullptr_t) const {
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700131 return !IsNull();
132 }
133
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700134 // Ptr unchecked does not check that object pointer is valid. Do not use if you can avoid it.
135 ALWAYS_INLINE MirrorType* PtrUnchecked() const {
Mathieu Chartier0795f232016-09-27 18:43:30 -0700136 if (kPoison) {
137 return reinterpret_cast<MirrorType*>(
138 static_cast<uintptr_t>(static_cast<uint32_t>(reference_ << kObjectAlignmentShift)));
139 } else {
140 return reinterpret_cast<MirrorType*>(reference_);
141 }
142 }
143
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700144 // Static function to be friendly with null pointers.
145 template <typename SourceType>
146 static ObjPtr<MirrorType> DownCast(ObjPtr<SourceType> ptr) REQUIRES_SHARED(Locks::mutator_lock_) {
147 static_assert(std::is_base_of<SourceType, MirrorType>::value,
148 "Target type must be a subtype of source type");
149 return static_cast<MirrorType*>(ptr.Ptr());
150 }
151
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700152 private:
153 // Trim off high bits of thread local cookie.
154 ALWAYS_INLINE static uintptr_t TrimCookie(uintptr_t cookie) {
155 return cookie & kCookieMask;
156 }
157
158 ALWAYS_INLINE uintptr_t GetCookie() const {
159 return reference_ >> kCookieShift;
160 }
161
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700162 ALWAYS_INLINE static uintptr_t Encode(MirrorType* ptr) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700163 // The encoded reference and cookie.
164 uintptr_t reference_;
165};
166
Andreas Gampe52edc852016-11-03 15:46:34 -0700167static_assert(std::is_trivially_copyable<ObjPtr<void>>::value,
168 "ObjPtr should be trivially copyable");
169
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700170// Hash function for stl data structures.
171class HashObjPtr {
172 public:
173 template<class MirrorType, bool kPoison>
174 size_t operator()(const ObjPtr<MirrorType, kPoison>& ptr) const NO_THREAD_SAFETY_ANALYSIS {
175 return std::hash<MirrorType*>()(ptr.Ptr());
176 }
177};
178
Mathieu Chartier3398c782016-09-30 10:27:43 -0700179template<class MirrorType, bool kPoison, typename PointerType>
180ALWAYS_INLINE bool operator==(const PointerType* a, const ObjPtr<MirrorType, kPoison>& b)
181 REQUIRES_SHARED(Locks::mutator_lock_) {
182 return b == a;
183}
184
185template<class MirrorType, bool kPoison>
186ALWAYS_INLINE bool operator==(std::nullptr_t, const ObjPtr<MirrorType, kPoison>& b) {
187 return b == nullptr;
188}
189
190template<typename MirrorType, bool kPoison, typename PointerType>
191ALWAYS_INLINE bool operator!=(const PointerType* a, const ObjPtr<MirrorType, kPoison>& b)
192 REQUIRES_SHARED(Locks::mutator_lock_) {
193 return b != a;
194}
195
196template<class MirrorType, bool kPoison>
197ALWAYS_INLINE bool operator!=(std::nullptr_t, const ObjPtr<MirrorType, kPoison>& b) {
198 return b != nullptr;
199}
200
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700201template<class MirrorType, bool kPoison = kIsDebugBuild>
202static inline ObjPtr<MirrorType, kPoison> MakeObjPtr(MirrorType* ptr) {
203 return ObjPtr<MirrorType, kPoison>(ptr);
204}
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700205
Mathieu Chartier3398c782016-09-30 10:27:43 -0700206template<class MirrorType, bool kPoison = kIsDebugBuild>
207static inline ObjPtr<MirrorType, kPoison> MakeObjPtr(ObjPtr<MirrorType, kPoison> ptr) {
208 return ObjPtr<MirrorType, kPoison>(ptr);
209}
210
Mathieu Chartier0795f232016-09-27 18:43:30 -0700211template<class MirrorType, bool kPoison>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700212ALWAYS_INLINE std::ostream& operator<<(std::ostream& os, ObjPtr<MirrorType, kPoison> ptr);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700213
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700214} // namespace art
215
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700216#endif // ART_RUNTIME_OBJ_PTR_H_