[libcxx-commits] [libcxx] [libc++] Fix missing 'get_new_handler()' for Windows builds (PR #150182)
via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Dec 15 03:03:16 PST 2025
================
@@ -26,11 +24,33 @@
#if defined(_LIBPCPP_DEFINE_NEW_HANDLER)
-namespace std { // purposefully not versioned
-
static constinit std::new_handler __new_handler = nullptr;
-new_handler set_new_handler(new_handler handler) noexcept { return __libcpp_atomic_exchange(&__new_handler, handler); }
+# ifdef _LIBCPP_ABI_VCRUNTIME
+// to avoid including <new.h>
+using _new_h = int(__cdecl*)(size_t);
+extern "C" _new_h __cdecl _set_new_handler(_new_h);
+
+namespace {
+// adapter for _callnewh
+int __cdecl _new_handler_adapter(size_t) {
+ std::__libcpp_atomic_load (&__new_handler)();
+ return 1;
+}
+} // namespace
+# endif
+
+namespace std { // purposefully not versioned
+
+new_handler set_new_handler(new_handler handler) noexcept {
+# ifdef _LIBCPP_ABI_VCRUNTIME
+ auto old = __libcpp_atomic_exchange(&__new_handler, handler);
+ _set_new_handler(handler ? _new_handler_adapter : nullptr);
+ return old;
+# else
+ return __libcpp_atomic_exchange(&__new_handler, handler);
+# endif
+}
----------------
siradam7th wrote:
It was suggested previously by @frederick-vs-ja https://github.com/llvm/llvm-project/pull/150182#discussion_r2278123257 and also https://github.com/llvm/llvm-project/pull/150182#discussion_r2225123600.
It is similar to how MSVC's STL implements it, this also fixes building `std` module for Windows else it would fail.
https://github.com/llvm/llvm-project/pull/150182
More information about the libcxx-commits
mailing list