bug 801466 - part 3.2 remove some usage of prmem in content/ r=mounir, khuey
authorTrevor Saunders <trev.saunders@gmail.com>
Wed, 05 Dec 2012 17:20:57 -0500
changeset 115639 bb270f328b5a637ebd6c10315bd0926e2c2bed11
parent 115638 e54109656e933fc57dd99ac5d463f38be3edc403
child 115640 db40186d9d395fcb908ba5eeba8b88fe85f52ff8
push idunknown
push userunknown
push dateunknown
reviewersmounir, khuey
bugs801466
milestone20.0a1
bug 801466 - part 3.2 remove some usage of prmem in content/ r=mounir, khuey
content/base/public/nsDOMFile.h
content/base/src/WebSocket.cpp
content/base/src/nsAttrAndChildArray.cpp
content/base/src/nsContentUtils.cpp
content/base/src/nsDOMBlobBuilder.h
content/base/src/nsDOMFile.cpp
content/base/src/nsDOMFileReader.cpp
content/base/src/nsDOMFileReader.h
--- a/content/base/public/nsDOMFile.h
+++ b/content/base/public/nsDOMFile.h
@@ -12,17 +12,16 @@
 #include "nsIDOMFileList.h"
 #include "nsIInputStream.h"
 #include "nsIJSNativeInitializer.h"
 #include "nsIMutable.h"
 #include "nsCOMArray.h"
 #include "nsCOMPtr.h"
 #include "nsString.h"
 #include "nsIXMLHttpRequest.h"
-#include "prmem.h"
 #include "nsAutoPtr.h"
 
 #include "mozilla/GuardObjects.h"
 #include "mozilla/LinkedList.h"
 #include "mozilla/StandardInteger.h"
 #include "mozilla/StaticPtr.h"
 #include "mozilla/dom/DOMError.h"
 #include "mozilla/dom/indexedDB/FileInfo.h"
@@ -404,17 +403,17 @@ protected:
 
     ~DataOwner() {
       remove();
       if (sDataOwners->isEmpty()) {
         // Free the linked list if it's empty.
         sDataOwners = nullptr;
       }
 
-      PR_Free(mData);
+      moz_free(mData);
     }
 
     static void EnsureMemoryReporterRegistered();
 
     static bool sMemoryReporterRegistered;
     static mozilla::StaticAutoPtr<mozilla::LinkedList<DataOwner> > sDataOwners;
     void* mData;
     uint64_t mLength;
--- a/content/base/src/WebSocket.cpp
+++ b/content/base/src/WebSocket.cpp
@@ -37,17 +37,16 @@
 #include "nsIScriptError.h"
 #include "nsNetUtil.h"
 #include "nsILoadGroup.h"
 #include "mozilla/Preferences.h"
 #include "nsDOMLists.h"
 #include "xpcpublic.h"
 #include "nsContentPolicyUtils.h"
 #include "jsfriendapi.h"
-#include "prmem.h"
 #include "nsDOMFile.h"
 #include "nsWrapperCacheInlines.h"
 #include "nsDOMEventTargetHelper.h"
 #include "nsIObserverService.h"
 #include "GeneratedEvents.h"
 
 namespace mozilla {
 namespace dom {
--- a/content/base/src/nsAttrAndChildArray.cpp
+++ b/content/base/src/nsAttrAndChildArray.cpp
@@ -5,17 +5,16 @@
 
 /*
  * Storage of the children and attributes of a DOM node; storage for
  * the two is unified to minimize footprint.
  */
 
 #include "nsAttrAndChildArray.h"
 #include "nsMappedAttributeElement.h"
-#include "prmem.h"
 #include "prbit.h"
 #include "nsString.h"
 #include "nsHTMLStyleSheet.h"
 #include "nsRuleWalker.h"
 #include "nsMappedAttributes.h"
 #include "nsUnicharUtils.h"
 #include "nsAutoPtr.h"
 #include "nsContentUtils.h" // nsAutoScriptBlocker
@@ -95,17 +94,17 @@ nsAttrAndChildArray::nsAttrAndChildArray
 nsAttrAndChildArray::~nsAttrAndChildArray()
 {
   if (!mImpl) {
     return;
   }
 
   Clear();
 
-  PR_Free(mImpl);
+  moz_free(mImpl);
 }
 
 nsIContent*
 nsAttrAndChildArray::GetSafeChildAt(uint32_t aPos) const
 {
   if (aPos < ChildCount()) {
     return ChildAt(aPos);
   }
@@ -611,21 +610,21 @@ nsAttrAndChildArray::Compact()
             mImpl->mBuffer + slotCount * ATTRSIZE,
             childCount * sizeof(nsIContent*));
     SetAttrSlotCount(attrCount);
   }
 
   // Then resize or free buffer
   uint32_t newSize = attrCount * ATTRSIZE + childCount;
   if (!newSize && !mImpl->mMappedAttrs) {
-    PR_Free(mImpl);
+    moz_free(mImpl);
     mImpl = nullptr;
   }
   else if (newSize < mImpl->mBufferSize) {
-    mImpl = static_cast<Impl*>(PR_Realloc(mImpl, (newSize + NS_IMPL_EXTRA_SIZE) * sizeof(nsIContent*)));
+    mImpl = static_cast<Impl*>(moz_realloc(mImpl, (newSize + NS_IMPL_EXTRA_SIZE) * sizeof(nsIContent*)));
     NS_ASSERTION(mImpl, "failed to reallocate to smaller buffer");
 
     mImpl->mBufferSize = newSize;
   }
 }
 
 void
 nsAttrAndChildArray::Clear()
@@ -752,17 +751,17 @@ nsAttrAndChildArray::GrowBy(uint32_t aGr
       size += ATTRCHILD_ARRAY_GROWSIZE;
     } while (size < minSize);
   }
   else {
     size = 1u << PR_CeilingLog2(minSize);
   }
 
   bool needToInitialize = !mImpl;
-  Impl* newImpl = static_cast<Impl*>(PR_Realloc(mImpl, size * sizeof(void*)));
+  Impl* newImpl = static_cast<Impl*>(moz_realloc(mImpl, size * sizeof(void*)));
   NS_ENSURE_TRUE(newImpl, false);
 
   mImpl = newImpl;
 
   // Set initial counts if we didn't have a buffer before
   if (needToInitialize) {
     mImpl->mMappedAttrs = nullptr;
     SetAttrSlotAndChildCount(0, 0);
--- a/content/base/src/nsContentUtils.cpp
+++ b/content/base/src/nsContentUtils.cpp
@@ -6131,17 +6131,17 @@ nsContentUtils::CreateArrayBuffer(JSCont
 // Initial implementation: only stores to RAM, not file
 // TODO: bug 704447: large file support
 nsresult
 nsContentUtils::CreateBlobBuffer(JSContext* aCx,
                                  const nsACString& aData,
                                  jsval& aBlob)
 {
   uint32_t blobLen = aData.Length();
-  void* blobData = PR_Malloc(blobLen);
+  void* blobData = moz_malloc(blobLen);
   nsCOMPtr<nsIDOMBlob> blob;
   if (blobData) {
     memcpy(blobData, aData.BeginReading(), blobLen);
     blob = new nsDOMMemoryFile(blobData, blobLen, EmptyString());
   } else {
     return NS_ERROR_OUT_OF_MEMORY;
   }
   JSObject* scope = JS_GetGlobalForScopeChain(aCx);
--- a/content/base/src/nsDOMBlobBuilder.h
+++ b/content/base/src/nsDOMBlobBuilder.h
@@ -127,18 +127,17 @@ protected:
     // Start at 1 or we'll loop forever.
     CheckedUint32 bufferLen = NS_MAX<uint32_t>(mDataBufferLen, 1);
     while (bufferLen.isValid() && bufferLen.value() < mDataLen + aSize)
       bufferLen *= 2;
 
     if (!bufferLen.isValid())
       return false;
 
-    // PR_ memory functions are still fallible
-    void* data = PR_Realloc(mData, bufferLen.value());
+    void* data = moz_realloc(mData, bufferLen.value());
     if (!data)
       return false;
 
     mData = data;
     mDataBufferLen = bufferLen.value();
     mDataLen += aSize;
     return true;
   }
--- a/content/base/src/nsDOMFile.cpp
+++ b/content/base/src/nsDOMFile.cpp
@@ -31,18 +31,16 @@
 #include "nsStringStream.h"
 #include "nsJSUtils.h"
 #include "nsPrintfCString.h"
 #include "mozilla/SHA1.h"
 #include "mozilla/CheckedInt.h"
 #include "mozilla/Preferences.h"
 #include "mozilla/Attributes.h"
 
-#include "plbase64.h"
-#include "prmem.h"
 #include "mozilla/dom/FileListBinding.h"
 
 using namespace mozilla;
 using namespace mozilla::dom;
 
 // XXXkhuey the input stream that we pass out of a DOMFile
 // can outlive the actual DOMFile object.  Thus, we must
 // ensure that the buffer underlying the stream we get
--- a/content/base/src/nsDOMFileReader.cpp
+++ b/content/base/src/nsDOMFileReader.cpp
@@ -15,35 +15,33 @@
 #include "nsIFile.h"
 #include "nsIFileStreams.h"
 #include "nsIInputStream.h"
 #include "nsIMIMEService.h"
 #include "nsIUnicodeDecoder.h"
 #include "nsNetCID.h"
 #include "nsNetUtil.h"
 
-#include "plbase64.h"
-#include "prmem.h"
-
 #include "nsLayoutCID.h"
 #include "nsXPIDLString.h"
 #include "nsReadableUtils.h"
 #include "nsIURI.h"
 #include "nsStreamUtils.h"
 #include "nsXPCOM.h"
 #include "nsIDOMEventListener.h"
 #include "nsIJSContextStack.h"
 #include "nsJSEnvironment.h"
 #include "nsIScriptGlobalObject.h"
 #include "nsCExternalHandlerService.h"
 #include "nsIStreamConverterService.h"
 #include "nsCycleCollectionParticipant.h"
 #include "nsLayoutStatics.h"
 #include "nsIScriptObjectPrincipal.h"
 #include "nsHostObjectProtocolHandler.h"
+#include "mozilla/Base64.h"
 #include "mozilla/Preferences.h"
 #include "mozilla/dom/EncodingUtils.h"
 #include "xpcpublic.h"
 #include "nsIScriptSecurityManager.h"
 #include "nsDOMJSUtils.h"
 #include "nsDOMEventTargetHelper.h"
 
 #include "jsfriendapi.h"
@@ -314,17 +312,17 @@ nsDOMFileReader::DoOnDataAvailable(nsIRe
     NS_ASSERTION(bytesRead == aCount, "failed to read data");
   }
   else {
     //Update memory buffer to reflect the contents of the file
     if (aOffset + aCount > UINT32_MAX) {
       // PR_Realloc doesn't support over 4GB memory size even if 64-bit OS
       return NS_ERROR_OUT_OF_MEMORY;
     }
-    mFileData = (char *)PR_Realloc(mFileData, aOffset + aCount);
+    mFileData = (char *)moz_realloc(mFileData, aOffset + aCount);
     NS_ENSURE_TRUE(mFileData, NS_ERROR_OUT_OF_MEMORY);
 
     uint32_t bytesRead = 0;
     aInputStream->Read(mFileData + aOffset, aCount, &bytesRead);
     NS_ASSERTION(bytesRead == aCount, "failed to read data");
 
     mDataLen += aCount;
   }
@@ -486,36 +484,21 @@ nsDOMFileReader::GetAsDataURL(nsIDOMBlob
   rv = aFile->GetType(contentType);
   if (NS_SUCCEEDED(rv) && !contentType.IsEmpty()) {
     aResult.Append(contentType);
   } else {
     aResult.AppendLiteral("application/octet-stream");
   }
   aResult.AppendLiteral(";base64,");
 
-  uint32_t totalRead = 0;
-  while (aDataLen > totalRead) {
-    uint32_t numEncode = 4096;
-    uint32_t amtRemaining = aDataLen - totalRead;
-    if (numEncode > amtRemaining)
-      numEncode = amtRemaining;
+  nsCString encodedData;
+  rv = Base64Encode(Substring(aFileData, aDataLen), encodedData);
+  NS_ENSURE_SUCCESS(rv, rv);
 
-    //Unless this is the end of the file, encode in multiples of 3
-    if (numEncode > 3) {
-      uint32_t leftOver = numEncode % 3;
-      numEncode -= leftOver;
-    }
-
-    //Out buffer should be at least 4/3rds the read buf, plus a terminator
-    char *base64 = PL_Base64Encode(aFileData + totalRead, numEncode, nullptr);
-    AppendASCIItoUTF16(nsDependentCString(base64), aResult);
-    PR_Free(base64);
-
-    totalRead += numEncode;
-  }
+  AppendASCIItoUTF16(encodedData, aResult);
 
   return NS_OK;
 }
 
 nsresult
 nsDOMFileReader::ConvertStream(const char *aFileData,
                                uint32_t aDataLen,
                                const char *aCharset,
--- a/content/base/src/nsDOMFileReader.h
+++ b/content/base/src/nsDOMFileReader.h
@@ -19,17 +19,16 @@
 
 #include "nsIDOMFile.h"
 #include "nsIDOMFileReader.h"
 #include "nsIDOMFileList.h"
 #include "nsIInputStream.h"
 #include "nsCOMPtr.h"
 #include "nsIStreamLoader.h"
 #include "nsIChannel.h"
-#include "prmem.h"
 
 #include "FileIOObject.h"
 
 class nsDOMFileReader : public mozilla::dom::FileIOObject,
                         public nsIDOMFileReader,
                         public nsIInterfaceRequestor,
                         public nsSupportsWeakReference,
                         public nsIJSNativeInitializer
@@ -76,17 +75,17 @@ protected:
 
   nsresult ReadFileContent(JSContext* aCx, nsIDOMBlob *aFile, const nsAString &aCharset, eDataFormat aDataFormat); 
   nsresult GetAsText(const nsACString &aCharset,
                      const char *aFileData, uint32_t aDataLen, nsAString &aResult);
   nsresult GetAsDataURL(nsIDOMBlob *aFile, const char *aFileData, uint32_t aDataLen, nsAString &aResult); 
   nsresult ConvertStream(const char *aFileData, uint32_t aDataLen, const char *aCharset, nsAString &aResult); 
 
   void FreeFileData() {
-    PR_Free(mFileData);
+    moz_free(mFileData);
     mFileData = nullptr;
     mDataLen = 0;
   }
 
   char *mFileData;
   nsCOMPtr<nsIDOMBlob> mFile;
   nsCString mCharset;
   uint32_t mDataLen;