blob: 7c0c9df7d207e69085d1eab5aaba4069de7f0397 [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>
21
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070022#include "base/mutex.h" // For Locks::mutator_lock_.
23#include "globals.h"
24#include "mirror/object_reference.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 =
34 sizeof(mirror::HeapReference<mirror::Object>) * kBitsPerByte - kObjectAlignmentShift;
35 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_)
48 : reference_(Encode(static_cast<MirrorType*>(ptr))) {}
49
Mathieu Chartier0795f232016-09-27 18:43:30 -070050 template <typename Type>
51 ALWAYS_INLINE ObjPtr(const ObjPtr<Type>& other) REQUIRES_SHARED(Locks::mutator_lock_)
52 : reference_(Encode(static_cast<MirrorType*>(other.Decode()))) {}
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070053
Mathieu Chartier0795f232016-09-27 18:43:30 -070054 template <typename Type>
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070055 ALWAYS_INLINE ObjPtr& operator=(const ObjPtr& other) {
Mathieu Chartier0795f232016-09-27 18:43:30 -070056 reference_ = Encode(static_cast<MirrorType*>(other.Decode()));
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070057 return *this;
58 }
59
60 ALWAYS_INLINE ObjPtr& operator=(MirrorType* ptr) REQUIRES_SHARED(Locks::mutator_lock_) {
61 Assign(ptr);
62 return *this;
63 }
64
65 ALWAYS_INLINE void Assign(MirrorType* ptr) REQUIRES_SHARED(Locks::mutator_lock_) {
66 reference_ = Encode(ptr);
67 }
68
69 ALWAYS_INLINE MirrorType* operator->() const REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070070 return Decode();
71 }
72
73 ALWAYS_INLINE bool IsNull() const {
74 return reference_ == 0;
75 }
76
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070077 // Decode makes sure that the object pointer is valid.
78 ALWAYS_INLINE MirrorType* Decode() const REQUIRES_SHARED(Locks::mutator_lock_) {
79 AssertValid();
80 return DecodeUnchecked();
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070081 }
82
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070083 ALWAYS_INLINE bool IsValid() const REQUIRES_SHARED(Locks::mutator_lock_);
84
85 ALWAYS_INLINE void AssertValid() const REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070086
87 ALWAYS_INLINE bool operator==(const ObjPtr& ptr) const REQUIRES_SHARED(Locks::mutator_lock_) {
88 return Decode() == ptr.Decode();
89 }
90
Mathieu Chartier3398c782016-09-30 10:27:43 -070091 template <typename PointerType>
92 ALWAYS_INLINE bool operator==(const PointerType* ptr) const
93 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070094 return Decode() == ptr;
95 }
96
Mathieu Chartier3398c782016-09-30 10:27:43 -070097 ALWAYS_INLINE bool operator==(std::nullptr_t) const {
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -070098 return IsNull();
99 }
100
101 ALWAYS_INLINE bool operator!=(const ObjPtr& ptr) const REQUIRES_SHARED(Locks::mutator_lock_) {
102 return Decode() != ptr.Decode();
103 }
104
Mathieu Chartier3398c782016-09-30 10:27:43 -0700105 template <typename PointerType>
106 ALWAYS_INLINE bool operator!=(const PointerType* ptr) const
107 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700108 return Decode() != ptr;
109 }
110
Mathieu Chartier3398c782016-09-30 10:27:43 -0700111 ALWAYS_INLINE bool operator!=(std::nullptr_t) const {
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700112 return !IsNull();
113 }
114
Mathieu Chartier0795f232016-09-27 18:43:30 -0700115 // Decode unchecked does not check that object pointer is valid. Do not use if you can avoid it.
Mathieu Chartier3398c782016-09-30 10:27:43 -0700116 ALWAYS_INLINE MirrorType* DecodeUnchecked() const {
Mathieu Chartier0795f232016-09-27 18:43:30 -0700117 if (kPoison) {
118 return reinterpret_cast<MirrorType*>(
119 static_cast<uintptr_t>(static_cast<uint32_t>(reference_ << kObjectAlignmentShift)));
120 } else {
121 return reinterpret_cast<MirrorType*>(reference_);
122 }
123 }
124
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700125 private:
126 // Trim off high bits of thread local cookie.
127 ALWAYS_INLINE static uintptr_t TrimCookie(uintptr_t cookie) {
128 return cookie & kCookieMask;
129 }
130
131 ALWAYS_INLINE uintptr_t GetCookie() const {
132 return reference_ >> kCookieShift;
133 }
134
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700135 ALWAYS_INLINE static uintptr_t Encode(MirrorType* ptr) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700136 // The encoded reference and cookie.
137 uintptr_t reference_;
138};
139
Mathieu Chartier3398c782016-09-30 10:27:43 -0700140template<class MirrorType, bool kPoison, typename PointerType>
141ALWAYS_INLINE bool operator==(const PointerType* a, const ObjPtr<MirrorType, kPoison>& b)
142 REQUIRES_SHARED(Locks::mutator_lock_) {
143 return b == a;
144}
145
146template<class MirrorType, bool kPoison>
147ALWAYS_INLINE bool operator==(std::nullptr_t, const ObjPtr<MirrorType, kPoison>& b) {
148 return b == nullptr;
149}
150
151template<typename MirrorType, bool kPoison, typename PointerType>
152ALWAYS_INLINE bool operator!=(const PointerType* a, const ObjPtr<MirrorType, kPoison>& b)
153 REQUIRES_SHARED(Locks::mutator_lock_) {
154 return b != a;
155}
156
157template<class MirrorType, bool kPoison>
158ALWAYS_INLINE bool operator!=(std::nullptr_t, const ObjPtr<MirrorType, kPoison>& b) {
159 return b != nullptr;
160}
161
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700162template<class MirrorType, bool kPoison = kIsDebugBuild>
163static inline ObjPtr<MirrorType, kPoison> MakeObjPtr(MirrorType* ptr) {
164 return ObjPtr<MirrorType, kPoison>(ptr);
165}
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700166
Mathieu Chartier3398c782016-09-30 10:27:43 -0700167template<class MirrorType, bool kPoison = kIsDebugBuild>
168static inline ObjPtr<MirrorType, kPoison> MakeObjPtr(ObjPtr<MirrorType, kPoison> ptr) {
169 return ObjPtr<MirrorType, kPoison>(ptr);
170}
171
Mathieu Chartier0795f232016-09-27 18:43:30 -0700172template<class MirrorType, bool kPoison>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700173ALWAYS_INLINE std::ostream& operator<<(std::ostream& os, ObjPtr<MirrorType, kPoison> ptr);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700174
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700175} // namespace art
176
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700177#endif // ART_RUNTIME_OBJ_PTR_H_