gfx/layers/AtomicRefCountedWithFinalize.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/layers/AtomicRefCountedWithFinalize.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,73 @@
     1.4 +/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef MOZILLA_ATOMICREFCOUNTEDWITHFINALIZE_H_
    1.10 +#define MOZILLA_ATOMICREFCOUNTEDWITHFINALIZE_H_
    1.11 +
    1.12 +#include "mozilla/RefPtr.h"
    1.13 +#include "mozilla/NullPtr.h"
    1.14 +
    1.15 +namespace mozilla {
    1.16 +
    1.17 +template<typename T>
    1.18 +class AtomicRefCountedWithFinalize
    1.19 +{
    1.20 +  protected:
    1.21 +    AtomicRefCountedWithFinalize()
    1.22 +      : mRecycleCallback(nullptr)
    1.23 +      , mRefCount(0)
    1.24 +    {}
    1.25 +
    1.26 +    ~AtomicRefCountedWithFinalize() {}
    1.27 +
    1.28 +  public:
    1.29 +    void AddRef() {
    1.30 +      MOZ_ASSERT(mRefCount >= 0);
    1.31 +      ++mRefCount;
    1.32 +    }
    1.33 +
    1.34 +    void Release() {
    1.35 +      MOZ_ASSERT(mRefCount > 0);
    1.36 +      // Read mRecycleCallback early so that it does not get set to
    1.37 +      // deleted memory, if the object is goes away.
    1.38 +      RecycleCallback recycleCallback = mRecycleCallback;
    1.39 +      int currCount = --mRefCount;
    1.40 +      if (0 == currCount) {
    1.41 +        // Recycle listeners must call ClearRecycleCallback
    1.42 +        // before releasing their strong reference.
    1.43 +        MOZ_ASSERT(mRecycleCallback == nullptr);
    1.44 +#ifdef DEBUG
    1.45 +        mRefCount = detail::DEAD;
    1.46 +#endif
    1.47 +        T* derived = static_cast<T*>(this);
    1.48 +        derived->Finalize();
    1.49 +        delete derived;
    1.50 +      } else if (1 == currCount && recycleCallback) {
    1.51 +        T* derived = static_cast<T*>(this);
    1.52 +        recycleCallback(derived, mClosure);
    1.53 +      }
    1.54 +    }
    1.55 +
    1.56 +    typedef void (*RecycleCallback)(T* aObject, void* aClosure);
    1.57 +    /**
    1.58 +     * Set a callback responsible for recycling this object
    1.59 +     * before it is finalized.
    1.60 +     */
    1.61 +    void SetRecycleCallback(RecycleCallback aCallback, void* aClosure)
    1.62 +    {
    1.63 +      mRecycleCallback = aCallback;
    1.64 +      mClosure = aClosure;
    1.65 +    }
    1.66 +    void ClearRecycleCallback() { SetRecycleCallback(nullptr, nullptr); }
    1.67 +
    1.68 +private:
    1.69 +    RecycleCallback mRecycleCallback;
    1.70 +    void *mClosure;
    1.71 +    Atomic<int> mRefCount;
    1.72 +};
    1.73 +
    1.74 +}
    1.75 +
    1.76 +#endif

mercurial