Skip to content

Commit

Permalink
[PATCH] bug 801466 - remove some usage of prmem in intl/ and
Browse files Browse the repository at this point in the history
From 9dadb8c05ecddfe60c415c1ff62627e998148c77 Mon Sep 17 00:00:00 2001
 network/ r=jduell
---
 intl/strres/src/nsStringBundle.cpp         |    2 --
 intl/uconv/src/nsConverterInputStream.cpp  |    8 +++---
 intl/uconv/src/nsConverterInputStream.h    |    6 +++--
 netwerk/base/public/nsReadLine.h           |   38 +++-------------------------
 netwerk/base/src/nsFileStreams.cpp         |    9 +++----
 netwerk/base/src/nsFileStreams.h           |    3 ++-
 netwerk/base/src/nsSerializationHelper.cpp |   31 +++++------------------
 netwerk/base/src/nsSocketTransport2.cpp    |    1 -
 netwerk/cache/nsCacheMetaData.cpp          |    3 +--
 netwerk/cache/nsCacheMetaData.h            |    3 ++-
 netwerk/protocol/data/nsDataChannel.cpp    |   24 +++++++-----------
 11 files changed, 36 insertions(+), 92 deletions(-)
  • Loading branch information
Trevor Saunders committed Dec 4, 2012
1 parent 5f8e48f commit d0feee7
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 92 deletions.
2 changes: 0 additions & 2 deletions intl/strres/src/nsStringBundle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
#include "nsIComponentManager.h"
#include "nsIMemory.h"
#include "nsIObserverService.h"
#include "pratom.h"
#include "prmem.h"
#include "nsCOMArray.h"
#include "nsTextFormatter.h"
#include "nsIErrorService.h"
Expand Down
8 changes: 4 additions & 4 deletions intl/uconv/src/nsConverterInputStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "nsIInputStream.h"
#include "nsICharsetConverterManager.h"
#include "nsIServiceManager.h"
#include "nsReadLine.h"

#define CONVERTER_BUFFER_SIZE 8192

Expand Down Expand Up @@ -53,7 +54,7 @@ NS_IMETHODIMP
nsConverterInputStream::Close()
{
nsresult rv = mInput ? mInput->Close() : NS_OK;
PR_FREEIF(mLineBuffer);
mLineBuffer = nullptr;
mInput = nullptr;
mConverter = nullptr;
mByteData = nullptr;
Expand Down Expand Up @@ -234,8 +235,7 @@ 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);
}
6 changes: 4 additions & 2 deletions intl/uconv/src/nsConverterInputStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
#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"

template<typename T> class nsLineBuffer;

#define NS_CONVERTERINPUTSTREAM_CONTRACTID "@mozilla.org/intl/converter-input-stream;1"

// {2BC2AD62-AD5D-4b7b-A9DB-F74AE203C527}
Expand Down Expand Up @@ -58,5 +60,5 @@ class nsConverterInputStream : public nsIConverterInputStream,
uint32_t mUnicharDataLength;
PRUnichar mReplacementChar;

nsLineBuffer<PRUnichar>* mLineBuffer;
nsAutoPtr<nsLineBuffer<PRUnichar> > mLineBuffer;
};
38 changes: 3 additions & 35 deletions netwerk/base/public/nsReadLine.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#ifndef nsReadLine_h__
#define nsReadLine_h__

#include "prmem.h"
#include "nsIInputStream.h"
#include "mozilla/Likely.h"

Expand Down Expand Up @@ -42,52 +41,21 @@
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.
Expand Down
9 changes: 4 additions & 5 deletions netwerk/base/src/nsFileStreams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ nsFileInputStream::Close()
}

// 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)) {
Expand Down Expand Up @@ -453,10 +453,9 @@ nsFileInputStream::ReadLine(nsACString& aLine, bool* aResult)
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
Expand All @@ -465,7 +464,7 @@ 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);
Expand Down
3 changes: 2 additions & 1 deletion netwerk/base/src/nsFileStreams.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define nsFileStreams_h__

#include "nsAlgorithm.h"
#include "nsAutoPtr.h"
#include "nsIFileStreams.h"
#include "nsIFile.h"
#include "nsIInputStream.h"
Expand Down Expand Up @@ -141,7 +142,7 @@ class nsFileInputStream : public nsFileStreamBase,
Create(nsISupports *aOuter, REFNSIID aIID, void **aResult);

protected:
nsLineBuffer<char> *mLineBuffer;
nsAutoPtr<nsLineBuffer<char> > mLineBuffer;

/**
* The file being opened.
Expand Down
31 changes: 7 additions & 24 deletions netwerk/base/src/nsSerializationHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

#include "nsSerializationHelper.h"

#include "plbase64.h"
#include "prmem.h"

#include "mozilla/Base64.h"
#include "nsISerializable.h"
#include "nsIObjectOutputStream.h"
#include "nsIObjectInputStream.h"
Expand All @@ -16,6 +15,8 @@
#include "nsComponentManagerUtils.h"
#include "nsStringStream.h"

using namespace mozilla;

nsresult
NS_SerializeToString(nsISerializable* obj, nsCSubstring& str)
{
Expand All @@ -38,30 +39,12 @@ NS_SerializeToString(nsISerializable* obj, nsCSubstring& 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 =
Expand Down
1 change: 0 additions & 1 deletion netwerk/base/src/nsSocketTransport2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "nsAutoPtr.h"
#include "nsCOMPtr.h"
#include "netCore.h"
#include "prmem.h"
#include "plstr.h"
#include "prnetdb.h"
#include "prerror.h"
Expand Down
3 changes: 1 addition & 2 deletions netwerk/cache/nsCacheMetaData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

#include "nsCacheMetaData.h"
#include "nsICacheEntryDescriptor.h"
#include "prmem.h"

const char *
nsCacheMetaData::GetElement(const char * key)
Expand Down Expand Up @@ -152,7 +151,7 @@ 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;
}
Expand Down
3 changes: 2 additions & 1 deletion netwerk/cache/nsCacheMetaData.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class nsCacheMetaData {

~nsCacheMetaData() {
mBufferSize = mMetaSize = 0;
PR_FREEIF(mBuffer);
moz_free(mBuffer);
mBuffer = nullptr;
}

const char * GetElement(const char * key);
Expand Down
24 changes: 9 additions & 15 deletions netwerk/protocol/data/nsDataChannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@

// data implementation

#include "nsIOService.h"
#include "nsDataChannel.h"

#include "mozilla/Base64.h"
#include "nsIOService.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,
Expand Down Expand Up @@ -70,17 +71,10 @@ nsDataChannel::OpenContentStream(bool async, nsIInputStream **result,
}
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);
}
Expand Down

0 comments on commit d0feee7

Please sign in to comment.