Bug 935778 - Part 1: Add RefCountType, a type compatible with nsrefcnt, to MFBT; r=dbaron,froydnj
authorEhsan Akhgari <ehsan@mozilla.com>
Fri, 21 Feb 2014 14:45:50 -0500
changeset 170282 80b3309661a19f62900ca9050f4de52ce256b657
parent 170281 5b5af9a67885a19761c8bc5293c68bd32843d0fb
child 170283 cbd2e8c512f64d7911636082f2d083573349410d
push id270
push userpvanderbeken@mozilla.com
push dateThu, 06 Mar 2014 09:24:21 +0000
reviewersdbaron, froydnj
bugs935778
milestone30.0a1
Bug 935778 - Part 1: Add RefCountType, a type compatible with nsrefcnt, to MFBT; r=dbaron,froydnj
gfx/2d/GenericRefCounted.h
mfbt/RefCountType.h
mfbt/RefPtr.h
mfbt/moz.build
mozglue/linker/ElfLoader.h
xpcom/base/nscore.h
--- a/gfx/2d/GenericRefCounted.h
+++ b/gfx/2d/GenericRefCounted.h
@@ -48,38 +48,37 @@ class GenericRefCounted : public Generic
     GenericRefCounted() : refCnt(0) { }
 
     virtual ~GenericRefCounted() {
       MOZ_ASSERT(refCnt == detail::DEAD);
     }
 
   public:
     virtual void AddRef() {
-      MOZ_ASSERT(refCnt >= 0);
       ++refCnt;
     }
 
     virtual void Release() {
       MOZ_ASSERT(refCnt > 0);
       if (0 == --refCnt) {
 #ifdef DEBUG
         refCnt = detail::DEAD;
 #endif
         delete this;
       }
     }
 
-    int refCount() const { return refCnt; }
+    MozRefCountType refCount() const { return refCnt; }
     bool hasOneRef() const {
       MOZ_ASSERT(refCnt > 0);
       return refCnt == 1;
     }
 
   private:
-    typename Conditional<Atomicity == AtomicRefCount, Atomic<int>, int>::Type refCnt;
+    typename Conditional<Atomicity == AtomicRefCount, Atomic<MozRefCountType>, MozRefCountType>::Type refCnt;
 };
 
 } // namespace detail
 
 /**
  * This reference-counting base class is virtual instead of
  * being templated, which is useful in cases where one needs
  * genericity at binary code level, but comes at the cost
new file mode 100644
--- /dev/null
+++ b/mfbt/RefCountType.h
@@ -0,0 +1,32 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef mozilla_RefCountType_h
+#define mozilla_RefCountType_h
+
+/**
+ * MozRefCountType is Mozilla's reference count type.
+ *
+ * This is the return type for AddRef() and Release() in nsISupports.
+ * IUnknown of COM returns an unsigned long from equivalent functions.
+ *
+ * We use the same type to represent the refcount of RefCounted objects
+ * as well, in order to be able to use the leak detection facilities
+ * that are implemented by XPCOM.
+ *
+ * The following ifdef exists to maintain binary compatibility with
+ * IUnknown, the base interface in Microsoft COM.
+ *
+ * Note that this type is not in the mozilla namespace so that it is
+ * usable for both C and C++ code.
+ */
+#ifdef XP_WIN
+typedef unsigned long MozRefCountType;
+#else
+typedef uint32_t MozRefCountType;
+#endif
+
+#endif
--- a/mfbt/RefPtr.h
+++ b/mfbt/RefPtr.h
@@ -7,16 +7,17 @@
 /* Helpers for defining and using refcounted objects. */
 
 #ifndef mozilla_RefPtr_h
 #define mozilla_RefPtr_h
 
 #include "mozilla/Assertions.h"
 #include "mozilla/Atomics.h"
 #include "mozilla/Attributes.h"
+#include "mozilla/RefCountType.h"
 #include "mozilla/TypeTraits.h"
 
 namespace mozilla {
 
 template<typename T> class RefCounted;
 template<typename T> class RefPtr;
 template<typename T> class TemporaryRef;
 template<typename T> class OutParamRef;
@@ -44,17 +45,17 @@ template<typename T> OutParamRef<T> byRe
  * use-after-destroy (refcount==0xffffdead).
  *
  * 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.
  */
 namespace detail {
 #ifdef DEBUG
-static const int DEAD = 0xffffdead;
+static const MozRefCountType DEAD = 0xffffdead;
 #endif
 
 // This is used WeakPtr.h as well as this file.
 enum RefCountAtomicity
 {
   AtomicRefCount,
   NonAtomicRefCount
 };
@@ -68,41 +69,40 @@ class RefCounted
     RefCounted() : refCnt(0) { }
     ~RefCounted() {
       MOZ_ASSERT(refCnt == detail::DEAD);
     }
 
   public:
     // Compatibility with nsRefPtr.
     void AddRef() const {
-      MOZ_ASSERT(refCnt >= 0);
       ++refCnt;
     }
 
     void Release() const {
       MOZ_ASSERT(refCnt > 0);
       if (0 == --refCnt) {
 #ifdef DEBUG
         refCnt = detail::DEAD;
 #endif
         delete static_cast<const T*>(this);
       }
     }
 
     // Compatibility with wtf::RefPtr.
     void ref() { AddRef(); }
     void deref() { Release(); }
-    int refCount() const { return refCnt; }
+    MozRefCountType refCount() const { return refCnt; }
     bool hasOneRef() const {
       MOZ_ASSERT(refCnt > 0);
       return refCnt == 1;
     }
 
   private:
-    mutable typename Conditional<Atomicity == AtomicRefCount, Atomic<int>, int>::Type refCnt;
+    mutable typename Conditional<Atomicity == AtomicRefCount, Atomic<MozRefCountType>, MozRefCountType>::Type refCnt;
 };
 
 #define MOZ_DECLARE_REFCOUNTED_TYPENAME(T) \
   const char* typeName() const { return #T; }
 
 }
 
 template<typename T>
--- a/mfbt/moz.build
+++ b/mfbt/moz.build
@@ -40,16 +40,17 @@ EXPORTS.mozilla = [
     'Move.h',
     'MSIntTypes.h',
     'NullPtr.h',
     'NumericLimits.h',
     'PodOperations.h',
     'Poison.h',
     'Range.h',
     'RangedPtr.h',
+    'RefCountType.h',
     'ReentrancyGuard.h',
     'RefPtr.h',
     'RollingMean.h',
     'Scoped.h',
     'SHA1.h',
     'SplayTree.h',
     'TemplateLib.h',
     'ThreadLocal.h',
--- a/mozglue/linker/ElfLoader.h
+++ b/mozglue/linker/ElfLoader.h
@@ -158,17 +158,17 @@ public:
       mozilla::AtomicRefCounted<LibHandle>::Release();
     }
     return ret;
   }
 
   /**
    * Returns the number of direct references
    */
-  int DirectRefCount()
+  MozRefCountType DirectRefCount()
   {
     return directRefCnt;
   }
 
   /**
    * Returns the complete size of the file or stream behind the library
    * handle.
    */
@@ -205,17 +205,17 @@ protected:
    * to do this without RTTI)
    */
   friend class ElfLoader;
   friend class CustomElf;
   friend class SEGVHandler;
   virtual bool IsSystemElf() const { return false; }
 
 private:
-  int directRefCnt;
+  MozRefCountType directRefCnt;
   char *path;
 
   /* Mappable object keeping the result of GetMappable() */
   mutable mozilla::RefPtr<Mappable> mappable;
 };
 
 /**
  * Specialized RefCounted<LibHandle>::Release. Under normal operation, when
--- a/xpcom/base/nscore.h
+++ b/xpcom/base/nscore.h
@@ -24,16 +24,18 @@
  */
 #include <stddef.h>
 #include <stdint.h>
 
 #ifdef __cplusplus
 #  include "mozilla/NullPtr.h"
 #endif
 
+#include "mozilla/RefCountType.h"
+
 /* Core XPCOM declarations. */
 
 /*----------------------------------------------------------------------*/
 /* Import/export defines */
 
 /**
  * Using the visibility("hidden") attribute allows the compiler to use
  * PC-relative addressing to call this function.  If a function does not
@@ -292,29 +294,17 @@
 #endif
 
 
 /**
  * Generic XPCOM result data type
  */
 #include "nsError.h"
 
-/**
- * Reference count values
- *
- * This is the return type for AddRef() and Release() in nsISupports.
- * IUnknown of COM returns an unsigned long from equivalent functions.
- * The following ifdef exists to maintain binary compatibility with
- * IUnknown.
- */
-#ifdef XP_WIN
-typedef unsigned long nsrefcnt;
-#else
-typedef uint32_t nsrefcnt;
-#endif
+typedef MozRefCountType nsrefcnt;
 
 /*
  * Use these macros to do 64bit safe pointer conversions.
  */
 
 #define NS_PTR_TO_INT32(x)  ((int32_t)  (intptr_t) (x))
 #define NS_PTR_TO_UINT32(x) ((uint32_t) (intptr_t) (x))
 #define NS_INT32_TO_PTR(x)  ((void *)   (intptr_t) (x))