SkRefCnt.h revision 4efd9d9872420b40f618294a0db4bbf7f70405d2
1
2/*
3 * Copyright 2006 The Android Open Source Project
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9
10#ifndef SkRefCnt_DEFINED
11#define SkRefCnt_DEFINED
12
13#include "SkThread.h"
14#include "SkInstCnt.h"
15#include "SkTemplates.h"
16
17/** \class SkRefCnt
18
19    SkRefCnt is the base class for objects that may be shared by multiple
20    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.
26*/
27class SK_API SkRefCnt : SkNoncopyable {
28public:
29    SK_DECLARE_INST_COUNT_ROOT(SkRefCnt)
30
31    /** Default construct, initializing the reference count to 1.
32    */
33    SkRefCnt() : fRefCnt(1) {}
34
35    /** Destruct, asserting that the reference count is 1.
36    */
37    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    }
43
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);
52        sk_atomic_inc(&fRefCnt);  // No barrier required.
53    }
54
55    /** Decrement the reference count. If the reference count is 1 before the
56        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.
58    */
59    void unref() const {
60        SkASSERT(fRefCnt > 0);
61        // Release barrier (SL/S), if not provided below.
62        if (sk_atomic_dec(&fRefCnt) == 1) {
63            // 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();
67        }
68    }
69
70    void validate() const {
71        SkASSERT(fRefCnt > 0);
72    }
73
74protected:
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 {
82#ifdef SK_DEBUG
83        SkASSERT(0 == fRefCnt);
84        fRefCnt = 1;
85#endif
86    }
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();
94        SkDELETE(this);
95    }
96
97    friend class SkWeakRefCnt;
98    friend class GrTexture;     // to allow GrTexture's internal_dispose to
99                                // call SkRefCnt's & directly set fRefCnt (to 1)
100
101    mutable int32_t fRefCnt;
102
103    typedef SkNoncopyable INHERITED;
104};
105
106///////////////////////////////////////////////////////////////////////////////
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
120/** Call obj->ref() and return obj. The obj must not be NULL.
121 */
122template <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) {
131    if (obj) {
132        obj->ref();
133    }
134    return obj;
135}
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
147/**
148 *  Utility class that simply unref's its argument in the destructor.
149 */
150template <typename T> class SkAutoTUnref : SkNoncopyable {
151public:
152    explicit SkAutoTUnref(T* obj = NULL) : fObj(obj) {}
153    ~SkAutoTUnref() { SkSafeUnref(fObj); }
154
155    T* get() const { return fObj; }
156
157    void reset(T* obj) {
158        if (obj != fObj) {
159            SkSafeUnref(fObj);
160            fObj = obj;
161        }
162    }
163
164    void swap(SkAutoTUnref* other) {
165        T* tmp = fObj;
166        fObj = other->fObj;
167        other->fObj = tmp;
168    }
169
170    /**
171     *  Return the hosted object (which may be null), transferring ownership.
172     *  The reference count is not modified, and the internal ptr is set to NULL
173     *  so unref() will not be called in our destructor. A subsequent call to
174     *  detach() will do nothing and return null.
175     */
176    T* detach() {
177        T* obj = fObj;
178        fObj = NULL;
179        return obj;
180    }
181
182    /**
183     * BlockRef<B> is a type which inherits from B, cannot be created,
184     * and makes ref and unref private.
185     */
186    template<typename B> class BlockRef : public B {
187    private:
188        BlockRef();
189        void ref() const;
190        void unref() const;
191    };
192
193    /** If T is const, the type returned from operator-> will also be const. */
194    typedef typename SkTConstType<BlockRef<T>, SkTIsConst<T>::value>::type BlockRefType;
195
196    /**
197     *  SkAutoTUnref assumes ownership of the ref. As a result, it is an error
198     *  for the user to ref or unref through SkAutoTUnref. Therefore
199     *  SkAutoTUnref::operator-> returns BlockRef<T>*. This prevents use of
200     *  skAutoTUnrefInstance->ref() and skAutoTUnrefInstance->unref().
201     */
202    BlockRefType *operator->() const {
203        return static_cast<BlockRefType*>(fObj);
204    }
205    operator T*() { return fObj; }
206
207private:
208    T*  fObj;
209};
210
211class SkAutoUnref : public SkAutoTUnref<SkRefCnt> {
212public:
213    SkAutoUnref(SkRefCnt* obj) : SkAutoTUnref<SkRefCnt>(obj) {}
214};
215
216class SkAutoRef : SkNoncopyable {
217public:
218    SkAutoRef(SkRefCnt* obj) : fObj(obj) { SkSafeRef(obj); }
219    ~SkAutoRef() { SkSafeUnref(fObj); }
220private:
221    SkRefCnt* fObj;
222};
223
224/** Wrapper class for SkRefCnt pointers. This manages ref/unref of a pointer to
225    a SkRefCnt (or subclass) object.
226 */
227template <typename T> class SkRefPtr {
228public:
229    SkRefPtr() : fObj(NULL) {}
230    SkRefPtr(T* obj) : fObj(obj) { SkSafeRef(fObj); }
231    SkRefPtr(const SkRefPtr& o) : fObj(o.fObj) { SkSafeRef(fObj); }
232    ~SkRefPtr() { SkSafeUnref(fObj); }
233
234    SkRefPtr& operator=(const SkRefPtr& rp) {
235        SkRefCnt_SafeAssign(fObj, rp.fObj);
236        return *this;
237    }
238    SkRefPtr& operator=(T* obj) {
239        SkRefCnt_SafeAssign(fObj, obj);
240        return *this;
241    }
242
243    T* get() const { return fObj; }
244    T& operator*() const { return *fObj; }
245    T* operator->() const { return fObj; }
246
247    typedef T* SkRefPtr::*unspecified_bool_type;
248    operator unspecified_bool_type() const {
249        return fObj ? &SkRefPtr::fObj : NULL;
250    }
251
252private:
253    T* fObj;
254};
255
256#endif
257
258