| 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 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 SkAutoUnref(SkRefCnt* obj) : SkAutoTUnref<SkRefCnt>(obj) {} | 248 SkAutoUnref(SkRefCnt* obj) : SkAutoTUnref<SkRefCnt>(obj) {} |
| 249 }; | 249 }; |
| 250 #define SkAutoUnref(...) SK_REQUIRE_LOCAL_VAR(SkAutoUnref) | 250 #define SkAutoUnref(...) SK_REQUIRE_LOCAL_VAR(SkAutoUnref) |
| 251 | 251 |
| 252 // This is a variant of SkRefCnt that's Not Virtual, so weighs 4 bytes instead o
f 8 or 16. | 252 // This is a variant of SkRefCnt that's Not Virtual, so weighs 4 bytes instead o
f 8 or 16. |
| 253 // There's only benefit to using this if the deriving class does not otherwise n
eed a vtable. | 253 // There's only benefit to using this if the deriving class does not otherwise n
eed a vtable. |
| 254 template <typename Derived> | 254 template <typename Derived> |
| 255 class SkNVRefCnt : SkNoncopyable { | 255 class SkNVRefCnt : SkNoncopyable { |
| 256 public: | 256 public: |
| 257 SkNVRefCnt() : fRefCnt(1) {} | 257 SkNVRefCnt() : fRefCnt(1) {} |
| 258 ~SkNVRefCnt() { SkASSERTF(1 == fRefCnt, "NVRefCnt was %d", fRefCnt); } |
| 258 | 259 |
| 259 // Implementation is pretty much the same as SkRefCntBase. All required barr
iers are the same: | 260 // Implementation is pretty much the same as SkRefCntBase. All required barr
iers are the same: |
| 260 // - unique() needs acquire when it returns true, and no barrier if it ret
urns false; | 261 // - unique() needs acquire when it returns true, and no barrier if it ret
urns false; |
| 261 // - ref() doesn't need any barrier; | 262 // - ref() doesn't need any barrier; |
| 262 // - unref() needs a release barrier, and an acquire if it's going to call
delete. | 263 // - unref() needs a release barrier, and an acquire if it's going to call
delete. |
| 263 | 264 |
| 264 bool unique() const { return 1 == sk_acquire_load(&fRefCnt); } | 265 bool unique() const { return 1 == sk_acquire_load(&fRefCnt); } |
| 265 void ref() const { sk_atomic_inc(&fRefCnt); } | 266 void ref() const { sk_atomic_inc(&fRefCnt); } |
| 266 void unref() const { | 267 void unref() const { |
| 267 int32_t prevValue = sk_atomic_dec(&fRefCnt); | 268 int32_t prevValue = sk_atomic_dec(&fRefCnt); |
| 268 SkASSERT(prevValue >= 1); | 269 SkASSERT(prevValue >= 1); |
| 269 if (1 == prevValue) { | 270 if (1 == prevValue) { |
| 271 SkDEBUGCODE(fRefCnt = 1;) // restore the 1 for our destructor's as
sert |
| 270 SkDELETE((const Derived*)this); | 272 SkDELETE((const Derived*)this); |
| 271 } | 273 } |
| 272 } | 274 } |
| 273 void deref() const { this->unref(); } // Chrome prefers to call deref(). | 275 void deref() const { this->unref(); } // Chrome prefers to call deref(). |
| 274 | 276 |
| 275 protected: | |
| 276 #ifdef SK_DEBUG | |
| 277 ~SkNVRefCnt() { | |
| 278 SkASSERTF(0 == fRefCnt, "NVRefCnt was %d", fRefCnt); | |
| 279 } | |
| 280 #endif | |
| 281 | |
| 282 private: | 277 private: |
| 283 mutable int32_t fRefCnt; | 278 mutable int32_t fRefCnt; |
| 284 }; | 279 }; |
| 285 | 280 |
| 286 #endif | 281 #endif |
| OLD | NEW |