blob: 75a6f9fe5523763c523606a3a8ac3183f47058d4 [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
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070023#include "base/mutex.h" // For Locks::mutator_lock_.
24#include "globals.h"
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070025
26namespace art {
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070027
28// Value type representing a pointer to a mirror::Object of type MirrorType
29// Pass kPoison as a template boolean for testing in non-debug builds.
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070030// Since the cookie is thread based, it is not safe to share an ObjPtr between threads.
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070031template<class MirrorType, bool kPoison = kIsDebugBuild>
32class ObjPtr {
33 static constexpr size_t kCookieShift =
Mathieu Chartiera058fdf2016-10-06 15:13:58 -070034 sizeof(kHeapReferenceSize) * kBitsPerByte - kObjectAlignmentShift;
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070035 static constexpr size_t kCookieBits = sizeof(uintptr_t) * kBitsPerByte - kCookieShift;
36 static constexpr uintptr_t kCookieMask = (static_cast<uintptr_t>(1u) << kCookieBits) - 1;
37
38 static_assert(kCookieBits >= kObjectAlignmentShift,
39 "must have a least kObjectAlignmentShift bits");
40
41 public:
42 ALWAYS_INLINE ObjPtr() REQUIRES_SHARED(Locks::mutator_lock_) : reference_(0u) {}
43
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070044 ALWAYS_INLINE ObjPtr(std::nullptr_t) REQUIRES_SHARED(Locks::mutator_lock_) : reference_(0u) {}
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070045
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070046 template <typename Type>
47 ALWAYS_INLINE ObjPtr(Type* ptr) REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartierf8ac97f2016-10-05 15:56:52 -070048 : reference_(Encode(static_cast<MirrorType*>(ptr))) {
49 static_assert(std::is_base_of<MirrorType, Type>::value,
50 "Input type must be a subtype of the ObjPtr type");
51 }
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070052
Mathieu Chartier0795f232016-09-27 18:43:30 -070053 template <typename Type>
Mathieu Chartierf8ac97f2016-10-05 15:56:52 -070054 ALWAYS_INLINE ObjPtr(const ObjPtr<Type, kPoison>& other) REQUIRES_SHARED(Locks::mutator_lock_)
55 : reference_(Encode(static_cast<MirrorType*>(other.Ptr()))) {
56 static_assert(std::is_base_of<MirrorType, Type>::value,
57 "Input type must be a subtype of the ObjPtr type");
58 }
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070059
Mathieu Chartier0795f232016-09-27 18:43:30 -070060 template <typename Type>
Mathieu Chartierf8ac97f2016-10-05 15:56:52 -070061 ALWAYS_INLINE ObjPtr& operator=(const ObjPtr<Type, kPoison>& other)
62 REQUIRES_SHARED(Locks::mutator_lock_) {
63 static_assert(std::is_base_of<MirrorType, Type>::value,
64 "Input type must be a subtype of the ObjPtr type");
Mathieu Chartier1cc62e42016-10-03 18:01:28 -070065 reference_ = Encode(static_cast<MirrorType*>(other.Ptr()));
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070066 return *this;
67 }
68
69 ALWAYS_INLINE ObjPtr& operator=(MirrorType* ptr) REQUIRES_SHARED(Locks::mutator_lock_) {
70 Assign(ptr);
71 return *this;
72 }
73
74 ALWAYS_INLINE void Assign(MirrorType* ptr) REQUIRES_SHARED(Locks::mutator_lock_) {
75 reference_ = Encode(ptr);
76 }
77
78 ALWAYS_INLINE MirrorType* operator->() const REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier1cc62e42016-10-03 18:01:28 -070079 return Ptr();
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070080 }
81
82 ALWAYS_INLINE bool IsNull() const {
83 return reference_ == 0;
84 }
85
Mathieu Chartier1cc62e42016-10-03 18:01:28 -070086 // Ptr makes sure that the object pointer is valid.
87 ALWAYS_INLINE MirrorType* Ptr() const REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070088 AssertValid();
Mathieu Chartier1cc62e42016-10-03 18:01:28 -070089 return PtrUnchecked();
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070090 }
91
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070092 ALWAYS_INLINE bool IsValid() const REQUIRES_SHARED(Locks::mutator_lock_);
93
94 ALWAYS_INLINE void AssertValid() const REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070095
96 ALWAYS_INLINE bool operator==(const ObjPtr& ptr) const REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier1cc62e42016-10-03 18:01:28 -070097 return Ptr() == ptr.Ptr();
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070098 }
99
Mathieu Chartier3398c782016-09-30 10:27:43 -0700100 template <typename PointerType>
101 ALWAYS_INLINE bool operator==(const PointerType* ptr) const
102 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700103 return Ptr() == ptr;
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700104 }
105
Mathieu Chartier3398c782016-09-30 10:27:43 -0700106 ALWAYS_INLINE bool operator==(std::nullptr_t) const {
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700107 return IsNull();
108 }
109
110 ALWAYS_INLINE bool operator!=(const ObjPtr& ptr) const REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700111 return Ptr() != ptr.Ptr();
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700112 }
113
Mathieu Chartier3398c782016-09-30 10:27:43 -0700114 template <typename PointerType>
115 ALWAYS_INLINE bool operator!=(const PointerType* ptr) const
116 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700117 return Ptr() != ptr;
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700118 }
119
Mathieu Chartier3398c782016-09-30 10:27:43 -0700120 ALWAYS_INLINE bool operator!=(std::nullptr_t) const {
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700121 return !IsNull();
122 }
123
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700124 // Ptr unchecked does not check that object pointer is valid. Do not use if you can avoid it.
125 ALWAYS_INLINE MirrorType* PtrUnchecked() const {
Mathieu Chartier0795f232016-09-27 18:43:30 -0700126 if (kPoison) {
127 return reinterpret_cast<MirrorType*>(
128 static_cast<uintptr_t>(static_cast<uint32_t>(reference_ << kObjectAlignmentShift)));
129 } else {
130 return reinterpret_cast<MirrorType*>(reference_);
131 }
132 }
133
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700134 // Static function to be friendly with null pointers.
135 template <typename SourceType>
136 static ObjPtr<MirrorType> DownCast(ObjPtr<SourceType> ptr) REQUIRES_SHARED(Locks::mutator_lock_) {
137 static_assert(std::is_base_of<SourceType, MirrorType>::value,
138 "Target type must be a subtype of source type");
139 return static_cast<MirrorType*>(ptr.Ptr());
140 }
141
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700142 private:
143 // Trim off high bits of thread local cookie.
144 ALWAYS_INLINE static uintptr_t TrimCookie(uintptr_t cookie) {
145 return cookie & kCookieMask;
146 }
147
148 ALWAYS_INLINE uintptr_t GetCookie() const {
149 return reference_ >> kCookieShift;
150 }
151
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700152 ALWAYS_INLINE static uintptr_t Encode(MirrorType* ptr) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700153 // The encoded reference and cookie.
154 uintptr_t reference_;
155};
156
Mathieu Chartier3398c782016-09-30 10:27:43 -0700157template<class MirrorType, bool kPoison, typename PointerType>
158ALWAYS_INLINE bool operator==(const PointerType* a, const ObjPtr<MirrorType, kPoison>& b)
159 REQUIRES_SHARED(Locks::mutator_lock_) {
160 return b == a;
161}
162
163template<class MirrorType, bool kPoison>
164ALWAYS_INLINE bool operator==(std::nullptr_t, const ObjPtr<MirrorType, kPoison>& b) {
165 return b == nullptr;
166}
167
168template<typename MirrorType, bool kPoison, typename PointerType>
169ALWAYS_INLINE bool operator!=(const PointerType* a, const ObjPtr<MirrorType, kPoison>& b)
170 REQUIRES_SHARED(Locks::mutator_lock_) {
171 return b != a;
172}
173
174template<class MirrorType, bool kPoison>
175ALWAYS_INLINE bool operator!=(std::nullptr_t, const ObjPtr<MirrorType, kPoison>& b) {
176 return b != nullptr;
177}
178
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700179template<class MirrorType, bool kPoison = kIsDebugBuild>
180static inline ObjPtr<MirrorType, kPoison> MakeObjPtr(MirrorType* ptr) {
181 return ObjPtr<MirrorType, kPoison>(ptr);
182}
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700183
Mathieu Chartier3398c782016-09-30 10:27:43 -0700184template<class MirrorType, bool kPoison = kIsDebugBuild>
185static inline ObjPtr<MirrorType, kPoison> MakeObjPtr(ObjPtr<MirrorType, kPoison> ptr) {
186 return ObjPtr<MirrorType, kPoison>(ptr);
187}
188
Mathieu Chartier0795f232016-09-27 18:43:30 -0700189template<class MirrorType, bool kPoison>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700190ALWAYS_INLINE std::ostream& operator<<(std::ostream& os, ObjPtr<MirrorType, kPoison> ptr);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700191
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700192} // namespace art
193
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700194#endif // ART_RUNTIME_OBJ_PTR_H_