OLD | NEW |
---|---|
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2006 The Android Open Source Project | 3 * Copyright 2006 The Android Open Source Project |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 | 9 |
10 #ifndef SkRefCnt_DEFINED | 10 #ifndef SkRefCnt_DEFINED |
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
246 SkAutoUnref(SkRefCnt* obj) : SkAutoTUnref<SkRefCnt>(obj) {} | 246 SkAutoUnref(SkRefCnt* obj) : SkAutoTUnref<SkRefCnt>(obj) {} |
247 }; | 247 }; |
248 #define SkAutoUnref(...) SK_REQUIRE_LOCAL_VAR(SkAutoUnref) | 248 #define SkAutoUnref(...) SK_REQUIRE_LOCAL_VAR(SkAutoUnref) |
249 | 249 |
250 // This is a variant of SkRefCnt that's Not Virtual, so weighs 4 bytes instead o f 8 or 16. | 250 // This is a variant of SkRefCnt that's Not Virtual, so weighs 4 bytes instead o f 8 or 16. |
251 // There's only benefit to using this if the deriving class does not otherwise n eed a vtable. | 251 // There's only benefit to using this if the deriving class does not otherwise n eed a vtable. |
252 template <typename Derived> | 252 template <typename Derived> |
253 class SkNVRefCnt : SkNoncopyable { | 253 class SkNVRefCnt : SkNoncopyable { |
254 public: | 254 public: |
255 SkNVRefCnt() : fRefCnt(1) {} | 255 SkNVRefCnt() : fRefCnt(1) {} |
256 #ifdef SK_DEBUG | |
mtklein
2014/11/22 00:08:14
Double debug guards seem like overkill? Should be
| |
257 ~SkNVRefCnt() { | |
258 SkASSERTF(1 == fRefCnt, "NVRefCnt was %d", fRefCnt); | |
259 } | |
260 #endif | |
256 | 261 |
257 // Implementation is pretty much the same as SkRefCntBase. All required barr iers are the same: | 262 // Implementation is pretty much the same as SkRefCntBase. All required barr iers are the same: |
258 // - unique() needs acquire when it returns true, and no barrier if it ret urns false; | 263 // - unique() needs acquire when it returns true, and no barrier if it ret urns false; |
259 // - ref() doesn't need any barrier; | 264 // - ref() doesn't need any barrier; |
260 // - unref() needs a release barrier, and an acquire if it's going to call delete. | 265 // - unref() needs a release barrier, and an acquire if it's going to call delete. |
261 | 266 |
262 bool unique() const { return 1 == sk_acquire_load(&fRefCnt); } | 267 bool unique() const { return 1 == sk_acquire_load(&fRefCnt); } |
263 void ref() const { sk_atomic_inc(&fRefCnt); } | 268 void ref() const { sk_atomic_inc(&fRefCnt); } |
264 void unref() const { if (1 == sk_atomic_dec(&fRefCnt)) { SkDELETE((const De rived*)this); } } | 269 void unref() const { |
270 int32_t prevValue = sk_atomic_dec(&fRefCnt); | |
271 SkASSERT(prevValue >= 1); | |
272 if (1 == prevValue) { | |
273 SkDEBUGCODE(fRefCnt = 1;) // for the assert in our destructor | |
bsalomon
2014/11/21 22:48:50
Why 1? For stack objects? Do we want to allow that
mtklein
2014/11/22 00:08:14
I'm game to try disallowing the stack and delete i
| |
274 SkDELETE((const Derived*)this); | |
275 } | |
276 } | |
265 void deref() const { this->unref(); } // Chrome prefers to call deref(). | 277 void deref() const { this->unref(); } // Chrome prefers to call deref(). |
266 int32_t getRefCnt() const { return fRefCnt; } // Used by Chrome unit tests. | 278 int32_t getRefCnt() const { return fRefCnt; } // Used by Chrome unit tests. |
267 | 279 |
268 private: | 280 private: |
269 mutable int32_t fRefCnt; | 281 mutable int32_t fRefCnt; |
270 }; | 282 }; |
271 | 283 |
272 #endif | 284 #endif |
OLD | NEW |