fix for bug 177401 - use nsAString& classes instead of wstring in nsIBinaryInputStream, to speed up fastload startup

sr=darin, r=dougt
This commit is contained in:
alecf%netscape.com 2002-11-08 23:30:53 +00:00
parent 42304de422
commit 4721428275
16 changed files with 154 additions and 153 deletions

View File

@ -86,7 +86,7 @@ protected:
nsJSPrincipals mJSPrincipals;
nsVoidArray mAnnotations;
nsHashtable *mCapabilities;
char *mPrefName;
nsCString mPrefName;
static int mCapabilitiesOrdinal;
};

View File

@ -82,8 +82,8 @@ public:
virtual ~nsCertificatePrincipal(void);
protected:
char* mCertificateID;
char* mCommonName;
nsCString mCertificateID;
nsCString mCommonName;
};
#endif // _NS_CERTIFICATE_PRINCIPAL_H_

View File

@ -32,7 +32,7 @@
//////////////////////////
nsBasePrincipal::nsBasePrincipal()
: mCapabilities(nsnull), mPrefName(nsnull)
: mCapabilities(nsnull)
{
}
@ -48,8 +48,6 @@ nsBasePrincipal::~nsBasePrincipal(void)
{
mAnnotations.EnumerateForwards(deleteElement, nsnull);
delete mCapabilities;
if (mPrefName)
Recycle(mPrefName);
}
NS_IMETHODIMP
@ -237,9 +235,7 @@ nsBasePrincipal::InitFromPersistent(const char* aPrefName, const char* aID,
mCapabilities->Reset();
//-- Save the preference name
mPrefName = PL_strdup(aPrefName);
if (!mPrefName)
return NS_ERROR_OUT_OF_MEMORY;
mPrefName = aPrefName;
const char* ordinalBegin = PL_strpbrk(aPrefName, "1234567890");
if (ordinalBegin) {
@ -288,7 +284,7 @@ nsBasePrincipal::GetPreferences(char** aPrefName, char** aID,
char** aGrantedList, char** aDeniedList)
{
//-- Preference name
*aPrefName = nsCRT::strdup(mPrefName);
*aPrefName = ToNewCString(mPrefName);
if (!aPrefName)
return NS_ERROR_OUT_OF_MEMORY;
@ -384,7 +380,7 @@ nsBasePrincipal::Read(nsIObjectInputStream* aStream)
}
if (NS_FAILED(rv)) return rv;
rv = NS_ReadOptionalStringZ(aStream, &mPrefName);
rv = NS_ReadOptionalCString(aStream, mPrefName);
if (NS_FAILED(rv)) return rv;
return NS_OK;
@ -419,7 +415,7 @@ nsBasePrincipal::Write(nsIObjectOutputStream* aStream)
rv = mCapabilities->Write(aStream, WriteScalarValue);
if (NS_FAILED(rv)) return rv;
rv = NS_WriteOptionalStringZ(aStream, mPrefName);
rv = NS_WriteOptionalStringZ(aStream, mPrefName.get());
if (NS_FAILED(rv)) return rv;
return NS_OK;

View File

@ -64,23 +64,22 @@ NSBASEPRINCIPALS_RELEASE(nsCertificatePrincipal);
NS_IMETHODIMP
nsCertificatePrincipal::GetCertificateID(char** aCertificateID)
{
*aCertificateID = nsCRT::strdup(mCertificateID);
*aCertificateID = ToNewCString(mCertificateID);
return *aCertificateID ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
}
NS_IMETHODIMP
nsCertificatePrincipal::GetCommonName(char** aCommonName)
{
*aCommonName = nsCRT::strdup(mCommonName);
*aCommonName = ToNewCString(mCommonName);
return *aCommonName ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
}
NS_IMETHODIMP
nsCertificatePrincipal::SetCommonName(const char* aCommonName)
{
PR_FREEIF(mCommonName);
mCommonName = nsCRT::strdup(aCommonName);
return mCommonName ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
mCommonName = aCommonName;
return NS_OK;
}
///////////////////////////////////////
@ -113,12 +112,10 @@ NS_IMETHODIMP
nsCertificatePrincipal::GetPreferences(char** aPrefName, char** aID,
char** aGrantedList, char** aDeniedList)
{
if (!mPrefName) {
nsCAutoString s;
s.Assign("capability.principal.certificate.p");
s.AppendInt(mCapabilitiesOrdinal++);
s.Append(".id");
mPrefName = ToNewCString(s);
if (mPrefName.IsEmpty()) {
mPrefName.Assign("capability.principal.certificate.p");
mPrefName.AppendInt(mCapabilitiesOrdinal++);
mPrefName.Append(".id");
}
return nsBasePrincipal::GetPreferences(aPrefName, aID,
aGrantedList, aDeniedList);
@ -147,7 +144,7 @@ nsCertificatePrincipal::Equals(nsIPrincipal * other, PRBool * result)
PR_FREEIF(otherID);
return rv;
}
*result = (PL_strcmp(mCertificateID, otherID) == 0);
*result = mCertificateID.Equals(otherID);
PR_FREEIF(otherID);
return NS_OK;
}
@ -174,10 +171,10 @@ nsCertificatePrincipal::Read(nsIObjectInputStream* aStream)
rv = nsBasePrincipal::Read(aStream);
if (NS_FAILED(rv)) return rv;
rv = aStream->ReadStringZ(&mCertificateID);
rv = aStream->ReadCString(mCertificateID);
if (NS_FAILED(rv)) return rv;
rv = NS_ReadOptionalStringZ(aStream, &mCommonName);
rv = NS_ReadOptionalCString(aStream, mCommonName);
if (NS_FAILED(rv)) return rv;
return NS_OK;
@ -191,10 +188,10 @@ nsCertificatePrincipal::Write(nsIObjectOutputStream* aStream)
rv = nsBasePrincipal::Write(aStream);
if (NS_FAILED(rv)) return rv;
rv = aStream->WriteStringZ(mCertificateID);
rv = aStream->WriteStringZ(mCertificateID.get());
if (NS_FAILED(rv)) return rv;
rv = NS_WriteOptionalStringZ(aStream, mCommonName);
rv = NS_WriteOptionalStringZ(aStream, mCommonName.get());
if (NS_FAILED(rv)) return rv;
return NS_OK;
@ -217,19 +214,15 @@ nsCertificatePrincipal::InitFromPersistent(const char* aPrefName, const char* a
NS_IMETHODIMP
nsCertificatePrincipal::Init(const char* aCertificateID)
{
mCertificateID = nsCRT::strdup(aCertificateID);
if (!mCertificateID) return NS_ERROR_OUT_OF_MEMORY;
mCertificateID = aCertificateID;
return NS_OK;
}
nsCertificatePrincipal::nsCertificatePrincipal() : mCertificateID(nsnull),
mCommonName(nsnull)
nsCertificatePrincipal::nsCertificatePrincipal()
{
NS_INIT_ISUPPORTS();
}
nsCertificatePrincipal::~nsCertificatePrincipal()
{
PR_FREEIF(mCertificateID);
PR_FREEIF(mCommonName);
}

View File

@ -82,13 +82,11 @@ NS_IMETHODIMP
nsCodebasePrincipal::GetPreferences(char** aPrefName, char** aID,
char** aGrantedList, char** aDeniedList)
{
if (!mPrefName)
if (mPrefName.IsEmpty())
{
nsCAutoString s;
s.Assign("capability.principal.codebase.p");
s.AppendInt(mCapabilitiesOrdinal++);
s.Append(".id");
mPrefName = ToNewCString(s);
mPrefName.Assign("capability.principal.codebase.p");
mPrefName.AppendInt(mCapabilitiesOrdinal++);
mPrefName.Append(".id");
}
return nsBasePrincipal::GetPreferences(aPrefName, aID,
aGrantedList, aDeniedList);

View File

@ -5008,14 +5008,14 @@ nsXULPrototypeElement::Deserialize(nsIObjectInputStream* aStream,
if (! mAttributes)
return NS_ERROR_OUT_OF_MEMORY;
nsXPIDLString attributeValue, attributeNamespaceURI, attributeName;
nsAutoString attributeValue;
for (i = 0; i < mNumAttributes; ++i) {
rv |= aStream->Read32(&number);
mAttributes[i].mNodeInfo = do_QueryElementAt(aNodeInfos, number);
if (!mAttributes[i].mNodeInfo)
return NS_ERROR_UNEXPECTED;
rv |= aStream->ReadWStringZ(getter_Copies(attributeValue));
rv |= aStream->ReadString(attributeValue);
mAttributes[i].mValue.SetValue(attributeValue);
}
@ -5530,9 +5530,7 @@ nsXULPrototypeText::Deserialize(nsIObjectInputStream* aStream,
nsresult rv;
// Write basic prototype data
PRUnichar* str = nsnull;
rv = aStream->ReadWStringZ(&str);
mValue.Adopt(str);
rv = aStream->ReadString(mValue);
return rv;
}

View File

@ -950,9 +950,8 @@ nsXULPrototypeCache::StartFastLoad(nsIURI* aURI)
#endif
rv = NS_ERROR_UNEXPECTED;
} else {
nsXPIDLCString fileChromePath;
rv = objectInput->ReadStringZ(
getter_Copies(fileChromePath));
nsCAutoString fileChromePath;
rv = objectInput->ReadCString(fileChromePath);
if (NS_SUCCEEDED(rv) &&
!fileChromePath.Equals(chromePath)) {
rv = NS_ERROR_UNEXPECTED;

View File

@ -47,6 +47,7 @@
*/
#include "nsCOMPtr.h"
#include "nsAString.h"
#include "nsIObjectInputStream.h"
#include "nsIObjectOutputStream.h"
#include "nsIPrincipal.h"
@ -373,10 +374,10 @@ nsXULPrototypeDocument::Read(nsIObjectInputStream* aStream)
NS_ENSURE_TRUE(nodeInfos, rv);
rv |= aStream->Read32(&referenceCount);
nsXPIDLString namespaceURI, qualifiedName;
nsAutoString namespaceURI, qualifiedName;
for (i = 0; i < referenceCount; ++i) {
rv |= aStream->ReadWStringZ(getter_Copies(namespaceURI));
rv |= aStream->ReadWStringZ(getter_Copies(qualifiedName));
rv |= aStream->ReadString(namespaceURI);
rv |= aStream->ReadString(qualifiedName);
nsCOMPtr<nsINodeInfo> nodeInfo;
rv |= mNodeInfoManager->GetNodeInfo(qualifiedName, namespaceURI, *getter_AddRefs(nodeInfo));

View File

@ -98,15 +98,11 @@ nsSimpleURI::Read(nsIObjectInputStream* aStream)
{
nsresult rv;
nsXPIDLCString buf;
rv = aStream->ReadStringZ(getter_Copies(buf));
rv = aStream->ReadCString(mScheme);
if (NS_FAILED(rv)) return rv;
mScheme = buf;
rv = aStream->ReadStringZ(getter_Copies(buf));
rv = aStream->ReadCString(mPath);
if (NS_FAILED(rv)) return rv;
mPath = buf;
return NS_OK;
}

View File

@ -2325,7 +2325,6 @@ NS_IMETHODIMP
nsStandardURL::Read(nsIObjectInputStream *stream)
{
nsresult rv;
nsXPIDLCString buf;
rv = stream->Read32(&mURLType);
if (NS_FAILED(rv)) return rv;
@ -2350,9 +2349,8 @@ nsStandardURL::Read(nsIObjectInputStream *stream)
rv = stream->Read32((PRUint32 *) &mDefaultPort);
if (NS_FAILED(rv)) return rv;
rv = NS_ReadOptionalStringZ(stream, getter_Copies(buf));
rv = NS_ReadOptionalCString(stream, mSpec);
if (NS_FAILED(rv)) return rv;
mSpec = buf;
rv = ReadSegment(stream, mScheme);
if (NS_FAILED(rv)) return rv;
@ -2393,9 +2391,8 @@ nsStandardURL::Read(nsIObjectInputStream *stream)
rv = ReadSegment(stream, mRef);
if (NS_FAILED(rv)) return rv;
rv = NS_ReadOptionalStringZ(stream, getter_Copies(buf));
rv = NS_ReadOptionalCString(stream, mOriginCharset);
if (NS_FAILED(rv)) return rv;
mOriginCharset = buf;
return NS_OK;
}

View File

@ -639,9 +639,11 @@ nsCStringKey::Clone() const
nsCStringKey::nsCStringKey(nsIObjectInputStream* aStream, nsresult *aResult)
: mStr(nsnull), mStrLen(0), mOwnership(OWN)
{
nsresult rv = aStream->ReadStringZ(&mStr);
nsCAutoString str;
nsresult rv = aStream->ReadCString(str);
mStr = ToNewCString(str);
if (NS_SUCCEEDED(rv))
mStrLen = strlen(mStr);
mStrLen = str.Length();
*aResult = rv;
MOZ_COUNT_CTOR(nsCStringKey);
}
@ -759,9 +761,11 @@ nsStringKey::Clone() const
nsStringKey::nsStringKey(nsIObjectInputStream* aStream, nsresult *aResult)
: mStr(nsnull), mStrLen(0), mOwnership(OWN)
{
nsresult rv = aStream->ReadWStringZ(&mStr);
nsAutoString str;
nsresult rv = aStream->ReadString(str);
mStr = ToNewUnicode(str);
if (NS_SUCCEEDED(rv))
mStrLen = nsCRT::strlen(mStr);
mStrLen = str.Length();
*aResult = rv;
MOZ_COUNT_CTOR(nsStringKey);
}

View File

@ -40,6 +40,7 @@
#include <string.h>
#include "nsSupportsArray.h"
#include "nsSupportsArrayEnumerator.h"
#include "nsAString.h"
#include "nsIObjectInputStream.h"
#include "nsIObjectOutputStream.h"
#include <string.h>

View File

@ -291,11 +291,43 @@ nsBinaryInputStream::Read(char* aBuffer, PRUint32 aCount, PRUint32 *aNumRead)
return mInputStream->Read(aBuffer, aCount, aNumRead);
}
// when forwarding ReadSegments to mInputStream, we need to make sure
// 'this' is being passed to the writer each time. To do this, we need
// a thunking function which keeps the real input stream around.
// the closure wrapper
struct ReadSegmentsClosure {
nsIInputStream* mRealInputStream;
void* mRealClosure;
nsWriteSegmentFun mRealWriter;
};
// the thunking function
static NS_METHOD
ReadSegmentForwardingThunk(nsIInputStream* aStream,
void *aClosure,
const char* aFromSegment,
PRUint32 aToOffset,
PRUint32 aCount,
PRUint32 *aWriteCount)
{
ReadSegmentsClosure* thunkClosure =
NS_REINTERPRET_CAST(ReadSegmentsClosure*, aClosure);
return thunkClosure->mRealWriter(thunkClosure->mRealInputStream,
thunkClosure->mRealClosure,
aFromSegment, aToOffset,
aCount, aWriteCount);
}
NS_IMETHODIMP
nsBinaryInputStream::ReadSegments(nsWriteSegmentFun writer, void * closure, PRUint32 count, PRUint32 *_retval)
{
NS_NOTREACHED("ReadSegments");
return NS_ERROR_NOT_IMPLEMENTED;
ReadSegmentsClosure thunkClosure = { this, closure, writer };
return mInputStream->ReadSegments(ReadSegmentForwardingThunk, &thunkClosure, count, _retval);
}
NS_IMETHODIMP
@ -395,70 +427,86 @@ nsBinaryInputStream::ReadDouble(double* aDouble)
return Read64(NS_REINTERPRET_CAST(PRUint64*, aDouble));
}
static NS_METHOD
WriteSegmentToCString(nsIInputStream* aStream,
void *aClosure,
const char* aFromSegment,
PRUint32 aToOffset,
PRUint32 aCount,
PRUint32 *aWriteCount)
{
nsACString* outString = NS_STATIC_CAST(nsACString*,aClosure);
outString->Append(aFromSegment, aCount);
*aWriteCount = aCount;
return NS_OK;
}
NS_IMETHODIMP
nsBinaryInputStream::ReadStringZ(char* *aString)
nsBinaryInputStream::ReadCString(nsACString& aString)
{
nsresult rv;
PRUint32 length, bytesRead;
char *s;
rv = Read32(&length);
if (NS_FAILED(rv)) return rv;
s = NS_REINTERPRET_CAST(char*, nsMemory::Alloc(length + 1));
if (!s)
return NS_ERROR_OUT_OF_MEMORY;
rv = Read(s, length, &bytesRead);
aString.Truncate();
rv = ReadSegments(WriteSegmentToCString, &aString, length, &bytesRead);
if (NS_FAILED(rv)) return rv;
if (bytesRead != length) {
nsMemory::Free(s);
if (bytesRead != length)
return NS_ERROR_FAILURE;
}
s[length] = '\0';
*aString = s;
return NS_OK;
}
NS_IMETHODIMP
nsBinaryInputStream::ReadWStringZ(PRUnichar* *aString)
// same version of the above, but with correct casting and endian swapping
static NS_METHOD
WriteSegmentToString(nsIInputStream* aStream,
void *aClosure,
const char* aFromSegment,
PRUint32 aToOffset,
PRUint32 aCount,
PRUint32 *aWriteCount)
{
nsresult rv;
PRUint32 length, byteCount, bytesRead;
PRUnichar *ws;
rv = Read32(&length);
if (NS_FAILED(rv)) return rv;
byteCount = length * sizeof(PRUnichar);
ws = NS_REINTERPRET_CAST(PRUnichar*,
nsMemory::Alloc(byteCount + sizeof(PRUnichar)));
if (!ws)
return NS_ERROR_OUT_OF_MEMORY;
rv = Read(NS_REINTERPRET_CAST(char*, ws), byteCount, &bytesRead);
if (NS_FAILED(rv)) return rv;
if (bytesRead != byteCount) {
nsMemory::Free(ws);
return NS_ERROR_FAILURE;
}
nsAString* outString = NS_STATIC_CAST(nsAString*,aClosure);
const PRUnichar *unicodeSegment =
NS_REINTERPRET_CAST(const PRUnichar*, aFromSegment);
PRUint32 segmentLength = aCount / sizeof(PRUnichar);
// this sucks. we have to swap every 2 bytes on some machines
#ifdef IS_LITTLE_ENDIAN
for (PRUint32 i = 0; i < length; i++)
ws[i] = NS_SWAP16(ws[i]);
for (PRUint32 i = 0; i < segmentLength; i++)
outString->Append(PRUnichar(NS_SWAP16(unicodeSegment[i])));
#else
outString->Append(unicodeSegment, segmentLength);
#endif
ws[length] = 0;
*aString = ws;
*aWriteCount = aCount;
return NS_OK;
}
NS_IMETHODIMP
nsBinaryInputStream::ReadUtf8Z(PRUnichar* *aString)
nsBinaryInputStream::ReadString(nsAString& aString)
{
NS_NOTREACHED("ReadUtf8Z");
return NS_ERROR_NOT_IMPLEMENTED;
nsresult rv;
PRUint32 length, bytesRead;
rv = Read32(&length);
if (NS_FAILED(rv)) return rv;
aString.Truncate();
rv = ReadSegments(WriteSegmentToString, &aString, length, &bytesRead);
if (NS_FAILED(rv)) return rv;
if (bytesRead != length)
return NS_ERROR_FAILURE;
return NS_OK;
}
NS_IMETHODIMP
@ -512,26 +560,3 @@ nsBinaryInputStream::PutBuffer(char* aBuffer, PRUint32 aLength)
mBufferAccess->PutBuffer(aBuffer, aLength);
}
NS_COM nsresult
NS_NewBinaryOutputStream(nsIBinaryOutputStream* *aResult, nsIOutputStream* aDestStream)
{
NS_ENSURE_ARG_POINTER(aResult);
nsIBinaryOutputStream *stream = new nsBinaryOutputStream(aDestStream);
if (!stream)
return NS_ERROR_OUT_OF_MEMORY;
NS_ADDREF(stream);
*aResult = stream;
return NS_OK;
}
NS_COM nsresult
NS_NewBinaryInputStream(nsIBinaryInputStream* *aResult, nsIInputStream* aSrcStream)
{
NS_ENSURE_ARG_POINTER(aResult);
nsIBinaryInputStream *stream = new nsBinaryInputStream(aSrcStream);
if (!stream)
return NS_ERROR_OUT_OF_MEMORY;
NS_ADDREF(stream);
*aResult = stream;
return NS_OK;
}

View File

@ -23,6 +23,7 @@
#define nsBinaryStream_h___
#include "nsCOMPtr.h"
#include "nsAString.h"
#include "nsIObjectInputStream.h"
#include "nsIObjectOutputStream.h"
#include "nsIStreamBufferAccess.h"

View File

@ -743,9 +743,9 @@ nsFastLoadFileReader::ReadFooter(nsFastLoadFooter *aFooter)
if (NS_FAILED(rv))
return rv;
nsCAutoString filename;
for (i = 0, n = aFooter->mNumDependencies; i < n; i++) {
nsXPIDLCString filename;
rv = ReadStringZ(getter_Copies(filename));
rv = ReadCString(filename);
if (NS_FAILED(rv))
return rv;
@ -868,18 +868,16 @@ nsFastLoadFileReader::ReadMuxedDocumentInfo(nsFastLoadMuxedDocumentInfo *aInfo)
{
nsresult rv;
char *spec;
rv = ReadStringZ(&spec);
nsCAutoString spec;
rv = ReadCString(spec);
if (NS_FAILED(rv))
return rv;
rv = Read32(&aInfo->mInitialSegmentOffset);
if (NS_FAILED(rv)) {
nsMemory::Free((void*) spec);
if (NS_FAILED(rv))
return rv;
}
aInfo->mURISpec = spec;
aInfo->mURISpec = ToNewCString(spec);
return NS_OK;
}

View File

@ -50,18 +50,12 @@ interface nsIBinaryInputStream : nsIInputStream {
/**
* Read a NUL-terminated 8-bit char* string from a binary stream.
*/
string readStringZ();
ACString readCString();
/**
* Read a NUL-terminated 16-bit PRUnichar* string from a binary stream.
*/
wstring readWStringZ();
/**
* Read a NUL-terminated UTF8-encoded string from a binary stream, producing
* a NUL-terminated 16-bit PRUnichar* string argument as a result.
*/
wstring readUtf8Z();
AString readString();
/**
* Read an opaque byte array from a binary stream.
@ -75,29 +69,29 @@ NS_COM nsresult
NS_NewBinaryInputStream(nsIBinaryInputStream* *aResult, nsIInputStream* aSrcStream);
inline nsresult
NS_ReadOptionalStringZ(nsIBinaryInputStream* aStream, char* *aResult)
NS_ReadOptionalCString(nsIBinaryInputStream* aStream, nsACString& aResult)
{
PRBool nonnull;
nsresult rv = aStream->ReadBoolean(&nonnull);
if (NS_SUCCEEDED(rv)) {
if (nonnull)
rv = aStream->ReadStringZ(aResult);
rv = aStream->ReadCString(aResult);
else
*aResult = nsnull;
aResult.Truncate();
}
return rv;
}
inline nsresult
NS_ReadOptionalWStringZ(nsIBinaryInputStream* aStream, PRUnichar* *aResult)
NS_ReadOptionalString(nsIBinaryInputStream* aStream, nsAString& aResult)
{
PRBool nonnull;
nsresult rv = aStream->ReadBoolean(&nonnull);
if (NS_SUCCEEDED(rv)) {
if (nonnull)
rv = aStream->ReadWStringZ(aResult);
rv = aStream->ReadString(aResult);
else
*aResult = nsnull;
aResult.Truncate();
}
return rv;
}