Bug 1216611 - add mozilla::MakeUniqueFallible and convert uses throughout the tree; r=Waldo

This commit is contained in:
Nathan Froyd 2015-11-05 16:24:24 -05:00
parent 4a8f619681
commit e763192040
8 changed files with 56 additions and 8 deletions

View File

@ -7,6 +7,7 @@
#include "GLContext.h"
#include "mozilla/CheckedInt.h"
#include "mozilla/UniquePtrExtensions.h"
#include "WebGLBuffer.h"
#include "WebGLContextUtils.h"
#include "WebGLFramebuffer.h"
@ -580,7 +581,7 @@ WebGLContext::DoFakeVertexAttrib0(GLuint vertexCount)
GetAndFlushUnderlyingGLErrors();
if (mFakeVertexAttrib0BufferStatus == WebGLVertexAttrib0Status::EmulatedInitializedArray) {
UniquePtr<GLfloat[]> array(new (fallible) GLfloat[4 * vertexCount]);
auto array = MakeUniqueFallible<GLfloat[]>(4 * vertexCount);
if (!array) {
ErrorOutOfMemory("Fake attrib0 array.");
return false;

View File

@ -52,6 +52,7 @@
#include "mozilla/dom/ToJSValue.h"
#include "mozilla/Endian.h"
#include "mozilla/RefPtr.h"
#include "mozilla/UniquePtrExtensions.h"
namespace mozilla {
@ -1622,7 +1623,7 @@ WebGLContext::ReadPixels(GLint x, GLint y, GLsizei width,
uint32_t subrect_byteLength = (subrect_height-1)*subrect_alignedRowSize + subrect_plainRowSize;
// create subrect buffer, call glReadPixels, copy pixels into destination buffer, delete subrect buffer
UniquePtr<GLubyte> subrect_data(new (fallible) GLubyte[subrect_byteLength]);
auto subrect_data = MakeUniqueFallible<GLubyte[]>(subrect_byteLength);
if (!subrect_data)
return ErrorOutOfMemory("readPixels: subrect_data");

View File

@ -21,6 +21,7 @@
#include "mozilla/gfx/Logging.h"
#include "mozilla/Maybe.h"
#include "mozilla/RefPtr.h"
#include "mozilla/UniquePtrExtensions.h"
#include "mozilla/Vector.h"
#include "nsComponentManagerUtils.h"
#include "nsIClipboardHelper.h"
@ -1560,7 +1561,7 @@ gfxUtils::GetImageBuffer(gfx::DataSourceSurface* aSurface,
return nullptr;
uint32_t bufferSize = aSurface->GetSize().width * aSurface->GetSize().height * 4;
UniquePtr<uint8_t[]> imageBuffer(new (fallible) uint8_t[bufferSize]);
auto imageBuffer = MakeUniqueFallible<uint8_t[]>(bufferSize);
if (!imageBuffer) {
aSurface->Unmap();
return nullptr;

View File

@ -5,6 +5,7 @@
#include "nsCRT.h"
#include "mozilla/Endian.h"
#include "mozilla/UniquePtrExtensions.h"
#include "nsBMPEncoder.h"
#include "prprf.h"
#include "nsString.h"
@ -187,9 +188,8 @@ nsBMPEncoder::AddImageFrame(const uint8_t* aData,
return NS_ERROR_INVALID_ARG;
}
UniquePtr<uint8_t[]> row(new (fallible)
uint8_t[mBMPInfoHeader.width *
BytesPerPixel(mBMPInfoHeader.bpp)]);
auto row = MakeUniqueFallible<uint8_t[]>(mBMPInfoHeader.width *
BytesPerPixel(mBMPInfoHeader.bpp));
if (!row) {
return NS_ERROR_OUT_OF_MEMORY;
}

View File

@ -45,6 +45,7 @@
#include "mozilla/gfx/Point.h"
#include "mozilla/gfx/Types.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/UniquePtrExtensions.h"
#include "logging.h"
@ -1179,7 +1180,7 @@ void MediaPipelineTransmit::PipelineListener::ProcessVideoChunk(
int half_height = (size.height + 1) >> 1;
int c_size = half_width * half_height;
int buffer_size = YSIZE(size.width, size.height) + 2 * c_size;
UniquePtr<uint8[]> yuv_scoped(new (fallible) uint8[buffer_size]);
auto yuv_scoped = MakeUniqueFallible<uint8[]>(buffer_size);
if (!yuv_scoped)
return;
uint8* yuv = yuv_scoped.get();

View File

@ -0,0 +1,42 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */
/* Useful extensions to UniquePtr. */
#ifndef mozilla_UniquePtrExtensions_h
#define mozilla_UniquePtrExtensions_h
#include "mozilla/fallible.h"
#include "mozilla/UniquePtr.h"
namespace mozilla {
/**
* MakeUniqueFallible works exactly like MakeUnique, except that the memory
* allocation performed is done fallibly, i.e. it can return nullptr.
*/
template<typename T, typename... Args>
typename detail::UniqueSelector<T>::SingleObject
MakeUniqueFallible(Args&&... aArgs)
{
return UniquePtr<T>(new (fallible) T(Forward<Args>(aArgs)...));
}
template<typename T>
typename detail::UniqueSelector<T>::UnknownBound
MakeUniqueFallible(decltype(sizeof(int)) aN)
{
typedef typename RemoveExtent<T>::Type ArrayType;
return UniquePtr<T>(new (fallible) ArrayType[aN]());
}
template<typename T, typename... Args>
typename detail::UniqueSelector<T>::KnownBound
MakeUniqueFallible(Args&&... aArgs) = delete;
} // namespace mozilla
#endif // mozilla_UniquePtrExtensions_h

View File

@ -89,6 +89,7 @@ EXPORTS.mozilla = [
'Types.h',
'TypeTraits.h',
'UniquePtr.h',
'UniquePtrExtensions.h',
'Variant.h',
'Vector.h',
'WeakPtr.h',

View File

@ -11,6 +11,7 @@
#include "mozilla/MouseEvents.h"
#include "mozilla/TextEvents.h"
#include "mozilla/TouchEvents.h"
#include "mozilla/UniquePtrExtensions.h"
#include <algorithm>
#include "GeckoProfiler.h"
@ -2322,7 +2323,7 @@ nsWindow::UpdateAlpha(gfxPattern* aPattern, nsIntRect aBoundsRect)
int32_t stride = GetAlignedStride<4>(BytesPerPixel(SurfaceFormat::A8) *
aBoundsRect.width);
int32_t bufferSize = stride * aBoundsRect.height;
UniquePtr<uint8_t[]> imageBuffer(new (std::nothrow) uint8_t[bufferSize]);
auto imageBuffer = MakeUniqueFallible<uint8_t[]>(bufferSize);
RefPtr<DrawTarget> drawTarget = gfxPlatform::GetPlatform()->
CreateDrawTargetForData(imageBuffer.get(), aBoundsRect.Size(),
stride, SurfaceFormat::A8);