gfx/2d/GenericRefCounted.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
michael@0 2 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 // This header provides virtual, non-templated alternatives to MFBT's RefCounted<T>.
michael@0 7 // It intentionally uses MFBT coding style with the intention of moving there
michael@0 8 // should there be other use cases for it.
michael@0 9
michael@0 10 #ifndef MOZILLA_GENERICREFCOUNTED_H_
michael@0 11 #define MOZILLA_GENERICREFCOUNTED_H_
michael@0 12
michael@0 13 #include "mozilla/RefPtr.h"
michael@0 14
michael@0 15 namespace mozilla {
michael@0 16
michael@0 17 /**
michael@0 18 * Common base class for GenericRefCounted and GenericAtomicRefCounted.
michael@0 19 *
michael@0 20 * Having this shared base class, common to both the atomic and non-atomic
michael@0 21 * cases, allows to have RefPtr's that don't care about whether the
michael@0 22 * objects they're managing have atomic refcounts or not.
michael@0 23 */
michael@0 24 class GenericRefCountedBase
michael@0 25 {
michael@0 26 protected:
michael@0 27 virtual ~GenericRefCountedBase() {};
michael@0 28
michael@0 29 public:
michael@0 30 // AddRef() and Release() method names are for compatibility with nsRefPtr.
michael@0 31 virtual void AddRef() = 0;
michael@0 32
michael@0 33 virtual void Release() = 0;
michael@0 34
michael@0 35 // ref() and deref() method names are for compatibility with wtf::RefPtr.
michael@0 36 // No virtual keywords here: if a subclass wants to override the refcounting
michael@0 37 // mechanism, it is welcome to do so by overriding AddRef() and Release().
michael@0 38 void ref() { AddRef(); }
michael@0 39 void deref() { Release(); }
michael@0 40
michael@0 41 #ifdef MOZ_REFCOUNTED_LEAK_CHECKING
michael@0 42 virtual const char* typeName() const = 0;
michael@0 43 virtual size_t typeSize() const = 0;
michael@0 44 #endif
michael@0 45 };
michael@0 46
michael@0 47 namespace detail {
michael@0 48
michael@0 49 template<RefCountAtomicity Atomicity>
michael@0 50 class GenericRefCounted : public GenericRefCountedBase
michael@0 51 {
michael@0 52 protected:
michael@0 53 GenericRefCounted() : refCnt(0) { }
michael@0 54
michael@0 55 virtual ~GenericRefCounted() {
michael@0 56 MOZ_ASSERT(refCnt == detail::DEAD);
michael@0 57 }
michael@0 58
michael@0 59 public:
michael@0 60 virtual void AddRef() {
michael@0 61 // Note: this method must be thread safe for GenericAtomicRefCounted.
michael@0 62 MOZ_ASSERT(int32_t(refCnt) >= 0);
michael@0 63 #ifndef MOZ_REFCOUNTED_LEAK_CHECKING
michael@0 64 ++refCnt;
michael@0 65 #else
michael@0 66 const char* type = typeName();
michael@0 67 uint32_t size = typeSize();
michael@0 68 const void* ptr = this;
michael@0 69 MozRefCountType cnt = ++refCnt;
michael@0 70 detail::RefCountLogger::logAddRef(ptr, cnt, type, size);
michael@0 71 #endif
michael@0 72 }
michael@0 73
michael@0 74 virtual void Release() {
michael@0 75 // Note: this method must be thread safe for GenericAtomicRefCounted.
michael@0 76 MOZ_ASSERT(int32_t(refCnt) > 0);
michael@0 77 #ifndef MOZ_REFCOUNTED_LEAK_CHECKING
michael@0 78 MozRefCountType cnt = --refCnt;
michael@0 79 #else
michael@0 80 const char* type = typeName();
michael@0 81 const void* ptr = this;
michael@0 82 MozRefCountType cnt = --refCnt;
michael@0 83 // Note: it's not safe to touch |this| after decrementing the refcount,
michael@0 84 // except for below.
michael@0 85 detail::RefCountLogger::logRelease(ptr, cnt, type);
michael@0 86 #endif
michael@0 87 if (0 == cnt) {
michael@0 88 // Because we have atomically decremented the refcount above, only
michael@0 89 // one thread can get a 0 count here, so as long as we can assume that
michael@0 90 // everything else in the system is accessing this object through
michael@0 91 // RefPtrs, it's safe to access |this| here.
michael@0 92 #ifdef DEBUG
michael@0 93 refCnt = detail::DEAD;
michael@0 94 #endif
michael@0 95 delete this;
michael@0 96 }
michael@0 97 }
michael@0 98
michael@0 99 MozRefCountType refCount() const { return refCnt; }
michael@0 100 bool hasOneRef() const {
michael@0 101 MOZ_ASSERT(refCnt > 0);
michael@0 102 return refCnt == 1;
michael@0 103 }
michael@0 104
michael@0 105 private:
michael@0 106 typename Conditional<Atomicity == AtomicRefCount, Atomic<MozRefCountType>, MozRefCountType>::Type refCnt;
michael@0 107 };
michael@0 108
michael@0 109 } // namespace detail
michael@0 110
michael@0 111 /**
michael@0 112 * This reference-counting base class is virtual instead of
michael@0 113 * being templated, which is useful in cases where one needs
michael@0 114 * genericity at binary code level, but comes at the cost
michael@0 115 * of a moderate performance and size overhead, like anything virtual.
michael@0 116 */
michael@0 117 class GenericRefCounted : public detail::GenericRefCounted<detail::NonAtomicRefCount>
michael@0 118 {
michael@0 119 };
michael@0 120
michael@0 121 /**
michael@0 122 * GenericAtomicRefCounted is like GenericRefCounted, with an atomically updated
michael@0 123 * reference counter.
michael@0 124 */
michael@0 125 class GenericAtomicRefCounted : public detail::GenericRefCounted<detail::AtomicRefCount>
michael@0 126 {
michael@0 127 };
michael@0 128
michael@0 129 } // namespace mozilla
michael@0 130
michael@0 131 #endif

mercurial