Bug 935778 - Part 3: Add trace-refcount logging for AddRef and Release in RefCounted objects; r=dbaron

This commit is contained in:
Ehsan Akhgari 2014-03-09 14:36:36 -04:00
parent 2f70114c7b
commit 8cd0c0f21d
2 changed files with 67 additions and 2 deletions

View File

@ -14,6 +14,13 @@
#include "mozilla/Attributes.h"
#include "mozilla/RefCountType.h"
#include "mozilla/TypeTraits.h"
#if defined(MOZILLA_INTERNAL_API)
#include "nsXPCOM.h"
#endif
#if defined(MOZILLA_INTERNAL_API) && (defined(DEBUG) || defined(FORCE_BUILD_REFCNT_LOGGING))
#define MOZ_REFCOUNTED_LEAK_CHECKING
#endif
namespace mozilla {
@ -53,6 +60,28 @@ namespace detail {
const MozRefCountType DEAD = 0xffffdead;
#endif
// When building code that gets compiled into Gecko, try to use the
// trace-refcount leak logging facilities.
#ifdef MOZ_REFCOUNTED_LEAK_CHECKING
class RefCountLogger
{
public:
static void logAddRef(const void* aPointer, MozRefCountType aRefCount,
const char* aTypeName, uint32_t aInstanceSize)
{
MOZ_ASSERT(aRefCount != DEAD);
NS_LogAddRef(const_cast<void*>(aPointer), aRefCount, aTypeName, aInstanceSize);
}
static void logRelease(const void* aPointer, MozRefCountType aRefCount,
const char* aTypeName)
{
MOZ_ASSERT(aRefCount != DEAD);
NS_LogRelease(const_cast<void*>(aPointer), aRefCount, aTypeName);
}
};
#endif
// This is used WeakPtr.h as well as this file.
enum RefCountAtomicity
{
@ -76,11 +105,21 @@ class RefCounted
void AddRef() const {
MOZ_ASSERT(int32_t(refCnt) >= 0);
++refCnt;
#ifdef MOZ_REFCOUNTED_LEAK_CHECKING
detail::RefCountLogger::logAddRef(static_cast<const T*>(this), refCnt,
static_cast<const T*>(this)->typeName(),
static_cast<const T*>(this)->typeSize());
#endif
}
void Release() const {
MOZ_ASSERT(int32_t(refCnt) > 0);
if (0 == --refCnt) {
--refCnt;
#ifdef MOZ_REFCOUNTED_LEAK_CHECKING
detail::RefCountLogger::logRelease(static_cast<const T*>(this), refCnt,
static_cast<const T*>(this)->typeName());
#endif
if (0 == refCnt) {
#ifdef DEBUG
refCnt = detail::DEAD;
#endif
@ -101,7 +140,7 @@ class RefCounted
mutable typename Conditional<Atomicity == AtomicRefCount, Atomic<MozRefCountType>, MozRefCountType>::Type refCnt;
};
#if defined(MOZILLA_INTERNAL_API) && (defined(DEBUG) || defined(FORCE_BUILD_REFCNT_LOGGING))
#ifdef MOZ_REFCOUNTED_LEAK_CHECKING
#define MOZ_DECLARE_REFCOUNTED_TYPENAME(T) \
const char* typeName() const { return #T; } \
size_t typeSize() const { return sizeof(*this); }

View File

@ -64,11 +64,14 @@
#ifndef mozilla_WeakPtr_h
#define mozilla_WeakPtr_h
#include "mozilla/ArrayUtils.h"
#include "mozilla/Assertions.h"
#include "mozilla/NullPtr.h"
#include "mozilla/RefPtr.h"
#include "mozilla/TypeTraits.h"
#include <string.h>
namespace mozilla {
template <typename T, class WeakReference> class WeakPtrBase;
@ -86,6 +89,29 @@ class WeakReference : public ::mozilla::RefCounted<WeakReference<T> >
return ptr;
}
#ifdef MOZ_REFCOUNTED_LEAK_CHECKING
#ifdef XP_WIN
#define snprintf _snprintf
#endif
const char* typeName() const {
static char nameBuffer[1024];
const char* innerType = ptr->typeName();
// We could do fancier length checks at runtime, but innerType is
// controlled by us so we can ensure that this never causes a buffer
// overflow by this assertion.
MOZ_ASSERT(strlen(innerType) + sizeof("WeakReference<>") < ArrayLength(nameBuffer),
"Exceedingly large type name");
snprintf(nameBuffer, ArrayLength(nameBuffer), "WeakReference<%s>", innerType);
// This is usually not OK, but here we are returning a pointer to a static
// buffer which will immediately be used by the caller.
return nameBuffer;
}
size_t typeSize() const {
return sizeof(*this);
}
#undef snprintf
#endif
private:
friend class WeakPtrBase<T, WeakReference<T> >;
friend class SupportsWeakPtrBase<T, WeakReference<T> >;