Skip to content

Commit 0f3598a

Browse files
committed
implement 'set_new_handler' and 'get_new_handler' for Windows
1 parent c4ae877 commit 0f3598a

File tree

4 files changed

+29
-7
lines changed

4 files changed

+29
-7
lines changed

libcxx/include/__new/global_new_delete.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#endif
3333

3434
#if defined(_LIBCPP_ABI_VCRUNTIME)
35-
# include <new.h>
35+
#include <vcruntime_new.h>
3636
#else
3737
[[__nodiscard__]] _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz) _THROW_BAD_ALLOC;
3838
[[__nodiscard__]] _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, const std::nothrow_t&) _NOEXCEPT

libcxx/include/__new/nothrow_t.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#endif
1717

1818
#if defined(_LIBCPP_ABI_VCRUNTIME)
19-
# include <new.h>
19+
#include <vcruntime_new.h>
2020
#else
2121
_LIBCPP_BEGIN_UNVERSIONED_NAMESPACE_STD
2222
struct _LIBCPP_EXPORTED_FROM_ABI nothrow_t {

libcxx/include/__new/placement_new_delete.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#endif
1818

1919
#if defined(_LIBCPP_ABI_VCRUNTIME)
20-
# include <new.h>
20+
#include <vcruntime_new.h>
2121
#else
2222
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void*
2323
operator new(std::size_t, void* __p) _NOEXCEPT {

libcxx/src/new_handler.cpp

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,35 @@
2424

2525
#if defined(_LIBPCPP_DEFINE_NEW_HANDLER)
2626

27-
namespace std { // purposefully not versioned
28-
2927
static constinit std::new_handler __new_handler = nullptr;
3028

31-
new_handler set_new_handler(new_handler handler) noexcept { return __libcpp_atomic_exchange(&__new_handler, handler); }
29+
#ifdef _LIBCPP_ABI_VCRUNTIME
30+
// to avoid including <new.h>
31+
using _new_h = int(__cdecl*)(size_t);
32+
extern "C" _new_h __cdecl _set_new_handler(_new_h);
33+
34+
namespace {
35+
// adapter for _callnewh
36+
int __cdecl _new_handler_adapter(size_t) {
37+
std::__libcpp_atomic_load(&__new_handler)();
38+
return 1;
39+
}
40+
}
41+
#endif
42+
43+
namespace std { // purposefully not versioned
44+
45+
new_handler set_new_handler(new_handler handler) noexcept {
46+
#ifdef _LIBCPP_ABI_VCRUNTIME
47+
auto old = __libcpp_atomic_exchange(&__new_handler, handler);
48+
_set_new_handler(handler ? _new_handler_adapter: nullptr);
49+
return old;
50+
#else
51+
return __libcpp_atomic_exchange(&__new_handler, handler);
52+
#endif
53+
}
3254

33-
new_handler get_new_handler() noexcept { return __libcpp_atomic_load(&__new_handler); }
55+
new_handler get_new_handler() noexcept {return __libcpp_atomic_load(&__new_handler);}
3456

3557
} // namespace std
3658

0 commit comments

Comments
 (0)