blob: aca6c2b785182b7ac6e0afed2e47820b1fa8f342 [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00006 */
7
8#ifndef SkRefCnt_DEFINED
9#define SkRefCnt_DEFINED
10
mtklein5f939ab2016-03-16 10:28:35 -070011#include "../private/SkTLogic.h"
bungemanf3c15b72015-08-19 11:56:48 -070012#include "SkTypes.h"
bungeman2c4bd072016-04-08 06:58:51 -070013#include <atomic>
bungemanc901c112016-03-08 08:35:23 -080014#include <functional>
mtklein5f939ab2016-03-16 10:28:35 -070015#include <memory>
bungeman801b44c2016-04-28 11:18:07 -070016#include <type_traits>
halcanary217c0b32016-03-02 08:06:20 -080017#include <utility>
reed@android.com8a1c16f2008-12-17 15:59:43 +000018
commit-bot@chromium.org6d2533e2013-10-16 15:15:58 +000019/** \class SkRefCntBase
20
bungeman@google.com10ba0062013-10-25 18:40:24 +000021 SkRefCntBase is the base class for objects that may be shared by multiple
bungeman@google.coma02bc152012-05-16 18:21:56 +000022 objects. When an existing owner wants to share a reference, it calls ref().
23 When an owner wants to release its reference, it calls unref(). When the
24 shared object's reference count goes to zero as the result of an unref()
25 call, its (virtual) destructor is called. It is an error for the
26 destructor to be called explicitly (or via the object going out of scope on
27 the stack or calling delete) if getRefCnt() > 1.
reed@android.com8a1c16f2008-12-17 15:59:43 +000028*/
commit-bot@chromium.orge3beb6b2014-04-07 19:34:38 +000029class SK_API SkRefCntBase : SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +000030public:
31 /** Default construct, initializing the reference count to 1.
32 */
bungeman@google.com10ba0062013-10-25 18:40:24 +000033 SkRefCntBase() : fRefCnt(1) {}
reed@android.com8a1c16f2008-12-17 15:59:43 +000034
bungeman@google.coma02bc152012-05-16 18:21:56 +000035 /** Destruct, asserting that the reference count is 1.
reed@android.com8a1c16f2008-12-17 15:59:43 +000036 */
bungeman@google.com10ba0062013-10-25 18:40:24 +000037 virtual ~SkRefCntBase() {
reed@google.com4c888aa2011-09-12 19:54:12 +000038#ifdef SK_DEBUG
bungeman2c4bd072016-04-08 06:58:51 -070039 SkASSERTF(getRefCnt() == 1, "fRefCnt was %d", getRefCnt());
40 // illegal value, to catch us if we reuse after delete
41 fRefCnt.store(0, std::memory_order_relaxed);
reed@google.com4c888aa2011-09-12 19:54:12 +000042#endif
43 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000044
mtklein21180e22014-11-25 07:30:19 -080045#ifdef SK_DEBUG
bungeman@google.comf64c6842013-07-19 23:18:52 +000046 /** Return the reference count. Use only for debugging. */
bungeman2c4bd072016-04-08 06:58:51 -070047 int32_t getRefCnt() const {
48 return fRefCnt.load(std::memory_order_relaxed);
49 }
50
51 void validate() const {
52 SkASSERT(getRefCnt() > 0);
53 }
mtklein21180e22014-11-25 07:30:19 -080054#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +000055
commit-bot@chromium.orgea6e14a2014-02-04 18:00:23 +000056 /** May return true if the caller is the only owner.
bungeman@google.comf64c6842013-07-19 23:18:52 +000057 * Ensures that all previous owner's actions are complete.
58 */
59 bool unique() const {
bungeman2c4bd072016-04-08 06:58:51 -070060 if (1 == fRefCnt.load(std::memory_order_acquire)) {
mtklein7b274c72015-02-03 13:38:58 -080061 // The acquire barrier is only really needed if we return true. It
62 // prevents code conditioned on the result of unique() from running
63 // until previous owners are all totally done calling unref().
64 return true;
bungeman@google.comf64c6842013-07-19 23:18:52 +000065 }
mtklein7b274c72015-02-03 13:38:58 -080066 return false;
bungeman@google.comf64c6842013-07-19 23:18:52 +000067 }
68
reed@android.com8a1c16f2008-12-17 15:59:43 +000069 /** Increment the reference count. Must be balanced by a call to unref().
70 */
71 void ref() const {
bungeman2c4bd072016-04-08 06:58:51 -070072 SkASSERT(getRefCnt() > 0);
bungeman2c4bd072016-04-08 06:58:51 -070073 // No barrier required.
74 (void)fRefCnt.fetch_add(+1, std::memory_order_relaxed);
reed@android.com8a1c16f2008-12-17 15:59:43 +000075 }
76
77 /** Decrement the reference count. If the reference count is 1 before the
bungeman@google.coma02bc152012-05-16 18:21:56 +000078 decrement, then delete the object. Note that if this is the case, then
79 the object needs to have been allocated via new, and not on the stack.
reed@android.com8a1c16f2008-12-17 15:59:43 +000080 */
81 void unref() const {
bungeman2c4bd072016-04-08 06:58:51 -070082 SkASSERT(getRefCnt() > 0);
mtklein7b274c72015-02-03 13:38:58 -080083 // A release here acts in place of all releases we "should" have been doing in ref().
bungeman2c4bd072016-04-08 06:58:51 -070084 if (1 == fRefCnt.fetch_add(-1, std::memory_order_acq_rel)) {
mtklein7b274c72015-02-03 13:38:58 -080085 // Like unique(), the acquire is only needed on success, to make sure
86 // code in internal_dispose() doesn't happen before the decrement.
87 this->internal_dispose();
reed@android.com8a1c16f2008-12-17 15:59:43 +000088 }
89 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000090
reed@google.comf7943032012-07-23 14:50:38 +000091protected:
92 /**
93 * Allow subclasses to call this if they've overridden internal_dispose
mtklein9c5052f2016-08-06 12:51:51 -070094 * so they can reset fRefCnt before the destructor is called or if they
95 * choose not to call the destructor (e.g. using a free list).
reed@google.comf7943032012-07-23 14:50:38 +000096 */
97 void internal_dispose_restore_refcnt_to_1() const {
bungeman2c4bd072016-04-08 06:58:51 -070098 SkASSERT(0 == getRefCnt());
99 fRefCnt.store(1, std::memory_order_relaxed);
reed@google.comf7943032012-07-23 14:50:38 +0000100 }
101
102private:
103 /**
104 * Called when the ref count goes to 0.
105 */
106 virtual void internal_dispose() const {
107 this->internal_dispose_restore_refcnt_to_1();
halcanary385fe4d2015-08-26 13:07:48 -0700108 delete this;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000109 }
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000110
bungeman@google.comf64c6842013-07-19 23:18:52 +0000111 // The following friends are those which override internal_dispose()
112 // and conditionally call SkRefCnt::internal_dispose().
bungeman@google.coma02bc152012-05-16 18:21:56 +0000113 friend class SkWeakRefCnt;
114
bungeman2c4bd072016-04-08 06:58:51 -0700115 mutable std::atomic<int32_t> fRefCnt;
robertphillips@google.com4d73ac22012-06-13 18:54:08 +0000116
bungeman@google.com10ba0062013-10-25 18:40:24 +0000117 typedef SkNoncopyable INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118};
119
bungeman@google.com10ba0062013-10-25 18:40:24 +0000120#ifdef SK_REF_CNT_MIXIN_INCLUDE
121// It is the responsibility of the following include to define the type SkRefCnt.
122// This SkRefCnt should normally derive from SkRefCntBase.
123#include SK_REF_CNT_MIXIN_INCLUDE
124#else
benjaminwagnerf9634b92016-04-13 17:51:57 -0700125class SK_API SkRefCnt : public SkRefCntBase {
126 // "#include SK_REF_CNT_MIXIN_INCLUDE" doesn't work with this build system.
127 #if defined(GOOGLE3)
128 public:
129 void deref() const { this->unref(); }
130 #endif
131};
bungeman@google.com10ba0062013-10-25 18:40:24 +0000132#endif
133
reed@google.com7f6d6d42011-07-15 15:25:22 +0000134///////////////////////////////////////////////////////////////////////////////
135
136/** Helper macro to safely assign one SkRefCnt[TS]* to another, checking for
137 null in on each side of the assignment, and ensuring that ref() is called
138 before unref(), in case the two pointers point to the same object.
139 */
140#define SkRefCnt_SafeAssign(dst, src) \
141 do { \
142 if (src) src->ref(); \
143 if (dst) dst->unref(); \
144 dst = src; \
145 } while (0)
146
147
bungeman77a53de2015-10-01 12:28:49 -0700148/** Call obj->ref() and return obj. The obj must not be nullptr.
reed@google.com7f6d6d42011-07-15 15:25:22 +0000149 */
bungeman@google.com1fd201b2012-08-22 18:56:56 +0000150template <typename T> static inline T* SkRef(T* obj) {
151 SkASSERT(obj);
152 obj->ref();
153 return obj;
154}
155
156/** Check if the argument is non-null, and if so, call obj->ref() and return obj.
157 */
158template <typename T> static inline T* SkSafeRef(T* obj) {
reed@google.com7f6d6d42011-07-15 15:25:22 +0000159 if (obj) {
160 obj->ref();
161 }
bungeman@google.com1fd201b2012-08-22 18:56:56 +0000162 return obj;
reed@google.com7f6d6d42011-07-15 15:25:22 +0000163}
164
165/** Check if the argument is non-null, and if so, call obj->unref()
166 */
167template <typename T> static inline void SkSafeUnref(T* obj) {
168 if (obj) {
169 obj->unref();
170 }
171}
172
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000173template<typename T> static inline void SkSafeSetNull(T*& obj) {
bsalomon49f085d2014-09-05 13:34:00 -0700174 if (obj) {
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000175 obj->unref();
bungeman77a53de2015-10-01 12:28:49 -0700176 obj = nullptr;
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000177 }
178}
179
reed@google.com7f6d6d42011-07-15 15:25:22 +0000180///////////////////////////////////////////////////////////////////////////////
181
mtklein08d1fcc2014-11-20 09:18:31 -0800182// This is a variant of SkRefCnt that's Not Virtual, so weighs 4 bytes instead of 8 or 16.
183// There's only benefit to using this if the deriving class does not otherwise need a vtable.
184template <typename Derived>
185class SkNVRefCnt : SkNoncopyable {
186public:
187 SkNVRefCnt() : fRefCnt(1) {}
bungeman2c4bd072016-04-08 06:58:51 -0700188 ~SkNVRefCnt() { SkASSERTF(1 == getRefCnt(), "NVRefCnt was %d", getRefCnt()); }
mtklein08d1fcc2014-11-20 09:18:31 -0800189
190 // Implementation is pretty much the same as SkRefCntBase. All required barriers are the same:
191 // - unique() needs acquire when it returns true, and no barrier if it returns false;
192 // - ref() doesn't need any barrier;
193 // - unref() needs a release barrier, and an acquire if it's going to call delete.
194
bungeman2c4bd072016-04-08 06:58:51 -0700195 bool unique() const { return 1 == fRefCnt.load(std::memory_order_acquire); }
196 void ref() const { (void)fRefCnt.fetch_add(+1, std::memory_order_relaxed); }
reed90d0ff02014-11-24 12:02:31 -0800197 void unref() const {
bungeman2c4bd072016-04-08 06:58:51 -0700198 if (1 == fRefCnt.fetch_add(-1, std::memory_order_acq_rel)) {
199 // restore the 1 for our destructor's assert
200 SkDEBUGCODE(fRefCnt.store(1, std::memory_order_relaxed));
mtklein97466ab2015-12-07 13:37:00 -0800201 delete (const Derived*)this;
reed90d0ff02014-11-24 12:02:31 -0800202 }
203 }
mtklein7b274c72015-02-03 13:38:58 -0800204 void deref() const { this->unref(); }
Florin Malita844aa332014-11-20 16:56:22 -0500205
mtklein08d1fcc2014-11-20 09:18:31 -0800206private:
bungeman2c4bd072016-04-08 06:58:51 -0700207 mutable std::atomic<int32_t> fRefCnt;
208 int32_t getRefCnt() const {
209 return fRefCnt.load(std::memory_order_relaxed);
210 }
mtklein08d1fcc2014-11-20 09:18:31 -0800211};
212
reedbb7b0432016-03-01 07:28:51 -0800213///////////////////////////////////////////////////////////////////////////////////////////////////
214
215/**
216 * Shared pointer class to wrap classes that support a ref()/unref() interface.
217 *
218 * This can be used for classes inheriting from SkRefCnt, but it also works for other
219 * classes that match the interface, but have different internal choices: e.g. the hosted class
220 * may have its ref/unref be thread-safe, but that is not assumed/imposed by sk_sp.
221 */
222template <typename T> class sk_sp {
bungemanbeab9e72016-03-03 07:50:49 -0800223 /** Supports safe bool idiom. Obsolete with explicit operator bool. */
224 using unspecified_bool_type = T* sk_sp::*;
reedbb7b0432016-03-01 07:28:51 -0800225public:
bungemanc901c112016-03-08 08:35:23 -0800226 using element_type = T;
227
bungeman06ca8ec2016-06-09 08:01:03 -0700228 constexpr sk_sp() : fPtr(nullptr) {}
229 constexpr sk_sp(std::nullptr_t) : fPtr(nullptr) {}
reedbb7b0432016-03-01 07:28:51 -0800230
231 /**
232 * Shares the underlying object by calling ref(), so that both the argument and the newly
233 * created sk_sp both have a reference to it.
234 */
235 sk_sp(const sk_sp<T>& that) : fPtr(SkSafeRef(that.get())) {}
bungeman801b44c2016-04-28 11:18:07 -0700236 template <typename U, typename = skstd::enable_if_t<std::is_convertible<U*, T*>::value>>
halcanarycb6cb382016-03-02 08:11:26 -0800237 sk_sp(const sk_sp<U>& that) : fPtr(SkSafeRef(that.get())) {}
reedbb7b0432016-03-01 07:28:51 -0800238
239 /**
240 * Move the underlying object from the argument to the newly created sk_sp. Afterwards only
241 * the new sk_sp will have a reference to the object, and the argument will point to null.
242 * No call to ref() or unref() will be made.
243 */
244 sk_sp(sk_sp<T>&& that) : fPtr(that.release()) {}
bungeman801b44c2016-04-28 11:18:07 -0700245 template <typename U, typename = skstd::enable_if_t<std::is_convertible<U*, T*>::value>>
halcanarycb6cb382016-03-02 08:11:26 -0800246 sk_sp(sk_sp<U>&& that) : fPtr(that.release()) {}
reedbb7b0432016-03-01 07:28:51 -0800247
248 /**
249 * Adopt the bare pointer into the newly created sk_sp.
250 * No call to ref() or unref() will be made.
251 */
252 explicit sk_sp(T* obj) : fPtr(obj) {}
253
254 /**
255 * Calls unref() on the underlying object pointer.
256 */
257 ~sk_sp() {
258 SkSafeUnref(fPtr);
bungemanc901c112016-03-08 08:35:23 -0800259 SkDEBUGCODE(fPtr = nullptr);
reedbb7b0432016-03-01 07:28:51 -0800260 }
261
halcanarycb6cb382016-03-02 08:11:26 -0800262 sk_sp<T>& operator=(std::nullptr_t) { this->reset(); return *this; }
reedbb7b0432016-03-01 07:28:51 -0800263
264 /**
265 * Shares the underlying object referenced by the argument by calling ref() on it. If this
266 * sk_sp previously had a reference to an object (i.e. not null) it will call unref() on that
267 * object.
268 */
269 sk_sp<T>& operator=(const sk_sp<T>& that) {
270 this->reset(SkSafeRef(that.get()));
271 return *this;
272 }
bungeman801b44c2016-04-28 11:18:07 -0700273 template <typename U, typename = skstd::enable_if_t<std::is_convertible<U*, T*>::value>>
halcanarycb6cb382016-03-02 08:11:26 -0800274 sk_sp<T>& operator=(const sk_sp<U>& that) {
275 this->reset(SkSafeRef(that.get()));
276 return *this;
277 }
reedbb7b0432016-03-01 07:28:51 -0800278
279 /**
280 * Move the underlying object from the argument to the sk_sp. If the sk_sp previously held
281 * a reference to another object, unref() will be called on that object. No call to ref()
282 * will be made.
283 */
284 sk_sp<T>& operator=(sk_sp<T>&& that) {
285 this->reset(that.release());
286 return *this;
287 }
bungeman801b44c2016-04-28 11:18:07 -0700288 template <typename U, typename = skstd::enable_if_t<std::is_convertible<U*, T*>::value>>
halcanarycb6cb382016-03-02 08:11:26 -0800289 sk_sp<T>& operator=(sk_sp<U>&& that) {
290 this->reset(that.release());
291 return *this;
292 }
reedbb7b0432016-03-01 07:28:51 -0800293
bungemanbeab9e72016-03-03 07:50:49 -0800294 T& operator*() const {
295 SkASSERT(this->get() != nullptr);
296 return *this->get();
297 }
298
halcanary89bd99b2016-03-02 08:21:39 -0800299 // MSVC 2013 does not work correctly with explicit operator bool.
300 // https://chromium-cpp.appspot.com/#core-blacklist
bungemanbeab9e72016-03-03 07:50:49 -0800301 // When explicit operator bool can be used, remove operator! and operator unspecified_bool_type.
halcanary89bd99b2016-03-02 08:21:39 -0800302 //explicit operator bool() const { return this->get() != nullptr; }
bungemanbeab9e72016-03-03 07:50:49 -0800303 operator unspecified_bool_type() const { return this->get() ? &sk_sp::fPtr : nullptr; }
halcanary89bd99b2016-03-02 08:21:39 -0800304 bool operator!() const { return this->get() == nullptr; }
reedbb7b0432016-03-01 07:28:51 -0800305
306 T* get() const { return fPtr; }
307 T* operator->() const { return fPtr; }
308
309 /**
310 * Adopt the new bare pointer, and call unref() on any previously held object (if not null).
311 * No call to ref() will be made.
312 */
313 void reset(T* ptr = nullptr) {
bungemanc901c112016-03-08 08:35:23 -0800314 // Calling fPtr->unref() may call this->~() or this->reset(T*).
315 // http://wg21.cmeerw.net/lwg/issue998
316 // http://wg21.cmeerw.net/lwg/issue2262
317 T* oldPtr = fPtr;
reed941da9d2016-03-06 13:54:00 -0800318 fPtr = ptr;
bungemanc901c112016-03-08 08:35:23 -0800319 SkSafeUnref(oldPtr);
reedbb7b0432016-03-01 07:28:51 -0800320 }
321
322 /**
323 * Return the bare pointer, and set the internal object pointer to nullptr.
324 * The caller must assume ownership of the object, and manage its reference count directly.
325 * No call to unref() will be made.
326 */
327 T* SK_WARN_UNUSED_RESULT release() {
328 T* ptr = fPtr;
329 fPtr = nullptr;
330 return ptr;
331 }
332
bungemanc901c112016-03-08 08:35:23 -0800333 void swap(sk_sp<T>& that) /*noexcept*/ {
334 using std::swap;
335 swap(fPtr, that.fPtr);
336 }
337
reedbb7b0432016-03-01 07:28:51 -0800338private:
339 T* fPtr;
340};
341
bungemanc901c112016-03-08 08:35:23 -0800342template <typename T> inline void swap(sk_sp<T>& a, sk_sp<T>& b) /*noexcept*/ {
343 a.swap(b);
344}
345
346template <typename T, typename U> inline bool operator==(const sk_sp<T>& a, const sk_sp<U>& b) {
347 return a.get() == b.get();
348}
349template <typename T> inline bool operator==(const sk_sp<T>& a, std::nullptr_t) /*noexcept*/ {
350 return !a;
351}
352template <typename T> inline bool operator==(std::nullptr_t, const sk_sp<T>& b) /*noexcept*/ {
353 return !b;
354}
355
356template <typename T, typename U> inline bool operator!=(const sk_sp<T>& a, const sk_sp<U>& b) {
357 return a.get() != b.get();
358}
359template <typename T> inline bool operator!=(const sk_sp<T>& a, std::nullptr_t) /*noexcept*/ {
360 return static_cast<bool>(a);
361}
362template <typename T> inline bool operator!=(std::nullptr_t, const sk_sp<T>& b) /*noexcept*/ {
363 return static_cast<bool>(b);
364}
365
366template <typename T, typename U> inline bool operator<(const sk_sp<T>& a, const sk_sp<U>& b) {
367 // Provide defined total order on sk_sp.
368 // http://wg21.cmeerw.net/lwg/issue1297
369 // http://wg21.cmeerw.net/lwg/issue1401 .
370 return std::less<skstd::common_type_t<T*, U*>>()(a.get(), b.get());
371}
372template <typename T> inline bool operator<(const sk_sp<T>& a, std::nullptr_t) {
373 return std::less<T*>()(a.get(), nullptr);
374}
375template <typename T> inline bool operator<(std::nullptr_t, const sk_sp<T>& b) {
376 return std::less<T*>()(nullptr, b.get());
377}
378
379template <typename T, typename U> inline bool operator<=(const sk_sp<T>& a, const sk_sp<U>& b) {
380 return !(b < a);
381}
382template <typename T> inline bool operator<=(const sk_sp<T>& a, std::nullptr_t) {
383 return !(nullptr < a);
384}
385template <typename T> inline bool operator<=(std::nullptr_t, const sk_sp<T>& b) {
386 return !(b < nullptr);
387}
388
389template <typename T, typename U> inline bool operator>(const sk_sp<T>& a, const sk_sp<U>& b) {
390 return b < a;
391}
392template <typename T> inline bool operator>(const sk_sp<T>& a, std::nullptr_t) {
393 return nullptr < a;
394}
395template <typename T> inline bool operator>(std::nullptr_t, const sk_sp<T>& b) {
396 return b < nullptr;
397}
398
399template <typename T, typename U> inline bool operator>=(const sk_sp<T>& a, const sk_sp<U>& b) {
400 return !(a < b);
401}
402template <typename T> inline bool operator>=(const sk_sp<T>& a, std::nullptr_t) {
403 return !(a < nullptr);
404}
405template <typename T> inline bool operator>=(std::nullptr_t, const sk_sp<T>& b) {
406 return !(nullptr < b);
407}
408
halcanary217c0b32016-03-02 08:06:20 -0800409template <typename T, typename... Args>
410sk_sp<T> sk_make_sp(Args&&... args) {
411 return sk_sp<T>(new T(std::forward<Args>(args)...));
412}
413
reed647cc842016-03-08 12:54:48 -0800414/*
415 * Returns a sk_sp wrapping the provided ptr AND calls ref on it (if not null).
416 *
417 * This is different than the semantics of the constructor for sk_sp, which just wraps the ptr,
418 * effectively "adopting" it.
reed647cc842016-03-08 12:54:48 -0800419 */
420template <typename T> sk_sp<T> sk_ref_sp(T* obj) {
421 return sk_sp<T>(SkSafeRef(obj));
422}
423
424#endif