Bug 943511 - Remove allocator parameters from methods using nsSegmentedBuffer. r=bsmedberg

This commit is contained in:
Alessio Placitelli 2013-12-11 14:14:56 -05:00
parent 8f85f15f16
commit dbb81a3eba
9 changed files with 26 additions and 180 deletions

View File

@ -129,7 +129,7 @@ BackgroundFileSaver::Init()
rv = NS_NewPipe2(getter_AddRefs(mPipeInputStream),
getter_AddRefs(mPipeOutputStream), true, true, 0,
HasInfiniteBuffer() ? UINT32_MAX : 0, nullptr);
HasInfiniteBuffer() ? UINT32_MAX : 0);
NS_ENSURE_SUCCESS(rv, rv);
rv = NS_GetCurrentThread(getter_AddRefs(mControlThread));

View File

@ -171,7 +171,7 @@ TestWriteObject() {
= do_CreateInstance("@mozilla.org/storagestream;1");
NS_ENSURE_ARG_POINTER(storageStream);
rv = storageStream->Init(256, (uint32_t) -1, nullptr);
rv = storageStream->Init(256, (uint32_t) -1);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIObjectOutputStream> objectOutput

View File

@ -7,7 +7,6 @@
interface nsIAsyncInputStream;
interface nsIAsyncOutputStream;
interface nsIMemory;
/**
* nsIPipe represents an in-process buffer that can be read using nsIInputStream
@ -37,7 +36,7 @@ interface nsIMemory;
* references to the pipe's input and output end. In which case, the pipe is
* automatically closed when the respective pipe ends are released.
*/
[scriptable, uuid(f4211abc-61b3-11d4-9877-00c04fa0cf4a)]
[scriptable, uuid(25d0de93-685e-4ea4-95d3-d884e31df63c)]
interface nsIPipe : nsISupports
{
/**
@ -55,15 +54,11 @@ interface nsIPipe : nsISupports
* "infinite" space. This mode can be useful in some cases, but
* should always be used with caution. The default value for this
* parameter is a finite value.
* @param segmentAllocator
* pass reference to nsIMemory to have all pipe allocations use this
* allocator (pass null to use the default allocator)
*/
void init(in boolean nonBlockingInput,
in boolean nonBlockingOutput,
in unsigned long segmentSize,
in unsigned long segmentCount,
in nsIMemory segmentAllocator);
in unsigned long segmentCount);
/**
* The pipe's input end, which also implements nsISearchableInputStream.
@ -129,9 +124,6 @@ class nsIOutputStream;
* passing UINT32_MAX here causes the pipe to have "infinite" space.
* this mode can be useful in some cases, but should always be used with
* caution. the default value for this parameter is a finite value.
* @param segmentAlloc
* pass reference to nsIMemory to have all pipe allocations use this
* allocator (pass null to use the default allocator)
*/
extern nsresult
NS_NewPipe2(nsIAsyncInputStream **pipeIn,
@ -139,8 +131,7 @@ NS_NewPipe2(nsIAsyncInputStream **pipeIn,
bool nonBlockingInput = false,
bool nonBlockingOutput = false,
uint32_t segmentSize = 0,
uint32_t segmentCount = 0,
nsIMemory *segmentAlloc = nullptr);
uint32_t segmentCount = 0);
/**
* NS_NewPipe
@ -166,9 +157,6 @@ NS_NewPipe2(nsIAsyncInputStream **pipeIn,
* true specifies non-blocking input stream behavior
* @param nonBlockingOutput
* true specifies non-blocking output stream behavior
* @param segmentAlloc
* pass reference to nsIMemory to have all pipe allocations use this
* allocator (pass null to use the default allocator)
*/
extern nsresult
NS_NewPipe(nsIInputStream **pipeIn,
@ -176,7 +164,6 @@ NS_NewPipe(nsIInputStream **pipeIn,
uint32_t segmentSize = 0,
uint32_t maxSize = 0,
bool nonBlockingInput = false,
bool nonBlockingOutput = false,
nsIMemory *segmentAlloc = nullptr);
bool nonBlockingOutput = false);
%}

View File

@ -5,7 +5,6 @@
#include "nsISupports.idl"
interface nsIMemory;
interface nsIInputStream;
interface nsIOutputStream;
@ -15,7 +14,7 @@ interface nsIOutputStream;
* can be created to read the data from the buffer non-destructively.
*/
[scriptable, uuid(604ad9d0-753e-11d3-90ca-34278643278f)]
[scriptable, uuid(44a200fe-6c2b-4b41-b4e3-63e8c14e7c0d)]
interface nsIStorageStream : nsISupports
{
/**
@ -28,11 +27,8 @@ interface nsIStorageStream : nsISupports
* @param maxSize
* Maximum total size of this stream. length will always be less
* than or equal to this value. Passing UINT32_MAX is safe.
* @param segmentAllocator
* Which allocator to use for the segments. May be null, in which
* case a default allocator will be used.
*/
void init(in uint32_t segmentSize, in uint32_t maxSize, in nsIMemory segmentAllocator);
void init(in uint32_t segmentSize, in uint32_t maxSize);
/**
* Get a reference to the one and only output stream for this instance.

View File

@ -322,8 +322,7 @@ NS_IMETHODIMP
nsPipe::Init(bool nonBlockingIn,
bool nonBlockingOut,
uint32_t segmentSize,
uint32_t segmentCount,
nsIMemory *segmentAlloc)
uint32_t segmentCount)
{
mInited = true;
@ -337,7 +336,7 @@ nsPipe::Init(bool nonBlockingIn,
if (segmentCount > maxCount)
segmentCount = maxCount;
nsresult rv = mBuffer.Init(segmentSize, segmentSize * segmentCount, segmentAlloc);
nsresult rv = mBuffer.Init(segmentSize, segmentSize * segmentCount);
if (NS_FAILED(rv))
return rv;
@ -1242,8 +1241,7 @@ NS_NewPipe(nsIInputStream **pipeIn,
uint32_t segmentSize,
uint32_t maxSize,
bool nonBlockingInput,
bool nonBlockingOutput,
nsIMemory *segmentAlloc)
bool nonBlockingOutput)
{
if (segmentSize == 0)
segmentSize = DEFAULT_SEGMENT_SIZE;
@ -1258,7 +1256,7 @@ NS_NewPipe(nsIInputStream **pipeIn,
nsIAsyncInputStream *in;
nsIAsyncOutputStream *out;
nsresult rv = NS_NewPipe2(&in, &out, nonBlockingInput, nonBlockingOutput,
segmentSize, segmentCount, segmentAlloc);
segmentSize, segmentCount);
if (NS_FAILED(rv)) return rv;
*pipeIn = in;
@ -1272,8 +1270,7 @@ NS_NewPipe2(nsIAsyncInputStream **pipeIn,
bool nonBlockingInput,
bool nonBlockingOutput,
uint32_t segmentSize,
uint32_t segmentCount,
nsIMemory *segmentAlloc)
uint32_t segmentCount)
{
nsresult rv;
@ -1284,8 +1281,7 @@ NS_NewPipe2(nsIAsyncInputStream **pipeIn,
rv = pipe->Init(nonBlockingInput,
nonBlockingOutput,
segmentSize,
segmentCount,
segmentAlloc);
segmentCount);
if (NS_FAILED(rv)) {
NS_ADDREF(pipe);
NS_RELEASE(pipe);

View File

@ -7,8 +7,7 @@
#include "nsMemory.h"
nsresult
nsSegmentedBuffer::Init(uint32_t segmentSize, uint32_t maxSize,
nsIMemory* allocator)
nsSegmentedBuffer::Init(uint32_t segmentSize, uint32_t maxSize)
{
if (mSegmentArrayCount != 0)
return NS_ERROR_FAILURE; // initialized more than once

View File

@ -22,8 +22,7 @@ public:
}
nsresult Init(uint32_t segmentSize, uint32_t maxSize,
nsIMemory* allocator = nullptr);
nsresult Init(uint32_t segmentSize, uint32_t maxSize);
char* AppendNewSegment(); // pushes at end

View File

@ -67,8 +67,7 @@ NS_IMPL_ISUPPORTS2(nsStorageStream,
nsIOutputStream)
NS_IMETHODIMP
nsStorageStream::Init(uint32_t segmentSize, uint32_t maxSize,
nsIMemory *segmentAllocator)
nsStorageStream::Init(uint32_t segmentSize, uint32_t maxSize)
{
mSegmentedBuffer = new nsSegmentedBuffer();
if (!mSegmentedBuffer)
@ -81,7 +80,7 @@ nsStorageStream::Init(uint32_t segmentSize, uint32_t maxSize,
if (mSegmentSize != ((uint32_t)1 << mSegmentSizeLog2))
return NS_ERROR_INVALID_ARG;
return mSegmentedBuffer->Init(segmentSize, maxSize, segmentAllocator);
return mSegmentedBuffer->Init(segmentSize, maxSize);
}
NS_IMETHODIMP
@ -531,7 +530,7 @@ NS_NewStorageStream(uint32_t segmentSize, uint32_t maxSize, nsIStorageStream **r
if (!storageStream) return NS_ERROR_OUT_OF_MEMORY;
NS_ADDREF(storageStream);
nsresult rv = storageStream->Init(segmentSize, maxSize, nullptr);
nsresult rv = storageStream->Init(segmentSize, maxSize);
if (NS_FAILED(rv)) {
NS_RELEASE(storageStream);
return rv;

View File

@ -17,8 +17,7 @@ nsresult TP_NewPipe2(nsIAsyncInputStream** input,
bool nonBlockingInput,
bool nonBlockingOutput,
uint32_t segmentSize,
uint32_t segmentCount,
nsIMemory* segmentAlloc)
uint32_t segmentCount)
{
nsCOMPtr<nsIPipe> pipe = do_CreateInstance("@mozilla.org/pipe;1");
if (!pipe)
@ -27,8 +26,7 @@ nsresult TP_NewPipe2(nsIAsyncInputStream** input,
nsresult rv = pipe->Init(nonBlockingInput,
nonBlockingOutput,
segmentSize,
segmentCount,
segmentAlloc);
segmentCount);
if (NS_FAILED(rv))
return rv;
@ -38,146 +36,18 @@ nsresult TP_NewPipe2(nsIAsyncInputStream** input,
return NS_OK;
}
/**
* Allocator can allocate exactly count * size bytes, stored at mMemory;
* immediately after the end of this is a byte-map of 0/1 values indicating
* which <size>-byte locations in mMemory are empty and which are filled.
* Pretty stupid, but enough to test bug 394692.
*/
class BackwardsAllocator MOZ_FINAL : public nsIMemory
{
public:
BackwardsAllocator()
: mMemory(0),
mIndex(0xFFFFFFFF),
mCount(0xFFFFFFFF),
mSize(0)
{ }
~BackwardsAllocator()
{
delete [] mMemory;
}
nsresult Init(uint32_t count, size_t size);
NS_DECL_ISUPPORTS
NS_DECL_NSIMEMORY
private:
uint32_t previous(uint32_t i)
{
if (i == 0)
return mCount - 1;
return i - 1;
}
private:
uint8_t* mMemory;
uint32_t mIndex;
uint32_t mCount;
size_t mSize;
};
NS_IMPL_ISUPPORTS1(BackwardsAllocator, nsIMemory)
nsresult BackwardsAllocator::Init(uint32_t count, size_t size)
{
if (mMemory)
{
fail("allocator already initialized!");
return NS_ERROR_ALREADY_INITIALIZED;
}
mMemory = new uint8_t[count * size + count];
if (!mMemory)
{
fail("failed to allocate mMemory!");
return NS_ERROR_OUT_OF_MEMORY;
}
memset(mMemory, 0, count * size + count);
mIndex = 0;
mCount = count;
mSize = size;
return NS_OK;
}
NS_IMETHODIMP_(void*) BackwardsAllocator::Alloc(size_t size)
{
if (size != mSize)
{
NS_ERROR("umm, why would this be reached for this test?");
return nullptr;
}
uint32_t index = mIndex;
while ((index = previous(index)) != mIndex)
{
if (mMemory[mSize * mCount + index] == 1)
continue;
mMemory[mSize * mCount + index] = 1;
mIndex = index;
return &mMemory[mSize * index];
}
NS_ERROR("shouldn't reach here in this test");
return nullptr;
}
NS_IMETHODIMP_(void*) BackwardsAllocator::Realloc(void* ptr, size_t newSize)
{
NS_ERROR("shouldn't reach here in this test");
return nullptr;
}
NS_IMETHODIMP_(void) BackwardsAllocator::Free(void* ptr)
{
uint8_t* p = static_cast<uint8_t*>(ptr);
if (p)
mMemory[mCount * mSize + (p - mMemory) / mSize] = 0;
}
NS_IMETHODIMP BackwardsAllocator::HeapMinimize(bool immediate)
{
return NS_OK;
}
NS_IMETHODIMP BackwardsAllocator::IsLowMemory(bool* retval)
{
*retval = false;
return NS_OK;
}
NS_IMETHODIMP BackwardsAllocator::IsLowMemoryPlatform(bool* retval)
{
*retval = false;
return NS_OK;
}
nsresult TestBackwardsAllocator()
nsresult TestPipe()
{
const uint32_t SEGMENT_COUNT = 10;
const uint32_t SEGMENT_SIZE = 10;
nsRefPtr<BackwardsAllocator> allocator = new BackwardsAllocator();
if (!allocator)
{
fail("Allocation of BackwardsAllocator failed!");
return NS_ERROR_OUT_OF_MEMORY;
}
nsresult rv = allocator->Init(SEGMENT_COUNT, SEGMENT_SIZE);
if (NS_FAILED(rv))
return rv;
nsCOMPtr<nsIAsyncInputStream> input;
nsCOMPtr<nsIAsyncOutputStream> output;
rv = TP_NewPipe2(getter_AddRefs(input),
nsresult rv = TP_NewPipe2(getter_AddRefs(input),
getter_AddRefs(output),
false,
false,
SEGMENT_SIZE, SEGMENT_COUNT, allocator);
SEGMENT_SIZE, SEGMENT_COUNT);
if (NS_FAILED(rv))
{
fail("TP_NewPipe2 failed: %x", rv);
@ -227,7 +97,7 @@ nsresult TestBackwardsAllocator()
return NS_ERROR_FAILURE;
}
passed("TestBackwardsAllocator");
passed("TestPipe");
return NS_OK;
}
@ -239,7 +109,7 @@ int main(int argc, char** argv)
int rv = 0;
if (NS_FAILED(TestBackwardsAllocator()))
if (NS_FAILED(TestPipe()))
rv = 1;
return rv;