michael@0: /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef MOZILLA_ATOMICREFCOUNTEDWITHFINALIZE_H_ michael@0: #define MOZILLA_ATOMICREFCOUNTEDWITHFINALIZE_H_ michael@0: michael@0: #include "mozilla/RefPtr.h" michael@0: #include "mozilla/NullPtr.h" michael@0: michael@0: namespace mozilla { michael@0: michael@0: template michael@0: class AtomicRefCountedWithFinalize michael@0: { michael@0: protected: michael@0: AtomicRefCountedWithFinalize() michael@0: : mRecycleCallback(nullptr) michael@0: , mRefCount(0) michael@0: {} michael@0: michael@0: ~AtomicRefCountedWithFinalize() {} michael@0: michael@0: public: michael@0: void AddRef() { michael@0: MOZ_ASSERT(mRefCount >= 0); michael@0: ++mRefCount; michael@0: } michael@0: michael@0: void Release() { michael@0: MOZ_ASSERT(mRefCount > 0); michael@0: // Read mRecycleCallback early so that it does not get set to michael@0: // deleted memory, if the object is goes away. michael@0: RecycleCallback recycleCallback = mRecycleCallback; michael@0: int currCount = --mRefCount; michael@0: if (0 == currCount) { michael@0: // Recycle listeners must call ClearRecycleCallback michael@0: // before releasing their strong reference. michael@0: MOZ_ASSERT(mRecycleCallback == nullptr); michael@0: #ifdef DEBUG michael@0: mRefCount = detail::DEAD; michael@0: #endif michael@0: T* derived = static_cast(this); michael@0: derived->Finalize(); michael@0: delete derived; michael@0: } else if (1 == currCount && recycleCallback) { michael@0: T* derived = static_cast(this); michael@0: recycleCallback(derived, mClosure); michael@0: } michael@0: } michael@0: michael@0: typedef void (*RecycleCallback)(T* aObject, void* aClosure); michael@0: /** michael@0: * Set a callback responsible for recycling this object michael@0: * before it is finalized. michael@0: */ michael@0: void SetRecycleCallback(RecycleCallback aCallback, void* aClosure) michael@0: { michael@0: mRecycleCallback = aCallback; michael@0: mClosure = aClosure; michael@0: } michael@0: void ClearRecycleCallback() { SetRecycleCallback(nullptr, nullptr); } michael@0: michael@0: private: michael@0: RecycleCallback mRecycleCallback; michael@0: void *mClosure; michael@0: Atomic mRefCount; michael@0: }; michael@0: michael@0: } michael@0: michael@0: #endif