blob: 3a0f8be3833364251dac5339826ecefe688a9fca [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@android.com8a1c16f2008-12-17 15:59:43 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#ifndef SkRefCnt_DEFINED
11#define SkRefCnt_DEFINED
12
13#include "SkThread.h"
robertphillips@google.com977b9c82012-06-05 19:35:09 +000014#include "SkInstCnt.h"
bungeman@google.com91208922012-07-30 15:03:59 +000015#include "SkTemplates.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000016
17/** \class SkRefCnt
18
19 SkRefCnt is the base class for objects that may be shared by multiple
bungeman@google.coma02bc152012-05-16 18:21:56 +000020 objects. When an existing owner wants to share a reference, it calls ref().
21 When an owner wants to release its reference, it calls unref(). When the
22 shared object's reference count goes to zero as the result of an unref()
23 call, its (virtual) destructor is called. It is an error for the
24 destructor to be called explicitly (or via the object going out of scope on
25 the stack or calling delete) if getRefCnt() > 1.
reed@android.com8a1c16f2008-12-17 15:59:43 +000026*/
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +000027class SK_API SkRefCnt : SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +000028public:
robertphillips@google.com4d73ac22012-06-13 18:54:08 +000029 SK_DECLARE_INST_COUNT_ROOT(SkRefCnt)
robertphillips@google.com977b9c82012-06-05 19:35:09 +000030
reed@android.com8a1c16f2008-12-17 15:59:43 +000031 /** Default construct, initializing the reference count to 1.
32 */
33 SkRefCnt() : fRefCnt(1) {}
34
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 */
reed@google.com4c888aa2011-09-12 19:54:12 +000037 virtual ~SkRefCnt() {
38#ifdef SK_DEBUG
39 SkASSERT(fRefCnt == 1);
40 fRefCnt = 0; // illegal value, to catch us if we reuse after delete
41#endif
42 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000043
44 /** Return the reference count.
45 */
46 int32_t getRefCnt() const { return fRefCnt; }
47
48 /** Increment the reference count. Must be balanced by a call to unref().
49 */
50 void ref() const {
51 SkASSERT(fRefCnt > 0);
bungeman@google.coma02bc152012-05-16 18:21:56 +000052 sk_atomic_inc(&fRefCnt); // No barrier required.
reed@android.com8a1c16f2008-12-17 15:59:43 +000053 }
54
55 /** Decrement the reference count. If the reference count is 1 before the
bungeman@google.coma02bc152012-05-16 18:21:56 +000056 decrement, then delete the object. Note that if this is the case, then
57 the object needs to have been allocated via new, and not on the stack.
reed@android.com8a1c16f2008-12-17 15:59:43 +000058 */
59 void unref() const {
60 SkASSERT(fRefCnt > 0);
bungeman@google.coma02bc152012-05-16 18:21:56 +000061 // Release barrier (SL/S), if not provided below.
reed@android.com8a1c16f2008-12-17 15:59:43 +000062 if (sk_atomic_dec(&fRefCnt) == 1) {
bungeman@google.coma02bc152012-05-16 18:21:56 +000063 // Aquire barrier (L/SL), if not provided above.
64 // Prevents code in dispose from happening before the decrement.
65 sk_membar_aquire__after_atomic_dec();
66 internal_dispose();
reed@android.com8a1c16f2008-12-17 15:59:43 +000067 }
68 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000069
reed@google.com7f6d6d42011-07-15 15:25:22 +000070 void validate() const {
71 SkASSERT(fRefCnt > 0);
72 }
73
reed@google.comf7943032012-07-23 14:50:38 +000074protected:
75 /**
76 * Allow subclasses to call this if they've overridden internal_dispose
77 * so they can reset fRefCnt before the destructor is called. Should only
78 * be called right before calling through to inherited internal_dispose()
79 * or before calling the destructor.
80 */
81 void internal_dispose_restore_refcnt_to_1() const {
bungeman@google.coma02bc152012-05-16 18:21:56 +000082#ifdef SK_DEBUG
reed@google.comf7943032012-07-23 14:50:38 +000083 SkASSERT(0 == fRefCnt);
bungeman@google.coma02bc152012-05-16 18:21:56 +000084 fRefCnt = 1;
85#endif
reed@google.comf7943032012-07-23 14:50:38 +000086 }
87
88private:
89 /**
90 * Called when the ref count goes to 0.
91 */
92 virtual void internal_dispose() const {
93 this->internal_dispose_restore_refcnt_to_1();
bungeman@google.coma02bc152012-05-16 18:21:56 +000094 SkDELETE(this);
95 }
robertphillips@google.com15c0fea2012-06-22 12:41:43 +000096
bungeman@google.coma02bc152012-05-16 18:21:56 +000097 friend class SkWeakRefCnt;
robertphillips@google.com15c0fea2012-06-22 12:41:43 +000098 friend class GrTexture; // to allow GrTexture's internal_dispose to
99 // call SkRefCnt's & directly set fRefCnt (to 1)
bungeman@google.coma02bc152012-05-16 18:21:56 +0000100
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101 mutable int32_t fRefCnt;
robertphillips@google.com4d73ac22012-06-13 18:54:08 +0000102
103 typedef SkNoncopyable INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104};
105
reed@google.com7f6d6d42011-07-15 15:25:22 +0000106///////////////////////////////////////////////////////////////////////////////
107
108/** Helper macro to safely assign one SkRefCnt[TS]* to another, checking for
109 null in on each side of the assignment, and ensuring that ref() is called
110 before unref(), in case the two pointers point to the same object.
111 */
112#define SkRefCnt_SafeAssign(dst, src) \
113 do { \
114 if (src) src->ref(); \
115 if (dst) dst->unref(); \
116 dst = src; \
117 } while (0)
118
119
bungeman@google.com1fd201b2012-08-22 18:56:56 +0000120/** Call obj->ref() and return obj. The obj must not be NULL.
reed@google.com7f6d6d42011-07-15 15:25:22 +0000121 */
bungeman@google.com1fd201b2012-08-22 18:56:56 +0000122template <typename T> static inline T* SkRef(T* obj) {
123 SkASSERT(obj);
124 obj->ref();
125 return obj;
126}
127
128/** Check if the argument is non-null, and if so, call obj->ref() and return obj.
129 */
130template <typename T> static inline T* SkSafeRef(T* obj) {
reed@google.com7f6d6d42011-07-15 15:25:22 +0000131 if (obj) {
132 obj->ref();
133 }
bungeman@google.com1fd201b2012-08-22 18:56:56 +0000134 return obj;
reed@google.com7f6d6d42011-07-15 15:25:22 +0000135}
136
137/** Check if the argument is non-null, and if so, call obj->unref()
138 */
139template <typename T> static inline void SkSafeUnref(T* obj) {
140 if (obj) {
141 obj->unref();
142 }
143}
144
145///////////////////////////////////////////////////////////////////////////////
146
reed@google.coma67573e2011-02-25 18:10:29 +0000147/**
bungeman@google.com91208922012-07-30 15:03:59 +0000148 * Utility class that simply unref's its argument in the destructor.
reed@google.coma67573e2011-02-25 18:10:29 +0000149 */
bungeman@google.com91208922012-07-30 15:03:59 +0000150template <typename T> class SkAutoTUnref : SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000151public:
bungeman@google.com91208922012-07-30 15:03:59 +0000152 explicit SkAutoTUnref(T* obj = NULL) : fObj(obj) {}
153 ~SkAutoTUnref() { SkSafeUnref(fObj); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000154
reed@google.coma67573e2011-02-25 18:10:29 +0000155 T* get() const { return fObj; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000157 void reset(T* obj) {
reed@google.com68d6bb02012-11-27 18:37:52 +0000158 SkSafeUnref(fObj);
159 fObj = obj;
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000160 }
161
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000162 void swap(SkAutoTUnref* other) {
163 T* tmp = fObj;
164 fObj = other->fObj;
165 other->fObj = tmp;
166 }
167
reed@google.coma67573e2011-02-25 18:10:29 +0000168 /**
169 * Return the hosted object (which may be null), transferring ownership.
170 * The reference count is not modified, and the internal ptr is set to NULL
171 * so unref() will not be called in our destructor. A subsequent call to
172 * detach() will do nothing and return null.
173 */
174 T* detach() {
175 T* obj = fObj;
176 fObj = NULL;
177 return obj;
178 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000179
bungeman@google.come70f7982012-06-01 19:38:19 +0000180 /**
bungeman@google.com91208922012-07-30 15:03:59 +0000181 * BlockRef<B> is a type which inherits from B, cannot be created,
182 * and makes ref and unref private.
bungeman@google.come70f7982012-06-01 19:38:19 +0000183 */
bungeman@google.com04640292012-06-01 19:47:51 +0000184 template<typename B> class BlockRef : public B {
bungeman@google.come70f7982012-06-01 19:38:19 +0000185 private:
186 BlockRef();
187 void ref() const;
188 void unref() const;
189 };
bsalomon@google.com1448cf82012-07-27 13:27:35 +0000190
bungeman@google.com91208922012-07-30 15:03:59 +0000191 /** If T is const, the type returned from operator-> will also be const. */
192 typedef typename SkTConstType<BlockRef<T>, SkTIsConst<T>::value>::type BlockRefType;
193
194 /**
195 * SkAutoTUnref assumes ownership of the ref. As a result, it is an error
196 * for the user to ref or unref through SkAutoTUnref. Therefore
197 * SkAutoTUnref::operator-> returns BlockRef<T>*. This prevents use of
198 * skAutoTUnrefInstance->ref() and skAutoTUnrefInstance->unref().
199 */
200 BlockRefType *operator->() const {
201 return static_cast<BlockRefType*>(fObj);
202 }
203 operator T*() { return fObj; }
bsalomon@google.com1448cf82012-07-27 13:27:35 +0000204
205private:
bungeman@google.com91208922012-07-30 15:03:59 +0000206 T* fObj;
reed@google.coma67573e2011-02-25 18:10:29 +0000207};
208
209class SkAutoUnref : public SkAutoTUnref<SkRefCnt> {
210public:
211 SkAutoUnref(SkRefCnt* obj) : SkAutoTUnref<SkRefCnt>(obj) {}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000212};
213
reed@google.com7f6d6d42011-07-15 15:25:22 +0000214class SkAutoRef : SkNoncopyable {
215public:
216 SkAutoRef(SkRefCnt* obj) : fObj(obj) { SkSafeRef(obj); }
217 ~SkAutoRef() { SkSafeUnref(fObj); }
218private:
219 SkRefCnt* fObj;
220};
reed@android.com149e2f62009-05-22 14:39:03 +0000221
reed@android.com756f6dd2010-04-16 20:56:00 +0000222/** Wrapper class for SkRefCnt pointers. This manages ref/unref of a pointer to
223 a SkRefCnt (or subclass) object.
224 */
225template <typename T> class SkRefPtr {
reed@android.comb00cd722010-04-16 20:35:47 +0000226public:
227 SkRefPtr() : fObj(NULL) {}
228 SkRefPtr(T* obj) : fObj(obj) { SkSafeRef(fObj); }
229 SkRefPtr(const SkRefPtr& o) : fObj(o.fObj) { SkSafeRef(fObj); }
230 ~SkRefPtr() { SkSafeUnref(fObj); }
231
232 SkRefPtr& operator=(const SkRefPtr& rp) {
233 SkRefCnt_SafeAssign(fObj, rp.fObj);
234 return *this;
235 }
236 SkRefPtr& operator=(T* obj) {
237 SkRefCnt_SafeAssign(fObj, obj);
238 return *this;
239 }
240
reed@android.comb00cd722010-04-16 20:35:47 +0000241 T* get() const { return fObj; }
242 T& operator*() const { return *fObj; }
243 T* operator->() const { return fObj; }
reed@android.comb00cd722010-04-16 20:35:47 +0000244
245 typedef T* SkRefPtr::*unspecified_bool_type;
reed@google.comddbf4c82011-03-08 16:06:06 +0000246 operator unspecified_bool_type() const {
247 return fObj ? &SkRefPtr::fObj : NULL;
248 }
reed@android.comb00cd722010-04-16 20:35:47 +0000249
250private:
251 T* fObj;
252};
253
reed@android.com8a1c16f2008-12-17 15:59:43 +0000254#endif
255