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