aboutsummaryrefslogtreecommitdiffstats
path: root/include/core/SkRefCnt.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/core/SkRefCnt.h')
-rw-r--r--include/core/SkRefCnt.h103
1 files changed, 60 insertions, 43 deletions
diff --git a/include/core/SkRefCnt.h b/include/core/SkRefCnt.h
index 36f4249..7af0017 100644
--- a/include/core/SkRefCnt.h
+++ b/include/core/SkRefCnt.h
@@ -1,19 +1,12 @@
+
/*
- * Copyright (C) 2006 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
+ * Copyright 2006 The Android Open Source Project
*
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
*/
+
#ifndef SkRefCnt_DEFINED
#define SkRefCnt_DEFINED
@@ -37,7 +30,12 @@ public:
/** Destruct, asserting that the reference count is 1.
*/
- virtual ~SkRefCnt() { SkASSERT(fRefCnt == 1); }
+ virtual ~SkRefCnt() {
+#ifdef SK_DEBUG
+ SkASSERT(fRefCnt == 1);
+ fRefCnt = 0; // illegal value, to catch us if we reuse after delete
+#endif
+ }
/** Return the reference count.
*/
@@ -63,20 +61,61 @@ public:
}
}
+ void validate() const {
+ SkASSERT(fRefCnt > 0);
+ }
+
private:
mutable int32_t fRefCnt;
};
+///////////////////////////////////////////////////////////////////////////////
+
+/** Helper macro to safely assign one SkRefCnt[TS]* to another, checking for
+ null in on each side of the assignment, and ensuring that ref() is called
+ before unref(), in case the two pointers point to the same object.
+ */
+#define SkRefCnt_SafeAssign(dst, src) \
+ do { \
+ if (src) src->ref(); \
+ if (dst) dst->unref(); \
+ dst = src; \
+ } while (0)
+
+
+/** Check if the argument is non-null, and if so, call obj->ref()
+ */
+template <typename T> static inline void SkSafeRef(T* obj) {
+ if (obj) {
+ obj->ref();
+ }
+}
+
+/** Check if the argument is non-null, and if so, call obj->unref()
+ */
+template <typename T> static inline void SkSafeUnref(T* obj) {
+ if (obj) {
+ obj->unref();
+ }
+}
+
+///////////////////////////////////////////////////////////////////////////////
+
/**
* Utility class that simply unref's its argument in the destructor.
*/
template <typename T> class SkAutoTUnref : SkNoncopyable {
public:
- SkAutoTUnref(T* obj) : fObj(obj) {}
+ explicit SkAutoTUnref(T* obj = NULL) : fObj(obj) {}
~SkAutoTUnref() { SkSafeUnref(fObj); }
T* get() const { return fObj; }
+ void reset(T* obj) {
+ SkSafeUnref(fObj);
+ fObj = obj;
+ }
+
/**
* Return the hosted object (which may be null), transferring ownership.
* The reference count is not modified, and the internal ptr is set to NULL
@@ -98,35 +137,13 @@ public:
SkAutoUnref(SkRefCnt* obj) : SkAutoTUnref<SkRefCnt>(obj) {}
};
-///////////////////////////////////////////////////////////////////////////////
-
-/** Helper macro to safely assign one SkRefCnt[TS]* to another, checking for
- null in on each side of the assignment, and ensuring that ref() is called
- before unref(), in case the two pointers point to the same object.
-*/
-#define SkRefCnt_SafeAssign(dst, src) \
- do { \
- if (src) src->ref(); \
- if (dst) dst->unref(); \
- dst = src; \
- } while (0)
-
-
-/** Check if the argument is non-null, and if so, call obj->ref()
- */
-template <typename T> static inline void SkSafeRef(T* obj) {
- if (obj) {
- obj->ref();
- }
-}
-
-/** Check if the argument is non-null, and if so, call obj->unref()
- */
-template <typename T> static inline void SkSafeUnref(T* obj) {
- if (obj) {
- obj->unref();
- }
-}
+class SkAutoRef : SkNoncopyable {
+public:
+ SkAutoRef(SkRefCnt* obj) : fObj(obj) { SkSafeRef(obj); }
+ ~SkAutoRef() { SkSafeUnref(fObj); }
+private:
+ SkRefCnt* fObj;
+};
/** Wrapper class for SkRefCnt pointers. This manages ref/unref of a pointer to
a SkRefCnt (or subclass) object.