[libcxx-commits] [libcxx] [libc++] Fix missing 'get_new_handler()' for Windows builds (PR #150182)

A. Jiang via libcxx-commits libcxx-commits at lists.llvm.org
Fri Dec 26 09:03:54 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
+}
----------------
frederick-vs-ja wrote:

> AFAICT the point of the previous implementation was that we shared the new handler with MSVC, which this doesn't do anymore IIUC.

I think that was the point too. But I gradually believe that it made a wrong assumption.

https://github.com/llvm/llvm-project/pull/150182


More information about the libcxx-commits mailing list