20
20
// in this shared library, so that they can be overridden by programs
21
21
// that define non-weak copies of the functions.
22
22
23
- _LIBCPP_WEAK void * operator new (std::size_t size) _THROW_BAD_ALLOC {
23
+ static void * operator_new_impl (std::size_t size) noexcept {
24
24
if (size == 0 )
25
25
size = 1 ;
26
26
void * p;
@@ -31,15 +31,20 @@ _LIBCPP_WEAK void* operator new(std::size_t size) _THROW_BAD_ALLOC {
31
31
if (nh)
32
32
nh ();
33
33
else
34
- # ifndef _LIBCPP_HAS_NO_EXCEPTIONS
35
- throw std::bad_alloc ();
36
- # else
37
34
break ;
38
- # endif
39
35
}
40
36
return p;
41
37
}
42
38
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
+
43
48
_LIBCPP_WEAK void * operator new (size_t size, const std::nothrow_t &) noexcept {
44
49
void * p = nullptr ;
45
50
# ifndef _LIBCPP_HAS_NO_EXCEPTIONS
@@ -82,7 +87,7 @@ _LIBCPP_WEAK void operator delete[](void* ptr, size_t) noexcept { ::operator del
82
87
83
88
# if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)
84
89
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 {
86
91
if (size == 0 )
87
92
size = 1 ;
88
93
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
91
96
// Try allocating memory. If allocation fails and there is a new_handler,
92
97
// call it to try free up memory, and try again until it succeeds, or until
93
98
// 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).
97
99
void * p;
98
100
while ((p = std::__libcpp_aligned_alloc (static_cast <std::size_t >(alignment), size)) == nullptr ) {
99
101
std::new_handler nh = std::get_new_handler ();
100
102
if (nh)
101
103
nh ();
102
- else {
103
- # ifndef _LIBCPP_HAS_NO_EXCEPTIONS
104
- throw std::bad_alloc ();
105
- # else
104
+ else
106
105
break ;
107
- # endif
108
- }
109
106
}
110
107
return p;
111
108
}
112
109
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
+
113
119
_LIBCPP_WEAK void * operator new (size_t size, std::align_val_t alignment, const std::nothrow_t &) noexcept {
114
120
void * p = nullptr ;
115
121
# ifndef _LIBCPP_HAS_NO_EXCEPTIONS
0 commit comments