Bug 1313351 - Fix js::RefCounted to not do leak checking. r=jandem

This commit is contained in:
Luke Wagner 2016-12-23 21:13:26 +01:00
Родитель f68cf3f349
Коммит 7f0959b60f
3 изменённых файлов: 80 добавлений и 15 удалений

Просмотреть файл

@ -7,20 +7,84 @@
#ifndef js_RefCounted_h
#define js_RefCounted_h
#include "mozilla/RefCounted.h"
#include "mozilla/Atomics.h"
#include "mozilla/RefCountType.h"
#include "js/Utility.h"
// These types implement the same interface as mozilla::(Atomic)RefCounted and
// must be used instead of mozilla::(Atomic)RefCounted for everything in
// SpiderMonkey. There are two reasons:
// - Release() needs to call js_delete, not delete
// - SpiderMonkey does not have MOZILLA_INTERNAL_API defined which can lead
// to ODR violations that show up as spurious leak reports when ref-counted
// types are allocated by SpiderMonkey and released by Gecko (or vice versa).
namespace js {
// Replacement for mozilla::RefCounted and mozilla::external::AtomicRefCounted
// that default to JS::DeletePolicy.
template <typename T>
class RefCounted
{
static const MozRefCountType DEAD = 0xffffdead;
template <typename T, typename D = JS::DeletePolicy<T>>
using RefCounted = mozilla::RefCounted<T, D>;
protected:
RefCounted() : mRefCnt(0) {}
~RefCounted() { MOZ_ASSERT(mRefCnt == DEAD); }
template <typename T, typename D = JS::DeletePolicy<T>>
using AtomicRefCounted = mozilla::external::AtomicRefCounted<T, D>;
public:
void AddRef() const
{
MOZ_ASSERT(int32_t(mRefCnt) >= 0);
++mRefCnt;
}
void Release() const
{
MOZ_ASSERT(int32_t(mRefCnt) > 0);
MozRefCountType cnt = --mRefCnt;
if (0 == cnt) {
#ifdef DEBUG
mRefCnt = DEAD;
#endif
js_delete(const_cast<T*>(static_cast<const T*>(this)));
}
}
private:
mutable MozRefCountType mRefCnt;
};
template <typename T>
class AtomicRefCounted
{
static const MozRefCountType DEAD = 0xffffdead;
protected:
AtomicRefCounted() : mRefCnt(0) {}
~AtomicRefCounted() { MOZ_ASSERT(mRefCnt == DEAD); }
public:
void AddRef() const
{
MOZ_ASSERT(int32_t(mRefCnt) >= 0);
++mRefCnt;
}
void Release() const
{
MOZ_ASSERT(int32_t(mRefCnt) > 0);
MozRefCountType cnt = --mRefCnt;
if (0 == cnt) {
#ifdef DEBUG
mRefCnt = DEAD;
#endif
js_delete(const_cast<T*>(static_cast<const T*>(this)));
}
}
private:
mutable mozilla::Atomic<MozRefCountType> mRefCnt;
};
} // namespace js

Просмотреть файл

@ -6108,7 +6108,6 @@ SetBuildIdOp(JSContext* cx, BuildIdOp buildIdOp);
struct WasmModule : js::AtomicRefCounted<WasmModule>
{
MOZ_DECLARE_REFCOUNTED_TYPENAME(WasmModule)
virtual ~WasmModule() {}
virtual void serializedSize(size_t* maybeBytecodeSize, size_t* maybeCompiledSize) const = 0;

Просмотреть файл

@ -16,7 +16,6 @@
#include "mozilla/Move.h"
#include "mozilla/RefCountType.h"
#include "mozilla/TypeTraits.h"
#include "mozilla/UniquePtr.h"
#if defined(MOZILLA_INTERNAL_API)
#include "nsXPCOM.h"
@ -53,6 +52,9 @@ namespace mozilla {
* Note that when deriving from RefCounted or AtomicRefCounted, you
* should add MOZ_DECLARE_REFCOUNTED_TYPENAME(ClassName) to the public
* section of your class, where ClassName is the name of your class.
*
* Note: SpiderMonkey should use js::RefCounted instead since that type
* will use appropriate js_delete and also not break ref-count logging.
*/
namespace detail {
const MozRefCountType DEAD = 0xffffdead;
@ -87,7 +89,7 @@ enum RefCountAtomicity
NonAtomicRefCount
};
template<typename T, RefCountAtomicity Atomicity, typename D>
template<typename T, RefCountAtomicity Atomicity>
class RefCounted
{
protected:
@ -133,7 +135,7 @@ public:
#ifdef DEBUG
mRefCnt = detail::DEAD;
#endif
D()(const_cast<T*>(static_cast<const T*>(this)));
delete static_cast<const T*>(this);
}
}
@ -172,8 +174,8 @@ private:
} // namespace detail
template<typename T, class D = DefaultDelete<T>>
class RefCounted : public detail::RefCounted<T, detail::NonAtomicRefCount, D>
template<typename T>
class RefCounted : public detail::RefCounted<T, detail::NonAtomicRefCount>
{
public:
~RefCounted()
@ -192,9 +194,9 @@ namespace external {
* NOTE: Please do not use this class, use NS_INLINE_DECL_THREADSAFE_REFCOUNTING
* instead.
*/
template<typename T, typename D = DefaultDelete<T>>
template<typename T>
class AtomicRefCounted :
public mozilla::detail::RefCounted<T, mozilla::detail::AtomicRefCount, D>
public mozilla::detail::RefCounted<T, mozilla::detail::AtomicRefCount>
{
public:
~AtomicRefCounted()