[libcxx-commits] [libcxx] [libc++] Add __new_at_least (PR #215813)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Tue Aug 25 02:28:37 PDT 2026


================
@@ -211,3 +217,80 @@ OVERRIDABLE_FUNCTION void* operator new[](size_t size, std::align_val_t alignmen
   ::operator delete[](ptr, alignment);
 }
 #endif // _LIBCPP_HAS_LIBRARY_ALIGNED_ALLOCATION
+
+#ifdef __APPLE__
+#  include <malloc/malloc.h>
+#elifdef __FreeBSD__
+#  include <malloc_np.h>
+#endif
+
+// FIXME: Clang should really accept functions in [[gnu::ifunc]] (or possibly [[clang::ifunc]])
+using std::__allocation_result;
+
+using new_t         = void*(std::size_t);
+using new_aligned_t = void*(std::size_t, std::align_val_t);
+
+using new_at_least_t         = __allocation_result<void*>(std::size_t);
+using new_at_least_aligned_t = __allocation_result<void*>(std::size_t, std::align_val_t);
+
+[[maybe_unused]] static new_at_least_t* new_at_least_resolver() {
+  if (std::__is_function_overridden < new_t, operator new>()) {
+    return [](std::size_t size) -> __allocation_result<void*> { return {::operator new(size), size}; };
+  } else {
+    return [](std::size_t size) -> __allocation_result<void*> {
+#ifdef __APPLE__
+      auto good_size = ::malloc_good_size(size);
+      return {operator_new_impl<on_failure::throw_bad_alloc>(good_size), good_size};
+#elifdef __FreeBSD__
+      auto good_size = ::nallocx(size, 0);
+      return {operator_new_impl<on_failure::throw_bad_alloc>(good_size), good_size};
+#else
+      // Other platforms should specialize this for their system allocator
+      return {operator_new_impl<on_failure::throw_bad_alloc>(size), size};
+#endif
+    };
+  }
+}
+
+[[maybe_unused]] static new_at_least_aligned_t* new_at_least_aligned_resolver() {
+  if (std::__is_function_overridden < new_aligned_t, operator new>()) {
----------------
philnik777 wrote:

I've added some tests for replaced new/deletes. Not sure if you'd like to see any more.

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


More information about the libcxx-commits mailing list