gfx/layers/AtomicRefCountedWithFinalize.h

branch
TOR_BUG_9701
changeset 8
97036ab72558
equal deleted inserted replaced
-1:000000000000 0:1543d94f1dc5
1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 #ifndef MOZILLA_ATOMICREFCOUNTEDWITHFINALIZE_H_
7 #define MOZILLA_ATOMICREFCOUNTEDWITHFINALIZE_H_
8
9 #include "mozilla/RefPtr.h"
10 #include "mozilla/NullPtr.h"
11
12 namespace mozilla {
13
14 template<typename T>
15 class AtomicRefCountedWithFinalize
16 {
17 protected:
18 AtomicRefCountedWithFinalize()
19 : mRecycleCallback(nullptr)
20 , mRefCount(0)
21 {}
22
23 ~AtomicRefCountedWithFinalize() {}
24
25 public:
26 void AddRef() {
27 MOZ_ASSERT(mRefCount >= 0);
28 ++mRefCount;
29 }
30
31 void Release() {
32 MOZ_ASSERT(mRefCount > 0);
33 // Read mRecycleCallback early so that it does not get set to
34 // deleted memory, if the object is goes away.
35 RecycleCallback recycleCallback = mRecycleCallback;
36 int currCount = --mRefCount;
37 if (0 == currCount) {
38 // Recycle listeners must call ClearRecycleCallback
39 // before releasing their strong reference.
40 MOZ_ASSERT(mRecycleCallback == nullptr);
41 #ifdef DEBUG
42 mRefCount = detail::DEAD;
43 #endif
44 T* derived = static_cast<T*>(this);
45 derived->Finalize();
46 delete derived;
47 } else if (1 == currCount && recycleCallback) {
48 T* derived = static_cast<T*>(this);
49 recycleCallback(derived, mClosure);
50 }
51 }
52
53 typedef void (*RecycleCallback)(T* aObject, void* aClosure);
54 /**
55 * Set a callback responsible for recycling this object
56 * before it is finalized.
57 */
58 void SetRecycleCallback(RecycleCallback aCallback, void* aClosure)
59 {
60 mRecycleCallback = aCallback;
61 mClosure = aClosure;
62 }
63 void ClearRecycleCallback() { SetRecycleCallback(nullptr, nullptr); }
64
65 private:
66 RecycleCallback mRecycleCallback;
67 void *mClosure;
68 Atomic<int> mRefCount;
69 };
70
71 }
72
73 #endif

mercurial