Bug 943511 - Use fallible allocations in nsSegmentedBuffer::AppendNewSegment. r=bsmedberg

This commit is contained in:
Alessio Placitelli 2013-12-11 14:14:28 -05:00
parent c4853d63c4
commit 9f52de8940
2 changed files with 6 additions and 15 deletions

View File

@ -14,13 +14,6 @@ nsSegmentedBuffer::Init(uint32_t segmentSize, uint32_t maxSize,
return NS_ERROR_FAILURE; // initialized more than once
mSegmentSize = segmentSize;
mMaxSize = maxSize;
mSegAllocator = allocator;
if (mSegAllocator == nullptr) {
mSegAllocator = nsMemory::GetGlobalMemoryService();
}
else {
NS_ADDREF(mSegAllocator);
}
#if 0 // testing...
mSegmentArrayCount = 2;
#else
@ -68,7 +61,7 @@ nsSegmentedBuffer::AppendNewSegment()
mSegmentArrayCount = newArraySize;
}
char* seg = (char*)mSegAllocator->Alloc(mSegmentSize);
char* seg = (char*)moz_malloc(mSegmentSize);
if (seg == nullptr) {
return nullptr;
}
@ -81,7 +74,7 @@ bool
nsSegmentedBuffer::DeleteFirstSegment()
{
NS_ASSERTION(mSegmentArray[mFirstSegmentIndex] != nullptr, "deleting bad segment");
(void)mSegAllocator->Free(mSegmentArray[mFirstSegmentIndex]);
moz_free(mSegmentArray[mFirstSegmentIndex]);
mSegmentArray[mFirstSegmentIndex] = nullptr;
int32_t last = ModSegArraySize(mLastSegmentIndex - 1);
if (mFirstSegmentIndex == last) {
@ -99,7 +92,7 @@ nsSegmentedBuffer::DeleteLastSegment()
{
int32_t last = ModSegArraySize(mLastSegmentIndex - 1);
NS_ASSERTION(mSegmentArray[last] != nullptr, "deleting bad segment");
(void)mSegAllocator->Free(mSegmentArray[last]);
moz_free(mSegmentArray[last]);
mSegmentArray[last] = nullptr;
mLastSegmentIndex = last;
return (bool)(mLastSegmentIndex == mFirstSegmentIndex);
@ -111,7 +104,7 @@ nsSegmentedBuffer::ReallocLastSegment(size_t newSize)
int32_t last = ModSegArraySize(mLastSegmentIndex - 1);
NS_ASSERTION(mSegmentArray[last] != nullptr, "realloc'ing bad segment");
char *newSegment =
(char*)mSegAllocator->Realloc(mSegmentArray[last], newSize);
(char*)moz_realloc(mSegmentArray[last], newSize);
if (newSegment) {
mSegmentArray[last] = newSegment;
return true;
@ -126,7 +119,7 @@ nsSegmentedBuffer::Empty()
if (mSegmentArray) {
for (uint32_t i = 0; i < mSegmentArrayCount; i++) {
if (mSegmentArray[i])
mSegAllocator->Free(mSegmentArray[i]);
moz_free(mSegmentArray[i]);
}
nsMemory::Free(mSegmentArray);
mSegmentArray = nullptr;

View File

@ -13,13 +13,12 @@ class nsSegmentedBuffer
public:
nsSegmentedBuffer()
: mSegmentSize(0), mMaxSize(0),
mSegAllocator(nullptr), mSegmentArray(nullptr),
mSegmentArray(nullptr),
mSegmentArrayCount(0),
mFirstSegmentIndex(0), mLastSegmentIndex(0) {}
~nsSegmentedBuffer() {
Empty();
NS_IF_RELEASE(mSegAllocator);
}
@ -72,7 +71,6 @@ protected:
protected:
uint32_t mSegmentSize;
uint32_t mMaxSize;
nsIMemory* mSegAllocator;
char** mSegmentArray;
uint32_t mSegmentArrayCount;
int32_t mFirstSegmentIndex;