Bug 1423803 - Turn mozilla::fallible into an alias for std::nothrow. r?njn draft
authorMike Hommey <mh+mozilla@glandium.org>
Thu, 07 Dec 2017 11:32:29 +0900
changeset 708759 4f99d5947995ce43fafbf4b19eb1ae7fc0b15fd3
parent 708706 4b94da21a9e6171f9911ffad171af23c26e6227b
child 708774 7a4b5f27d61a49bcccb84904a7d8d5002ba906a1
push id92437
push userbmo:mh+mozilla@glandium.org
push dateThu, 07 Dec 2017 03:10:24 +0000
reviewersnjn
bugs1423803, 1423802
milestone59.0a1
Bug 1423803 - Turn mozilla::fallible into an alias for std::nothrow. r?njn The std::nothrow variant of operator new is effectively a fallible operator new. It is used in third party code. The duplication with our own fallible operator new is unfortunate, and we can reduce it by making one an alias of the other. We keep the fallible library as a dummy on Android because bug 1423802 induces some linking problems.
build/gecko_templates.mozbuild
memory/fallible/fallible.cpp
memory/fallible/fallible.h
memory/fallible/moz.build
memory/mozalloc/mozalloc.h
xpcom/glue/standalone/moz.build
--- a/build/gecko_templates.mozbuild
+++ b/build/gecko_templates.mozbuild
@@ -45,17 +45,18 @@ def GeckoBinary(linkage='dependent', moz
                 LDFLAGS += ['-rdynamic']
         elif mozglue == 'library':
             LIBRARY_DEFINES['MOZ_HAS_MOZGLUE'] = True
             if not CONFIG['MOZ_GLUE_IN_PROGRAM']:
                 USE_LIBS += ['mozglue']
         else:
             error('`mozglue` must be "program" or "library"')
 
-    if CONFIG['MOZ_WIDGET_TOOLKIT']:
+    if CONFIG['MOZ_WIDGET_TOOLKIT'] and CONFIG['OS_TARGET'] == 'Android':
+        # Keep a dummy library until bug 1423802 is fixed.
         USE_LIBS += [
             'fallible',
         ]
 
 
 @template
 def GeckoProgram(name, linkage='standalone', **kwargs):
     '''Template for program executables related to Gecko.
--- a/memory/fallible/fallible.cpp
+++ b/memory/fallible/fallible.cpp
@@ -1,11 +1,1 @@
-/* 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/. */
-
-#include "fallible.h"
-
-namespace mozilla {
-
-const fallible_t fallible = {};
-
-} // namespace mozilla
+// Keep this file empty until bug 1423802 is fixed.
--- a/memory/fallible/fallible.h
+++ b/memory/fallible/fallible.h
@@ -43,26 +43,22 @@
  * 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 {
 
-struct fallible_t { };
+using fallible_t = std::nothrow_t;
 
-/* This symbol is kept unexported, such that in corner cases where the
- * compiler can't remove its use (essentially, cross compilation-unit
- * calls), the smallest machine code is used.
- * Depending how the linker packs symbols, it will consume between 1 and
- * 8 bytes of read-only data in each executable or shared library, but
- * only in those where it's actually not optimized out by the compiler.
- */
-extern const fallible_t fallible;
+const fallible_t fallible = std::nothrow;
 
 } // namespace mozilla
 
 #endif
 
 #endif // mozilla_fallible_h
--- a/memory/fallible/moz.build
+++ b/memory/fallible/moz.build
@@ -3,32 +3,15 @@
 # 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/.
 
 EXPORTS.mozilla += [
     'fallible.h',
 ]
 
-Library('fallible')
-
-SOURCES += [
-    'fallible.cpp',
-]
+if CONFIG['OS_TARGET'] == 'Android':
+    # Keep a dummy library until bug 1423802 is fixed.
+    Library('fallible')
 
-if CONFIG['_MSC_VER']:
-    # MSVC normally adds linker directives relative to the CRT in a .drectve
-    # section in .obj files. Then, when linking objects, it adds those
-    # directives as if they were given as command line arguments. This can
-    # lead to trying to include link CRTs because different objects are
-    # compiled with different CRT options (i.e. -MT vs. -MD), and failing.
-    # The only source in this directory doesn't expose anything that depends
-    # on a CRT, so it doesn't need to be bound to a specific one.
-    # Adding the -Zl option makes MSVC not store linker directives in the
-    # object. This allows to link fallible.obj to binaries independently of
-    # the CRT they use.
-    CXXFLAGS += [
-        '-Zl',
+    SOURCES += [
+        'fallible.cpp',
     ]
-
-    # This further prevents the CRT name from getting into the .obj file,
-    # by avoiding pulling in a bunch of string code that uses the CRT.
-    DEFINES['mozilla_Char16_h'] = True
--- a/memory/mozalloc/mozalloc.h
+++ b/memory/mozalloc/mozalloc.h
@@ -102,23 +102,24 @@ MOZ_END_EXTERN_C
  * libmozalloc, replacing their definitions in libstdc++.  The
  * operator new* definitions in libmozalloc will never return a NULL
  * pointer.
  *
  * Each operator new immediately below returns a pointer to memory
  * that can be delete'd by any of
  *
  *   (1) the matching infallible operator delete immediately below
- *   (2) the matching "fallible" operator delete further below
- *   (3) the matching system |operator delete(void*, std::nothrow)|
- *   (4) the matching system |operator delete(void*) throw(std::bad_alloc)|
+ *   (2) the matching system |operator delete(void*, std::nothrow)|
+ *   (3) the matching system |operator delete(void*) throw(std::bad_alloc)|
  *
  * NB: these are declared |throw(std::bad_alloc)|, though they will never
  * throw that exception.  This declaration is consistent with the rule
  * that |::operator new() throw(std::bad_alloc)| will never return NULL.
+ *
+ * NB: mozilla::fallible can be used instead of std::nothrow.
  */
 
 /* NB: This is defined just to silence vacuous warnings about symbol
  * visibility on OS X/gcc. These symbols are force-inline and not
  * exported. */
 #if defined(XP_MACOSX)
 #  define MOZALLOC_EXPORT_NEW MFBT_API
 #else
@@ -195,60 +196,16 @@ void operator delete[](void* ptr) MOZALL
 MOZALLOC_EXPORT_NEW MOZ_ALWAYS_INLINE_EVEN_DEBUG
 void operator delete[](void* ptr, const std::nothrow_t&) MOZALLOC_THROW_IF_HAS_EXCEPTIONS
 {
     return free_impl(ptr);
 }
 
 
 /*
- * We also add a new allocator variant: "fallible operator new."
- * Unlike libmozalloc's implementations of the standard nofail
- * allocators, this allocator is allowed to return NULL.  It can be used
- * as follows
- *
- *   Foo* f = new (mozilla::fallible) Foo(...);
- *
- * operator delete(fallible) is defined for completeness only.
- *
- * Each operator new below returns a pointer to memory that can be
- * delete'd by any of
- *
- *   (1) the matching "fallible" operator delete below
- *   (2) the matching infallible operator delete above
- *   (3) the matching system |operator delete(void*, std::nothrow)|
- *   (4) the matching system |operator delete(void*) throw(std::bad_alloc)|
- */
-
-MOZ_ALWAYS_INLINE_EVEN_DEBUG
-void* operator new(size_t size, const mozilla::fallible_t&) MOZALLOC_THROW_IF_HAS_EXCEPTIONS
-{
-    return malloc_impl(size);
-}
-
-MOZ_ALWAYS_INLINE_EVEN_DEBUG
-void* operator new[](size_t size, const mozilla::fallible_t&) MOZALLOC_THROW_IF_HAS_EXCEPTIONS
-{
-    return malloc_impl(size);
-}
-
-MOZ_ALWAYS_INLINE_EVEN_DEBUG
-void operator delete(void* ptr, const mozilla::fallible_t&) MOZALLOC_THROW_IF_HAS_EXCEPTIONS
-{
-    free_impl(ptr);
-}
-
-MOZ_ALWAYS_INLINE_EVEN_DEBUG
-void operator delete[](void* ptr, const mozilla::fallible_t&) MOZALLOC_THROW_IF_HAS_EXCEPTIONS
-{
-    free_impl(ptr);
-}
-
-
-/*
  * This policy is identical to MallocAllocPolicy, except it uses
  * moz_xmalloc/moz_xcalloc/moz_xrealloc instead of
  * malloc/calloc/realloc.
  */
 class InfallibleAllocPolicy
 {
 public:
     template <typename T>
--- a/xpcom/glue/standalone/moz.build
+++ b/xpcom/glue/standalone/moz.build
@@ -24,20 +24,15 @@ DEFINES['XPCOM_GLUE'] = True
 LOCAL_INCLUDES += [
     '../../build',
     '../../threads',
 ]
 
 # Don't use STL wrappers here (i.e. wrapped <new>); they require mozalloc
 DisableStlWrapping()
 
-# Include fallible for third party code using the xpcom glue
-USE_LIBS += [
-    'fallible',
-]
-
 # Force to build a static library only
 NO_EXPAND_LIBS = True
 
 DIST_INSTALL = True
 
 if CONFIG['MOZ_WIDGET_TOOLKIT'] in ('gtk2', 'gtk3'):
     CXXFLAGS += CONFIG['GLIB_CFLAGS']