bug 801466 - remove some usage of prmem in intl/ and network/ r=jduell
authorTrevor Saunders <trev.saunders@gmail.com>
Tue, 04 Dec 2012 18:04:39 -0500
changeset 124114 c0715c079f1e09961ea45dd3b0bb559b238ee96c
parent 124113 75a0cf19f231684d46aca3a3d044d3fdf39ff89d
child 124115 1d3c65ce01addd4c3a5a8a575467f04b300d43dc
push id2151
push userlsblakk@mozilla.com
push dateTue, 19 Feb 2013 18:06:57 +0000
treeherdermozilla-beta@4952e88741ec [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjduell
bugs801466
milestone20.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 801466 - remove some usage of prmem in intl/ and network/ r=jduell
intl/strres/src/nsStringBundle.cpp
intl/uconv/src/nsConverterInputStream.cpp
intl/uconv/src/nsConverterInputStream.h
netwerk/base/public/nsReadLine.h
netwerk/base/src/nsFileStreams.cpp
netwerk/base/src/nsFileStreams.h
netwerk/base/src/nsSerializationHelper.cpp
netwerk/base/src/nsSocketTransport2.cpp
netwerk/cache/nsCacheMetaData.cpp
netwerk/cache/nsCacheMetaData.h
netwerk/protocol/data/nsDataChannel.cpp
--- a/intl/strres/src/nsStringBundle.cpp
+++ b/intl/strres/src/nsStringBundle.cpp
@@ -19,18 +19,16 @@
 #include "nsHashtable.h"
 #include "nsMemory.h"
 #include "plstr.h"
 #include "nsNetUtil.h"
 #include "nsIURL.h"
 #include "nsIComponentManager.h"
 #include "nsIMemory.h"
 #include "nsIObserverService.h"
-#include "pratom.h"
-#include "prmem.h"
 #include "nsCOMArray.h"
 #include "nsTextFormatter.h"
 #include "nsIErrorService.h"
 #include "nsICategoryManager.h"
 
 #include "nsPrintfCString.h"
 // for async loading
 #ifdef ASYNC_LOADING
--- a/intl/uconv/src/nsConverterInputStream.cpp
+++ b/intl/uconv/src/nsConverterInputStream.cpp
@@ -2,16 +2,17 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "nsConverterInputStream.h"
 #include "nsIInputStream.h"
 #include "nsICharsetConverterManager.h"
 #include "nsIServiceManager.h"
+#include "nsReadLine.h"
 
 #define CONVERTER_BUFFER_SIZE 8192
 
 NS_IMPL_ISUPPORTS3(nsConverterInputStream, nsIConverterInputStream,
                    nsIUnicharInputStream, nsIUnicharLineInputStream)
     
 static NS_DEFINE_CID(kCharsetConverterManagerCID, NS_ICHARSETCONVERTERMANAGER_CID);
 
@@ -48,17 +49,17 @@ nsConverterInputStream::Init(nsIInputStr
     
     return NS_OK;
 }
 
 NS_IMETHODIMP
 nsConverterInputStream::Close()
 {
     nsresult rv = mInput ? mInput->Close() : NS_OK;
-    PR_FREEIF(mLineBuffer);
+    mLineBuffer = nullptr;
     mInput = nullptr;
     mConverter = nullptr;
     mByteData = nullptr;
     mUnicharData = nullptr;
     return rv;
 }
 
 NS_IMETHODIMP
@@ -229,13 +230,12 @@ nsConverterInputStream::Fill(nsresult * 
 
   return mUnicharDataLength;
 }
 
 NS_IMETHODIMP
 nsConverterInputStream::ReadLine(nsAString& aLine, bool* aResult)
 {
   if (!mLineBuffer) {
-    nsresult rv = NS_InitLineBuffer(&mLineBuffer);
-    if (NS_FAILED(rv)) return rv;
+    mLineBuffer = new nsLineBuffer<PRUnichar>;
   }
-  return NS_ReadLine(this, mLineBuffer, aLine, aResult);
+  return NS_ReadLine(this, mLineBuffer.get(), aLine, aResult);
 }
--- a/intl/uconv/src/nsConverterInputStream.h
+++ b/intl/uconv/src/nsConverterInputStream.h
@@ -2,22 +2,23 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "nsIInputStream.h"
 #include "nsIConverterInputStream.h"
 #include "nsIUnicharLineInputStream.h"
 #include "nsString.h"
-#include "nsReadLine.h"
 
+#include "nsAutoPtr.h"
 #include "nsCOMPtr.h"
 #include "nsIUnicodeDecoder.h"
 #include "nsIByteBuffer.h"
 #include "nsIUnicharBuffer.h"
+#include "nsReadLine.h"
 
 #define NS_CONVERTERINPUTSTREAM_CONTRACTID "@mozilla.org/intl/converter-input-stream;1"
 
 // {2BC2AD62-AD5D-4b7b-A9DB-F74AE203C527}
 #define NS_CONVERTERINPUTSTREAM_CID \
   { 0x2bc2ad62, 0xad5d, 0x4b7b, \
    { 0xa9, 0xdb, 0xf7, 0x4a, 0xe2, 0x3, 0xc5, 0x27 } }
 
@@ -53,10 +54,10 @@ class nsConverterInputStream : public ns
     nsCOMPtr<nsIInputStream> mInput;
 
     nsresult  mLastErrorCode;
     uint32_t  mLeftOverBytes;
     uint32_t  mUnicharDataOffset;
     uint32_t  mUnicharDataLength;
     PRUnichar mReplacementChar;
 
-    nsLineBuffer<PRUnichar>* mLineBuffer;    
+    nsAutoPtr<nsLineBuffer<PRUnichar> > mLineBuffer;
 };
--- a/netwerk/base/public/nsReadLine.h
+++ b/netwerk/base/public/nsReadLine.h
@@ -2,17 +2,16 @@
  *
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef nsReadLine_h__
 #define nsReadLine_h__
 
-#include "prmem.h"
 #include "nsIInputStream.h"
 #include "mozilla/Likely.h"
 
 /**
  * @file
  * Functions to read complete lines from an input stream.
  *
  * To properly use the helper function in here (NS_ReadLine) the caller
@@ -37,62 +36,31 @@
  * @internal
  * Line buffer structure, buffers data from an input stream.
  * The buffer is empty when |start| == |end|.
  * Invariant: |start| <= |end|
  */
 template<typename CharT>
 class nsLineBuffer {
   public:
+    nsLineBuffer() : start(buf), end(buf) { }
+
   CharT buf[kLineBufferSize+1];
   CharT* start;
   CharT* end;
 };
 
 /**
- * Initialize a line buffer for use with NS_ReadLine.
- *
- * @param aBufferPtr
- *        Pointer to pointer to a line buffer. Upon successful return,
- *        *aBufferPtr will contain a valid pointer to a line buffer, for use
- *        with NS_ReadLine. Use PR_Free when the buffer is no longer needed.
- *
- * @retval NS_OK Success.
- * @retval NS_ERROR_OUT_OF_MEMORY Not enough memory to allocate the line buffer.
- *
- * @par Example:
- * @code
- *    nsLineBuffer* lb;
- *    rv = NS_InitLineBuffer(&lb);
- *    if (NS_SUCCEEDED(rv)) {
- *      // do stuff...
- *      PR_Free(lb);
- *    }
- * @endcode
- */
-template<typename CharT>
-nsresult
-NS_InitLineBuffer (nsLineBuffer<CharT> ** aBufferPtr) {
-  *aBufferPtr = PR_NEW(nsLineBuffer<CharT>);
-  if (!(*aBufferPtr))
-    return NS_ERROR_OUT_OF_MEMORY;
-
-  (*aBufferPtr)->start = (*aBufferPtr)->end = (*aBufferPtr)->buf;
-  return NS_OK;
-}
-
-/**
  * Read a line from an input stream. Lines are separated by '\r' (0x0D) or '\n'
  * (0x0A), or "\r\n" or "\n\r".
  *
  * @param aStream
  *        The stream to read from
  * @param aBuffer
- *        The line buffer to use. Must have been inited with
- *        NS_InitLineBuffer before. A single line buffer must not be used with
+ *        The line buffer to use.  A single line buffer must not be used with
  *        different input streams.
  * @param aLine [out]
  *        The string where the line will be stored.
  * @param more [out]
  *        Whether more data is available in the buffer. If true, NS_ReadLine may
  *        be called again to read further lines. Otherwise, further calls to
  *        NS_ReadLine will return an error.
  *
--- a/netwerk/base/src/nsFileStreams.cpp
+++ b/netwerk/base/src/nsFileStreams.cpp
@@ -413,17 +413,17 @@ nsFileInputStream::Close()
     // NS_SEEK_CUR on a closed file that has been opened with
     // REOPEN_ON_REWIND.
     if (mBehaviorFlags & REOPEN_ON_REWIND) {
         // Get actual position. Not one modified by subclasses
         nsFileStreamBase::Tell(&mCachedPosition);
     }
 
     // null out mLineBuffer in case Close() is called again after failing
-    PR_FREEIF(mLineBuffer);
+    mLineBuffer = nullptr;
     nsresult rv = nsFileStreamBase::Close();
     if (NS_FAILED(rv)) return rv;
     if (mFile && (mBehaviorFlags & DELETE_ON_CLOSE)) {
         rv = mFile->Remove(false);
         NS_ASSERTION(NS_SUCCEEDED(rv), "failed to delete file");
         // If we don't need to save the file for reopening, free it up
         if (!(mBehaviorFlags & REOPEN_ON_REWIND)) {
           mFile = nullptr;
@@ -448,29 +448,28 @@ nsFileInputStream::Read(char* aBuf, uint
 
 NS_IMETHODIMP
 nsFileInputStream::ReadLine(nsACString& aLine, bool* aResult)
 {
     nsresult rv = DoPendingOpen();
     NS_ENSURE_SUCCESS(rv, rv);
 
     if (!mLineBuffer) {
-        nsresult rv = NS_InitLineBuffer(&mLineBuffer);
-        if (NS_FAILED(rv)) return rv;
+      mLineBuffer = new nsLineBuffer<char>;
     }
-    return NS_ReadLine(this, mLineBuffer, aLine, aResult);
+    return NS_ReadLine(this, mLineBuffer.get(), aLine, aResult);
 }
 
 NS_IMETHODIMP
 nsFileInputStream::Seek(int32_t aWhence, int64_t aOffset)
 {
     nsresult rv = DoPendingOpen();
     NS_ENSURE_SUCCESS(rv, rv);
 
-    PR_FREEIF(mLineBuffer); // this invalidates the line buffer
+    mLineBuffer = nullptr;
     if (!mFD) {
         if (mBehaviorFlags & REOPEN_ON_REWIND) {
             rv = Open(mFile, mIOFlags, mPerm);
             NS_ENSURE_SUCCESS(rv, rv);
 
             // If the file was closed, and we do a relative seek, use the
             // position we cached when we closed the file to seek to the right
             // location.
--- a/netwerk/base/src/nsFileStreams.h
+++ b/netwerk/base/src/nsFileStreams.h
@@ -2,29 +2,30 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef nsFileStreams_h__
 #define nsFileStreams_h__
 
 #include "nsAlgorithm.h"
+#include "nsAutoPtr.h"
 #include "nsIFileStreams.h"
 #include "nsIFile.h"
 #include "nsIInputStream.h"
 #include "nsIOutputStream.h"
 #include "nsISafeOutputStream.h"
 #include "nsISeekableStream.h"
 #include "nsILineInputStream.h"
 #include "nsCOMPtr.h"
 #include "prlog.h"
 #include "prio.h"
 #include "nsIIPCSerializableInputStream.h"
+#include "nsReadLine.h"
 
-template<class CharType> class nsLineBuffer;
 
 ////////////////////////////////////////////////////////////////////////////////
 
 class nsFileStreamBase : public nsISeekableStream
 {
 public:
     NS_DECL_ISUPPORTS
     NS_DECL_NSISEEKABLESTREAM
@@ -136,17 +137,17 @@ public:
     {
         Close();
     }
 
     static nsresult
     Create(nsISupports *aOuter, REFNSIID aIID, void **aResult);
 
 protected:
-    nsLineBuffer<char> *mLineBuffer;
+    nsAutoPtr<nsLineBuffer<char> > mLineBuffer;
 
     /**
      * The file being opened.
      */
     nsCOMPtr<nsIFile> mFile;
     /**
      * The IO flags passed to Init() for the file open.
      */
--- a/netwerk/base/src/nsSerializationHelper.cpp
+++ b/netwerk/base/src/nsSerializationHelper.cpp
@@ -1,26 +1,27 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "nsSerializationHelper.h"
 
-#include "plbase64.h"
-#include "prmem.h"
 
+#include "mozilla/Base64.h"
 #include "nsISerializable.h"
 #include "nsIObjectOutputStream.h"
 #include "nsIObjectInputStream.h"
 #include "nsString.h"
 #include "nsBase64Encoder.h"
 #include "nsAutoPtr.h"
 #include "nsComponentManagerUtils.h"
 #include "nsStringStream.h"
 
+using namespace mozilla;
+
 nsresult
 NS_SerializeToString(nsISerializable* obj, nsCSubstring& str)
 {
   nsRefPtr<nsBase64Encoder> stream(new nsBase64Encoder());
   if (!stream)
     return NS_ERROR_OUT_OF_MEMORY;
 
   nsCOMPtr<nsIObjectOutputStream> objstream =
@@ -33,40 +34,22 @@ NS_SerializeToString(nsISerializable* ob
       objstream->WriteCompoundObject(obj, NS_GET_IID(nsISupports), true);
   NS_ENSURE_SUCCESS(rv, rv);
   return stream->Finish(str);
 }
 
 nsresult
 NS_DeserializeObject(const nsCSubstring& str, nsISupports** obj)
 {
-  // Base64 maps 3 binary bytes -> 4 ASCII bytes.  If the original byte array
-  // does not have length 0 mod 3, the input is padded with zeros and the
-  // output is padded with a corresponding number of trailing '=' (which are
-  // then sometimes dropped).  To compute the correct length of the original
-  // byte array, we have to subtract the number of trailing '=' and then
-  // multiply by 3 and then divide by 4 (making sure this is an integer
-  // division).
+  nsCString decodedData;
+  nsresult rv = Base64Decode(str, decodedData);
+  NS_ENSURE_SUCCESS(rv, rv);
 
-  uint32_t size = str.Length();
-  if (size > 0 && str[size-1] == '=') {
-    if (size > 1 && str[size-2] == '=') {
-      size -= 2;
-    } else {
-      size -= 1;
-    }
-  }
-  size = (size * 3) / 4;
-  char* buf = PL_Base64Decode(str.BeginReading(), str.Length(), nullptr);
-  if (!buf)
-    return NS_ERROR_OUT_OF_MEMORY;
   nsCOMPtr<nsIInputStream> stream;
-  nsresult rv = NS_NewCStringInputStream(getter_AddRefs(stream),
-                                         Substring(buf, size));
-  PR_Free(buf);
+  rv = NS_NewCStringInputStream(getter_AddRefs(stream), decodedData);
   NS_ENSURE_SUCCESS(rv, rv);
 
   nsCOMPtr<nsIObjectInputStream> objstream =
       do_CreateInstance("@mozilla.org/binaryinputstream;1");
   if (!objstream)
     return NS_ERROR_OUT_OF_MEMORY;
 
   objstream->SetInputStream(stream);
--- a/netwerk/base/src/nsSocketTransport2.cpp
+++ b/netwerk/base/src/nsSocketTransport2.cpp
@@ -14,17 +14,16 @@
 #include "nsNetSegmentUtils.h"
 #include "nsNetAddr.h"
 #include "nsTransportUtils.h"
 #include "nsProxyInfo.h"
 #include "nsNetCID.h"
 #include "nsAutoPtr.h"
 #include "nsCOMPtr.h"
 #include "netCore.h"
-#include "prmem.h"
 #include "plstr.h"
 #include "prnetdb.h"
 #include "prerror.h"
 #include "prerr.h"
 #include "NetworkActivityMonitor.h"
 
 #include "nsIServiceManager.h"
 #include "nsISocketProviderService.h"
--- a/netwerk/cache/nsCacheMetaData.cpp
+++ b/netwerk/cache/nsCacheMetaData.cpp
@@ -1,17 +1,16 @@
 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  *
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "nsCacheMetaData.h"
 #include "nsICacheEntryDescriptor.h"
-#include "prmem.h"
 
 const char *
 nsCacheMetaData::GetElement(const char * key)
 {
     const char * data = mBuffer;
     const char * limit = mBuffer + mMetaSize;
 
     while (data < limit) {
@@ -147,17 +146,17 @@ nsCacheMetaData::VisitElements(nsICacheM
     NS_ABORT_IF_FALSE(data == limit, "Metadata corrupted");
     return NS_OK;
 }
 
 nsresult
 nsCacheMetaData::EnsureBuffer(uint32_t bufSize)
 {
     if (mBufferSize < bufSize) {
-        char * buf = (char *)PR_REALLOC(mBuffer, bufSize);
+        char * buf = (char *)moz_realloc(mBuffer, bufSize);
         if (!buf) {
             return NS_ERROR_OUT_OF_MEMORY;
         }
         mBuffer = buf;
         mBufferSize = bufSize;
     }
     return NS_OK;
 }        
--- a/netwerk/cache/nsCacheMetaData.h
+++ b/netwerk/cache/nsCacheMetaData.h
@@ -13,17 +13,18 @@
 class nsICacheMetaDataVisitor;
 
 class nsCacheMetaData {
 public:
     nsCacheMetaData() : mBuffer(nullptr), mBufferSize(0), mMetaSize(0) { }
 
     ~nsCacheMetaData() {
         mBufferSize = mMetaSize = 0;  
-        PR_FREEIF(mBuffer);
+        moz_free(mBuffer);
+        mBuffer = nullptr;
     }
 
     const char *  GetElement(const char * key);
 
     nsresult      SetElement(const char * key, const char * value);
 
     uint32_t      Size(void) { return mMetaSize; }
 
--- a/netwerk/protocol/data/nsDataChannel.cpp
+++ b/netwerk/protocol/data/nsDataChannel.cpp
@@ -1,27 +1,28 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 // data implementation
 
+#include "nsDataChannel.h"
+
+#include "mozilla/Base64.h"
 #include "nsIOService.h"
-#include "nsDataChannel.h"
 #include "nsDataHandler.h"
 #include "nsNetUtil.h"
 #include "nsIPipe.h"
 #include "nsIInputStream.h"
 #include "nsIOutputStream.h"
 #include "nsReadableUtils.h"
 #include "nsEscape.h"
-#include "plbase64.h"
-#include "plstr.h"
-#include "prmem.h"
+
+using namespace mozilla;
 
 nsresult
 nsDataChannel::OpenContentStream(bool async, nsIInputStream **result,
                                  nsIChannel** channel)
 {
     NS_ENSURE_TRUE(URI(), NS_ERROR_NOT_INITIALIZED);
 
     nsresult rv;
@@ -65,27 +66,20 @@ nsDataChannel::OpenContentStream(bool as
                 resultLen = dataLen-2;
             else
                 resultLen = dataLen-1;
         } else {
             resultLen = dataLen;
         }
         resultLen = ((resultLen * 3) / 4);
 
-        // XXX PL_Base64Decode will return a null pointer for decoding
-        // errors.  Since those are more likely than out-of-memory,
-        // should we return NS_ERROR_MALFORMED_URI instead?
-        char * decodedData = PL_Base64Decode(dataBuffer.get(), dataLen, nullptr);
-        if (!decodedData) {
-            return NS_ERROR_OUT_OF_MEMORY;
-        }
-
-        rv = bufOutStream->Write(decodedData, resultLen, &contentLen);
-
-        PR_Free(decodedData);
+        nsAutoCString decodedData;
+        rv = Base64Decode(dataBuffer, decodedData);
+        NS_ENSURE_SUCCESS(rv, rv);
+        rv = bufOutStream->Write(decodedData.get(), resultLen, &contentLen);
     } else {
         rv = bufOutStream->Write(dataBuffer.get(), dataBuffer.Length(), &contentLen);
     }
     if (NS_FAILED(rv))
         return rv;
 
     SetContentType(contentType);
     SetContentCharset(contentCharset);