blob: 1f79d375166ceae1bedd63035b97c9564cca009d [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
Andreas Gampe8764dc32019-01-07 15:20:12 -080020#include <iosfwd>
Mathieu Chartierf8ac97f2016-10-05 15:56:52 -070021#include <type_traits>
Mathieu Chartier0795f232016-09-27 18:43:30 -070022
Andreas Gampe7fbc4a52018-11-28 08:26:47 -080023#include "base/locks.h" // For Locks::mutator_lock_.
Andreas Gampe52edc852016-11-03 15:46:34 -070024#include "base/macros.h"
Andreas Gampe5a0430d2019-01-04 14:33:57 -080025#include "runtime_globals.h"
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070026
David Srbecky3f704c02018-11-16 17:05:15 +000027// Always inline ObjPtr methods even in debug builds.
28#define OBJPTR_INLINE __attribute__ ((always_inline))
29
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070030namespace art {
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070031
Andreas Gampec73cb642017-02-22 10:11:30 -080032constexpr bool kObjPtrPoisoning = kIsDebugBuild;
33
Andreas Gampe81e89382017-10-11 21:52:42 -070034// It turns out that most of the performance overhead comes from copying. Don't validate for now.
35// This defers finding stale ObjPtr objects until they are used.
36constexpr bool kObjPtrPoisoningValidateOnCopy = false;
37
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070038// Value type representing a pointer to a mirror::Object of type MirrorType
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070039// Since the cookie is thread based, it is not safe to share an ObjPtr between threads.
Andreas Gampec73cb642017-02-22 10:11:30 -080040template<class MirrorType>
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070041class ObjPtr {
42 static constexpr size_t kCookieShift =
Evgenii Stepanov797ffe52018-01-09 16:21:46 -080043 kHeapReferenceSize * kBitsPerByte - kObjectAlignmentShift;
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070044 static constexpr size_t kCookieBits = sizeof(uintptr_t) * kBitsPerByte - kCookieShift;
45 static constexpr uintptr_t kCookieMask = (static_cast<uintptr_t>(1u) << kCookieBits) - 1;
46
47 static_assert(kCookieBits >= kObjectAlignmentShift,
48 "must have a least kObjectAlignmentShift bits");
49
50 public:
David Srbecky3f704c02018-11-16 17:05:15 +000051 OBJPTR_INLINE ObjPtr() REQUIRES_SHARED(Locks::mutator_lock_) : reference_(0u) {}
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070052
Andreas Gampe52edc852016-11-03 15:46:34 -070053 // Note: The following constructors allow implicit conversion. This simplifies code that uses
54 // them, e.g., for parameter passing. However, in general, implicit-conversion constructors
Igor Murashkin5573c372017-11-16 13:34:30 -080055 // are discouraged and detected by clang-tidy.
Andreas Gampe52edc852016-11-03 15:46:34 -070056
David Srbecky3f704c02018-11-16 17:05:15 +000057 OBJPTR_INLINE ObjPtr(std::nullptr_t)
Andreas Gampe52edc852016-11-03 15:46:34 -070058 REQUIRES_SHARED(Locks::mutator_lock_)
59 : reference_(0u) {}
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070060
Vladimir Marko19a4d372016-12-08 14:41:46 +000061 template <typename Type,
Vladimir Markof57f2ed2019-03-25 15:55:42 +000062 typename = typename std::enable_if_t<std::is_base_of_v<MirrorType, Type>>>
63 OBJPTR_INLINE ObjPtr(Type* ptr) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070064
Vladimir Marko19a4d372016-12-08 14:41:46 +000065 template <typename Type,
Vladimir Markof57f2ed2019-03-25 15:55:42 +000066 typename = typename std::enable_if_t<std::is_base_of_v<MirrorType, Type>>>
67 OBJPTR_INLINE ObjPtr(const ObjPtr<Type>& other) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070068
Vladimir Marko19a4d372016-12-08 14:41:46 +000069 template <typename Type,
Vladimir Markof57f2ed2019-03-25 15:55:42 +000070 typename = typename std::enable_if_t<std::is_base_of_v<MirrorType, Type>>>
71 OBJPTR_INLINE ObjPtr& operator=(const ObjPtr<Type>& other) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070072
Vladimir Markof57f2ed2019-03-25 15:55:42 +000073 OBJPTR_INLINE ObjPtr& operator=(MirrorType* ptr) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070074
Vladimir Markof57f2ed2019-03-25 15:55:42 +000075 OBJPTR_INLINE void Assign(MirrorType* ptr) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070076
Vladimir Markof57f2ed2019-03-25 15:55:42 +000077 OBJPTR_INLINE MirrorType* operator->() const REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070078
David Srbecky3f704c02018-11-16 17:05:15 +000079 OBJPTR_INLINE bool IsNull() const {
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070080 return reference_ == 0;
81 }
82
Mathieu Chartier1cc62e42016-10-03 18:01:28 -070083 // Ptr makes sure that the object pointer is valid.
Vladimir Markof57f2ed2019-03-25 15:55:42 +000084 OBJPTR_INLINE MirrorType* Ptr() const REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070085
David Srbecky3f704c02018-11-16 17:05:15 +000086 OBJPTR_INLINE bool IsValid() const REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070087
David Srbecky3f704c02018-11-16 17:05:15 +000088 OBJPTR_INLINE void AssertValid() const REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070089
Mathieu Chartier1cc62e42016-10-03 18:01:28 -070090 // Ptr unchecked does not check that object pointer is valid. Do not use if you can avoid it.
David Srbecky3f704c02018-11-16 17:05:15 +000091 OBJPTR_INLINE MirrorType* PtrUnchecked() const {
Andreas Gampec73cb642017-02-22 10:11:30 -080092 if (kObjPtrPoisoning) {
Mathieu Chartier0795f232016-09-27 18:43:30 -070093 return reinterpret_cast<MirrorType*>(
94 static_cast<uintptr_t>(static_cast<uint32_t>(reference_ << kObjectAlignmentShift)));
95 } else {
96 return reinterpret_cast<MirrorType*>(reference_);
97 }
98 }
99
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700100 // Static function to be friendly with null pointers.
101 template <typename SourceType>
Vladimir Markof57f2ed2019-03-25 15:55:42 +0000102 static ObjPtr<MirrorType> DownCast(ObjPtr<SourceType> ptr) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700103
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700104 private:
105 // Trim off high bits of thread local cookie.
David Srbecky3f704c02018-11-16 17:05:15 +0000106 OBJPTR_INLINE static uintptr_t GetCurrentTrimedCookie();
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700107
David Srbecky3f704c02018-11-16 17:05:15 +0000108 OBJPTR_INLINE uintptr_t GetCookie() const {
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700109 return reference_ >> kCookieShift;
110 }
111
David Srbecky3f704c02018-11-16 17:05:15 +0000112 OBJPTR_INLINE static uintptr_t Encode(MirrorType* ptr) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700113 // The encoded reference and cookie.
114 uintptr_t reference_;
Andreas Gampe81e89382017-10-11 21:52:42 -0700115
116 template <class T> friend class ObjPtr; // Required for reference_ access in copy cons/operator.
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700117};
118
Andreas Gampe52edc852016-11-03 15:46:34 -0700119static_assert(std::is_trivially_copyable<ObjPtr<void>>::value,
120 "ObjPtr should be trivially copyable");
121
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700122// Hash function for stl data structures.
123class HashObjPtr {
124 public:
Andreas Gampec73cb642017-02-22 10:11:30 -0800125 template<class MirrorType>
Vladimir Markof57f2ed2019-03-25 15:55:42 +0000126 size_t operator()(const ObjPtr<MirrorType>& ptr) const NO_THREAD_SAFETY_ANALYSIS;
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700127};
128
Vladimir Markof57f2ed2019-03-25 15:55:42 +0000129template<class MirrorType1, class MirrorType2>
130OBJPTR_INLINE std::enable_if_t<std::is_base_of_v<MirrorType1, MirrorType2> ||
131 std::is_base_of_v<MirrorType2, MirrorType1>, bool>
132operator==(ObjPtr<MirrorType1> lhs, ObjPtr<MirrorType2> rhs)
133 REQUIRES_SHARED(Locks::mutator_lock_);
134
135template<class MirrorType1, class MirrorType2>
136OBJPTR_INLINE std::enable_if_t<std::is_base_of_v<MirrorType1, MirrorType2> ||
137 std::is_base_of_v<MirrorType2, MirrorType1>, bool>
138operator==(const MirrorType1* lhs, ObjPtr<MirrorType2> rhs)
139 REQUIRES_SHARED(Locks::mutator_lock_);
140
141template<class MirrorType1, class MirrorType2>
142OBJPTR_INLINE std::enable_if_t<std::is_base_of_v<MirrorType1, MirrorType2> ||
143 std::is_base_of_v<MirrorType2, MirrorType1>, bool>
144operator==(ObjPtr<MirrorType1> lhs, const MirrorType2* rhs)
145 REQUIRES_SHARED(Locks::mutator_lock_);
146
147template<class MirrorType>
148OBJPTR_INLINE bool operator==(ObjPtr<MirrorType> ptr, std::nullptr_t) {
149 return ptr.IsNull();
Mathieu Chartier3398c782016-09-30 10:27:43 -0700150}
151
Andreas Gampec73cb642017-02-22 10:11:30 -0800152template<class MirrorType>
Vladimir Markof57f2ed2019-03-25 15:55:42 +0000153OBJPTR_INLINE bool operator==(std::nullptr_t, ObjPtr<MirrorType> ptr) {
154 return ptr.IsNull();
Mathieu Chartier3398c782016-09-30 10:27:43 -0700155}
156
Vladimir Markof57f2ed2019-03-25 15:55:42 +0000157template<class MirrorType1, class MirrorType2>
158OBJPTR_INLINE std::enable_if_t<std::is_base_of_v<MirrorType1, MirrorType2> ||
159 std::is_base_of_v<MirrorType2, MirrorType1>, bool>
160operator!=(ObjPtr<MirrorType1> lhs, ObjPtr<MirrorType2> rhs)
161 REQUIRES_SHARED(Locks::mutator_lock_);
162
163template<class MirrorType1, class MirrorType2>
164OBJPTR_INLINE std::enable_if_t<std::is_base_of_v<MirrorType1, MirrorType2> ||
165 std::is_base_of_v<MirrorType2, MirrorType1>, bool>
166operator!=(const MirrorType1* lhs, ObjPtr<MirrorType2> rhs)
167 REQUIRES_SHARED(Locks::mutator_lock_);
168
169template<class MirrorType1, class MirrorType2>
170OBJPTR_INLINE std::enable_if_t<std::is_base_of_v<MirrorType1, MirrorType2> ||
171 std::is_base_of_v<MirrorType2, MirrorType1>, bool>
172operator!=(ObjPtr<MirrorType1> lhs, const MirrorType2* rhs)
173 REQUIRES_SHARED(Locks::mutator_lock_);
174
175template<class MirrorType>
176OBJPTR_INLINE bool operator!=(ObjPtr<MirrorType> ptr, std::nullptr_t) {
177 return !(ptr == nullptr);
Mathieu Chartier3398c782016-09-30 10:27:43 -0700178}
179
Andreas Gampec73cb642017-02-22 10:11:30 -0800180template<class MirrorType>
Vladimir Markof57f2ed2019-03-25 15:55:42 +0000181OBJPTR_INLINE bool operator!=(std::nullptr_t, ObjPtr<MirrorType> ptr) {
182 return !(nullptr == ptr);
Mathieu Chartier3398c782016-09-30 10:27:43 -0700183}
184
Andreas Gampec73cb642017-02-22 10:11:30 -0800185template<class MirrorType>
Vladimir Markof57f2ed2019-03-25 15:55:42 +0000186static OBJPTR_INLINE ObjPtr<MirrorType> MakeObjPtr(MirrorType* ptr);
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700187
Andreas Gampec73cb642017-02-22 10:11:30 -0800188template<class MirrorType>
Vladimir Markof57f2ed2019-03-25 15:55:42 +0000189static OBJPTR_INLINE ObjPtr<MirrorType> MakeObjPtr(ObjPtr<MirrorType> ptr);
Mathieu Chartier3398c782016-09-30 10:27:43 -0700190
Andreas Gampec73cb642017-02-22 10:11:30 -0800191template<class MirrorType>
David Srbecky3f704c02018-11-16 17:05:15 +0000192OBJPTR_INLINE std::ostream& operator<<(std::ostream& os, ObjPtr<MirrorType> ptr);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700193
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700194} // namespace art
195
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700196#endif // ART_RUNTIME_OBJ_PTR_H_