Bug 614171 part 1. Switch away from Init() to using a constructor for nodeinfo. r=sicking
authorBoris Zbarsky <bzbarsky@mit.edu>
Thu, 05 May 2011 12:26:32 -0400
changeset 69001 3d82b6d2ec58deacb29a3e16ca6c5b855a2f67e1
parent 69000 e5668cb2a02fb6b87f94739f721e813e462330d6
child 69002 0226c56cdd58d624925b1cf2277d71b83dcbe555
push idunknown
push userunknown
push dateunknown
reviewerssicking
bugs614171
milestone6.0a1
Bug 614171 part 1. Switch away from Init() to using a constructor for nodeinfo. r=sicking
content/base/src/nsNodeInfo.cpp
content/base/src/nsNodeInfo.h
content/base/src/nsNodeInfoManager.cpp
--- a/content/base/src/nsNodeInfo.cpp
+++ b/content/base/src/nsNodeInfo.cpp
@@ -63,17 +63,18 @@ static const size_t kNodeInfoPoolSizes[]
 static const PRInt32 kNodeInfoPoolInitialSize = 
   (NS_SIZE_IN_HEAP(sizeof(nsNodeInfo))) * 64;
 
 // static
 nsFixedSizeAllocator* nsNodeInfo::sNodeInfoPool = nsnull;
 
 // static
 nsNodeInfo*
-nsNodeInfo::Create()
+nsNodeInfo::Create(nsIAtom *aName, nsIAtom *aPrefix, PRInt32 aNamespaceID,
+                   nsNodeInfoManager *aOwnerManager)
 {
   if (!sNodeInfoPool) {
     sNodeInfoPool = new nsFixedSizeAllocator();
     if (!sNodeInfoPool)
       return nsnull;
 
     nsresult rv = sNodeInfoPool->Init("NodeInfo Pool", kNodeInfoPoolSizes,
                                       1, kNodeInfoPoolInitialSize);
@@ -81,56 +82,47 @@ nsNodeInfo::Create()
       delete sNodeInfoPool;
       sNodeInfoPool = nsnull;
       return nsnull;
     }
   }
 
   // Create a new one
   void* place = sNodeInfoPool->Alloc(sizeof(nsNodeInfo));
-  return place ? new (place) nsNodeInfo() : nsnull;
-}
-
-nsNodeInfo::nsNodeInfo()
-{
+  return place ?
+    new (place) nsNodeInfo(aName, aPrefix, aNamespaceID, aOwnerManager) :
+    nsnull;
 }
 
 nsNodeInfo::~nsNodeInfo()
 {
-  if (mOwnerManager) {
-    mOwnerManager->RemoveNodeInfo(this);
-    NS_RELEASE(mOwnerManager);
-  }
+  mOwnerManager->RemoveNodeInfo(this);
+  NS_RELEASE(mOwnerManager);
 
-  NS_IF_RELEASE(mInner.mName);
+  NS_RELEASE(mInner.mName);
   NS_IF_RELEASE(mInner.mPrefix);
 }
 
 
-nsresult
-nsNodeInfo::Init(nsIAtom *aName, nsIAtom *aPrefix, PRInt32 aNamespaceID,
-                 nsNodeInfoManager *aOwnerManager)
+nsNodeInfo::nsNodeInfo(nsIAtom *aName, nsIAtom *aPrefix, PRInt32 aNamespaceID,
+                       nsNodeInfoManager *aOwnerManager)
 {
-  NS_ENSURE_TRUE(!mInner.mName && !mInner.mPrefix && !mOwnerManager,
-                 NS_ERROR_ALREADY_INITIALIZED);
-  NS_ENSURE_ARG_POINTER(aName);
-  NS_ENSURE_ARG_POINTER(aOwnerManager);
+  NS_ABORT_IF_FALSE(aName, "Must have a name");
+  NS_ABORT_IF_FALSE(aOwnerManager, "Must have an owner manager");
 
   mInner.mName = aName;
   NS_ADDREF(mInner.mName);
 
   mInner.mPrefix = aPrefix;
   NS_IF_ADDREF(mInner.mPrefix);
 
   mInner.mNamespaceID = aNamespaceID;
 
   mOwnerManager = aOwnerManager;
   NS_ADDREF(mOwnerManager);
-
-  return NS_OK;
 }
 
 
 // nsISupports
 
 NS_IMPL_CYCLE_COLLECTION_CLASS(nsNodeInfo)
 NS_IMPL_CYCLE_COLLECTION_UNLINK_0(nsNodeInfo)
 
--- a/content/base/src/nsNodeInfo.h
+++ b/content/base/src/nsNodeInfo.h
@@ -70,33 +70,31 @@ public:
                         PRInt32 aNamespaceID) const;
   virtual PRBool NamespaceEquals(const nsAString& aNamespaceURI) const;
   virtual PRBool
     QualifiedNameEqualsInternal(const nsAString& aQualifiedName) const;
 
   // nsNodeInfo
   // Create objects with Create
 public:
-  static nsNodeInfo *Create();
+  /*
+   * aName and aOwnerManager may not be null.
+   */
+  static nsNodeInfo *Create(nsIAtom *aName, nsIAtom *aPrefix,
+                            PRInt32 aNamespaceID,
+                            nsNodeInfoManager *aOwnerManager);
 private:
-  nsNodeInfo();
+  nsNodeInfo(); // Unimplemented
+  nsNodeInfo(const nsNodeInfo& aOther); // Unimplemented
+  nsNodeInfo(nsIAtom *aName, nsIAtom *aPrefix, PRInt32 aNamespaceID,
+             nsNodeInfoManager *aOwnerManager);
 protected:
   virtual ~nsNodeInfo();
 
 public:
-  /*
-   * Note! Init() must be called exactly once on every nsNodeInfo before
-   * the object is used, if Init() returns an error code the nsNodeInfo
-   * should not be used.
-   *
-   * aName and aOwnerManager may not be null.
-   */
-  nsresult Init(nsIAtom *aName, nsIAtom *aPrefix, PRInt32 aNamespaceID,
-                nsNodeInfoManager *aOwnerManager);
-
   /**
    * Call before shutdown to clear the cache and free memory for this class.
    */
   static void ClearCache();
 
 private:
   static nsFixedSizeAllocator* sNodeInfoPool;
 
--- a/content/base/src/nsNodeInfoManager.cpp
+++ b/content/base/src/nsNodeInfoManager.cpp
@@ -218,26 +218,26 @@ nsNodeInfoManager::GetNodeInfo(nsIAtom *
   if (node) {
     nsINodeInfo* nodeInfo = static_cast<nsINodeInfo *>(node);
 
     NS_ADDREF(nodeInfo);
 
     return nodeInfo;
   }
 
-  nsRefPtr<nsNodeInfo> newNodeInfo = nsNodeInfo::Create();
+  nsRefPtr<nsNodeInfo> newNodeInfo =
+    nsNodeInfo::Create(aName, aPrefix, aNamespaceID, this);
   NS_ENSURE_TRUE(newNodeInfo, nsnull);
   
-  nsresult rv = newNodeInfo->Init(aName, aPrefix, aNamespaceID, this);
-  NS_ENSURE_SUCCESS(rv, nsnull);
-
   PLHashEntry *he;
   he = PL_HashTableAdd(mNodeInfoHash, &newNodeInfo->mInner, newNodeInfo);
   NS_ENSURE_TRUE(he, nsnull);
 
+  // Have to do the swap thing, because already_AddRefed<nsNodeInfo>
+  // doesn't cast to already_AddRefed<nsINodeInfo>
   nsNodeInfo *nodeInfo = nsnull;
   newNodeInfo.swap(nodeInfo);
 
   return nodeInfo;
 }
 
 
 nsresult
@@ -254,22 +254,23 @@ nsNodeInfoManager::GetNodeInfo(const nsA
   if (node) {
     nsINodeInfo* nodeInfo = static_cast<nsINodeInfo *>(node);
 
     NS_ADDREF(*aNodeInfo = nodeInfo);
 
     return NS_OK;
   }
 
-  nsRefPtr<nsNodeInfo> newNodeInfo = nsNodeInfo::Create();
-  NS_ENSURE_TRUE(newNodeInfo, nsnull);
   
   nsCOMPtr<nsIAtom> nameAtom = do_GetAtom(aName);
-  nsresult rv = newNodeInfo->Init(nameAtom, aPrefix, aNamespaceID, this);
-  NS_ENSURE_SUCCESS(rv, rv);
+  NS_ENSURE_TRUE(nameAtom, NS_ERROR_OUT_OF_MEMORY);
+
+  nsRefPtr<nsNodeInfo> newNodeInfo =
+      nsNodeInfo::Create(nameAtom, aPrefix, aNamespaceID, this);
+  NS_ENSURE_TRUE(newNodeInfo, NS_ERROR_OUT_OF_MEMORY);
 
   PLHashEntry *he;
   he = PL_HashTableAdd(mNodeInfoHash, &newNodeInfo->mInner, newNodeInfo);
   NS_ENSURE_TRUE(he, NS_ERROR_FAILURE);
 
   newNodeInfo.forget(aNodeInfo);
 
   return NS_OK;