diff --git a/embedding/browser/powerplant/source/CTextInputEventHandling.cpp b/embedding/browser/powerplant/source/CTextInputEventHandling.cpp index a0d8c928c5e..fed2766d633 100644 --- a/embedding/browser/powerplant/source/CTextInputEventHandling.cpp +++ b/embedding/browser/powerplant/source/CTextInputEventHandling.cpp @@ -29,72 +29,10 @@ #include "CTextInputEventHandler.h" #include "nsCRT.h" +#include "nsAutoBuffer.h" #pragma mark - -class nsSpillableStackBuffer -{ -protected: - - enum { - kStackBufferSize = 256 - - }; - -public: - - nsSpillableStackBuffer() - : mBufferPtr(mBuffer) - , mCurCapacity(kStackBufferSize) - { - } - - ~nsSpillableStackBuffer() - { - DeleteBuffer(); - } - - - PRBool EnsureCapacity(PRInt32 inCharsCapacity) - { - if (inCharsCapacity < mCurCapacity) - return PR_TRUE; - - if (inCharsCapacity > kStackBufferSize) - { - DeleteBuffer(); - mBufferPtr = (PRUnichar*)nsMemory::Alloc(inCharsCapacity * sizeof(PRUnichar)); - mCurCapacity = inCharsCapacity; - return (mBufferPtr != NULL); - } - - mCurCapacity = kStackBufferSize; - return PR_TRUE; - } - - PRUnichar* GetBuffer() { return mBufferPtr; } - - PRInt32 GetCapacity() { return mCurCapacity; } - -protected: - - void DeleteBuffer() - { - if (mBufferPtr != mBuffer) - { - nsMemory::Free(mBufferPtr); - mBufferPtr = mBuffer; - } - } - -protected: - - PRUnichar *mBufferPtr; - PRUnichar mBuffer[kStackBufferSize]; - PRInt32 mCurCapacity; - -}; - #pragma mark - @@ -131,15 +69,15 @@ OSStatus CTextInputEventHandler::GetText(EventRef inEvent, nsString& outString) if (neededSize > 0) { - nsSpillableStackBuffer buf; - if (! buf.EnsureCapacity(neededSize/sizeof(PRUnichar))) + nsAutoBuffer buf; + if (! buf.EnsureElemCapacity(neededSize/sizeof(PRUnichar))) return eventParameterNotFoundErr; err = ::GetEventParameter(inEvent, kEventParamTextInputSendText, typeUnicodeText, NULL, - neededSize, &neededSize, buf.GetBuffer()); + neededSize, &neededSize, buf.get()); if (noErr == err) - outString.Assign(buf.GetBuffer(), neededSize/sizeof(PRUnichar)); + outString.Assign(buf.get(), neededSize/sizeof(PRUnichar)); } return err; } diff --git a/gfx/src/gtk/nsFontMetricsXft.cpp b/gfx/src/gtk/nsFontMetricsXft.cpp index 768714b809f..7f3499e7fe6 100644 --- a/gfx/src/gtk/nsFontMetricsXft.cpp +++ b/gfx/src/gtk/nsFontMetricsXft.cpp @@ -63,6 +63,7 @@ #include "nsCompressedCharMap.h" #include "nsNetUtil.h" #include "nsClassHashtable.h" +#include "nsAutoBuffer.h" #include #include @@ -232,7 +233,8 @@ struct BoundingMetricsData { }; #endif /* MOZ_MATHML */ -class nsAutoBuffer; +#define AUTO_BUFFER_SIZE 3000 +typedef nsAutoBuffer nsAutoFcChar32Buffer; static int CalculateSlant (PRUint8 aStyle); static int CalculateWeight (PRUint16 aWeight); @@ -251,17 +253,17 @@ static const MozXftLangGroup* FindFCLangGroup (nsACString &aLangGroup); static void ConvertCharToUCS4 (const char *aString, PRUint32 aLength, - nsAutoBuffer &aOutBuffer, + nsAutoFcChar32Buffer &aOutBuffer, PRUint32 *aOutLen); static void ConvertUnicharToUCS4 (const PRUnichar *aString, PRUint32 aLength, - nsAutoBuffer &aOutBuffer, + nsAutoFcChar32Buffer &aOutBuffer, PRUint32 *aOutLen); static nsresult ConvertUCS4ToCustom (FcChar32 *aSrc, PRUint32 aSrcLen, PRUint32& aDestLen, nsIUnicodeEncoder *aConverter, PRBool aIsWide, - nsAutoBuffer& aResult); + nsAutoFcChar32Buffer &Result); #ifdef MOZ_WIDGET_GTK2 static void GdkRegionSetXftClip(GdkRegion *aGdkRegion, XftDraw *aDraw); @@ -273,7 +275,6 @@ static void GdkRegionSetXftClip(GdkRegion *aGdkRegion, XftDraw *aDraw); // that value instead of the requested size. #define FONT_MAX_FONT_SCALE 2 -#define AUTO_BUFFER_SIZE 3000 #define UCS2_REPLACEMENT 0xFFFD #define IS_NON_BMP(c) ((c) >> 16) @@ -300,18 +301,6 @@ private: XftGlyphFontSpec mSpecBuffer[BUFFER_LEN]; }; -// a helper class for automatic buffer allocation -class nsAutoBuffer { -public: - nsAutoBuffer(); - ~nsAutoBuffer(); - void* GetArray(PRInt32 aMinLen = 0); - -private: - char* mArray; - char mAutoArray[AUTO_BUFFER_SIZE]; - PRInt32 mCount; -}; PRLogModuleInfo *gXftFontLoad = nsnull; static int gNumInstances = 0; @@ -1452,14 +1441,15 @@ nsFontMetricsXft::EnumerateGlyphs(const PRUnichar *aString, void *aCallbackData) { PRUint32 len; - nsAutoBuffer charBuffer; - FcChar32 *chars; + nsAutoFcChar32Buffer charBuffer; + + NS_ENSURE_TRUE(aLen, NS_OK); ConvertUnicharToUCS4(aString, aLen, charBuffer, &len); - if (!len || !(chars = NS_STATIC_CAST(FcChar32 *, charBuffer.GetArray()))) + if (!len) return NS_ERROR_OUT_OF_MEMORY; - return EnumerateXftGlyphs(chars, len, aCallback, aCallbackData); + return EnumerateXftGlyphs(charBuffer.get(), len, aCallback, aCallbackData); } nsresult @@ -1469,15 +1459,16 @@ nsFontMetricsXft::EnumerateGlyphs(const char *aString, void *aCallbackData) { PRUint32 len; - nsAutoBuffer charBuffer; - FcChar32 *chars; + nsAutoFcChar32Buffer charBuffer; + + NS_ENSURE_TRUE(aLen, NS_OK); // Convert the incoming string into an array of UCS4 chars ConvertCharToUCS4(aString, aLen, charBuffer, &len); - if (!len || !(chars = NS_STATIC_CAST(FcChar32 *, charBuffer.GetArray()))) + if (!len) return NS_ERROR_OUT_OF_MEMORY; - return EnumerateXftGlyphs(chars, len, aCallback, aCallbackData); + return EnumerateXftGlyphs(charBuffer.get(), len, aCallback, aCallbackData); } void @@ -2062,7 +2053,7 @@ nsresult nsFontXftCustom::GetTextExtents32(const FcChar32 *aString, PRUint32 aLen, XGlyphInfo &aGlyphInfo) { - nsAutoBuffer buffer; + nsAutoFcChar32Buffer buffer; nsresult rv; PRUint32 destLen = aLen; PRBool isWide = (mFontInfo->mFontType == eFontTypeCustomWide); @@ -2073,9 +2064,7 @@ nsFontXftCustom::GetTextExtents32(const FcChar32 *aString, PRUint32 aLen, isWide, buffer); NS_ENSURE_SUCCESS(rv, rv); - FcChar32 *str = NS_STATIC_CAST(FcChar32 *, buffer.GetArray()); - if (!str) - return NS_ERROR_OUT_OF_MEMORY; + FcChar32 *str = buffer.get(); if (!mXftFont && !GetXftFont()) return NS_ERROR_NOT_AVAILABLE; @@ -2124,7 +2113,7 @@ nsFontXftCustom::DrawStringSpec(FcChar32* aString, PRUint32 aLen, void* aData) { nsresult rv = NS_OK; - nsAutoBuffer buffer; + nsAutoFcChar32Buffer buffer; PRUint32 destLen = aLen; PRBool isWide = (mFontInfo->mFontType == eFontTypeCustomWide); @@ -2147,7 +2136,7 @@ nsFontXftCustom::DrawStringSpec(FcChar32* aString, PRUint32 aLen, NS_ENSURE_SUCCESS(rv, rv); } - FcChar32 *str = NS_STATIC_CAST(FcChar32 *, buffer.GetArray()); + FcChar32 *str = buffer.get(); return nsFontXft::DrawStringSpec(str, destLen, aData); } @@ -2174,40 +2163,6 @@ nsFontXftCustom::SetFT_FaceCharmap(void) return NS_OK; } -// class nsAutoBuffer - -nsAutoBuffer::nsAutoBuffer() - : mArray(mAutoArray), - mCount(AUTO_BUFFER_SIZE) -{ -} - -nsAutoBuffer::~nsAutoBuffer() -{ - if (mArray && (mArray != mAutoArray)) { - delete [] mArray; - } -} - -void* nsAutoBuffer::GetArray(PRInt32 aMinLen) -{ - if (aMinLen > mCount) { - char* newArray = new char[aMinLen]; - if (!newArray) { - return nsnull; - } - - if (mArray != mAutoArray) { - delete [] mArray; - } - - mArray = newArray; - mCount = aMinLen; - } - - return (void *) mArray; -} - void nsAutoDrawSpecBuffer::Draw(nscoord x, nscoord y, XftFont *font, FT_UInt glyph) { @@ -2566,15 +2521,14 @@ FindFCLangGroup (nsACString &aLangGroup) /* static */ void ConvertCharToUCS4(const char *aString, PRUint32 aLength, - nsAutoBuffer &aOutBuffer, PRUint32 *aOutLen) + nsAutoFcChar32Buffer &aOutBuffer, PRUint32 *aOutLen) { *aOutLen = 0; FcChar32 *outBuffer; - // It takes 4bytes to represent a char. in UCS4. - outBuffer = NS_STATIC_CAST(FcChar32 *, aOutBuffer.GetArray(aLength * 4)); - if (!outBuffer) + if (!aOutBuffer.EnsureElemCapacity(aLength)) return; + outBuffer = aOutBuffer.get(); for (PRUint32 i = 0; i < aLength; ++i) { outBuffer[i] = PRUint8(aString[i]); // to convert char >= 0x80 correctly @@ -2588,16 +2542,14 @@ ConvertCharToUCS4(const char *aString, PRUint32 aLength, /* static */ void ConvertUnicharToUCS4(const PRUnichar *aString, PRUint32 aLength, - nsAutoBuffer &aOutBuffer, PRUint32 *aOutLen) + nsAutoFcChar32Buffer &aOutBuffer, PRUint32 *aOutLen) { *aOutLen = 0; FcChar32 *outBuffer; - // It takes 4bytes to represent a char. in UCS4. - outBuffer = NS_STATIC_CAST(FcChar32 *, aOutBuffer.GetArray(aLength * 4)); - - if (!outBuffer) + if (!aOutBuffer.EnsureElemCapacity(aLength)) return; + outBuffer = aOutBuffer.get(); PRUint32 outLen = 0; @@ -2856,7 +2808,7 @@ GetFontXftInfo(FcPattern* aPattern) nsresult ConvertUCS4ToCustom(FcChar32 *aSrc, PRUint32 aSrcLen, PRUint32& aDestLen, nsIUnicodeEncoder *aConverter, - PRBool aIsWide, nsAutoBuffer& aResult) + PRBool aIsWide, nsAutoFcChar32Buffer& aResult) { nsresult rv = NS_OK; @@ -2890,10 +2842,10 @@ ConvertUCS4ToCustom(FcChar32 *aSrc, PRUint32 aSrcLen, return NS_ERROR_UNEXPECTED; } - nsAutoBuffer medBuffer; - char *med = NS_STATIC_CAST(char *, medBuffer.GetArray(medLen)); - if (!med) + nsAutoBuffer medBuffer; + if (!medBuffer.EnsureElemCapacity(medLen)) return NS_ERROR_OUT_OF_MEMORY; + char *med = medBuffer.get(); // Convert utf16Src to font-specific encoding with mConverter. rv = converter->Convert(utf16Src, &utf16SrcLen, med, &medLen); @@ -2914,10 +2866,12 @@ ConvertUCS4ToCustom(FcChar32 *aSrc, PRUint32 aSrcLen, // Convert 16bit custom font codes to UCS4 ConvertUnicharToUCS4(NS_REINTERPRET_CAST(PRUnichar *, med), medLen >> 1, aResult, &aDestLen); + rv = aDestLen ? rv : NS_ERROR_OUT_OF_MEMORY; } else { // Convert 8bit custom font codes to UCS4 ConvertCharToUCS4(med, medLen, aResult, &aDestLen); + rv = aDestLen ? rv : NS_ERROR_OUT_OF_MEMORY; } return rv; diff --git a/gfx/src/windows/nsFontMetricsWin.cpp b/gfx/src/windows/nsFontMetricsWin.cpp index 848ac07ba70..532c9fdc29b 100644 --- a/gfx/src/windows/nsFontMetricsWin.cpp +++ b/gfx/src/windows/nsFontMetricsWin.cpp @@ -60,6 +60,7 @@ #include "prprf.h" #include "nsReadableUtils.h" #include "nsUnicodeRange.h" +#include "nsAutoBuffer.h" #define DEFAULT_TTF_SYMBOL_ENCODING "windows-1252" @@ -578,55 +579,9 @@ nsFontMetricsWin::FillLogFont(LOGFONT* logFont, PRInt32 aWeight, #undef CHAR_BUFFER_SIZE #define CHAR_BUFFER_SIZE 1024 -// template for helper classes for temporary buffer allocation -template class nsAutoArray { -public: - nsAutoArray(); - ~nsAutoArray(); - - T* GetArray(PRInt32 aMinLength = 0); - -private: - T* mArray; - T mAutoArray[sz]; - PRInt32 mCount; -}; - -template -nsAutoArray::nsAutoArray() - : mArray(mAutoArray), - mCount(sz) -{ -} - -template -nsAutoArray::~nsAutoArray() -{ - if (mArray && (mArray != mAutoArray)) { - delete [] mArray; - } -} - -template T* -nsAutoArray::GetArray(PRInt32 aMinCount) -{ - if (aMinCount > mCount) { - T* newArray = new T[aMinCount]; - if (!newArray) { - return nsnull; - } - if (mArray != mAutoArray) { - delete [] mArray; - } - mArray = newArray; - mCount = aMinCount; - } - return mArray; -} - -typedef nsAutoArray nsAutoFontDataBuffer; -typedef nsAutoArray nsAutoCharBuffer; -typedef nsAutoArray nsAutoChar16Buffer; +typedef nsAutoBuffer nsAutoFontDataBuffer; +typedef nsAutoBuffer nsAutoCharBuffer; +typedef nsAutoBuffer nsAutoChar16Buffer; static PRUint16 GetGlyphIndex(PRUint16 segCount, PRUint16* endCode, PRUint16* startCode, @@ -680,10 +635,11 @@ GetNAME(HDC aDC, nsString* aName, PRBool* aIsSymbolEncoding = nsnull) return eGetName_OtherError; } nsAutoFontDataBuffer buffer; - PRUint8* buf = buffer.GetArray(len); - if (!buf) { + if (!buffer.EnsureElemCapacity(len)) { return eGetName_OtherError; } + PRUint8* buf = buffer.get(); + DWORD newLen = GetFontData(aDC, NAME, 0, buf, len); if (newLen != len) { return eGetName_OtherError; @@ -774,10 +730,10 @@ GetSpaces(HDC aDC, PRUint32* aMaxGlyph, nsAutoFontDataBuffer& aIsSpace) if ((len == GDI_ERROR) || (!len)) { return NS_ERROR_FAILURE; } - PRUint8* buf = aIsSpace.GetArray(len); - if (!buf) { + if (!aIsSpace.EnsureElemCapacity(len)) { return NS_ERROR_OUT_OF_MEMORY; } + PRUint8* buf = aIsSpace.get(); DWORD newLen = GetFontData(aDC, LOCA, 0, buf, len); if (newLen != len) { return NS_ERROR_FAILURE; @@ -1398,8 +1354,8 @@ ConvertUnicodeToGlyph(const PRUnichar* aSrc, PRInt32 aSrcLength, return NS_ERROR_UNEXPECTED; } - char* str = aResult.GetArray(aDestLength); - if (!str) return NS_ERROR_OUT_OF_MEMORY; + if (!aResult.EnsureElemCapacity(aDestLength)) return NS_ERROR_OUT_OF_MEMORY; + char* str = aResult.get(); aConverter->Convert(aSrc, &aSrcLength, str, &aDestLength); @@ -1641,10 +1597,10 @@ nsFontMetricsWin::GetFontCCMAP(HDC aDC, const char* aShortName, return nsnull; } nsAutoFontDataBuffer buffer; - PRUint8* buf = buffer.GetArray(len); - if (!buf) { + if (!buffer.EnsureElemCapacity(len)) { return nsnull; } + PRUint8* buf = buffer.get(); DWORD newLen = GetFontData(aDC, CMAP, 0, buf, len); if (newLen != len) { return nsnull; @@ -1721,7 +1677,7 @@ nsFontMetricsWin::GetFontCCMAP(HDC aDC, const char* aShortName, PRUint32 maxGlyph; nsAutoFontDataBuffer isSpace; if (NS_SUCCEEDED(GetSpaces(aDC, &maxGlyph, isSpace))) { - ReadCMAPTableFormat4(buf+keepOffset, len-keepOffset, map, isSpace.GetArray(), maxGlyph); + ReadCMAPTableFormat4(buf+keepOffset, len-keepOffset, map, isSpace.get(), maxGlyph); ccmap = MapToCCMap(map); aCharset = DEFAULT_CHARSET; aFontType = eFontType_Unicode; @@ -1897,10 +1853,10 @@ GetGlyphIndices(HDC aDC, if ((len == GDI_ERROR) || (!len)) { return NS_ERROR_UNEXPECTED; } - buf = buffer.GetArray(len); - if (!buf) { + if (!buffer.EnsureElemCapacity(len)) { return NS_ERROR_OUT_OF_MEMORY; } + buf = buffer.get(); DWORD newLen = GetFontData(aDC, CMAP, 0, buf, len); if (newLen != len) { return NS_ERROR_UNEXPECTED; @@ -1993,10 +1949,10 @@ GetGlyphIndices(HDC aDC, PRUint16* idDelta = startCode + segCount; PRUint16* idRangeOffset = idDelta + segCount; - PRUnichar* result = aResult.GetArray(aLength); - if (!result) { + if (!aResult.EnsureElemCapacity(aLength)) { return NS_ERROR_OUT_OF_MEMORY; } + PRUnichar* result = aResult.get(); for (i = 0; i < aLength; ++i) { result[i] = GetGlyphIndex(segCount, endCode, startCode, idRangeOffset, idDelta, end, @@ -2105,7 +2061,7 @@ nsGlyphAgent::GetGlyphMetrics(HDC aDC, if (0 == aGlyphIndex) { // caller doesn't know the glyph index, so find it nsAutoChar16Buffer buf; if (NS_SUCCEEDED(GetGlyphIndices(aDC, nsnull, &aChar, 1, buf))) - aGlyphIndex = *(buf.GetArray()); + aGlyphIndex = *(buf.get()); } if (0 < aGlyphIndex) { return GetGlyphOutlineA(aDC, aGlyphIndex, GGO_METRICS | GGO_GLYPH_INDEX, aGlyphMetrics, 0, nsnull, &mMat); @@ -4243,7 +4199,7 @@ nsFontWinUnicode::GetBoundingMetrics(HDC aDC, } } - return GetBoundingMetricsCommon(aDC, mOverhangCorrection, aString, aLength, aBoundingMetrics, buffer.GetArray()); + return GetBoundingMetricsCommon(aDC, mOverhangCorrection, aString, aLength, aBoundingMetrics, buffer.get()); } #ifdef NS_DEBUG @@ -4281,9 +4237,9 @@ nsFontWinNonUnicode::GetWidth(HDC aDC, const PRUnichar* aString, SIZE size; if (!mIsWide) - ::GetTextExtentPoint32A(aDC, buffer.GetArray(), destLength, &size); + ::GetTextExtentPoint32A(aDC, buffer.get(), destLength, &size); else - ::GetTextExtentPoint32W(aDC, (PRUnichar*) buffer.GetArray(), destLength / 2, &size); + ::GetTextExtentPoint32W(aDC, (PRUnichar*) buffer.get(), destLength / 2, &size); size.cx -= mOverhangCorrection; return size.cx; @@ -4302,9 +4258,9 @@ nsFontWinNonUnicode::DrawString(HDC aDC, PRInt32 aX, PRInt32 aY, } if (!mIsWide) - NS_ExtTextOutA(aDC, this, aX, aY, 0, NULL, buffer.GetArray(), aLength, NULL); + NS_ExtTextOutA(aDC, this, aX, aY, 0, NULL, buffer.get(), aLength, NULL); else - NS_ExtTextOutW(aDC, this, aX, aY, 0, NULL, (PRUnichar*) buffer.GetArray(), destLength / 2, NULL); + NS_ExtTextOutW(aDC, this, aX, aY, 0, NULL, (PRUnichar*) buffer.get(), destLength / 2, NULL); } #ifdef MOZ_MATHML @@ -4332,7 +4288,7 @@ nsFontWinNonUnicode::GetBoundingMetrics(HDC aDC, if (gGlyphAgent.GetState() != eGlyphAgent_UNICODE) { // we are on a platform that doesn't implement GetGlyphOutlineW() // we need to use glyph indices - rv = GetGlyphIndices(aDC, &mCMAP, (PRUnichar*)buffer.GetArray(), destLength / 2, buf); + rv = GetGlyphIndices(aDC, &mCMAP, (PRUnichar*)buffer.get(), destLength / 2, buf); if (NS_FAILED(rv)) { return rv; } @@ -4340,12 +4296,12 @@ nsFontWinNonUnicode::GetBoundingMetrics(HDC aDC, // buffer.mBuffer is now a pseudo-Unicode string so that we can use // GetBoundingMetricsCommon() also used by nsFontWinUnicode. - return GetBoundingMetricsCommon(aDC, mOverhangCorrection, (PRUnichar*)buffer.GetArray(), - destLength / 2, aBoundingMetrics, buf.GetArray()); + return GetBoundingMetricsCommon(aDC, mOverhangCorrection, (PRUnichar*)buffer.get(), + destLength / 2, aBoundingMetrics, buf.get()); } - return GetBoundingMetricsCommonA(aDC, mOverhangCorrection, buffer.GetArray(), destLength, + return GetBoundingMetricsCommonA(aDC, mOverhangCorrection, buffer.get(), destLength, aBoundingMetrics); } @@ -4417,10 +4373,10 @@ SubstituteChars(PRBool aDisplayUnicode, if (NS_SUCCEEDED(res)) { *aCount = conv.Length(); if (*aCount > 0) { - result = aResult.GetArray(*aCount); - if (!result) { + if (!aResult.EnsureElemCapacity(*aCount)) { return NS_ERROR_OUT_OF_MEMORY; } + result = aResult.get(); PRUnichar* u = result; const char* c = conv.get(); for (; *c; ++c, ++u) { @@ -4432,8 +4388,8 @@ SubstituteChars(PRBool aDisplayUnicode, } // we reach here if we couldn't transliterate, so fallback to question marks - result = aResult.GetArray(aLength); - if (!result) return NS_ERROR_OUT_OF_MEMORY; + if (!aResult.EnsureElemCapacity(aLength)) return NS_ERROR_OUT_OF_MEMORY; + result = aResult.get(); for (PRUint32 i = 0; i < aLength; i++) { result[i] = NS_REPLACEMENT_CHAR; } @@ -4452,7 +4408,7 @@ nsFontWinSubstitute::GetWidth(HDC aDC, const PRUnichar* aString, if (NS_FAILED(rv) || !aLength) return 0; SIZE size; - ::GetTextExtentPoint32W(aDC, buffer.GetArray(), aLength, &size); + ::GetTextExtentPoint32W(aDC, buffer.get(), aLength, &size); size.cx -= mOverhangCorrection; return size.cx; @@ -4468,7 +4424,7 @@ nsFontWinSubstitute::DrawString(HDC aDC, PRInt32 aX, PRInt32 aY, nsresult rv = SubstituteChars(PR_FALSE, aString, aLength, buffer, &aLength); if (NS_FAILED(rv) || !aLength) return; - NS_ExtTextOutW(aDC, this, aX, aY, 0, NULL, buffer.GetArray(), aLength, NULL); + NS_ExtTextOutW(aDC, this, aX, aY, 0, NULL, buffer.get(), aLength, NULL); } #ifdef MOZ_MATHML @@ -4495,14 +4451,14 @@ nsFontWinSubstitute::GetBoundingMetrics(HDC aDC, if (gGlyphAgent.GetState() != eGlyphAgent_UNICODE) { // we are on a platform that doesn't implement GetGlyphOutlineW() // we better get all glyph indices in one swoop - rv = GetGlyphIndices(aDC, &mCMAP, buffer.GetArray(), aLength, buf); + rv = GetGlyphIndices(aDC, &mCMAP, buffer.get(), aLength, buf); if (NS_FAILED(rv)) { return rv; } } - return GetBoundingMetricsCommon(aDC, mOverhangCorrection, buffer.GetArray(), aLength, - aBoundingMetrics, buf.GetArray()); + return GetBoundingMetricsCommon(aDC, mOverhangCorrection, buffer.get(), aLength, + aBoundingMetrics, buf.get()); } #ifdef NS_DEBUG @@ -4655,9 +4611,9 @@ nsFontSubset::Convert(const PRUnichar* aString, PRUint32 aLength, // Get the number of bytes needed for the conversion int nb = WideCharToMultiByte(mCodePage, 0, aString, aLength, nsnull, 0, nsnull, nsnull); - if (!nb) return; - char* buf = aResult.GetArray(nb); - if (!buf) return; + + if (!nb || !aResult.EnsureElemCapacity(nb)) return; + char* buf = aResult.get(); // Convert the Unicode string to ANSI *aResultLength = WideCharToMultiByte(mCodePage, 0, aString, aLength, buf, nb, nsnull, nsnull); @@ -4670,7 +4626,7 @@ nsFontSubset::GetWidth(HDC aDC, const PRUnichar* aString, PRUint32 aLength) Convert(aString, aLength, buffer, &aLength); if (aLength) { SIZE size; - ::GetTextExtentPoint32A(aDC, buffer.GetArray(), aLength, &size); + ::GetTextExtentPoint32A(aDC, buffer.get(), aLength, &size); size.cx -= mOverhangCorrection; return size.cx; } @@ -4684,7 +4640,7 @@ nsFontSubset::DrawString(HDC aDC, PRInt32 aX, PRInt32 aY, nsAutoCharBuffer buffer; Convert(aString, aLength, buffer, &aLength); if (aLength) { - NS_ExtTextOutA(aDC, this, aX, aY, 0, NULL, buffer.GetArray(), aLength, NULL); + NS_ExtTextOutA(aDC, this, aX, aY, 0, NULL, buffer.get(), aLength, NULL); } } @@ -4699,7 +4655,7 @@ nsFontSubset::GetBoundingMetrics(HDC aDC, nsAutoCharBuffer buffer; Convert(aString, aLength, buffer, &aLength); if (aLength) { - return GetBoundingMetricsCommonA(aDC, mOverhangCorrection, buffer.GetArray(), aLength, + return GetBoundingMetricsCommonA(aDC, mOverhangCorrection, buffer.get(), aLength, aBoundingMetrics); } return NS_OK; @@ -4751,11 +4707,11 @@ nsFontSubsetSubstitute::Convert(const PRUnichar* aString, PRUint32 aLength, } if (!aLength) { // this is the case where the substitute string collapsed to nothingness - *(aResult.GetArray()) = '\0'; + *(aResult.get()) = '\0'; *aResultLength = 0; return; } - nsFontSubset::Convert(buffer.GetArray(), aLength, aResult, aResultLength); + nsFontSubset::Convert(buffer.get(), aLength, aResult, aResultLength); } nsFontWinA::nsFontWinA(LOGFONT* aLogFont, HFONT aFont, PRUint16* aCCMap) diff --git a/xpcom/io/nsLocalFileOSX.cpp b/xpcom/io/nsLocalFileOSX.cpp index a14cd077ee3..50634c48129 100644 --- a/xpcom/io/nsLocalFileOSX.cpp +++ b/xpcom/io/nsLocalFileOSX.cpp @@ -51,6 +51,7 @@ #include "MoreFilesX.h" #include "FSCopyObject.h" +#include "nsAutoBuffer.h" // Mac Includes #include @@ -247,65 +248,7 @@ public: } }; -#pragma mark - -#pragma mark [StBuffer] - -template -class StBuffer -{ -public: - - StBuffer() : - mBufferPtr(mBuffer), - mCurElemCapacity(kStackBufferNumElems) - { - } - - ~StBuffer() - { - DeleteBuffer(); - } - - PRBool EnsureElemCapacity(PRInt32 inElemCapacity) - { - if (inElemCapacity <= mCurElemCapacity) - return PR_TRUE; - - if (inElemCapacity > kStackBufferNumElems) - { - DeleteBuffer(); - mBufferPtr = (T*)malloc(inElemCapacity * sizeof(T)); - mCurElemCapacity = inElemCapacity; - return (mBufferPtr != NULL); - } - - mCurElemCapacity = kStackBufferNumElems; - return PR_TRUE; - } - - T* get() { return mBufferPtr; } - - PRInt32 GetElemCapacity() { return mCurElemCapacity; } - -protected: - - void DeleteBuffer() - { - if (mBufferPtr != mBuffer) - { - free(mBufferPtr); - mBufferPtr = mBuffer; - } - } - -protected: - enum { kStackBufferNumElems = 512 }; - - T *mBufferPtr; - T mBuffer[kStackBufferNumElems]; - PRInt32 mCurElemCapacity; -}; - +#define FILENAME_BUFFER_SIZE 512 //***************************************************************************** // nsLocalFile @@ -443,7 +386,7 @@ NS_IMETHODIMP nsLocalFile::Create(PRUint32 type, PRUint32 permissions) CFURLRef pathURLRef = mBaseRef; FSRef pathFSRef; CFStringRef leafStrRef = nsnull; - StBuffer buffer; + nsAutoBuffer buffer; Boolean success; // Work backwards through the path to find the last node which @@ -2040,7 +1983,7 @@ nsresult nsLocalFile::CFStringReftoUTF8(CFStringRef aInStrRef, nsACString& aOutS CFIndex charsConverted = ::CFStringGetBytes(aInStrRef, CFRangeMake(0, inStrLen), kCFStringEncodingUTF8, 0, PR_FALSE, nsnull, 0, &usedBufLen); if (charsConverted == inStrLen) { - StBuffer buffer; + nsAutoBuffer buffer; if (buffer.EnsureElemCapacity(usedBufLen + 1)) { ::CFStringGetBytes(aInStrRef, CFRangeMake(0, inStrLen), kCFStringEncodingUTF8, 0, false, buffer.get(), usedBufLen, &usedBufLen); @@ -2228,7 +2171,7 @@ static void CopyUTF8toUTF16NFC(const nsACString& aSrc, nsAString& aResult) if (chars) aResult.Assign(chars, length); else { - StBuffer buffer; + nsAutoBuffer buffer; if (!buffer.EnsureElemCapacity(length)) CopyUTF8toUTF16(aSrc, aResult); else { diff --git a/xpcom/obsolete/nsFileSpecUnix.cpp b/xpcom/obsolete/nsFileSpecUnix.cpp index b81a1e84f16..bb3c7274940 100644 --- a/xpcom/obsolete/nsFileSpecUnix.cpp +++ b/xpcom/obsolete/nsFileSpecUnix.cpp @@ -48,7 +48,7 @@ #include "xpcom-private.h" #include "nsError.h" #include "prio.h" /* for PR_Rename */ -#include "nsAutoPtr.h" +#include "nsAutoBuffer.h" #if defined(_SCO_DS) #define _SVID3 /* for statvfs.h */ @@ -687,9 +687,13 @@ static void CopyUTF8toUTF16NFC(const nsACString& aSrc, nsAString& aResult) if (chars) aResult.Assign(chars, length); else { - nsAutoArrayPtr buffer(new UniChar[length]); - CFStringGetCharacters(inStr, CFRangeMake(0, length), buffer); - aResult.Assign(buffer, length); + nsAutoBuffer buffer; + if (buffer.EnsureElemCapacity(length)) { + CFStringGetCharacters(inStr, CFRangeMake(0, length), buffer.get()); + aResult.Assign(buffer, length); + } + else + CopyUTF8toUTF16(aSrc, aResult); } CFRelease(inStr); } diff --git a/xpfe/components/bookmarks/src/nsBookmarksService.cpp b/xpfe/components/bookmarks/src/nsBookmarksService.cpp index 3e40039416b..ec63fef7343 100644 --- a/xpfe/components/bookmarks/src/nsBookmarksService.cpp +++ b/xpfe/components/bookmarks/src/nsBookmarksService.cpp @@ -95,10 +95,12 @@ #include "nsICollation.h" #include "nsVoidArray.h" #include "nsUnicharUtils.h" +#include "nsAutoBuffer.h" #include "nsIParser.h" // for kCharsetFromBookmarks + #ifdef XP_WIN #include #include @@ -414,67 +416,6 @@ bm_ReleaseGlobals() } } - -class nsSpillableStackBuffer -{ -protected: - - enum { - kStackBufferSize = 256 - }; - -public: - - nsSpillableStackBuffer() : - mBufferPtr(mBuffer), - mCurCapacity(kStackBufferSize) - { } - - ~nsSpillableStackBuffer() - { - DeleteBuffer(); - } - - PRBool EnsureCapacity(PRInt32 inCharsCapacity) - { - if (inCharsCapacity < mCurCapacity) - return PR_TRUE; - - if (inCharsCapacity > kStackBufferSize) - { - DeleteBuffer(); - mBufferPtr = (PRUnichar*)nsMemory::Alloc(inCharsCapacity * sizeof(PRUnichar)); - mCurCapacity = inCharsCapacity; - return (mBufferPtr != NULL); - } - - mCurCapacity = kStackBufferSize; - return PR_TRUE; - } - - PRUnichar* GetBuffer() { return mBufferPtr; } - PRInt32 GetCapacity() { return mCurCapacity; } - -protected: - - void DeleteBuffer() - { - if (mBufferPtr != mBuffer) - { - nsMemory::Free(mBufferPtr); - mBufferPtr = mBuffer; - } - } - -protected: - - PRUnichar *mBufferPtr; - PRUnichar mBuffer[kStackBufferSize]; - PRInt32 mCurCapacity; - -}; - - //////////////////////////////////////////////////////////////////////// /** @@ -785,17 +726,17 @@ BookmarkParser::DecodeBuffer(nsString &line, char *buf, PRUint32 aLength) PRInt32 unicharBufLen = 0; mUnicodeDecoder->GetMaxLength(aBuffer, aLength, &unicharBufLen); - nsSpillableStackBuffer stackBuffer; - if (!stackBuffer.EnsureCapacity(unicharBufLen + 1)) + nsAutoBuffer stackBuffer; + if (!stackBuffer.EnsureElemCapacity(unicharBufLen + 1)) return NS_ERROR_OUT_OF_MEMORY; do { PRInt32 srcLength = aLength; PRInt32 unicharLength = unicharBufLen; - PRUnichar *unichars = stackBuffer.GetBuffer(); + PRUnichar *unichars = stackBuffer.get(); - rv = mUnicodeDecoder->Convert(aBuffer, &srcLength, stackBuffer.GetBuffer(), &unicharLength); + rv = mUnicodeDecoder->Convert(aBuffer, &srcLength, stackBuffer.get(), &unicharLength); unichars[unicharLength]=0; //add this since the unicode converters can't be trusted to do so. // Move the nsParser.cpp 00 -> space hack to here so it won't break UCS2 file