Bug 1249389 - part 2 - change NewBufferFromStorageStream's outparam into a UniquePtr; r=erahm
authorNathan Froyd <froydnj@mozilla.com>
Thu, 18 Feb 2016 12:04:40 -0500
changeset 321784 14e29cda9552844c1bf593df50ab85c555d345b3
parent 321783 b688e63090589bcee8766ea557f1a74682c020d5
child 321785 1f79d41d9098733aefe7da16c690ec4624ce4e7b
push id5913
push userjlund@mozilla.com
push dateMon, 25 Apr 2016 16:57:49 +0000
treeherdermozilla-beta@dcaf0a6fa115 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerserahm
bugs1249389
milestone47.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 1249389 - part 2 - change NewBufferFromStorageStream's outparam into a UniquePtr; r=erahm Similar to the previous change to NewObjectInputStreamFromBuffer, we want to make the ownership transfer out of NewBufferFromStorageStream more obvious. Doing this also lets us get rid of some uses of nsAutoArrayPtr, which is less idiomatic than UniquePtr.
dom/xbl/nsXBLDocumentInfo.cpp
dom/xul/nsXULPrototypeCache.cpp
startupcache/StartupCacheUtils.cpp
startupcache/StartupCacheUtils.h
startupcache/test/TestStartupCache.cpp
--- a/dom/xbl/nsXBLDocumentInfo.cpp
+++ b/dom/xbl/nsXBLDocumentInfo.cpp
@@ -285,21 +285,21 @@ nsXBLDocumentInfo::WritePrototypeBinding
   // write a end marker at the end
   rv = stream->Write8(XBLBinding_Serialize_NoMoreBindings);
   NS_ENSURE_SUCCESS(rv, rv);
 
   stream->Close();
   NS_ENSURE_SUCCESS(rv, rv);
 
   uint32_t len;
-  nsAutoArrayPtr<char> buf;
-  rv = NewBufferFromStorageStream(storageStream, getter_Transfers(buf), &len);
+  UniquePtr<char[]> buf;
+  rv = NewBufferFromStorageStream(storageStream, &buf, &len);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  return startupCache->PutBuffer(spec.get(), buf, len);
+  return startupCache->PutBuffer(spec.get(), buf.get(), len);
 }
 
 void
 nsXBLDocumentInfo::SetFirstPrototypeBinding(nsXBLPrototypeBinding* aBinding)
 {
   mFirstBinding = aBinding;
 }
 
--- a/dom/xul/nsXULPrototypeCache.cpp
+++ b/dom/xul/nsXULPrototypeCache.cpp
@@ -396,28 +396,27 @@ nsXULPrototypeCache::FinishOutputStream(
     nsCOMPtr<nsIStorageStream> storageStream;
     bool found = mOutputStreamTable.Get(uri, getter_AddRefs(storageStream));
     if (!found)
         return NS_ERROR_UNEXPECTED;
     nsCOMPtr<nsIOutputStream> outputStream
         = do_QueryInterface(storageStream);
     outputStream->Close();
 
-    nsAutoArrayPtr<char> buf;
+    UniquePtr<char[]> buf;
     uint32_t len;
-    rv = NewBufferFromStorageStream(storageStream, getter_Transfers(buf),
-                                    &len);
+    rv = NewBufferFromStorageStream(storageStream, &buf, &len);
     NS_ENSURE_SUCCESS(rv, rv);
 
     if (!mStartupCacheURITable.GetEntry(uri)) {
         nsAutoCString spec(kXULCachePrefix);
         rv = PathifyURI(uri, spec);
         if (NS_FAILED(rv))
             return NS_ERROR_NOT_AVAILABLE;
-        rv = sc->PutBuffer(spec.get(), buf, len);
+        rv = sc->PutBuffer(spec.get(), buf.get(), len);
         if (NS_SUCCEEDED(rv)) {
             mOutputStreamTable.Remove(uri);
             mStartupCacheURITable.PutEntry(uri);
         }
     }
 
     return rv;
 }
--- a/startupcache/StartupCacheUtils.cpp
+++ b/startupcache/StartupCacheUtils.cpp
@@ -68,41 +68,41 @@ NewObjectOutputWrappedStorageStream(nsIO
 #endif
   
   storageStream.forget(stream);
   return NS_OK;
 }
 
 NS_EXPORT nsresult
 NewBufferFromStorageStream(nsIStorageStream *storageStream, 
-                           char** buffer, uint32_t* len) 
+                           UniquePtr<char[]>* buffer, uint32_t* len) 
 {
   nsresult rv;
   nsCOMPtr<nsIInputStream> inputStream;
   rv = storageStream->NewInputStream(0, getter_AddRefs(inputStream));
   NS_ENSURE_SUCCESS(rv, rv);
   
   uint64_t avail64;
   rv = inputStream->Available(&avail64);
   NS_ENSURE_SUCCESS(rv, rv);
   NS_ENSURE_TRUE(avail64 <= UINT32_MAX, NS_ERROR_FILE_TOO_BIG);
 
   uint32_t avail = (uint32_t)avail64;
-  nsAutoArrayPtr<char> temp (new char[avail]);
+  auto temp = MakeUnique<char[]>(avail);
   uint32_t read;
-  rv = inputStream->Read(temp, avail, &read);
+  rv = inputStream->Read(temp.get(), avail, &read);
   if (NS_SUCCEEDED(rv) && avail != read)
     rv = NS_ERROR_UNEXPECTED;
   
   if (NS_FAILED(rv)) {
     return rv;
   }
   
   *len = avail;
-  *buffer = temp.forget();
+  *buffer = Move(temp);
   return NS_OK;
 }
 
 static const char baseName[2][5] = { "gre/", "app/" };
 
 static inline bool
 canonicalizeBase(nsAutoCString &spec,
                  nsACString &out)
--- a/startupcache/StartupCacheUtils.h
+++ b/startupcache/StartupCacheUtils.h
@@ -24,20 +24,20 @@ NewObjectInputStreamFromBuffer(UniquePtr
 // This could cause them to be deserialized incorrectly (as multiple copies
 // instead of references).
 NS_EXPORT nsresult
 NewObjectOutputWrappedStorageStream(nsIObjectOutputStream **wrapperStream,
                                     nsIStorageStream** stream,
                                     bool wantDebugStream);
 
 // Creates a buffer for storing the stream into the cache. The buffer is
-// allocated with 'new []'. Typically, the caller would store the buffer in
-// an nsAutoArrayPtr<char> and then call nsIStartupCache::PutBuffer with it.
+// allocated with 'new []'.  After calling this function, the caller would
+// typically call nsIStartupCache::PutBuffer with the returned buffer.
 NS_EXPORT nsresult
 NewBufferFromStorageStream(nsIStorageStream *storageStream, 
-                           char** buffer, uint32_t* len);
+                           UniquePtr<char[]>* buffer, uint32_t* len);
 
 NS_EXPORT nsresult
 PathifyURI(nsIURI *in, nsACString &out);
 } // namespace scache
 } // namespace mozilla
 
 #endif //nsStartupCacheUtils_h_
--- a/startupcache/test/TestStartupCache.cpp
+++ b/startupcache/test/TestStartupCache.cpp
@@ -37,17 +37,17 @@ NewObjectInputStreamFromBuffer(UniquePtr
 // We can't retrieve the wrapped stream from the objectOutputStream later,
 // so we return it here.
 NS_IMPORT nsresult
 NewObjectOutputWrappedStorageStream(nsIObjectOutputStream **wrapperStream,
                                     nsIStorageStream** stream);
 
 NS_IMPORT nsresult
 NewBufferFromStorageStream(nsIStorageStream *storageStream, 
-                           char** buffer, uint32_t* len);
+                           UniquePtr<char[]>* buffer, uint32_t* len);
 } // namespace scache
 } // namespace mozilla
 
 using namespace mozilla::scache;
 using mozilla::UniquePtr;
 
 #define NS_ENSURE_STR_MATCH(str1, str2, testname)  \
 PR_BEGIN_MACRO                                     \
@@ -192,23 +192,23 @@ TestWriteObject() {
   }
   nsCOMPtr<nsISupports> objQI(do_QueryInterface(obj));
   rv = objectOutput->WriteObject(objQI, true);
   if (NS_FAILED(rv)) {
     fail("failed to write object");
     return rv;
   }
 
-  nsAutoArrayPtr<char> buf;
+  UniquePtr<char[]> buf;
   uint32_t len;
-  NewBufferFromStorageStream(storageStream, getter_Transfers(buf), &len);
+  NewBufferFromStorageStream(storageStream, &buf, &len);
 
   // Since this is a post-startup write, it should be written and
   // available.
-  rv = sc->PutBuffer(id, buf, len);
+  rv = sc->PutBuffer(id, buf.get(), len);
   if (NS_FAILED(rv)) {
     fail("failed to insert input stream");
     return rv;
   }
     
   nsAutoArrayPtr<char> buf2;
   uint32_t len2;
   nsCOMPtr<nsIObjectInputStream> objectInput;