gfx/layers/AtomicRefCountedWithFinalize.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

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 #ifndef MOZILLA_ATOMICREFCOUNTEDWITHFINALIZE_H_
michael@0 7 #define MOZILLA_ATOMICREFCOUNTEDWITHFINALIZE_H_
michael@0 8
michael@0 9 #include "mozilla/RefPtr.h"
michael@0 10 #include "mozilla/NullPtr.h"
michael@0 11
michael@0 12 namespace mozilla {
michael@0 13
michael@0 14 template<typename T>
michael@0 15 class AtomicRefCountedWithFinalize
michael@0 16 {
michael@0 17 protected:
michael@0 18 AtomicRefCountedWithFinalize()
michael@0 19 : mRecycleCallback(nullptr)
michael@0 20 , mRefCount(0)
michael@0 21 {}
michael@0 22
michael@0 23 ~AtomicRefCountedWithFinalize() {}
michael@0 24
michael@0 25 public:
michael@0 26 void AddRef() {
michael@0 27 MOZ_ASSERT(mRefCount >= 0);
michael@0 28 ++mRefCount;
michael@0 29 }
michael@0 30
michael@0 31 void Release() {
michael@0 32 MOZ_ASSERT(mRefCount > 0);
michael@0 33 // Read mRecycleCallback early so that it does not get set to
michael@0 34 // deleted memory, if the object is goes away.
michael@0 35 RecycleCallback recycleCallback = mRecycleCallback;
michael@0 36 int currCount = --mRefCount;
michael@0 37 if (0 == currCount) {
michael@0 38 // Recycle listeners must call ClearRecycleCallback
michael@0 39 // before releasing their strong reference.
michael@0 40 MOZ_ASSERT(mRecycleCallback == nullptr);
michael@0 41 #ifdef DEBUG
michael@0 42 mRefCount = detail::DEAD;
michael@0 43 #endif
michael@0 44 T* derived = static_cast<T*>(this);
michael@0 45 derived->Finalize();
michael@0 46 delete derived;
michael@0 47 } else if (1 == currCount && recycleCallback) {
michael@0 48 T* derived = static_cast<T*>(this);
michael@0 49 recycleCallback(derived, mClosure);
michael@0 50 }
michael@0 51 }
michael@0 52
michael@0 53 typedef void (*RecycleCallback)(T* aObject, void* aClosure);
michael@0 54 /**
michael@0 55 * Set a callback responsible for recycling this object
michael@0 56 * before it is finalized.
michael@0 57 */
michael@0 58 void SetRecycleCallback(RecycleCallback aCallback, void* aClosure)
michael@0 59 {
michael@0 60 mRecycleCallback = aCallback;
michael@0 61 mClosure = aClosure;
michael@0 62 }
michael@0 63 void ClearRecycleCallback() { SetRecycleCallback(nullptr, nullptr); }
michael@0 64
michael@0 65 private:
michael@0 66 RecycleCallback mRecycleCallback;
michael@0 67 void *mClosure;
michael@0 68 Atomic<int> mRefCount;
michael@0 69 };
michael@0 70
michael@0 71 }
michael@0 72
michael@0 73 #endif

mercurial