Skip to content

Merge Emscripten downstream changes to emscripten-libs-16 #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions libcxx/src/new.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,34 @@ operator new(std::size_t size) _THROW_BAD_ALLOC
return p;
}

#if defined(__EMSCRIPTEN__) && defined(_LIBCPP_NO_EXCEPTIONS)
void* _new_nothrow(size_t size) noexcept
{
/// We cannot call ::operator new(size) here because it would abort
/// when malloc returns 0 and exceptions are disabled.
/// Expected behaviour of std::nothrow is to return 0 in that case.
void* p = nullptr;
if (size == 0)
size = 1;
while ((p = ::malloc(size)) == nullptr)
{
std::new_handler nh = std::get_new_handler();
if (nh)
nh();
else
break;
}
return p;
}
#endif

_LIBCPP_WEAK
void*
operator new(size_t size, const std::nothrow_t&) noexcept
{
#if defined(__EMSCRIPTEN__) && defined(_LIBCPP_NO_EXCEPTIONS)
return _new_nothrow(size);
#else
void* p = nullptr;
#ifndef _LIBCPP_NO_EXCEPTIONS
try
Expand All @@ -107,6 +131,7 @@ operator new(size_t size, const std::nothrow_t&) noexcept
}
#endif // _LIBCPP_NO_EXCEPTIONS
return p;
#endif // __EMSCRIPTEN__ && _LIBCPP_NO_EXCEPTIONS
}

_LIBCPP_WEAK
Expand All @@ -120,6 +145,9 @@ _LIBCPP_WEAK
void*
operator new[](size_t size, const std::nothrow_t&) noexcept
{
#if defined(__EMSCRIPTEN__) && defined(_LIBCPP_NO_EXCEPTIONS)
return _new_nothrow(size);
#else
void* p = nullptr;
#ifndef _LIBCPP_NO_EXCEPTIONS
try
Expand All @@ -133,6 +161,7 @@ operator new[](size_t size, const std::nothrow_t&) noexcept
}
#endif // _LIBCPP_NO_EXCEPTIONS
return p;
#endif // __EMSCRIPTEN__ && _LIBCPP_NO_EXCEPTIONS
}

_LIBCPP_WEAK
Expand Down