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

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Wed Aug 12 08:01:08 PDT 2026


================
@@ -211,3 +212,69 @@ 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
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+_LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS
+
+#if __has_cpp_attribute(gnu::ifunc)
+
+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);
+
+new_at_least_t* new_at_least_resolver() {
+  if (std::__is_function_overridden < void*(std::size_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(good_size), good_size};
+#elifdef __FreeBSD__
+      auto good_size = nallocx(size, 0);
+      return {operator_new_impl(good_size), good_size};
+#  else
+      return {operator_new_impl(size), size};
+#  endif
+    };
+  }
+}
+
+[[gnu::weak, gnu::ifunc("new_at_least_resolver")]] new_at_least_t __new_at_least;
+
+new_at_least_t* new_at_least_aligned_resolver() {
+  if (std::__is_function_overridden < void*(std::size_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);
----------------
ldionne wrote:

```suggestion
      auto good_size = ::malloc_good_size(size);
```

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


More information about the libcxx-commits mailing list