From 14908eadc8525306239f0e31597de8220973876e Mon Sep 17 00:00:00 2001 From: Xidorn Quan Date: Sat, 30 Jan 2016 10:33:41 +1100 Subject: [PATCH] Bug 1241901 part 4 - Stop using nsAutoPtr for holding primitive arrays. r=froydnj --- dom/media/AudioPacketizer.h | 8 ++++---- dom/media/webspeech/synth/pico/nsPicoService.cpp | 5 +++-- dom/media/webspeech/synth/pico/nsPicoService.h | 4 ++-- ipc/chromium/src/base/message_pump_libevent.h | 6 +++--- widget/windows/nsWindowGfx.cpp | 9 +++++---- xpcom/base/nsAutoPtr.h | 4 ++++ xpcom/tests/gtest/TestCloneInputStream.cpp | 5 ++--- 7 files changed, 23 insertions(+), 18 deletions(-) diff --git a/dom/media/AudioPacketizer.h b/dom/media/AudioPacketizer.h index 94b005ce6e8..b0452cdf092 100644 --- a/dom/media/AudioPacketizer.h +++ b/dom/media/AudioPacketizer.h @@ -8,7 +8,7 @@ #include #include -#include +#include #include // Enable this to warn when `Output` has been called but not enough data was @@ -62,8 +62,8 @@ public: // the exact right size in order to not waste space. uint32_t newLength = AvailableSamples() + inputSamples; uint32_t toCopy = AvailableSamples(); - nsAutoPtr oldStorage = mStorage; - mStorage = new InputType[newLength]; + UniquePtr oldStorage = mozilla::Move(mStorage); + mStorage = mozilla::MakeUnique(newLength); // Copy the old data at the beginning of the new storage. if (WriteIndex() >= ReadIndex()) { PodCopy(mStorage.get(), @@ -186,7 +186,7 @@ private: uint64_t mReadIndex; uint64_t mWriteIndex; // Storage for the samples - nsAutoPtr mStorage; + mozilla::UniquePtr mStorage; // Length of the buffer, in samples uint32_t mLength; }; diff --git a/dom/media/webspeech/synth/pico/nsPicoService.cpp b/dom/media/webspeech/synth/pico/nsPicoService.cpp index b2358c56037..3fa57df2b26 100644 --- a/dom/media/webspeech/synth/pico/nsPicoService.cpp +++ b/dom/media/webspeech/synth/pico/nsPicoService.cpp @@ -640,10 +640,11 @@ nsPicoService::LoadEngine(PicoVoice* aVoice) } if (!mPicoMemArea) { - mPicoMemArea = new uint8_t[PICO_MEM_SIZE]; + mPicoMemArea = MakeUnique(PICO_MEM_SIZE); } - status = sPicoApi.pico_initialize(mPicoMemArea, PICO_MEM_SIZE, &mPicoSystem); + status = sPicoApi.pico_initialize(mPicoMemArea.get(), + PICO_MEM_SIZE, &mPicoSystem); PICO_ENSURE_SUCCESS_VOID("pico_initialize", status); status = sPicoApi.pico_loadResource(mPicoSystem, aVoice->mTaFile.get(), &mTaResource); diff --git a/dom/media/webspeech/synth/pico/nsPicoService.h b/dom/media/webspeech/synth/pico/nsPicoService.h index a22568ec3c0..f47258d9d1e 100644 --- a/dom/media/webspeech/synth/pico/nsPicoService.h +++ b/dom/media/webspeech/synth/pico/nsPicoService.h @@ -8,7 +8,6 @@ #define nsPicoService_h #include "mozilla/Mutex.h" -#include "nsAutoPtr.h" #include "nsTArray.h" #include "nsIObserver.h" #include "nsIThread.h" @@ -16,6 +15,7 @@ #include "nsRefPtrHashtable.h" #include "mozilla/StaticPtr.h" #include "mozilla/Monitor.h" +#include "mozilla/UniquePtr.h" namespace mozilla { namespace dom { @@ -82,7 +82,7 @@ private: pico_Resource mTaResource; - nsAutoPtr mPicoMemArea; + mozilla::UniquePtr mPicoMemArea; static StaticRefPtr sSingleton; }; diff --git a/ipc/chromium/src/base/message_pump_libevent.h b/ipc/chromium/src/base/message_pump_libevent.h index bd7d3b2dbd2..a239256f8d2 100644 --- a/ipc/chromium/src/base/message_pump_libevent.h +++ b/ipc/chromium/src/base/message_pump_libevent.h @@ -7,7 +7,7 @@ #include "base/message_pump.h" #include "base/time.h" -#include "nsAutoPtr.h" +#include "mozilla/UniquePtr.h" // Declare structs we need from libevent.h rather than including it struct event_base; @@ -192,7 +192,7 @@ public: mBufferSize(aBufferSize), mTerminator(aTerminator) { - mReceiveBuffer = new char[mBufferSize]; + mReceiveBuffer = mozilla::MakeUnique(mBufferSize); } ~LineWatcher() {} @@ -208,7 +208,7 @@ protected: private: virtual void OnFileCanReadWithoutBlocking(int aFd) final override; - nsAutoPtr mReceiveBuffer; + mozilla::UniquePtr mReceiveBuffer; int mReceivedIndex; int mBufferSize; char mTerminator; diff --git a/widget/windows/nsWindowGfx.cpp b/widget/windows/nsWindowGfx.cpp index e702d87fcd3..2399dff9199 100644 --- a/widget/windows/nsWindowGfx.cpp +++ b/widget/windows/nsWindowGfx.cpp @@ -32,6 +32,7 @@ using mozilla::plugins::PluginInstanceParent; #include "mozilla/gfx/DataSurfaceHelpers.h" #include "mozilla/gfx/Tools.h" #include "mozilla/RefPtr.h" +#include "mozilla/UniquePtrExtensions.h" #include "nsGfxCIID.h" #include "gfxContext.h" #include "prmem.h" @@ -69,7 +70,7 @@ using namespace mozilla::plugins; * **************************************************************/ -static nsAutoPtr sSharedSurfaceData; +static UniquePtr sSharedSurfaceData; static IntSize sSharedSurfaceSize; struct IconMetrics { @@ -151,11 +152,11 @@ EnsureSharedSurfaceSize(IntSize size) if (!sSharedSurfaceData || (WORDSSIZE(size) > WORDSSIZE(sSharedSurfaceSize))) { sSharedSurfaceSize = size; - sSharedSurfaceData = nullptr; - sSharedSurfaceData = (uint8_t *)malloc(WORDSSIZE(sSharedSurfaceSize) * 4); + sSharedSurfaceData = + MakeUniqueFallible(WORDSSIZE(sSharedSurfaceSize) * 4); } - return (sSharedSurfaceData != nullptr); + return !sSharedSurfaceData; } nsIWidgetListener* nsWindow::GetPaintListener() diff --git a/xpcom/base/nsAutoPtr.h b/xpcom/base/nsAutoPtr.h index 4719794a6f0..160f6ec3145 100644 --- a/xpcom/base/nsAutoPtr.h +++ b/xpcom/base/nsAutoPtr.h @@ -9,6 +9,7 @@ #include "nsCOMPtr.h" #include "mozilla/RefPtr.h" +#include "mozilla/TypeTraits.h" #include "nsCycleCollectionNoteChild.h" #include "mozilla/MemoryReporting.h" @@ -21,6 +22,9 @@ template class nsAutoPtr { private: + static_assert(!mozilla::IsScalar::value, "If you are using " + "nsAutoPtr to hold an array, use UniquePtr instead"); + void** begin_assignment() { diff --git a/xpcom/tests/gtest/TestCloneInputStream.cpp b/xpcom/tests/gtest/TestCloneInputStream.cpp index 3c213b42f29..71f9628ed39 100644 --- a/xpcom/tests/gtest/TestCloneInputStream.cpp +++ b/xpcom/tests/gtest/TestCloneInputStream.cpp @@ -13,7 +13,6 @@ #include "nsStreamUtils.h" #include "nsStringStream.h" #include "nsComponentManagerUtils.h" -#include "nsAutoPtr.h" TEST(CloneInputStream, InvalidInput) { @@ -145,7 +144,7 @@ TEST(CloneInputStream, CloneMultiplexStream) testing::ConsumeAndValidateStream(clone, doubled); // Stream that has been read should fail. - nsAutoPtr buffer(new char[512]); + char buffer[512]; uint32_t read; rv = stream->Read(buffer, 512, &read); ASSERT_TRUE(NS_SUCCEEDED(rv)); @@ -175,7 +174,7 @@ TEST(CloneInputStream, CloneMultiplexStreamPartial) } // Fail when first stream read, but second hasn't been started. - nsAutoPtr buffer(new char[1024]); + char buffer[1024]; uint32_t read; nsresult rv = stream->Read(buffer, 1024, &read); ASSERT_TRUE(NS_SUCCEEDED(rv));