From 5f8de423f190bbb79a62f804151bc24824fa32d8 Mon Sep 17 00:00:00 2001 From: "Matt A. Tobin" Date: Fri, 2 Feb 2018 04:16:08 -0500 Subject: Add m-esr52 at 52.6.0 --- mfbt/UniquePtrExtensions.h | 57 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 mfbt/UniquePtrExtensions.h (limited to 'mfbt/UniquePtrExtensions.h') diff --git a/mfbt/UniquePtrExtensions.h b/mfbt/UniquePtrExtensions.h new file mode 100644 index 000000000..d94f33eea --- /dev/null +++ b/mfbt/UniquePtrExtensions.h @@ -0,0 +1,57 @@ +/* -*- 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 detail::UniqueSelector::SingleObject +MakeUniqueFallible(Args&&... aArgs) +{ + return UniquePtr(new (fallible) T(Forward(aArgs)...)); +} + +template +typename detail::UniqueSelector::UnknownBound +MakeUniqueFallible(decltype(sizeof(int)) aN) +{ + typedef typename RemoveExtent::Type ArrayType; + return UniquePtr(new (fallible) ArrayType[aN]()); +} + +template +typename detail::UniqueSelector::KnownBound +MakeUniqueFallible(Args&&... aArgs) = delete; + +namespace detail { + +template +struct FreePolicy +{ + void operator()(const void* ptr) { + free(const_cast(ptr)); + } +}; + +} // namespace detail + +template +using UniqueFreePtr = UniquePtr>; + +} // namespace mozilla + +#endif // mozilla_UniquePtrExtensions_h -- cgit v1.2.3