Bug 1241901 part 4 - Stop using nsAutoPtr for holding primitive arrays. r=froydnj draft
authorXidorn Quan <quanxunzhen@gmail.com>
Wed, 27 Jan 2016 11:31:06 +1100
changeset 326694 018e6c20446a9b5762a476a13cabeb8373eb9f5f
parent 326693 f5bd175d33ff28bcc7b0405b2621de6917e9e0df
child 513629 5502b51994b775e79bf745f6e970d8905d4ecbd1
push id10164
push userxquan@mozilla.com
push dateThu, 28 Jan 2016 11:17:52 +0000
reviewersfroydnj
bugs1241901
milestone47.0a1
Bug 1241901 part 4 - Stop using nsAutoPtr for holding primitive arrays. r=froydnj
dom/media/AudioPacketizer.h
dom/media/webspeech/synth/pico/nsPicoService.cpp
dom/media/webspeech/synth/pico/nsPicoService.h
ipc/chromium/src/base/message_pump_libevent.h
widget/windows/nsWindowGfx.cpp
xpcom/base/nsAutoPtr.h
xpcom/tests/gtest/TestCloneInputStream.cpp
--- a/dom/media/AudioPacketizer.h
+++ b/dom/media/AudioPacketizer.h
@@ -3,17 +3,17 @@
  * 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/. */
 
 #ifndef AudioPacketizer_h_
 #define AudioPacketizer_h_
 
 #include <mozilla/PodOperations.h>
 #include <mozilla/Assertions.h>
-#include <nsAutoPtr.h>
+#include <mozilla/UniquePtr.h>
 #include <AudioSampleFormat.h>
 
 // Enable this to warn when `Output` has been called but not enough data was
 // buffered.
 // #define LOG_PACKETIZER_UNDERRUN
 
 namespace mozilla {
 /**
@@ -57,18 +57,18 @@ public:
     // array has the right size.
     if (inputSamples > EmptySlots()) {
       // Calls to Input and Output are roughtly interleaved
       // (Input,Output,Input,Output, etc.), or balanced
       // (Input,Input,Input,Output,Output,Output), so we update the buffer to
       // the exact right size in order to not waste space.
       uint32_t newLength = AvailableSamples() + inputSamples;
       uint32_t toCopy = AvailableSamples();
-      nsAutoPtr<InputType> oldStorage = mStorage;
-      mStorage = new InputType[newLength];
+      UniquePtr<InputType[]> oldStorage = mozilla::Move(mStorage);
+      mStorage = mozilla::MakeUnique<InputType[]>(newLength);
       // Copy the old data at the beginning of the new storage.
       if (WriteIndex() >= ReadIndex()) {
         PodCopy(mStorage.get(),
                 oldStorage.get() + ReadIndex(),
                 AvailableSamples());
       } else {
         uint32_t firstPartLength = mLength - ReadIndex();
         uint32_t secondPartLength = AvailableSamples() - firstPartLength;
@@ -181,16 +181,16 @@ private:
   uint32_t mPacketSize;
   // Number of channels of the stream flowing through this packetizer
   uint32_t mChannels;
   // Two virtual index into the buffer: the read position and the write
   // position.
   uint64_t mReadIndex;
   uint64_t mWriteIndex;
   // Storage for the samples
-  nsAutoPtr<InputType> mStorage;
+  mozilla::UniquePtr<InputType[]> mStorage;
   // Length of the buffer, in samples
   uint32_t mLength;
 };
 
 } // mozilla
 
 #endif // AudioPacketizer_h_
--- a/dom/media/webspeech/synth/pico/nsPicoService.cpp
+++ b/dom/media/webspeech/synth/pico/nsPicoService.cpp
@@ -635,20 +635,21 @@ nsPicoService::LoadEngine(PicoVoice* aVo
 {
   PicoApi::pico_Status status = 0;
 
   if (mPicoSystem) {
     UnloadEngine();
   }
 
   if (!mPicoMemArea) {
-    mPicoMemArea = new uint8_t[PICO_MEM_SIZE];
+    mPicoMemArea = MakeUnique<uint8_t[]>(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);
   PICO_ENSURE_SUCCESS_VOID("pico_loadResource", status);
 
   status = sPicoApi.pico_loadResource(mPicoSystem, aVoice->mSgFile.get(), &mSgResource);
   PICO_ENSURE_SUCCESS_VOID("pico_loadResource", status);
 
--- a/dom/media/webspeech/synth/pico/nsPicoService.h
+++ b/dom/media/webspeech/synth/pico/nsPicoService.h
@@ -3,24 +3,24 @@
 /* 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/. */
 
 #ifndef nsPicoService_h
 #define nsPicoService_h
 
 #include "mozilla/Mutex.h"
-#include "nsAutoPtr.h"
 #include "nsTArray.h"
 #include "nsIObserver.h"
 #include "nsIThread.h"
 #include "nsISpeechService.h"
 #include "nsRefPtrHashtable.h"
 #include "mozilla/StaticPtr.h"
 #include "mozilla/Monitor.h"
+#include "mozilla/UniquePtr.h"
 
 namespace mozilla {
 namespace dom {
 
 class PicoVoice;
 class PicoCallbackRunnable;
 
 typedef void* pico_System;
@@ -77,17 +77,17 @@ private:
   pico_System mPicoSystem;
 
   pico_Engine mPicoEngine;
 
   pico_Resource mSgResource;
 
   pico_Resource mTaResource;
 
-  nsAutoPtr<uint8_t> mPicoMemArea;
+  mozilla::UniquePtr<uint8_t[]> mPicoMemArea;
 
   static StaticRefPtr<nsPicoService> sSingleton;
 };
 
 } // namespace dom
 } // namespace mozilla
 
 #endif
--- a/ipc/chromium/src/base/message_pump_libevent.h
+++ b/ipc/chromium/src/base/message_pump_libevent.h
@@ -2,17 +2,17 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
 #ifndef BASE_MESSAGE_PUMP_LIBEVENT_H_
 #define BASE_MESSAGE_PUMP_LIBEVENT_H_
 
 #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;
 struct event;
 
 class nsDependentCSubstring;
 
 namespace base {
@@ -187,32 +187,32 @@ class MessagePumpLibevent : public Messa
  */
 class LineWatcher : public MessagePumpLibevent::Watcher
 {
 public:
   LineWatcher(char aTerminator, int aBufferSize) : mReceivedIndex(0),
     mBufferSize(aBufferSize),
     mTerminator(aTerminator)
   {
-    mReceiveBuffer = new char[mBufferSize];
+    mReceiveBuffer = mozilla::MakeUnique<char[]>(mBufferSize);
   }
 
   ~LineWatcher() {}
 
 protected:
   /**
    * OnError will be called when |read| returns error. Derived class should
    * implement this function to handle error cases when needed.
    */
   virtual void OnError() {}
   virtual void OnLineRead(int aFd, nsDependentCSubstring& aMessage) = 0;
   virtual void OnFileCanWriteWithoutBlocking(int /* aFd */) override {}
 private:
   virtual void OnFileCanReadWithoutBlocking(int aFd) final override;
 
-  nsAutoPtr<char> mReceiveBuffer;
+  mozilla::UniquePtr<char[]> mReceiveBuffer;
   int mReceivedIndex;
   int mBufferSize;
   char mTerminator;
 };
 }  // namespace base
 
 #endif  // BASE_MESSAGE_PUMP_LIBEVENT_H_
--- a/widget/windows/nsWindowGfx.cpp
+++ b/widget/windows/nsWindowGfx.cpp
@@ -27,16 +27,17 @@ using mozilla::plugins::PluginInstancePa
 #include "gfxImageSurface.h"
 #include "gfxUtils.h"
 #include "gfxWindowsSurface.h"
 #include "gfxWindowsPlatform.h"
 #include "mozilla/gfx/2D.h"
 #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"
 #include "WinUtils.h"
 #include "nsIWidgetListener.h"
 #include "mozilla/unused.h"
 #include "nsDebug.h"
 #include "nsIXULRuntime.h"
@@ -64,17 +65,17 @@ using namespace mozilla::plugins;
  **************************************************************/
 
 /**************************************************************
  *
  * SECTION: nsWindow statics
  *
  **************************************************************/
 
-static nsAutoPtr<uint8_t>  sSharedSurfaceData;
+static UniquePtr<uint8_t[]> sSharedSurfaceData;
 static IntSize             sSharedSurfaceSize;
 
 struct IconMetrics {
   int32_t xMetric;
   int32_t yMetric;
   int32_t defaultSize;
 };
 
@@ -146,21 +147,21 @@ EnsureSharedSurfaceSize(IntSize size)
   if (WORDSSIZE(screenSize) > WORDSSIZE(size))
     size = screenSize;
 
   if (WORDSSIZE(screenSize) < WORDSSIZE(size))
     NS_WARNING("Trying to create a shared surface larger than the screen");
 
   if (!sSharedSurfaceData || (WORDSSIZE(size) > WORDSSIZE(sSharedSurfaceSize))) {
     sSharedSurfaceSize = size;
-    sSharedSurfaceData = nullptr;
-    sSharedSurfaceData = (uint8_t *)malloc(WORDSSIZE(sSharedSurfaceSize) * 4);
+    sSharedSurfaceData =
+      MakeUniqueFallible<uint8_t[]>(WORDSSIZE(sSharedSurfaceSize) * 4);
   }
 
-  return (sSharedSurfaceData != nullptr);
+  return !sSharedSurfaceData;
 }
 
 nsIWidgetListener* nsWindow::GetPaintListener()
 {
   if (mDestroyCalled)
     return nullptr;
   return mAttachedWidgetListener ? mAttachedWidgetListener : mWidgetListener;
 }
--- a/xpcom/base/nsAutoPtr.h
+++ b/xpcom/base/nsAutoPtr.h
@@ -4,28 +4,32 @@
  * 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/. */
 
 #ifndef nsAutoPtr_h
 #define nsAutoPtr_h
 
 #include "nsCOMPtr.h"
 #include "mozilla/RefPtr.h"
+#include "mozilla/TypeTraits.h"
 
 #include "nsCycleCollectionNoteChild.h"
 #include "mozilla/MemoryReporting.h"
 
 /*****************************************************************************/
 
 // template <class T> class nsAutoPtrGetterTransfers;
 
 template <class T>
 class nsAutoPtr
 {
 private:
+  static_assert(!mozilla::IsScalar<T>::value, "If you are using "
+                "nsAutoPtr to hold an array, use UniquePtr<T[]> instead");
+
   void**
   begin_assignment()
   {
     assign(0);
     return reinterpret_cast<void**>(&mRawPtr);
   }
 
   void
--- a/xpcom/tests/gtest/TestCloneInputStream.cpp
+++ b/xpcom/tests/gtest/TestCloneInputStream.cpp
@@ -8,17 +8,16 @@
 #include "Helpers.h"
 #include "mozilla/unused.h"
 #include "nsICloneableInputStream.h"
 #include "nsIMultiplexInputStream.h"
 #include "nsNetUtil.h"
 #include "nsStreamUtils.h"
 #include "nsStringStream.h"
 #include "nsComponentManagerUtils.h"
-#include "nsAutoPtr.h"
 
 TEST(CloneInputStream, InvalidInput)
 {
   nsCOMPtr<nsIInputStream> clone;
   nsresult rv = NS_CloneInputStream(nullptr, getter_AddRefs(clone));
   ASSERT_TRUE(NS_FAILED(rv));
   ASSERT_FALSE(clone);
 }
@@ -140,17 +139,17 @@ TEST(CloneInputStream, CloneMultiplexStr
   doubled.AppendElements(inputData);
 
   nsCOMPtr<nsIInputStream> clone;
   nsresult rv = NS_CloneInputStream(stream, getter_AddRefs(clone));
   ASSERT_TRUE(NS_SUCCEEDED(rv));
   testing::ConsumeAndValidateStream(clone, doubled);
 
   // Stream that has been read should fail.
-  nsAutoPtr<char> buffer(new char[512]);
+  char buffer[512];
   uint32_t read;
   rv = stream->Read(buffer, 512, &read);
   ASSERT_TRUE(NS_SUCCEEDED(rv));
 
   nsCOMPtr<nsIInputStream> clone2;
   rv = NS_CloneInputStream(stream, getter_AddRefs(clone2));
   ASSERT_TRUE(NS_FAILED(rv));
 }
@@ -170,17 +169,17 @@ TEST(CloneInputStream, CloneMultiplexStr
     nsresult rv = NS_NewCStringInputStream(getter_AddRefs(base), inputString);
     ASSERT_TRUE(NS_SUCCEEDED(rv));
 
     rv = stream->AppendStream(base);
     ASSERT_TRUE(NS_SUCCEEDED(rv));
   }
 
   // Fail when first stream read, but second hasn't been started.
-  nsAutoPtr<char> buffer(new char[1024]);
+  char buffer[1024];
   uint32_t read;
   nsresult rv = stream->Read(buffer, 1024, &read);
   ASSERT_TRUE(NS_SUCCEEDED(rv));
 
   nsCOMPtr<nsIInputStream> clone;
   rv = NS_CloneInputStream(stream, getter_AddRefs(clone));
   ASSERT_TRUE(NS_FAILED(rv));