Bug 1192230 - clean up reference-counting in xpcom/; r=erahm
authorNathan Froyd <froydnj@mozilla.com>
Wed, 01 Jul 2015 12:27:43 -0400
changeset 257308 69980d43dea7
parent 257307 4c3c5f9bce35
child 257309 a369646b9ca5
push id63590
push usernfroyd@mozilla.com
push date2015-08-11 20:56 +0000
treeherdermozilla-inbound@a369646b9ca5 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerserahm
bugs1192230
milestone43.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1192230 - clean up reference-counting in xpcom/; r=erahm
xpcom/glue/nsArrayEnumerator.cpp
xpcom/glue/nsEnumeratorUtils.cpp
xpcom/io/nsAppFileLocationProvider.cpp
xpcom/io/nsDirectoryService.cpp
xpcom/io/nsInputStreamTee.cpp
xpcom/io/nsLocalFileWin.cpp
xpcom/io/nsMultiplexInputStream.cpp
xpcom/io/nsScriptableInputStream.cpp
xpcom/io/nsStorageStream.cpp
xpcom/io/nsStringStream.cpp
xpcom/io/nsUnicharInputStream.cpp
--- a/xpcom/glue/nsArrayEnumerator.cpp
+++ b/xpcom/glue/nsArrayEnumerator.cpp
@@ -8,16 +8,17 @@
 
 #include "nsArrayEnumerator.h"
 
 #include "nsIArray.h"
 #include "nsISimpleEnumerator.h"
 
 #include "nsCOMArray.h"
 #include "nsCOMPtr.h"
+#include "mozilla/nsRefPtr.h"
 
 class nsSimpleArrayEnumerator final : public nsISimpleEnumerator
 {
 public:
   // nsISupports interface
   NS_DECL_ISUPPORTS
 
   // nsISimpleEnumerator interface
@@ -86,22 +87,18 @@ nsSimpleArrayEnumerator::GetNext(nsISupp
 
   return mValueArray->QueryElementAt(mIndex++, NS_GET_IID(nsISupports),
                                      (void**)aResult);
 }
 
 nsresult
 NS_NewArrayEnumerator(nsISimpleEnumerator** aResult, nsIArray* aArray)
 {
-  nsSimpleArrayEnumerator* enumer = new nsSimpleArrayEnumerator(aArray);
-  if (!enumer) {
-    return NS_ERROR_OUT_OF_MEMORY;
-  }
-
-  NS_ADDREF(*aResult = enumer);
+  nsRefPtr<nsSimpleArrayEnumerator> enumer = new nsSimpleArrayEnumerator(aArray);
+  enumer.forget(aResult);
   return NS_OK;
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 
 // enumerator implementation for nsCOMArray
 // creates a snapshot of the array in question
 // you MUST use NS_NewArrayEnumerator to create this, so that
@@ -205,16 +202,12 @@ nsCOMArrayEnumerator::operator new(size_
 
   return result;
 }
 
 nsresult
 NS_NewArrayEnumerator(nsISimpleEnumerator** aResult,
                       const nsCOMArray_base& aArray)
 {
-  nsCOMArrayEnumerator* enumerator = new (aArray) nsCOMArrayEnumerator();
-  if (!enumerator) {
-    return NS_ERROR_OUT_OF_MEMORY;
-  }
-
-  NS_ADDREF(*aResult = enumerator);
+  nsRefPtr<nsCOMArrayEnumerator> enumerator = new (aArray) nsCOMArrayEnumerator();
+  enumerator.forget(aResult);
   return NS_OK;
 }
--- a/xpcom/glue/nsEnumeratorUtils.cpp
+++ b/xpcom/glue/nsEnumeratorUtils.cpp
@@ -7,16 +7,17 @@
 #include "mozilla/Attributes.h"
 
 #include "nsEnumeratorUtils.h"
 
 #include "nsISimpleEnumerator.h"
 #include "nsIStringEnumerator.h"
 
 #include "nsCOMPtr.h"
+#include "mozilla/nsRefPtr.h"
 
 class EmptyEnumeratorImpl
   : public nsISimpleEnumerator
   , public nsIUTF8StringEnumerator
   , public nsIStringEnumerator
 {
 public:
   EmptyEnumeratorImpl() {}
@@ -158,22 +159,18 @@ nsSingletonEnumerator::GetNext(nsISuppor
   NS_ADDREF(*aResult);
   return NS_OK;
 }
 
 nsresult
 NS_NewSingletonEnumerator(nsISimpleEnumerator** aResult,
                           nsISupports* aSingleton)
 {
-  nsSingletonEnumerator* enumer = new nsSingletonEnumerator(aSingleton);
-  if (!enumer) {
-    return NS_ERROR_OUT_OF_MEMORY;
-  }
-  *aResult = enumer;
-  NS_ADDREF(*aResult);
+  nsRefPtr<nsSingletonEnumerator> enumer = new nsSingletonEnumerator(aSingleton);
+  enumer.forget(aResult);
   return NS_OK;
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 
 class nsUnionEnumerator final : public nsISimpleEnumerator
 {
 public:
--- a/xpcom/io/nsAppFileLocationProvider.cpp
+++ b/xpcom/io/nsAppFileLocationProvider.cpp
@@ -343,18 +343,17 @@ nsAppFileLocationProvider::GetProductDir
   if (NS_SUCCEEDED(rv) && !exists) {
     rv = localDir->Create(nsIFile::DIRECTORY_TYPE, 0700);
   }
 
   if (NS_FAILED(rv)) {
     return rv;
   }
 
-  *aLocalFile = localDir;
-  NS_ADDREF(*aLocalFile);
+  localDir.forget(aLocalFile);
 
   return rv;
 }
 
 
 //----------------------------------------------------------------------------------------
 // GetDefaultUserProfileRoot - Gets the directory which contains each user profile dir
 //
@@ -390,18 +389,17 @@ nsAppFileLocationProvider::GetDefaultUse
   if (NS_SUCCEEDED(rv) && !exists) {
     rv = localDir->Create(nsIFile::DIRECTORY_TYPE, 0775);
   }
   if (NS_FAILED(rv)) {
     return rv;
   }
 #endif
 
-  *aLocalFile = localDir;
-  NS_ADDREF(*aLocalFile);
+  localDir.forget(aLocalFile);
 
   return rv;
 }
 
 //*****************************************************************************
 // nsAppFileLocationProvider::nsIDirectoryServiceProvider2
 //*****************************************************************************
 
--- a/xpcom/io/nsDirectoryService.cpp
+++ b/xpcom/io/nsDirectoryService.cpp
@@ -71,42 +71,40 @@ nsDirectoryService::GetCurrentProcessDir
   rv = nsDirectoryService::Create(nullptr,
                                   NS_GET_IID(nsIProperties),
                                   getter_AddRefs(dirService));  // needs to be around for life of product
   if (NS_FAILED(rv)) {
     return rv;
   }
 
   if (dirService) {
-    nsCOMPtr <nsIFile> aLocalFile;
+    nsCOMPtr<nsIFile> localFile;
     dirService->Get(NS_XPCOM_INIT_CURRENT_PROCESS_DIR, NS_GET_IID(nsIFile),
-                    getter_AddRefs(aLocalFile));
-    if (aLocalFile) {
-      *aFile = aLocalFile;
-      NS_ADDREF(*aFile);
+                    getter_AddRefs(localFile));
+    if (localFile) {
+      localFile.forget(aFile);
       return NS_OK;
     }
   }
 
-  nsLocalFile* localFile = new nsLocalFile;
-  NS_ADDREF(localFile);
+  nsRefPtr<nsLocalFile> localFile = new nsLocalFile;
 
 #ifdef XP_WIN
   wchar_t buf[MAX_PATH + 1];
   SetLastError(ERROR_SUCCESS);
   if (GetModuleFileNameW(0, buf, mozilla::ArrayLength(buf)) &&
       GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
     // chop off the executable name by finding the rightmost backslash
     wchar_t* lastSlash = wcsrchr(buf, L'\\');
     if (lastSlash) {
       *(lastSlash + 1) = L'\0';
     }
 
     localFile->InitWithPath(nsDependentString(buf));
-    *aFile = localFile;
+    localFile.forget(aFile);
     return NS_OK;
   }
 
 #elif defined(MOZ_WIDGET_COCOA)
   // Works even if we're not bundled.
   CFBundleRef appBundle = CFBundleGetMainBundle();
   if (appBundle) {
     CFURLRef bundleURL = CFBundleCopyExecutableURL(appBundle);
@@ -120,17 +118,17 @@ nsDirectoryService::GetCurrentProcessDir
         char buffer[PATH_MAX];
         if (CFURLGetFileSystemRepresentation(parentURL, true,
                                              (UInt8*)buffer, sizeof(buffer))) {
 #ifdef DEBUG_conrad
           printf("nsDirectoryService - CurrentProcessDir is: %s\n", buffer);
 #endif
           rv = localFile->InitWithNativePath(nsDependentCString(buffer));
           if (NS_SUCCEEDED(rv)) {
-            *aFile = localFile;
+            localFile.forget(aFile);
           }
         }
         CFRelease(parentURL);
       }
       CFRelease(bundleURL);
     }
   }
 
@@ -162,41 +160,39 @@ nsDirectoryService::GetCurrentProcessDir
     putenv("MOZILLA_FIVE_HOME=" MOZ_DEFAULT_MOZILLA_FIVE_HOME);
   }
 #endif
 
   char* moz5 = PR_GetEnv("MOZILLA_FIVE_HOME");
   if (moz5 && *moz5) {
     if (realpath(moz5, buf)) {
       localFile->InitWithNativePath(nsDependentCString(buf));
-      *aFile = localFile;
+      localFile.forget(aFile);
       return NS_OK;
     }
   }
 #if defined(DEBUG)
   static bool firstWarning = true;
 
   if ((!moz5 || !*moz5) && firstWarning) {
     // Warn that MOZILLA_FIVE_HOME not set, once.
     printf("Warning: MOZILLA_FIVE_HOME not set.\n");
     firstWarning = false;
   }
 #endif /* DEBUG */
 
   // Fall back to current directory.
   if (getcwd(buf, sizeof(buf))) {
     localFile->InitWithNativePath(nsDependentCString(buf));
-    *aFile = localFile;
+    localFile.forget(aFile);
     return NS_OK;
   }
 
 #endif
 
-  NS_RELEASE(localFile);
-
   NS_ERROR("unable to get current process directory");
   return NS_ERROR_FAILURE;
 } // GetCurrentProcessDirectory()
 
 nsDirectoryService* nsDirectoryService::gService = nullptr;
 
 nsDirectoryService::nsDirectoryService()
   : mHashtable(128)
--- a/xpcom/io/nsInputStreamTee.cpp
+++ b/xpcom/io/nsInputStreamTee.cpp
@@ -353,17 +353,17 @@ NS_NewInputStreamTeeAsync(nsIInputStream
     return rv;
   }
 
   rv = tee->SetEventTarget(aEventTarget);
   if (NS_FAILED(rv)) {
     return rv;
   }
 
-  NS_ADDREF(*aResult = tee);
+  tee.forget(aResult);
   return rv;
 }
 
 nsresult
 NS_NewInputStreamTee(nsIInputStream** aResult,
                      nsIInputStream* aSource,
                      nsIOutputStream* aSink)
 {
--- a/xpcom/io/nsLocalFileWin.cpp
+++ b/xpcom/io/nsLocalFileWin.cpp
@@ -1174,18 +1174,18 @@ nsLocalFile::Resolve()
 //-----------------------------------------------------------------------------
 // nsLocalFile::nsIFile,nsILocalFile
 //-----------------------------------------------------------------------------
 
 NS_IMETHODIMP
 nsLocalFile::Clone(nsIFile** aFile)
 {
   // Just copy-construct ourselves
-  *aFile = new nsLocalFile(*this);
-  NS_ADDREF(*aFile);
+  nsRefPtr<nsLocalFile> file = new nsLocalFile(*this);
+  file.forget(aFile);
 
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsLocalFile::InitWithFile(nsIFile* aFile)
 {
   if (NS_WARN_IF(!aFile)) {
@@ -3209,24 +3209,22 @@ nsLocalFile::SetFollowLinks(bool aFollow
 
 NS_IMETHODIMP
 nsLocalFile::GetDirectoryEntries(nsISimpleEnumerator** aEntries)
 {
   nsresult rv;
 
   *aEntries = nullptr;
   if (mWorkingPath.EqualsLiteral("\\\\.")) {
-    nsDriveEnumerator* drives = new nsDriveEnumerator;
-    NS_ADDREF(drives);
+    nsRefPtr<nsDriveEnumerator> drives = new nsDriveEnumerator;
     rv = drives->Init();
     if (NS_FAILED(rv)) {
-      NS_RELEASE(drives);
       return rv;
     }
-    *aEntries = drives;
+    drives.forget(aEntries);
     return NS_OK;
   }
 
   nsRefPtr<nsDirEnumerator> dirEnum = new nsDirEnumerator();
   rv = dirEnum->Init(this);
   if (NS_FAILED(rv)) {
     return rv;
   }
@@ -3355,30 +3353,28 @@ nsLocalFile::Launch()
   // thread, so we can let it go.
   mythread->Dispatch(runnable, NS_DISPATCH_NORMAL);
   return NS_OK;
 }
 
 nsresult
 NS_NewLocalFile(const nsAString& aPath, bool aFollowLinks, nsIFile** aResult)
 {
-  nsLocalFile* file = new nsLocalFile();
-  NS_ADDREF(file);
+  nsRefPtr<nsLocalFile> file = new nsLocalFile();
 
   file->SetFollowLinks(aFollowLinks);
 
   if (!aPath.IsEmpty()) {
     nsresult rv = file->InitWithPath(aPath);
     if (NS_FAILED(rv)) {
-      NS_RELEASE(file);
       return rv;
     }
   }
 
-  *aResult = file;
+  file.forget(aResult);
   return NS_OK;
 }
 
 //-----------------------------------------------------------------------------
 // Native (lossy) interface
 //-----------------------------------------------------------------------------
 
 NS_IMETHODIMP
--- a/xpcom/io/nsMultiplexInputStream.cpp
+++ b/xpcom/io/nsMultiplexInputStream.cpp
@@ -691,23 +691,19 @@ nsMultiplexInputStreamConstructor(nsISup
                                   void** aResult)
 {
   *aResult = nullptr;
 
   if (aOuter) {
     return NS_ERROR_NO_AGGREGATION;
   }
 
-  nsMultiplexInputStream* inst = new nsMultiplexInputStream();
+  nsRefPtr<nsMultiplexInputStream> inst = new nsMultiplexInputStream();
 
-  NS_ADDREF(inst);
-  nsresult rv = inst->QueryInterface(aIID, aResult);
-  NS_RELEASE(inst);
-
-  return rv;
+  return inst->QueryInterface(aIID, aResult);
 }
 
 void
 nsMultiplexInputStream::Serialize(InputStreamParams& aParams,
                                   FileDescriptorArray& aFileDescriptors)
 {
   MutexAutoLock lock(mLock);
 
--- a/xpcom/io/nsScriptableInputStream.cpp
+++ b/xpcom/io/nsScriptableInputStream.cpp
@@ -124,14 +124,11 @@ nsScriptableInputStream::ReadHelper(char
 nsresult
 nsScriptableInputStream::Create(nsISupports* aOuter, REFNSIID aIID,
                                 void** aResult)
 {
   if (aOuter) {
     return NS_ERROR_NO_AGGREGATION;
   }
 
-  nsScriptableInputStream* sis = new nsScriptableInputStream();
-  NS_ADDREF(sis);
-  nsresult rv = sis->QueryInterface(aIID, aResult);
-  NS_RELEASE(sis);
-  return rv;
+  nsRefPtr<nsScriptableInputStream> sis = new nsScriptableInputStream();
+  return sis->QueryInterface(aIID, aResult);
 }
--- a/xpcom/io/nsStorageStream.cpp
+++ b/xpcom/io/nsStorageStream.cpp
@@ -395,27 +395,25 @@ NS_IMPL_ISUPPORTS(nsStorageInputStream,
 NS_IMETHODIMP
 nsStorageStream::NewInputStream(int32_t aStartingOffset,
                                 nsIInputStream** aInputStream)
 {
   if (NS_WARN_IF(!mSegmentedBuffer)) {
     return NS_ERROR_NOT_INITIALIZED;
   }
 
-  nsStorageInputStream* inputStream =
+  nsRefPtr<nsStorageInputStream> inputStream =
     new nsStorageInputStream(this, mSegmentSize);
-  NS_ADDREF(inputStream);
 
   nsresult rv = inputStream->Seek(aStartingOffset);
   if (NS_FAILED(rv)) {
-    NS_RELEASE(inputStream);
     return rv;
   }
 
-  *aInputStream = inputStream;
+  inputStream.forget(aInputStream);
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsStorageInputStream::Close()
 {
   mStatus = NS_BASE_STREAM_CLOSED;
   return NS_OK;
@@ -620,22 +618,20 @@ nsStorageInputStream::Clone(nsIInputStre
 {
   return mStorageStream->NewInputStream(mLogicalCursor, aCloneOut);
 }
 
 nsresult
 NS_NewStorageStream(uint32_t aSegmentSize, uint32_t aMaxSize,
                     nsIStorageStream** aResult)
 {
-  nsStorageStream* storageStream = new nsStorageStream();
-  NS_ADDREF(storageStream);
+  nsRefPtr<nsStorageStream> storageStream = new nsStorageStream();
   nsresult rv = storageStream->Init(aSegmentSize, aMaxSize);
   if (NS_FAILED(rv)) {
-    NS_RELEASE(storageStream);
     return rv;
   }
-  *aResult = storageStream;
+  storageStream.forget(aResult);
   return NS_OK;
 }
 
 // Undefine LOG, so that other .cpp files (or their includes) won't complain
 // about it already being defined, when we build in unified mode.
 #undef LOG
--- a/xpcom/io/nsStringStream.cpp
+++ b/xpcom/io/nsStringStream.cpp
@@ -375,18 +375,17 @@ nsStringInputStream::Clone(nsIInputStrea
 
 nsresult
 NS_NewByteInputStream(nsIInputStream** aStreamResult,
                       const char* aStringToRead, int32_t aLength,
                       nsAssignmentType aAssignment)
 {
   NS_PRECONDITION(aStreamResult, "null out ptr");
 
-  nsStringInputStream* stream = new nsStringInputStream();
-  NS_ADDREF(stream);
+  nsRefPtr<nsStringInputStream> stream = new nsStringInputStream();
 
   nsresult rv;
   switch (aAssignment) {
     case NS_ASSIGNMENT_COPY:
       rv = stream->SetData(aStringToRead, aLength);
       break;
     case NS_ASSIGNMENT_DEPEND:
       rv = stream->ShareData(aStringToRead, aLength);
@@ -395,21 +394,20 @@ NS_NewByteInputStream(nsIInputStream** a
       rv = stream->AdoptData(const_cast<char*>(aStringToRead), aLength);
       break;
     default:
       NS_ERROR("invalid assignment type");
       rv = NS_ERROR_INVALID_ARG;
   }
 
   if (NS_FAILED(rv)) {
-    NS_RELEASE(stream);
     return rv;
   }
 
-  *aStreamResult = stream;
+  stream.forget(aStreamResult);
   return NS_OK;
 }
 
 nsresult
 NS_NewStringInputStream(nsIInputStream** aStreamResult,
                         const nsAString& aStringToRead)
 {
   NS_LossyConvertUTF16toASCII data(aStringToRead); // truncates high-order bytes
@@ -417,35 +415,30 @@ NS_NewStringInputStream(nsIInputStream**
 }
 
 nsresult
 NS_NewCStringInputStream(nsIInputStream** aStreamResult,
                          const nsACString& aStringToRead)
 {
   NS_PRECONDITION(aStreamResult, "null out ptr");
 
-  nsStringInputStream* stream = new nsStringInputStream();
-  NS_ADDREF(stream);
+  nsRefPtr<nsStringInputStream> stream = new nsStringInputStream();
 
   stream->SetData(aStringToRead);
 
-  *aStreamResult = stream;
+  stream.forget(aStreamResult);
   return NS_OK;
 }
 
 // factory method for constructing a nsStringInputStream object
 nsresult
 nsStringInputStreamConstructor(nsISupports* aOuter, REFNSIID aIID,
                                void** aResult)
 {
   *aResult = nullptr;
 
   if (NS_WARN_IF(aOuter)) {
     return NS_ERROR_NO_AGGREGATION;
   }
 
-  nsStringInputStream* inst = new nsStringInputStream();
-  NS_ADDREF(inst);
-  nsresult rv = inst->QueryInterface(aIID, aResult);
-  NS_RELEASE(inst);
-
-  return rv;
+  nsRefPtr<nsStringInputStream> inst = new nsStringInputStream();
+  return inst->QueryInterface(aIID, aResult);
 }
--- a/xpcom/io/nsUnicharInputStream.cpp
+++ b/xpcom/io/nsUnicharInputStream.cpp
@@ -407,19 +407,19 @@ nsSimpleUnicharStreamFactory::LockFactor
 {
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsSimpleUnicharStreamFactory::CreateInstanceFromString(const nsAString& aString,
                                                        nsIUnicharInputStream** aResult)
 {
-  StringUnicharInputStream* it = new StringUnicharInputStream(aString);
+  nsRefPtr<StringUnicharInputStream> it = new StringUnicharInputStream(aString);
 
-  NS_ADDREF(*aResult = it);
+  it.forget(aResult);
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsSimpleUnicharStreamFactory::CreateInstanceFromUTF8Stream(
     nsIInputStream* aStreamToWrap,
     nsIUnicharInputStream** aResult)
 {
@@ -427,17 +427,17 @@ nsSimpleUnicharStreamFactory::CreateInst
 
   // Create converter input stream
   nsRefPtr<UTF8InputStream> it = new UTF8InputStream();
   nsresult rv = it->Init(aStreamToWrap);
   if (NS_FAILED(rv)) {
     return rv;
   }
 
-  NS_ADDREF(*aResult = it);
+  it.forget(aResult);
   return NS_OK;
 }
 
 nsSimpleUnicharStreamFactory*
 nsSimpleUnicharStreamFactory::GetInstance()
 {
   static const nsSimpleUnicharStreamFactory kInstance;
   return const_cast<nsSimpleUnicharStreamFactory*>(&kInstance);