fallible.h (1873B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 #ifndef mozilla_fallible_h 6 #define mozilla_fallible_h 7 8 #if defined(__cplusplus) 9 10 /* Explicit fallible allocation 11 * 12 * mozilla::fallible_t and mozilla::fallible_t provide aliases to 13 * std::nothrow_t and std::nothrow from the C++ standard header <new>. 14 * 15 * Memory allocation (normally) defaults to abort in case of failed 16 * allocation. That is, it never returns NULL, and crashes instead. 17 * 18 * Code can explicitly request for fallible memory allocation thanks 19 * to the declarations below. 20 * 21 * The typical use of the mozilla::fallible const is with placement new, 22 * like the following: 23 * 24 * foo = new (mozilla::fallible) Foo(); 25 * 26 * The following forms, or derivatives, are also possible but deprecated: 27 * 28 * foo = new ((mozilla::fallible_t())) Foo(); 29 * 30 * const mozilla::fallible_t f = mozilla::fallible_t(); 31 * bar = new (f) Bar(); 32 * 33 * It is also possible to declare method overloads with fallible allocation 34 * alternatives, like so: 35 * 36 * class Foo { 37 * public: 38 * void Method(void *); 39 * void Method(void *, const mozilla::fallible_t&); 40 * }; 41 * 42 * Foo foo; 43 * foo.Method(nullptr, mozilla::fallible); 44 * 45 * If that last method call is in a method that itself takes a const 46 * fallible_t& argument, it is recommended to propagate that argument 47 * instead of using mozilla::fallible: 48 * 49 * void Func(Foo &foo, const mozilla::fallible_t& aFallible) { 50 * foo.Method(nullptr, aFallible); 51 * } 52 * 53 */ 54 55 # include <new> 56 57 namespace mozilla { 58 59 using fallible_t = std::nothrow_t; 60 61 static const fallible_t& fallible = std::nothrow; 62 63 } // namespace mozilla 64 65 #endif 66 67 #endif // mozilla_fallible_h