SkRefCnt.h revision f79430350d9f06a72b307af879d7f3bdec7ff706
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
16/** \class SkRefCnt
17
18    SkRefCnt is the base class for objects that may be shared by multiple
19    objects. When an existing owner wants to share a reference, it calls ref().
20    When an owner wants to release its reference, it calls unref(). When the
21    shared object's reference count goes to zero as the result of an unref()
22    call, its (virtual) destructor is called. It is an error for the
23    destructor to be called explicitly (or via the object going out of scope on
24    the stack or calling delete) if getRefCnt() > 1.
25*/
26class SK_API SkRefCnt : SkNoncopyable {
27public:
28    SK_DECLARE_INST_COUNT_ROOT(SkRefCnt)
29
30    /** Default construct, initializing the reference count to 1.
31    */
32    SkRefCnt() : fRefCnt(1) {}
33
34    /** Destruct, asserting that the reference count is 1.
35    */
36    virtual ~SkRefCnt() {
37#ifdef SK_DEBUG
38        SkASSERT(fRefCnt == 1);
39        fRefCnt = 0;    // illegal value, to catch us if we reuse after delete
40#endif
41    }
42
43    /** Return the reference count.
44    */
45    int32_t getRefCnt() const { return fRefCnt; }
46
47    /** Increment the reference count. Must be balanced by a call to unref().
48    */
49    void ref() const {
50        SkASSERT(fRefCnt > 0);
51        sk_atomic_inc(&fRefCnt);  // No barrier required.
52    }
53
54    /** Decrement the reference count. If the reference count is 1 before the
55        decrement, then delete the object. Note that if this is the case, then
56        the object needs to have been allocated via new, and not on the stack.
57    */
58    void unref() const {
59        SkASSERT(fRefCnt > 0);
60        // Release barrier (SL/S), if not provided below.
61        if (sk_atomic_dec(&fRefCnt) == 1) {
62            // Aquire barrier (L/SL), if not provided above.
63            // Prevents code in dispose from happening before the decrement.
64            sk_membar_aquire__after_atomic_dec();
65            internal_dispose();
66        }
67    }
68
69    void validate() const {
70        SkASSERT(fRefCnt > 0);
71    }
72
73protected:
74    /**
75     *  Allow subclasses to call this if they've overridden internal_dispose
76     *  so they can reset fRefCnt before the destructor is called. Should only
77     *  be called right before calling through to inherited internal_dispose()
78     *  or before calling the destructor.
79     */
80    void internal_dispose_restore_refcnt_to_1() const {
81#ifdef SK_DEBUG
82        SkASSERT(0 == fRefCnt);
83        fRefCnt = 1;
84#endif
85    }
86
87private:
88    /**
89     *  Called when the ref count goes to 0.
90     */
91    virtual void internal_dispose() const {
92        this->internal_dispose_restore_refcnt_to_1();
93        SkDELETE(this);
94    }
95
96    friend class SkWeakRefCnt;
97    friend class GrTexture;     // to allow GrTexture's internal_dispose to
98                                // call SkRefCnt's & directly set fRefCnt (to 1)
99
100    mutable int32_t fRefCnt;
101
102    typedef SkNoncopyable INHERITED;
103};
104
105///////////////////////////////////////////////////////////////////////////////
106
107/** Helper macro to safely assign one SkRefCnt[TS]* to another, checking for
108    null in on each side of the assignment, and ensuring that ref() is called
109    before unref(), in case the two pointers point to the same object.
110 */
111#define SkRefCnt_SafeAssign(dst, src)   \
112    do {                                \
113        if (src) src->ref();            \
114        if (dst) dst->unref();          \
115        dst = src;                      \
116    } while (0)
117
118
119/** Check if the argument is non-null, and if so, call obj->ref()
120 */
121template <typename T> static inline void SkSafeRef(T* obj) {
122    if (obj) {
123        obj->ref();
124    }
125}
126
127/** Check if the argument is non-null, and if so, call obj->unref()
128 */
129template <typename T> static inline void SkSafeUnref(T* obj) {
130    if (obj) {
131        obj->unref();
132    }
133}
134
135///////////////////////////////////////////////////////////////////////////////
136
137/**
138 *  Utility class that simply unref's its argument in the destructor.
139 */
140template <typename T> class SkAutoTUnref : SkNoncopyable {
141public:
142    explicit SkAutoTUnref(T* obj = NULL) : fObj(obj) {}
143    ~SkAutoTUnref() { SkSafeUnref(fObj); }
144
145    T* get() const { return fObj; }
146
147    void reset(T* obj) {
148        SkSafeUnref(fObj);
149        fObj = obj;
150    }
151
152    /**
153     *  Return the hosted object (which may be null), transferring ownership.
154     *  The reference count is not modified, and the internal ptr is set to NULL
155     *  so unref() will not be called in our destructor. A subsequent call to
156     *  detach() will do nothing and return null.
157     */
158    T* detach() {
159        T* obj = fObj;
160        fObj = NULL;
161        return obj;
162    }
163
164    /**
165     * BlockRef<B> is a type which inherits from B, cannot be created,
166     * and makes ref and unref private.
167     */
168    template<typename B> class BlockRef : public B {
169    private:
170        BlockRef();
171        void ref() const;
172        void unref() const;
173    };
174    /**
175     *  SkAutoTUnref assumes ownership of the ref. As a result, it is an error
176     *  for the user to ref or unref through SkAutoTUnref. Therefore
177     *  SkAutoTUnref::operator-> returns BlockRef<T>*. This prevents use of
178     *  skAutoTUnrefInstance->ref() and skAutoTUnrefInstance->unref().
179     */
180    BlockRef<T> *operator->() const {
181        return static_cast<BlockRef<T>*>(fObj);
182    }
183    operator T*() { return fObj; }
184
185private:
186    T*  fObj;
187};
188
189class SkAutoUnref : public SkAutoTUnref<SkRefCnt> {
190public:
191    SkAutoUnref(SkRefCnt* obj) : SkAutoTUnref<SkRefCnt>(obj) {}
192};
193
194class SkAutoRef : SkNoncopyable {
195public:
196    SkAutoRef(SkRefCnt* obj) : fObj(obj) { SkSafeRef(obj); }
197    ~SkAutoRef() { SkSafeUnref(fObj); }
198private:
199    SkRefCnt* fObj;
200};
201
202/** Wrapper class for SkRefCnt pointers. This manages ref/unref of a pointer to
203    a SkRefCnt (or subclass) object.
204 */
205template <typename T> class SkRefPtr {
206public:
207    SkRefPtr() : fObj(NULL) {}
208    SkRefPtr(T* obj) : fObj(obj) { SkSafeRef(fObj); }
209    SkRefPtr(const SkRefPtr& o) : fObj(o.fObj) { SkSafeRef(fObj); }
210    ~SkRefPtr() { SkSafeUnref(fObj); }
211
212    SkRefPtr& operator=(const SkRefPtr& rp) {
213        SkRefCnt_SafeAssign(fObj, rp.fObj);
214        return *this;
215    }
216    SkRefPtr& operator=(T* obj) {
217        SkRefCnt_SafeAssign(fObj, obj);
218        return *this;
219    }
220
221    T* get() const { return fObj; }
222    T& operator*() const { return *fObj; }
223    T* operator->() const { return fObj; }
224
225    typedef T* SkRefPtr::*unspecified_bool_type;
226    operator unspecified_bool_type() const {
227        return fObj ? &SkRefPtr::fObj : NULL;
228    }
229
230private:
231    T* fObj;
232};
233
234#endif
235
236