gecko-dev/memory/fallible/fallible.h
Mike Hommey 154fd9c9ad Bug 1424116 - Change the definition of mozilla::fallible again. r=njn
In bug 1423803, mozilla::fallible was made an "alias" of std::nothrow.
Except C++ doesn't allow compilers to be too smart, and in many cases,
they would actually create data fields to hold a copy of std::nothrow,
even creating a static initializer on non-optimized builds to do so.

By turning it into a reference, we allow compilers to just use
std::nothrow directly, as if it were passed directly, but they can still
create unused data fields. Turning it into a static allows compilers to
skip the data fields altogether.

On a local linux64 build, this saves 242 bytes of .bss.

Note this does change a `lea` (address calculation) into a `mov` (read),
but it shouldn't matter too much.

--HG--
extra : rebase_source : 9c08e8263aef267b8ad5962b0248c7effcb67796
2017-12-08 11:51:59 +09:00

65 lines
1.7 KiB
C++

/* 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 mozilla_fallible_h
#define mozilla_fallible_h
#if defined(__cplusplus)
/* Explicit fallible allocation
*
* Memory allocation (normally) defaults to abort in case of failed
* allocation. That is, it never returns NULL, and crashes instead.
*
* Code can explicitely request for fallible memory allocation thanks
* to the declarations below.
*
* The typical use of the mozilla::fallible const is with placement new,
* like the following:
*
* foo = new (mozilla::fallible) Foo();
*
* The following forms, or derivatives, are also possible but deprecated:
*
* foo = new ((mozilla::fallible_t())) Foo();
*
* const mozilla::fallible_t fallible = mozilla::fallible_t();
* bar = new (f) Bar();
*
* It is also possible to declare method overloads with fallible allocation
* alternatives, like so:
*
* class Foo {
* public:
* void Method(void *);
* void Method(void *, const mozilla::fallible_t&);
* };
*
* Foo foo;
* foo.Method(nullptr, mozilla::fallible);
*
* If that last method call is in a method that itself takes a const
* fallible_t& argument, it is recommended to propagate that argument
* instead of using mozilla::fallible:
*
* void Func(Foo &foo, const mozilla::fallible_t& aFallible) {
* foo.Method(nullptr, aFallible);
* }
*
*/
#include <new>
namespace mozilla {
using fallible_t = std::nothrow_t;
static const fallible_t& fallible = std::nothrow;
} // namespace mozilla
#endif
#endif // mozilla_fallible_h