Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005 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 | |
| 17 | #ifndef ANDROID_STRONG_POINTER_H |
| 18 | #define ANDROID_STRONG_POINTER_H |
| 19 | |
Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 20 | // --------------------------------------------------------------------------- |
| 21 | namespace android { |
| 22 | |
Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 23 | template<typename T> class wp; |
| 24 | |
| 25 | // --------------------------------------------------------------------------- |
| 26 | |
| 27 | #define COMPARE(_op_) \ |
| 28 | inline bool operator _op_ (const sp<T>& o) const { \ |
| 29 | return m_ptr _op_ o.m_ptr; \ |
| 30 | } \ |
| 31 | inline bool operator _op_ (const T* o) const { \ |
| 32 | return m_ptr _op_ o; \ |
| 33 | } \ |
| 34 | template<typename U> \ |
| 35 | inline bool operator _op_ (const sp<U>& o) const { \ |
| 36 | return m_ptr _op_ o.m_ptr; \ |
| 37 | } \ |
| 38 | template<typename U> \ |
| 39 | inline bool operator _op_ (const U* o) const { \ |
| 40 | return m_ptr _op_ o; \ |
| 41 | } \ |
| 42 | inline bool operator _op_ (const wp<T>& o) const { \ |
| 43 | return m_ptr _op_ o.m_ptr; \ |
| 44 | } \ |
| 45 | template<typename U> \ |
| 46 | inline bool operator _op_ (const wp<U>& o) const { \ |
| 47 | return m_ptr _op_ o.m_ptr; \ |
| 48 | } |
| 49 | |
| 50 | // --------------------------------------------------------------------------- |
| 51 | |
Mathias Agopian | 8db925f | 2013-05-09 13:04:32 -0700 | [diff] [blame] | 52 | template<typename T> |
| 53 | class sp { |
Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 54 | public: |
Yi Kong | c1a1562 | 2018-07-11 17:37:34 -0700 | [diff] [blame] | 55 | inline sp() : m_ptr(nullptr) { } |
Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 56 | |
Chih-Hung Hsieh | 2a92996 | 2016-08-02 12:14:47 -0700 | [diff] [blame] | 57 | sp(T* other); // NOLINT(implicit) |
Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 58 | sp(const sp<T>& other); |
Chih-Hung Hsieh | 747eb14 | 2018-09-25 11:16:22 -0700 | [diff] [blame] | 59 | sp(sp<T>&& other) noexcept; |
Chih-Hung Hsieh | 2a92996 | 2016-08-02 12:14:47 -0700 | [diff] [blame] | 60 | template<typename U> sp(U* other); // NOLINT(implicit) |
| 61 | template<typename U> sp(const sp<U>& other); // NOLINT(implicit) |
| 62 | template<typename U> sp(sp<U>&& other); // NOLINT(implicit) |
Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 63 | |
| 64 | ~sp(); |
| 65 | |
| 66 | // Assignment |
| 67 | |
| 68 | sp& operator = (T* other); |
| 69 | sp& operator = (const sp<T>& other); |
Chih-Hung Hsieh | 747eb14 | 2018-09-25 11:16:22 -0700 | [diff] [blame] | 70 | sp& operator=(sp<T>&& other) noexcept; |
Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 71 | |
| 72 | template<typename U> sp& operator = (const sp<U>& other); |
John Reck | d69089a | 2015-10-28 15:36:33 -0700 | [diff] [blame] | 73 | template<typename U> sp& operator = (sp<U>&& other); |
Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 74 | template<typename U> sp& operator = (U* other); |
| 75 | |
| 76 | //! Special optimization for use by ProcessState (and nobody else). |
| 77 | void force_set(T* other); |
| 78 | |
| 79 | // Reset |
| 80 | |
| 81 | void clear(); |
| 82 | |
| 83 | // Accessors |
| 84 | |
Jeff Sharkey | 147b881 | 2017-09-13 11:04:06 -0600 | [diff] [blame] | 85 | inline T& operator* () const { return *m_ptr; } |
| 86 | inline T* operator-> () const { return m_ptr; } |
| 87 | inline T* get() const { return m_ptr; } |
| 88 | inline explicit operator bool () const { return m_ptr != nullptr; } |
Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 89 | |
| 90 | // Operators |
| 91 | |
| 92 | COMPARE(==) |
| 93 | COMPARE(!=) |
| 94 | COMPARE(>) |
| 95 | COMPARE(<) |
| 96 | COMPARE(<=) |
| 97 | COMPARE(>=) |
| 98 | |
| 99 | private: |
| 100 | template<typename Y> friend class sp; |
| 101 | template<typename Y> friend class wp; |
Mathias Agopian | 3e0f875 | 2011-02-24 18:12:34 -0800 | [diff] [blame] | 102 | void set_pointer(T* ptr); |
| 103 | T* m_ptr; |
Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 104 | }; |
| 105 | |
Hans Boehm | 7f0b260 | 2017-03-15 11:01:03 -0700 | [diff] [blame] | 106 | // For code size reasons, we do not want this inlined or templated. |
| 107 | void sp_report_race(); |
| 108 | |
Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 109 | #undef COMPARE |
| 110 | |
Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 111 | // --------------------------------------------------------------------------- |
| 112 | // No user serviceable parts below here. |
| 113 | |
| 114 | template<typename T> |
| 115 | sp<T>::sp(T* other) |
Mathias Agopian | 8db925f | 2013-05-09 13:04:32 -0700 | [diff] [blame] | 116 | : m_ptr(other) { |
| 117 | if (other) |
| 118 | other->incStrong(this); |
| 119 | } |
Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 120 | |
| 121 | template<typename T> |
| 122 | sp<T>::sp(const sp<T>& other) |
Mathias Agopian | 8db925f | 2013-05-09 13:04:32 -0700 | [diff] [blame] | 123 | : m_ptr(other.m_ptr) { |
| 124 | if (m_ptr) |
| 125 | m_ptr->incStrong(this); |
| 126 | } |
Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 127 | |
Chih-Hung Hsieh | 747eb14 | 2018-09-25 11:16:22 -0700 | [diff] [blame] | 128 | template <typename T> |
| 129 | sp<T>::sp(sp<T>&& other) noexcept : m_ptr(other.m_ptr) { |
John Reck | d69089a | 2015-10-28 15:36:33 -0700 | [diff] [blame] | 130 | other.m_ptr = nullptr; |
| 131 | } |
| 132 | |
Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 133 | template<typename T> template<typename U> |
Mathias Agopian | 8db925f | 2013-05-09 13:04:32 -0700 | [diff] [blame] | 134 | sp<T>::sp(U* other) |
| 135 | : m_ptr(other) { |
| 136 | if (other) |
Colin Cross | 17b5b82 | 2016-09-15 18:15:37 -0700 | [diff] [blame] | 137 | (static_cast<T*>(other))->incStrong(this); |
Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | template<typename T> template<typename U> |
| 141 | sp<T>::sp(const sp<U>& other) |
Mathias Agopian | 8db925f | 2013-05-09 13:04:32 -0700 | [diff] [blame] | 142 | : m_ptr(other.m_ptr) { |
| 143 | if (m_ptr) |
| 144 | m_ptr->incStrong(this); |
Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 145 | } |
| 146 | |
John Reck | d69089a | 2015-10-28 15:36:33 -0700 | [diff] [blame] | 147 | template<typename T> template<typename U> |
| 148 | sp<T>::sp(sp<U>&& other) |
| 149 | : m_ptr(other.m_ptr) { |
| 150 | other.m_ptr = nullptr; |
| 151 | } |
| 152 | |
Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 153 | template<typename T> |
Mathias Agopian | 8db925f | 2013-05-09 13:04:32 -0700 | [diff] [blame] | 154 | sp<T>::~sp() { |
| 155 | if (m_ptr) |
| 156 | m_ptr->decStrong(this); |
| 157 | } |
| 158 | |
| 159 | template<typename T> |
| 160 | sp<T>& sp<T>::operator =(const sp<T>& other) { |
Hans Boehm | 7f0b260 | 2017-03-15 11:01:03 -0700 | [diff] [blame] | 161 | // Force m_ptr to be read twice, to heuristically check for data races. |
| 162 | T* oldPtr(*const_cast<T* volatile*>(&m_ptr)); |
Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 163 | T* otherPtr(other.m_ptr); |
Hans Boehm | 7f0b260 | 2017-03-15 11:01:03 -0700 | [diff] [blame] | 164 | if (otherPtr) otherPtr->incStrong(this); |
| 165 | if (oldPtr) oldPtr->decStrong(this); |
| 166 | if (oldPtr != *const_cast<T* volatile*>(&m_ptr)) sp_report_race(); |
Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 167 | m_ptr = otherPtr; |
| 168 | return *this; |
| 169 | } |
| 170 | |
Chih-Hung Hsieh | 747eb14 | 2018-09-25 11:16:22 -0700 | [diff] [blame] | 171 | template <typename T> |
| 172 | sp<T>& sp<T>::operator=(sp<T>&& other) noexcept { |
Hans Boehm | 7f0b260 | 2017-03-15 11:01:03 -0700 | [diff] [blame] | 173 | T* oldPtr(*const_cast<T* volatile*>(&m_ptr)); |
| 174 | if (oldPtr) oldPtr->decStrong(this); |
| 175 | if (oldPtr != *const_cast<T* volatile*>(&m_ptr)) sp_report_race(); |
John Reck | d69089a | 2015-10-28 15:36:33 -0700 | [diff] [blame] | 176 | m_ptr = other.m_ptr; |
| 177 | other.m_ptr = nullptr; |
| 178 | return *this; |
| 179 | } |
| 180 | |
| 181 | template<typename T> |
Mathias Agopian | 8db925f | 2013-05-09 13:04:32 -0700 | [diff] [blame] | 182 | sp<T>& sp<T>::operator =(T* other) { |
Hans Boehm | 7f0b260 | 2017-03-15 11:01:03 -0700 | [diff] [blame] | 183 | T* oldPtr(*const_cast<T* volatile*>(&m_ptr)); |
| 184 | if (other) other->incStrong(this); |
| 185 | if (oldPtr) oldPtr->decStrong(this); |
| 186 | if (oldPtr != *const_cast<T* volatile*>(&m_ptr)) sp_report_race(); |
Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 187 | m_ptr = other; |
| 188 | return *this; |
| 189 | } |
| 190 | |
| 191 | template<typename T> template<typename U> |
Mathias Agopian | 8db925f | 2013-05-09 13:04:32 -0700 | [diff] [blame] | 192 | sp<T>& sp<T>::operator =(const sp<U>& other) { |
Hans Boehm | 7f0b260 | 2017-03-15 11:01:03 -0700 | [diff] [blame] | 193 | T* oldPtr(*const_cast<T* volatile*>(&m_ptr)); |
Mathias Agopian | 7332f80 | 2011-02-25 16:11:44 -0800 | [diff] [blame] | 194 | T* otherPtr(other.m_ptr); |
Hans Boehm | 7f0b260 | 2017-03-15 11:01:03 -0700 | [diff] [blame] | 195 | if (otherPtr) otherPtr->incStrong(this); |
| 196 | if (oldPtr) oldPtr->decStrong(this); |
| 197 | if (oldPtr != *const_cast<T* volatile*>(&m_ptr)) sp_report_race(); |
Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 198 | m_ptr = otherPtr; |
| 199 | return *this; |
| 200 | } |
| 201 | |
| 202 | template<typename T> template<typename U> |
John Reck | d69089a | 2015-10-28 15:36:33 -0700 | [diff] [blame] | 203 | sp<T>& sp<T>::operator =(sp<U>&& other) { |
Hans Boehm | 7f0b260 | 2017-03-15 11:01:03 -0700 | [diff] [blame] | 204 | T* oldPtr(*const_cast<T* volatile*>(&m_ptr)); |
| 205 | if (m_ptr) m_ptr->decStrong(this); |
| 206 | if (oldPtr != *const_cast<T* volatile*>(&m_ptr)) sp_report_race(); |
John Reck | d69089a | 2015-10-28 15:36:33 -0700 | [diff] [blame] | 207 | m_ptr = other.m_ptr; |
| 208 | other.m_ptr = nullptr; |
| 209 | return *this; |
| 210 | } |
| 211 | |
| 212 | template<typename T> template<typename U> |
Mathias Agopian | 8db925f | 2013-05-09 13:04:32 -0700 | [diff] [blame] | 213 | sp<T>& sp<T>::operator =(U* other) { |
Hans Boehm | 7f0b260 | 2017-03-15 11:01:03 -0700 | [diff] [blame] | 214 | T* oldPtr(*const_cast<T* volatile*>(&m_ptr)); |
| 215 | if (other) (static_cast<T*>(other))->incStrong(this); |
| 216 | if (oldPtr) oldPtr->decStrong(this); |
| 217 | if (oldPtr != *const_cast<T* volatile*>(&m_ptr)) sp_report_race(); |
Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 218 | m_ptr = other; |
| 219 | return *this; |
| 220 | } |
| 221 | |
Mathias Agopian | 8db925f | 2013-05-09 13:04:32 -0700 | [diff] [blame] | 222 | template<typename T> |
| 223 | void sp<T>::force_set(T* other) { |
Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 224 | other->forceIncStrong(this); |
| 225 | m_ptr = other; |
| 226 | } |
| 227 | |
| 228 | template<typename T> |
Mathias Agopian | 8db925f | 2013-05-09 13:04:32 -0700 | [diff] [blame] | 229 | void sp<T>::clear() { |
Hans Boehm | f4f7620 | 2018-08-17 11:40:39 -0700 | [diff] [blame] | 230 | T* oldPtr(*const_cast<T* volatile*>(&m_ptr)); |
| 231 | if (oldPtr) { |
| 232 | oldPtr->decStrong(this); |
| 233 | if (oldPtr != *const_cast<T* volatile*>(&m_ptr)) sp_report_race(); |
Yi Kong | c1a1562 | 2018-07-11 17:37:34 -0700 | [diff] [blame] | 234 | m_ptr = nullptr; |
Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
| 238 | template<typename T> |
Mathias Agopian | 3e0f875 | 2011-02-24 18:12:34 -0800 | [diff] [blame] | 239 | void sp<T>::set_pointer(T* ptr) { |
| 240 | m_ptr = ptr; |
| 241 | } |
Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 242 | |
Pirama Arumuga Nainar | eab48ce | 2018-04-10 14:31:29 -0700 | [diff] [blame] | 243 | } // namespace android |
Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 244 | |
| 245 | // --------------------------------------------------------------------------- |
| 246 | |
| 247 | #endif // ANDROID_STRONG_POINTER_H |