Bug 1062479 - Use static strings for WeakReference type names. r=ehsan, a=sledru
authorAndrew McCreight <continuation@gmail.com>
Fri, 24 Apr 2015 09:43:01 -0700
changeset 260315 5d903629f9bd
parent 260314 c31c2a198a71
child 260316 cdfd06d73d17
push id746
push userryanvm@gmail.com
push date2015-04-28 22:47 +0000
treeherdermozilla-release@348c2ae68d50 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersehsan, sledru
bugs1062479
milestone38.0
Bug 1062479 - Use static strings for WeakReference type names. r=ehsan, a=sledru
docshell/base/nsDocShell.h
dom/canvas/WebGLContext.h
dom/canvas/WebGLFramebuffer.h
dom/canvas/WebGLProgram.h
dom/html/nsTextEditorState.h
dom/media/webaudio/PannerNode.h
dom/media/webspeech/recognition/SpeechRecognition.h
dom/promise/Promise.h
gfx/gl/SharedSurface.h
gfx/layers/ImageContainer.h
image/src/IProgressObserver.h
image/src/ProgressTracker.h
image/src/RasterImage.h
ipc/glue/MessageLink.h
mfbt/WeakPtr.h
mfbt/tests/TestWeakPtr.cpp
netwerk/sctp/datachannel/DataChannel.h
uriloader/prefetch/nsOfflineCacheUpdate.h
xpcom/base/nsMessageLoop.cpp
--- a/docshell/base/nsDocShell.h
+++ b/docshell/base/nsDocShell.h
@@ -154,17 +154,17 @@ class nsDocShell final
   , public nsIClipboardCommands
   , public nsIDOMStorageManager
   , public nsINetworkInterceptController
   , public mozilla::SupportsWeakPtr<nsDocShell>
 {
   friend class nsDSURIContentListener;
 
 public:
-  MOZ_DECLARE_REFCOUNTED_TYPENAME(nsDocShell)
+  MOZ_DECLARE_WEAKREFERENCE_TYPENAME(nsDocShell)
   // Object Management
   nsDocShell();
 
   NS_DECL_AND_IMPL_ZEROING_OPERATOR_NEW
 
   virtual nsresult Init() override;
 
   NS_DECL_ISUPPORTS_INHERITED
--- a/dom/canvas/WebGLContext.h
+++ b/dom/canvas/WebGLContext.h
@@ -185,17 +185,17 @@ class WebGLContext
         BROWSER_DEFAULT_WEBGL = 0x9244,
         UNMASKED_VENDOR_WEBGL = 0x9245,
         UNMASKED_RENDERER_WEBGL = 0x9246
     };
 
 public:
     WebGLContext();
 
-    MOZ_DECLARE_REFCOUNTED_TYPENAME(WebGLContext)
+    MOZ_DECLARE_WEAKREFERENCE_TYPENAME(WebGLContext)
 
     NS_DECL_CYCLE_COLLECTING_ISUPPORTS
 
     NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(WebGLContext,
                                                            nsIDOMWebGLRenderingContext)
 
     virtual JSObject* WrapObject(JSContext* aCx) override = 0;
 
--- a/dom/canvas/WebGLFramebuffer.h
+++ b/dom/canvas/WebGLFramebuffer.h
@@ -26,17 +26,17 @@ class WebGLFramebuffer final
     : public nsWrapperCache
     , public WebGLBindableName<FBTarget>
     , public WebGLRefCountedObject<WebGLFramebuffer>
     , public LinkedListElement<WebGLFramebuffer>
     , public WebGLContextBoundObject
     , public SupportsWeakPtr<WebGLFramebuffer>
 {
 public:
-    MOZ_DECLARE_REFCOUNTED_TYPENAME(WebGLFramebuffer)
+    MOZ_DECLARE_WEAKREFERENCE_TYPENAME(WebGLFramebuffer)
 
     explicit WebGLFramebuffer(WebGLContext* webgl, GLuint fbo);
 
     struct Attachment
     {
         // deleting a texture or renderbuffer immediately detaches it
         WebGLRefPtr<WebGLTexture> mTexturePtr;
         WebGLRefPtr<WebGLRenderbuffer> mRenderbufferPtr;
--- a/dom/canvas/WebGLProgram.h
+++ b/dom/canvas/WebGLProgram.h
@@ -24,16 +24,17 @@ class WebGLUniformLocation;
 
 namespace webgl {
 
 struct LinkedProgramInfo final
     : public RefCounted<LinkedProgramInfo>
     , public SupportsWeakPtr<LinkedProgramInfo>
 {
     MOZ_DECLARE_REFCOUNTED_TYPENAME(LinkedProgramInfo)
+    MOZ_DECLARE_WEAKREFERENCE_TYPENAME(LinkedProgramInfo)
 
     WebGLProgram* const prog;
     std::vector<nsRefPtr<WebGLActiveInfo>> activeAttribs;
     std::vector<nsRefPtr<WebGLActiveInfo>> activeUniforms;
 
     // Needed for Get{Attrib,Uniform}Location. The keys for these are non-mapped
     // user-facing `GLActiveInfo::name`s, without any final "[0]".
     std::map<nsCString, const WebGLActiveInfo*> attribMap;
--- a/dom/html/nsTextEditorState.h
+++ b/dom/html/nsTextEditorState.h
@@ -122,17 +122,17 @@ class HTMLInputElement;
  *     transferred to the mValue member variable, and will be managed there until a new
  *     frame is bound to the text editor state object.
  */
 
 class RestoreSelectionState;
 
 class nsTextEditorState : public mozilla::SupportsWeakPtr<nsTextEditorState> {
 public:
-  MOZ_DECLARE_REFCOUNTED_TYPENAME(nsTextEditorState)
+  MOZ_DECLARE_WEAKREFERENCE_TYPENAME(nsTextEditorState)
   explicit nsTextEditorState(nsITextControlElement* aOwningElement);
   ~nsTextEditorState();
 
   void Traverse(nsCycleCollectionTraversalCallback& cb);
   void Unlink();
 
   nsIEditor* GetEditor();
   nsISelectionController* GetSelectionController() const;
--- a/dom/media/webaudio/PannerNode.h
+++ b/dom/media/webaudio/PannerNode.h
@@ -20,17 +20,17 @@ namespace dom {
 
 class AudioContext;
 class AudioBufferSourceNode;
 
 class PannerNode : public AudioNode,
                    public SupportsWeakPtr<PannerNode>
 {
 public:
-  MOZ_DECLARE_REFCOUNTED_TYPENAME(PannerNode)
+  MOZ_DECLARE_WEAKREFERENCE_TYPENAME(PannerNode)
   explicit PannerNode(AudioContext* aContext);
 
   virtual JSObject* WrapObject(JSContext* aCx) override;
 
   virtual void DestroyMediaStream() override;
 
   virtual void SetChannelCount(uint32_t aChannelCount, ErrorResult& aRv) override
   {
--- a/dom/media/webspeech/recognition/SpeechRecognition.h
+++ b/dom/media/webspeech/recognition/SpeechRecognition.h
@@ -55,17 +55,17 @@ PRLogModuleInfo* GetSpeechRecognitionLog
 
 already_AddRefed<nsISpeechRecognitionService> GetSpeechRecognitionService();
 
 class SpeechRecognition final : public DOMEventTargetHelper,
                                     public nsIObserver,
                                     public SupportsWeakPtr<SpeechRecognition>
 {
 public:
-  MOZ_DECLARE_REFCOUNTED_TYPENAME(SpeechRecognition)
+  MOZ_DECLARE_WEAKREFERENCE_TYPENAME(SpeechRecognition)
   explicit SpeechRecognition(nsPIDOMWindow* aOwnerWindow);
 
   NS_DECL_ISUPPORTS_INHERITED
 
   NS_DECL_NSIOBSERVER
 
   nsISupports* GetParentObject() const;
 
--- a/dom/promise/Promise.h
+++ b/dom/promise/Promise.h
@@ -71,17 +71,17 @@ class Promise : public nsISupports,
   friend class ResolvePromiseCallback;
   friend class ThenableResolverTask;
   friend class WrapperPromiseCallback;
 
 public:
   NS_DECLARE_STATIC_IID_ACCESSOR(NS_PROMISE_IID)
   NS_DECL_CYCLE_COLLECTING_ISUPPORTS
   NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(Promise)
-  MOZ_DECLARE_REFCOUNTED_TYPENAME(Promise)
+  MOZ_DECLARE_WEAKREFERENCE_TYPENAME(Promise)
 
   // Promise creation tries to create a JS reflector for the Promise, so is
   // fallible.  Furthermore, we don't want to do JS-wrapping on a 0-refcount
   // object, so we addref before doing that and return the addrefed pointer
   // here.
   static already_AddRefed<Promise>
   Create(nsIGlobalObject* aGlobal, ErrorResult& aRv);
 
--- a/gfx/gl/SharedSurface.h
+++ b/gfx/gl/SharedSurface.h
@@ -200,17 +200,17 @@ public:
     }
 };
 
 class SurfaceFactory : public SupportsWeakPtr<SurfaceFactory>
 {
 public:
     // Should use the VIRTUAL version, but it's currently incompatible
     // with SupportsWeakPtr. (bug 1049278)
-    MOZ_DECLARE_REFCOUNTED_TYPENAME(SurfaceFactory)
+    MOZ_DECLARE_WEAKREFERENCE_TYPENAME(SurfaceFactory)
 
     GLContext* const mGL;
     const SurfaceCaps mCaps;
     const SharedSurfaceType mType;
     const GLFormats mFormats;
 
 protected:
     SurfaceCaps mDrawCaps;
--- a/gfx/layers/ImageContainer.h
+++ b/gfx/layers/ImageContainer.h
@@ -288,17 +288,17 @@ protected:
  * The ImageContainer uses a shared memory block containing a cross-process mutex
  * to communicate with the compositor thread. SetCurrentImage synchronously
  * updates the shared state to point to the new image and the old image
  * is immediately released (not true in Normal or Asynchronous modes).
  */
 class ImageContainer final : public SupportsWeakPtr<ImageContainer> {
   NS_INLINE_DECL_THREADSAFE_REFCOUNTING(ImageContainer)
 public:
-  MOZ_DECLARE_REFCOUNTED_TYPENAME(ImageContainer)
+  MOZ_DECLARE_WEAKREFERENCE_TYPENAME(ImageContainer)
 
   enum { DISABLE_ASYNC = 0x0, ENABLE_ASYNC = 0x01 };
 
   explicit ImageContainer(int flag = 0);
 
   /**
    * Create an Image in one of the given formats.
    * Picks the "best" format from the list and creates an Image of that
--- a/image/src/IProgressObserver.h
+++ b/image/src/IProgressObserver.h
@@ -23,17 +23,17 @@ namespace image {
  *
  * XXX(seth): It's preferable to avoid adding anything to this interface if
  * possible.  In the long term, it would be ideal to get to a place where we can
  * just use the imgINotificationObserver interface internally as well.
  */
 class IProgressObserver : public SupportsWeakPtr<IProgressObserver>
 {
 public:
-  MOZ_DECLARE_REFCOUNTED_TYPENAME(IProgressObserver)
+  MOZ_DECLARE_WEAKREFERENCE_TYPENAME(IProgressObserver)
 
   // Subclasses may or may not be XPCOM classes, so we just require that they
   // implement AddRef and Release.
   NS_IMETHOD_(MozExternalRefCountType) AddRef(void) = 0;
   NS_IMETHOD_(MozExternalRefCountType) Release(void) = 0;
 
   // imgINotificationObserver methods:
   virtual void Notify(int32_t aType, const nsIntRect* aRect = nullptr) = 0;
--- a/image/src/ProgressTracker.h
+++ b/image/src/ProgressTracker.h
@@ -68,17 +68,17 @@ inline Progress LoadCompleteProgress(boo
  * argument, and the notifications will be replayed to the observer
  * asynchronously.
  */
 class ProgressTracker : public mozilla::SupportsWeakPtr<ProgressTracker>
 {
   virtual ~ProgressTracker() { }
 
 public:
-  MOZ_DECLARE_REFCOUNTED_TYPENAME(ProgressTracker)
+  MOZ_DECLARE_WEAKREFERENCE_TYPENAME(ProgressTracker)
   NS_INLINE_DECL_THREADSAFE_REFCOUNTING(ProgressTracker)
 
   ProgressTracker()
     : mImage(nullptr)
     , mProgress(NoProgress)
   { }
 
   bool HasImage() const { return mImage; }
--- a/image/src/RasterImage.h
+++ b/image/src/RasterImage.h
@@ -150,17 +150,17 @@ class RasterImage final : public ImageRe
 #ifdef DEBUG
                             , public imgIContainerDebug
 #endif
 {
   // (no public constructor - use ImageFactory)
   virtual ~RasterImage();
 
 public:
-  MOZ_DECLARE_REFCOUNTED_TYPENAME(RasterImage)
+  MOZ_DECLARE_WEAKREFERENCE_TYPENAME(RasterImage)
   NS_DECL_THREADSAFE_ISUPPORTS
   NS_DECL_NSIPROPERTIES
   NS_DECL_IMGICONTAINER
 #ifdef DEBUG
   NS_DECL_IMGICONTAINERDEBUG
 #endif
 
   virtual nsresult StartAnimation() override;
--- a/ipc/glue/MessageLink.h
+++ b/ipc/glue/MessageLink.h
@@ -55,17 +55,17 @@ enum RacyInterruptPolicy {
     RIPParentWins
 };
 
 class MessageListener
   : protected HasResultCodes,
     public mozilla::SupportsWeakPtr<MessageListener>
 {
   public:
-    MOZ_DECLARE_REFCOUNTED_TYPENAME(MessageListener)
+    MOZ_DECLARE_WEAKREFERENCE_TYPENAME(MessageListener)
     typedef IPC::Message Message;
 
     virtual ~MessageListener() { }
 
     virtual void OnChannelClose() = 0;
     virtual void OnChannelError() = 0;
     virtual Result OnMessageReceived(const Message& aMessage) = 0;
     virtual Result OnMessageReceived(const Message& aMessage, Message *& aReply) = 0;
--- a/mfbt/WeakPtr.h
+++ b/mfbt/WeakPtr.h
@@ -12,31 +12,31 @@
  * (WeakReference) that each WeakPtr will access 'Foo' through. This lets 'Foo'
  * clear the pointer in the WeakReference without having to know about all of
  * the WeakPtrs to it and allows the WeakReference to live beyond the lifetime
  * of 'Foo'.
  *
  * PLEASE NOTE: This weak pointer implementation is not thread-safe.
  *
  * Note that when deriving from SupportsWeakPtr you should add
- * MOZ_DECLARE_REFCOUNTED_TYPENAME(ClassName) to the public section of your
+ * MOZ_DECLARE_WEAKREFERENCE_TYPENAME(ClassName) to the public section of your
  * class, where ClassName is the name of your class.
  *
  * The overhead of WeakPtr is that accesses to 'Foo' becomes an additional
  * dereference, and an additional heap allocated pointer sized object shared
  * between all of the WeakPtrs.
  *
  * Example of usage:
  *
  *   // To have a class C support weak pointers, inherit from
  *   // SupportsWeakPtr<C>.
  *   class C : public SupportsWeakPtr<C>
  *   {
  *   public:
- *     MOZ_DECLARE_REFCOUNTED_TYPENAME(C)
+ *     MOZ_DECLARE_WEAKREFERENCE_TYPENAME(C)
  *     int mNum;
  *     void act();
  *   };
  *
  *   C* ptr = new C();
  *
  *   // Get weak pointers to ptr. The first time a weak pointer
  *   // is obtained, a reference counted WeakReference object is created that
@@ -75,48 +75,41 @@
 
 #include <string.h>
 
 namespace mozilla {
 
 template <typename T> class WeakPtr;
 template <typename T> class SupportsWeakPtr;
 
+#ifdef MOZ_REFCOUNTED_LEAK_CHECKING
+#define MOZ_DECLARE_WEAKREFERENCE_TYPENAME(T) \
+  static const char* weakReferenceTypeName() { return "WeakReference<" #T ">"; }
+#else
+#define MOZ_DECLARE_WEAKREFERENCE_TYPENAME(T)
+#endif
+
 namespace detail {
 
 // This can live beyond the lifetime of the class derived from
 // SupportsWeakPtr.
 template<class T>
 class WeakReference : public ::mozilla::RefCounted<WeakReference<T> >
 {
 public:
   explicit WeakReference(T* p) : mPtr(p) {}
 
   T* get() const { return mPtr; }
 
 #ifdef MOZ_REFCOUNTED_LEAK_CHECKING
   const char* typeName() const
   {
-    static char nameBuffer[1024];
-    const char* innerType = mPtr->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");
-#if defined(_MSC_VER) && _MSC_VER < 1900
-    _snprintf
-#else
-    ::snprintf
-#endif
-         (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;
+    // The first time this is called mPtr is null, so don't
+    // invoke any methods on mPtr.
+    return T::weakReferenceTypeName();
   }
   size_t typeSize() const { return sizeof(*this); }
 #endif
 
 private:
   friend class mozilla::SupportsWeakPtr<T>;
 
   void detach() { mPtr = nullptr; }
--- a/mfbt/tests/TestWeakPtr.cpp
+++ b/mfbt/tests/TestWeakPtr.cpp
@@ -8,17 +8,17 @@
 
 using mozilla::SupportsWeakPtr;
 using mozilla::WeakPtr;
 
 // To have a class C support weak pointers, inherit from SupportsWeakPtr<C>.
 class C : public SupportsWeakPtr<C>
 {
 public:
-  MOZ_DECLARE_REFCOUNTED_TYPENAME(C)
+  MOZ_DECLARE_WEAKREFERENCE_TYPENAME(C)
 
   int mNum;
 
   C()
     : mNum(0)
   {}
 
   ~C()
--- a/netwerk/sctp/datachannel/DataChannel.h
+++ b/netwerk/sctp/datachannel/DataChannel.h
@@ -102,17 +102,17 @@ class DataChannelConnection: public nsIT
 
 public:
   NS_DECL_THREADSAFE_ISUPPORTS
   NS_DECL_NSITIMERCALLBACK
 
   class DataConnectionListener : public SupportsWeakPtr<DataConnectionListener>
   {
   public:
-    MOZ_DECLARE_REFCOUNTED_TYPENAME(DataChannelConnection::DataConnectionListener)
+    MOZ_DECLARE_WEAKREFERENCE_TYPENAME(DataChannelConnection::DataConnectionListener)
     virtual ~DataConnectionListener() {}
 
     // Called when a new DataChannel has been opened by the other side.
     virtual void NotifyDataChannel(already_AddRefed<DataChannel> channel) = 0;
   };
 
   explicit DataChannelConnection(DataConnectionListener *listener);
 
--- a/uriloader/prefetch/nsOfflineCacheUpdate.h
+++ b/uriloader/prefetch/nsOfflineCacheUpdate.h
@@ -183,28 +183,27 @@ private:
     nsCString mManifestHashValue;
     nsCString mOldManifestHashValue;
 };
 
 class nsOfflineCacheUpdateOwner
   : public mozilla::SupportsWeakPtr<nsOfflineCacheUpdateOwner>
 {
 public:
-    MOZ_DECLARE_REFCOUNTED_TYPENAME(nsOfflineCacheUpdateOwner)
+    MOZ_DECLARE_WEAKREFERENCE_TYPENAME(nsOfflineCacheUpdateOwner)
     virtual ~nsOfflineCacheUpdateOwner() {}
     virtual nsresult UpdateFinished(nsOfflineCacheUpdate *aUpdate) = 0;
 };
 
 class nsOfflineCacheUpdate final : public nsIOfflineCacheUpdate
                                      , public nsIOfflineCacheUpdateObserver
                                      , public nsIRunnable
                                      , public nsOfflineCacheUpdateOwner
 {
 public:
-    MOZ_DECLARE_REFCOUNTED_TYPENAME(nsOfflineCacheUpdate)
     NS_DECL_ISUPPORTS
     NS_DECL_NSIOFFLINECACHEUPDATE
     NS_DECL_NSIOFFLINECACHEUPDATEOBSERVER
     NS_DECL_NSIRUNNABLE
 
     nsOfflineCacheUpdate();
 
     static nsresult GetCacheKey(nsIURI *aURI, nsACString &aKey);
--- a/xpcom/base/nsMessageLoop.cpp
+++ b/xpcom/base/nsMessageLoop.cpp
@@ -27,17 +27,17 @@ namespace {
  * Note that the MessageLoop owns this object and will delete it after it calls
  * Run().  Tread lightly.
  */
 class MessageLoopIdleTask
   : public Task
   , public SupportsWeakPtr<MessageLoopIdleTask>
 {
 public:
-  MOZ_DECLARE_REFCOUNTED_TYPENAME(MessageLoopIdleTask)
+  MOZ_DECLARE_WEAKREFERENCE_TYPENAME(MessageLoopIdleTask)
   MessageLoopIdleTask(nsIRunnable* aTask, uint32_t aEnsureRunsAfterMS);
   virtual void Run();
 
 private:
   nsresult Init(uint32_t aEnsureRunsAfterMS);
 
   nsCOMPtr<nsIRunnable> mTask;
   nsCOMPtr<nsITimer> mTimer;