fallible.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. #ifndef mozilla_fallible_h
  5. #define mozilla_fallible_h
  6. #if defined(__cplusplus)
  7. /* Explicit fallible allocation
  8. *
  9. * Memory allocation (normally) defaults to abort in case of failed
  10. * allocation. That is, it never returns NULL, and crashes instead.
  11. *
  12. * Code can explicitely request for fallible memory allocation thanks
  13. * to the declarations below.
  14. *
  15. * The typical use of the mozilla::fallible const is with placement new,
  16. * like the following:
  17. *
  18. * foo = new (mozilla::fallible) Foo();
  19. *
  20. * The following forms, or derivatives, are also possible but deprecated:
  21. *
  22. * foo = new ((mozilla::fallible_t())) Foo();
  23. *
  24. * const mozilla::fallible_t fallible = mozilla::fallible_t();
  25. * bar = new (f) Bar();
  26. *
  27. * It is also possible to declare method overloads with fallible allocation
  28. * alternatives, like so:
  29. *
  30. * class Foo {
  31. * public:
  32. * void Method(void *);
  33. * void Method(void *, const mozilla::fallible_t&);
  34. * };
  35. *
  36. * Foo foo;
  37. * foo.Method(nullptr, mozilla::fallible);
  38. *
  39. * If that last method call is in a method that itself takes a const
  40. * fallible_t& argument, it is recommended to propagate that argument
  41. * instead of using mozilla::fallible:
  42. *
  43. * void Func(Foo &foo, const mozilla::fallible_t& aFallible) {
  44. * foo.Method(nullptr, aFallible);
  45. * }
  46. *
  47. */
  48. namespace mozilla {
  49. struct fallible_t { };
  50. /* This symbol is kept unexported, such that in corner cases where the
  51. * compiler can't remove its use (essentially, cross compilation-unit
  52. * calls), the smallest machine code is used.
  53. * Depending how the linker packs symbols, it will consume between 1 and
  54. * 8 bytes of read-only data in each executable or shared library, but
  55. * only in those where it's actually not optimized out by the compiler.
  56. */
  57. extern const fallible_t fallible;
  58. } // namespace mozilla
  59. #endif
  60. #endif // mozilla_fallible_h