blob: d5ac33d8b37777cd64b82d35487fe7bc7ac6c0ef [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
91 ALWAYS_INLINE bool operator==(const MirrorType* ptr) const REQUIRES_SHARED(Locks::mutator_lock_) {
92 return Decode() == ptr;
93 }
94
95 ALWAYS_INLINE bool operator==(std::nullptr_t) const REQUIRES_SHARED(Locks::mutator_lock_) {
96 return IsNull();
97 }
98
99 ALWAYS_INLINE bool operator!=(const ObjPtr& ptr) const REQUIRES_SHARED(Locks::mutator_lock_) {
100 return Decode() != ptr.Decode();
101 }
102
103 ALWAYS_INLINE bool operator!=(const MirrorType* ptr) const REQUIRES_SHARED(Locks::mutator_lock_) {
104 return Decode() != ptr;
105 }
106
107 ALWAYS_INLINE bool operator!=(std::nullptr_t) const REQUIRES_SHARED(Locks::mutator_lock_) {
108 return !IsNull();
109 }
110
Mathieu Chartier0795f232016-09-27 18:43:30 -0700111 // Decode unchecked does not check that object pointer is valid. Do not use if you can avoid it.
112 ALWAYS_INLINE MirrorType* DecodeUnchecked() const REQUIRES_SHARED(Locks::mutator_lock_) {
113 if (kPoison) {
114 return reinterpret_cast<MirrorType*>(
115 static_cast<uintptr_t>(static_cast<uint32_t>(reference_ << kObjectAlignmentShift)));
116 } else {
117 return reinterpret_cast<MirrorType*>(reference_);
118 }
119 }
120
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700121 private:
122 // Trim off high bits of thread local cookie.
123 ALWAYS_INLINE static uintptr_t TrimCookie(uintptr_t cookie) {
124 return cookie & kCookieMask;
125 }
126
127 ALWAYS_INLINE uintptr_t GetCookie() const {
128 return reference_ >> kCookieShift;
129 }
130
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700131 ALWAYS_INLINE static uintptr_t Encode(MirrorType* ptr) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700132 // The encoded reference and cookie.
133 uintptr_t reference_;
134};
135
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700136template<class MirrorType, bool kPoison = kIsDebugBuild>
137static inline ObjPtr<MirrorType, kPoison> MakeObjPtr(MirrorType* ptr) {
138 return ObjPtr<MirrorType, kPoison>(ptr);
139}
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700140
Mathieu Chartier0795f232016-09-27 18:43:30 -0700141template<class MirrorType, bool kPoison>
142ALWAYS_INLINE std::ostream& operator<<(std::ostream& os, ObjPtr<MirrorType, kPoison> ptr)
143 REQUIRES_SHARED(Locks::mutator_lock_);
144
Mathieu Chartier3f7f03c2016-09-26 11:39:52 -0700145} // namespace art
146
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700147#endif // ART_RUNTIME_OBJ_PTR_H_