OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef GrNonAtomicRef_DEFINED | 8 #ifndef GrNonAtomicRef_DEFINED |
9 #define GrNonAtomicRef_DEFINED | 9 #define GrNonAtomicRef_DEFINED |
10 | 10 |
11 #include "SkRefCnt.h" | 11 #include "SkRefCnt.h" |
12 #include "SkTArray.h" | 12 #include "SkTArray.h" |
13 | 13 |
14 /** | 14 /** |
15 * A simple non-atomic ref used in the GrBackend when we don't want to pay for t
he overhead of a | 15 * A simple non-atomic ref used in the GrBackend when we don't want to pay for t
he overhead of a |
16 * threadsafe ref counted object | 16 * threadsafe ref counted object |
17 */ | 17 */ |
18 class GrNonAtomicRef : public SkNoncopyable { | 18 template<typename TSubclass> class GrNonAtomicRef : public SkNoncopyable { |
19 public: | 19 public: |
20 GrNonAtomicRef() : fRefCnt(1) {} | 20 GrNonAtomicRef() : fRefCnt(1) {} |
21 virtual ~GrNonAtomicRef() { | 21 |
| 22 #ifdef SK_DEBUG |
| 23 ~GrNonAtomicRef() { |
22 // fRefCnt can be one when a subclass is created statically | 24 // fRefCnt can be one when a subclass is created statically |
23 SkASSERT((0 == fRefCnt || 1 == fRefCnt)); | 25 SkASSERT((0 == fRefCnt || 1 == fRefCnt)); |
24 // Set to invalid values. | 26 // Set to invalid values. |
25 SkDEBUGCODE(fRefCnt = -10;) | 27 fRefCnt = -10; |
26 } | 28 } |
| 29 #endif |
27 | 30 |
28 void ref() const { | 31 void ref() const { |
29 // Once the ref cnt reaches zero it should never be ref'ed again. | 32 // Once the ref cnt reaches zero it should never be ref'ed again. |
30 SkASSERT(fRefCnt > 0); | 33 SkASSERT(fRefCnt > 0); |
31 ++fRefCnt; | 34 ++fRefCnt; |
32 } | 35 } |
33 | 36 |
34 void unref() const { | 37 void unref() const { |
35 SkASSERT(fRefCnt > 0); | 38 SkASSERT(fRefCnt > 0); |
36 --fRefCnt; | 39 --fRefCnt; |
37 if (0 == fRefCnt) { | 40 if (0 == fRefCnt) { |
38 delete this; | 41 GrTDeleteNonAtomicRef(static_cast<const TSubclass*>(this)); |
39 return; | 42 return; |
40 } | 43 } |
41 } | 44 } |
42 | 45 |
43 private: | 46 private: |
44 mutable int32_t fRefCnt; | 47 mutable int32_t fRefCnt; |
45 | 48 |
46 typedef SkNoncopyable INHERITED; | 49 typedef SkNoncopyable INHERITED; |
47 }; | 50 }; |
48 | 51 |
| 52 template<typename T> inline void GrTDeleteNonAtomicRef(const T* ref) { |
| 53 delete ref; |
| 54 } |
| 55 |
49 #endif | 56 #endif |
OLD | NEW |