// Support routines for the -*- C++ -*- dynamic memory management.
-// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2004, 2009
-// Free Software Foundation
+// Copyright (C) 1997-2024 Free Software Foundation, Inc.
//
// This file is part of GCC.
//
// <http://www.gnu.org/licenses/>.
#include <bits/c++config.h>
-#include <exception_defines.h>
+#include <bits/exception_defines.h>
#include "new"
-using std::new_handler;
-using std::bad_alloc;
-
extern "C" void *malloc (std::size_t);
-extern new_handler __new_handler;
_GLIBCXX_WEAK_DEFINITION void *
-operator new (std::size_t sz, const std::nothrow_t&) throw()
+operator new (std::size_t sz, const std::nothrow_t&) noexcept
{
- void *p;
-
- /* malloc (0) is unpredictable; avoid it. */
- if (sz == 0)
- sz = 1;
- p = (void *) malloc (sz);
- while (p == 0)
+ // _GLIBCXX_RESOLVE_LIB_DEFECTS
+ // 206. operator new(size_t, nothrow) may become unlinked to ordinary
+ // operator new if ordinary version replaced
+ __try
{
- new_handler handler = __new_handler;
- if (! handler)
- return 0;
- __try
- {
- handler ();
- }
- __catch(const bad_alloc&)
- {
- return 0;
- }
-
- p = (void *) malloc (sz);
+ return ::operator new(sz);
+ }
+ __catch (...)
+ {
+ // N.B. catch (...) means the process will terminate if operator new(sz)
+ // exits with a __forced_unwind exception. The process will print
+ // "FATAL: exception not rethrown" to stderr before exiting.
+ //
+ // If we propagated that exception the process would still terminate
+ // (because this function is noexcept) but with a less informative error:
+ // "terminate called without active exception".
+ return nullptr;
}
-
- return p;
}