Bug 1507805 [wpt PR 14092] - [css-grid] Some fixes on grid-container-scrollbar-*...
[gecko.git] / mfbt / UniquePtrExtensions.h
blob004f313b9238c0a4d5ac77603eee0e0969ebddfa
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* Useful extensions to UniquePtr. */
9 #ifndef mozilla_UniquePtrExtensions_h
10 #define mozilla_UniquePtrExtensions_h
12 #include "mozilla/fallible.h"
13 #include "mozilla/UniquePtr.h"
15 namespace mozilla {
17 /**
18 * MakeUniqueFallible works exactly like MakeUnique, except that the memory
19 * allocation performed is done fallibly, i.e. it can return nullptr.
21 template<typename T, typename... Args>
22 typename detail::UniqueSelector<T>::SingleObject
23 MakeUniqueFallible(Args&&... aArgs)
25 return UniquePtr<T>(new (fallible) T(std::forward<Args>(aArgs)...));
28 template<typename T>
29 typename detail::UniqueSelector<T>::UnknownBound
30 MakeUniqueFallible(decltype(sizeof(int)) aN)
32 typedef typename RemoveExtent<T>::Type ArrayType;
33 return UniquePtr<T>(new (fallible) ArrayType[aN]());
36 template<typename T, typename... Args>
37 typename detail::UniqueSelector<T>::KnownBound
38 MakeUniqueFallible(Args&&... aArgs) = delete;
40 namespace detail {
42 template<typename T>
43 struct FreePolicy
45 void operator()(const void* ptr) {
46 free(const_cast<void*>(ptr));
50 } // namespace detail
52 template<typename T>
53 using UniqueFreePtr = UniquePtr<T, detail::FreePolicy<T>>;
55 } // namespace mozilla
57 #endif // mozilla_UniquePtrExtensions_h