Bug 943511 - Remove allocator parameters from methods using nsSegmentedBuffer. r=bsmedberg
authorAlessio Placitelli <alessio.placitelli@gmail.com>
Wed, 11 Dec 2013 14:14:56 -0500
changeset 175994 d48fc2819410b0fdb21bf2fb08101c0c7e0071ef
parent 175993 20b94cfce30c54a065c811e10e1fad16faa9bac2
child 175995 1e3bcdef8157b765a4976ea658725b8f9b6f497c
push id3343
push userffxbld
push dateMon, 17 Mar 2014 21:55:32 +0000
treeherdermozilla-beta@2f7d3415f79f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbsmedberg
bugs943511
milestone29.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 943511 - Remove allocator parameters from methods using nsSegmentedBuffer. r=bsmedberg
netwerk/base/src/BackgroundFileSaver.cpp
startupcache/test/TestStartupCache.cpp
xpcom/io/nsIPipe.idl
xpcom/io/nsIStorageStream.idl
xpcom/io/nsPipe3.cpp
xpcom/io/nsSegmentedBuffer.cpp
xpcom/io/nsSegmentedBuffer.h
xpcom/io/nsStorageStream.cpp
xpcom/tests/TestPipe.cpp
--- a/netwerk/base/src/BackgroundFileSaver.cpp
+++ b/netwerk/base/src/BackgroundFileSaver.cpp
@@ -124,17 +124,17 @@ nsresult
 BackgroundFileSaver::Init()
 {
   MOZ_ASSERT(NS_IsMainThread(), "This should be called on the main thread");
 
   nsresult rv;
 
   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));
   NS_ENSURE_SUCCESS(rv, rv);
 
   rv = NS_NewThread(getter_AddRefs(mWorkerThread));
   NS_ENSURE_SUCCESS(rv, rv);
 
--- a/startupcache/test/TestStartupCache.cpp
+++ b/startupcache/test/TestStartupCache.cpp
@@ -166,17 +166,17 @@ TestWriteObject() {
   // NewObjectOutputWrappedStorageStream, but that uses
   // StartupCache::GetSingleton in debug builds, and we
   // don't have access to that here. Obviously.
   const char* id = "id";
   nsCOMPtr<nsIStorageStream> storageStream
     = 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
     = do_CreateInstance("@mozilla.org/binaryoutputstream;1");
   if (!objectOutput)
     return NS_ERROR_OUT_OF_MEMORY;
   
   nsCOMPtr<nsIOutputStream> outputStream
--- a/xpcom/io/nsIPipe.idl
+++ b/xpcom/io/nsIPipe.idl
@@ -2,17 +2,16 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "nsISupports.idl"
 
 interface nsIAsyncInputStream;
 interface nsIAsyncOutputStream;
-interface nsIMemory;
 
 /**
  * nsIPipe represents an in-process buffer that can be read using nsIInputStream
  * and written using nsIOutputStream.  The reader and writer of a pipe do not
  * have to be on the same thread.  As a result, the pipe is an ideal mechanism
  * to bridge data exchange between two threads.  For example, a worker thread
  * might write data to a pipe from which the main thread will read.
  *
@@ -32,17 +31,17 @@ interface nsIMemory;
  * user can call AsyncWait on the input end of the pipe to be notified when 
  * the pipe has data to read (or when the pipe becomes closed).
  *
  * NS_NewPipe2 and NS_NewPipe provide convenient pipe constructors.  In most
  * cases nsIPipe is not actually used.  It is usually enough to just get
  * 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
 {
     /**
      * initialize this pipe
      *
      * @param nonBlockingInput
      *        true specifies non-blocking input stream behavior
      * @param nonBlockingOutput
@@ -50,25 +49,21 @@ interface nsIPipe : nsISupports
      * @param segmentSize
      *        specifies the segment size in bytes (pass 0 to use default value)
      * @param segmentCount
      *        specifies the max number of segments (pass 0 to use default
      *        value).   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 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.
      */
     readonly attribute nsIAsyncInputStream inputStream;
 
     /**
      * The pipe's output end.
@@ -124,28 +119,24 @@ class nsIOutputStream;
  *        true specifies non-blocking output stream behavior
  * @param segmentSize
  *        specifies the segment size in bytes (pass 0 to use default value)
  * @param segmentCount
  *        specifies the max number of segments (pass 0 to use default value)
  *        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,
             nsIAsyncOutputStream **pipeOut,
             bool nonBlockingInput = false,
             bool nonBlockingOutput = false,
             uint32_t segmentSize = 0,
-            uint32_t segmentCount = 0,
-            nsIMemory *segmentAlloc = nullptr);
+            uint32_t segmentCount = 0);
 
 /**
  * NS_NewPipe
  *
  * Preserved for backwards compatibility.  Plus, this interface is more
  * amiable in certain contexts (e.g., when you don't need the pipe's async
  * capabilities).
  *
@@ -161,22 +152,18 @@ NS_NewPipe2(nsIAsyncInputStream **pipeIn
  *        multiple of segmentSize.  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 nonBlockingInput
  *        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,
            nsIOutputStream **pipeOut,
            uint32_t segmentSize = 0,
            uint32_t maxSize = 0,
            bool nonBlockingInput = false,
-           bool nonBlockingOutput = false,
-           nsIMemory *segmentAlloc = nullptr);
+           bool nonBlockingOutput = false);
 
 %}
--- a/xpcom/io/nsIStorageStream.idl
+++ b/xpcom/io/nsIStorageStream.idl
@@ -1,43 +1,39 @@
 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "nsISupports.idl"
 
-interface nsIMemory;
 interface nsIInputStream;
 interface nsIOutputStream;
 
 /**
  * The nsIStorageStream interface maintains an internal data buffer that can be
  * filled using a single output stream.  One or more independent input streams
  * 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
 {
     /**
      *
      * Initialize the stream, setting up the amount of space that will be
      * allocated for the stream's backing-store.
      *
      * @param segmentSize
      *        Size of each segment. Must be a power of two.
      * @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.
      * The zero-based startPosition argument is used is used to set the initial
      * write cursor position.  The startPosition cannot be set larger than the
      * current buffer length.  Calling this method has the side-effect of
      * truncating the internal buffer to startPosition bytes.
      */
--- a/xpcom/io/nsPipe3.cpp
+++ b/xpcom/io/nsPipe3.cpp
@@ -317,32 +317,31 @@ nsPipe::~nsPipe()
 }
 
 NS_IMPL_ISUPPORTS1(nsPipe, nsIPipe)
 
 NS_IMETHODIMP
 nsPipe::Init(bool nonBlockingIn,
              bool nonBlockingOut,
              uint32_t segmentSize,
-             uint32_t segmentCount,
-             nsIMemory *segmentAlloc)
+             uint32_t segmentCount)
 {
     mInited = true;
 
     if (segmentSize == 0)
         segmentSize = DEFAULT_SEGMENT_SIZE;
     if (segmentCount == 0)
         segmentCount = DEFAULT_SEGMENT_COUNT;
 
     // protect against overflow
     uint32_t maxCount = uint32_t(-1) / segmentSize;
     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;
 
     mInput.SetNonBlocking(nonBlockingIn);
     mOutput.SetNonBlocking(nonBlockingOut);
     return NS_OK;
 }
 
@@ -1237,60 +1236,57 @@ nsPipeOutputStream::AsyncWait(nsIOutputS
 ////////////////////////////////////////////////////////////////////////////////
 
 nsresult
 NS_NewPipe(nsIInputStream **pipeIn,
            nsIOutputStream **pipeOut,
            uint32_t segmentSize,
            uint32_t maxSize,
            bool nonBlockingInput,
-           bool nonBlockingOutput,
-           nsIMemory *segmentAlloc)
+           bool nonBlockingOutput)
 {
     if (segmentSize == 0)
         segmentSize = DEFAULT_SEGMENT_SIZE;
 
     // Handle maxSize of UINT32_MAX as a special case
     uint32_t segmentCount;
     if (maxSize == UINT32_MAX)
         segmentCount = UINT32_MAX;
     else
         segmentCount = maxSize / segmentSize;
 
     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;
     *pipeOut = out;
     return NS_OK;
 }
 
 nsresult
 NS_NewPipe2(nsIAsyncInputStream **pipeIn,
             nsIAsyncOutputStream **pipeOut,
             bool nonBlockingInput,
             bool nonBlockingOutput,
             uint32_t segmentSize,
-            uint32_t segmentCount,
-            nsIMemory *segmentAlloc)
+            uint32_t segmentCount)
 {
     nsresult rv;
 
     nsPipe *pipe = new nsPipe();
     if (!pipe)
         return NS_ERROR_OUT_OF_MEMORY;
 
     rv = pipe->Init(nonBlockingInput,
                     nonBlockingOutput,
                     segmentSize,
-                    segmentCount,
-                    segmentAlloc);
+                    segmentCount);
     if (NS_FAILED(rv)) {
         NS_ADDREF(pipe);
         NS_RELEASE(pipe);
         return rv;
     }
 
     pipe->GetInputStream(pipeIn);
     pipe->GetOutputStream(pipeOut);
--- a/xpcom/io/nsSegmentedBuffer.cpp
+++ b/xpcom/io/nsSegmentedBuffer.cpp
@@ -2,18 +2,17 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "nsSegmentedBuffer.h"
 #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
     mSegmentSize = segmentSize;
     mMaxSize = maxSize;
 #if 0 // testing...
     mSegmentArrayCount = 2;
 #else
--- a/xpcom/io/nsSegmentedBuffer.h
+++ b/xpcom/io/nsSegmentedBuffer.h
@@ -17,18 +17,17 @@ public:
           mSegmentArrayCount(0),
           mFirstSegmentIndex(0), mLastSegmentIndex(0) {}
 
     ~nsSegmentedBuffer() {
         Empty();
     }
 
 
-    nsresult Init(uint32_t segmentSize, uint32_t maxSize,
-                  nsIMemory* allocator = nullptr);
+    nsresult Init(uint32_t segmentSize, uint32_t maxSize);
 
     char* AppendNewSegment();   // pushes at end
 
     // returns true if no more segments remain:
     bool DeleteFirstSegment();  // pops from beginning
 
     // returns true if no more segments remain:
     bool DeleteLastSegment();  // pops from beginning
--- a/xpcom/io/nsStorageStream.cpp
+++ b/xpcom/io/nsStorageStream.cpp
@@ -62,31 +62,30 @@ nsStorageStream::~nsStorageStream()
     delete mSegmentedBuffer;
 }
 
 NS_IMPL_ISUPPORTS2(nsStorageStream,
                    nsIStorageStream,
                    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)
         return NS_ERROR_OUT_OF_MEMORY;
     
     mSegmentSize = segmentSize;
     mSegmentSizeLog2 = mozilla::FloorLog2(segmentSize);
 
     // Segment size must be a power of two
     if (mSegmentSize != ((uint32_t)1 << mSegmentSizeLog2))
         return NS_ERROR_INVALID_ARG;
 
-    return mSegmentedBuffer->Init(segmentSize, maxSize, segmentAllocator);
+    return mSegmentedBuffer->Init(segmentSize, maxSize);
 }
 
 NS_IMETHODIMP
 nsStorageStream::GetOutputStream(int32_t aStartingOffset, 
                                  nsIOutputStream * *aOutputStream)
 {
     if (NS_WARN_IF(!aOutputStream))
         return NS_ERROR_INVALID_ARG;
@@ -526,17 +525,17 @@ nsStorageInputStream::Seek(uint32_t aPos
 
 nsresult
 NS_NewStorageStream(uint32_t segmentSize, uint32_t maxSize, nsIStorageStream **result)
 {
     nsStorageStream* storageStream = new nsStorageStream();
     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;
     }
     *result = storageStream;
     return NS_OK;
 }
 
--- a/xpcom/tests/TestPipe.cpp
+++ b/xpcom/tests/TestPipe.cpp
@@ -12,177 +12,47 @@
 #include "nsIAsyncOutputStream.h"
 
 /** NS_NewPipe2 reimplemented, because it's not exported by XPCOM */
 nsresult TP_NewPipe2(nsIAsyncInputStream** input,
                      nsIAsyncOutputStream** output,
                      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)
     return NS_ERROR_OUT_OF_MEMORY;
 
   nsresult rv = pipe->Init(nonBlockingInput,
                            nonBlockingOutput,
                            segmentSize,
-                           segmentCount,
-                           segmentAlloc);
+                           segmentCount);
 
   if (NS_FAILED(rv))
     return rv;
 
   pipe->GetInputStream(input);
   pipe->GetOutputStream(output);
   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);
     return rv;
   }
 
   const uint32_t BUFFER_LENGTH = 100;
   const char written[] =
@@ -222,25 +92,25 @@ nsresult TestBackwardsAllocator()
   }
 
   if (0 != memcmp(written, read, BUFFER_LENGTH))
   {
     fail("didn't read the written data correctly!");
     return NS_ERROR_FAILURE;
   }
 
-  passed("TestBackwardsAllocator");
+  passed("TestPipe");
   return NS_OK;
 }
 
 int main(int argc, char** argv)
 {
   ScopedXPCOM xpcom("nsPipe");
   if (xpcom.failed())
     return 1;
 
   int rv = 0;
 
-  if (NS_FAILED(TestBackwardsAllocator()))
+  if (NS_FAILED(TestPipe()))
     rv = 1;
 
   return rv;
 }