2020// in this shared library, so that they can be overridden by programs
2121// that define non-weak copies of the functions.
2222
23- _LIBCPP_WEAK void * operator new (std::size_t size) _THROW_BAD_ALLOC {
23+ static void * operator_new_impl (std::size_t size) noexcept {
2424 if (size == 0 )
2525 size = 1 ;
2626 void * p;
@@ -31,15 +31,20 @@ _LIBCPP_WEAK void* operator new(std::size_t size) _THROW_BAD_ALLOC {
3131 if (nh)
3232 nh ();
3333 else
34- # ifndef _LIBCPP_HAS_NO_EXCEPTIONS
35- throw std::bad_alloc ();
36- # else
3734 break ;
38- # endif
3935 }
4036 return p;
4137}
4238
39+ _LIBCPP_WEAK void * operator new (std::size_t size) _THROW_BAD_ALLOC {
40+ void * p = operator_new_impl (size);
41+ # ifndef _LIBCPP_HAS_NO_EXCEPTIONS
42+ if (p == nullptr )
43+ throw std::bad_alloc ();
44+ # endif
45+ return p;
46+ }
47+
4348_LIBCPP_WEAK void * operator new (size_t size, const std::nothrow_t &) noexcept {
4449 void * p = nullptr ;
4550# ifndef _LIBCPP_HAS_NO_EXCEPTIONS
@@ -82,7 +87,7 @@ _LIBCPP_WEAK void operator delete[](void* ptr, size_t) noexcept { ::operator del
8287
8388# if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)
8489
85- _LIBCPP_WEAK void * operator new (std::size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC {
90+ static void * operator_new_aligned_impl (std::size_t size, std::align_val_t alignment) noexcept {
8691 if (size == 0 )
8792 size = 1 ;
8893 if (static_cast <size_t >(alignment) < sizeof (void *))
@@ -91,25 +96,26 @@ _LIBCPP_WEAK void* operator new(std::size_t size, std::align_val_t alignment) _T
9196 // Try allocating memory. If allocation fails and there is a new_handler,
9297 // call it to try free up memory, and try again until it succeeds, or until
9398 // the new_handler decides to terminate.
94- //
95- // If allocation fails and there is no new_handler, we throw bad_alloc
96- // (or return nullptr if exceptions are disabled).
9799 void * p;
98100 while ((p = std::__libcpp_aligned_alloc (static_cast <std::size_t >(alignment), size)) == nullptr ) {
99101 std::new_handler nh = std::get_new_handler ();
100102 if (nh)
101103 nh ();
102- else {
103- # ifndef _LIBCPP_HAS_NO_EXCEPTIONS
104- throw std::bad_alloc ();
105- # else
104+ else
106105 break ;
107- # endif
108- }
109106 }
110107 return p;
111108}
112109
110+ _LIBCPP_WEAK void * operator new (std::size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC {
111+ void * p = operator_new_aligned_impl (size, alignment);
112+ # ifndef _LIBCPP_HAS_NO_EXCEPTIONS
113+ if (p == nullptr )
114+ throw std::bad_alloc ();
115+ # endif
116+ return p;
117+ }
118+
113119_LIBCPP_WEAK void * operator new (size_t size, std::align_val_t alignment, const std::nothrow_t &) noexcept {
114120 void * p = nullptr ;
115121# ifndef _LIBCPP_HAS_NO_EXCEPTIONS
0 commit comments