Skip to content

Commit 0938987

Browse files
committed
Merge Emscripten downstream changes to emscripten-libs-16
Given that I started emscripten-core/emscripten#20405 and OOO for nearly a month and then in the meantime llvm 17.0.4 came out, I'd like to update to not 17.0.3 but 17.0.4, with the most recent downstream changes. These are the downstream changes happened during the last month.
1 parent 3aeefe6 commit 0938987

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

libcxx/src/new.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,34 @@ operator new(std::size_t size) _THROW_BAD_ALLOC
9090
return p;
9191
}
9292

93+
#if defined(__EMSCRIPTEN__) && defined(_LIBCPP_NO_EXCEPTIONS)
94+
void* _new_nothrow(size_t size) noexcept
95+
{
96+
/// We cannot call ::operator new(size) here because it would abort
97+
/// when malloc returns 0 and exceptions are disabled.
98+
/// Expected behaviour of std::nothrow is to return 0 in that case.
99+
void* p = nullptr;
100+
if (size == 0)
101+
size = 1;
102+
while ((p = ::malloc(size)) == nullptr)
103+
{
104+
std::new_handler nh = std::get_new_handler();
105+
if (nh)
106+
nh();
107+
else
108+
break;
109+
}
110+
return p;
111+
}
112+
#endif
113+
93114
_LIBCPP_WEAK
94115
void*
95116
operator new(size_t size, const std::nothrow_t&) noexcept
96117
{
118+
#if defined(__EMSCRIPTEN__) && defined(_LIBCPP_NO_EXCEPTIONS)
119+
return _new_nothrow(size);
120+
#else
97121
void* p = nullptr;
98122
#ifndef _LIBCPP_NO_EXCEPTIONS
99123
try
@@ -107,6 +131,7 @@ operator new(size_t size, const std::nothrow_t&) noexcept
107131
}
108132
#endif // _LIBCPP_NO_EXCEPTIONS
109133
return p;
134+
#endif // __EMSCRIPTEN__ && _LIBCPP_NO_EXCEPTIONS
110135
}
111136

112137
_LIBCPP_WEAK
@@ -120,6 +145,9 @@ _LIBCPP_WEAK
120145
void*
121146
operator new[](size_t size, const std::nothrow_t&) noexcept
122147
{
148+
#if defined(__EMSCRIPTEN__) && defined(_LIBCPP_NO_EXCEPTIONS)
149+
return _new_nothrow(size);
150+
#else
123151
void* p = nullptr;
124152
#ifndef _LIBCPP_NO_EXCEPTIONS
125153
try
@@ -133,6 +161,7 @@ operator new[](size_t size, const std::nothrow_t&) noexcept
133161
}
134162
#endif // _LIBCPP_NO_EXCEPTIONS
135163
return p;
164+
#endif // __EMSCRIPTEN__ && _LIBCPP_NO_EXCEPTIONS
136165
}
137166

138167
_LIBCPP_WEAK

0 commit comments

Comments
 (0)