[libcxx-commits] [libcxx] [libc++] Provide sized deallocation declarations even when the compiler doesn't support sized deallocation (PR #125577)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Tue Feb 4 11:38:51 PST 2025


https://github.com/ldionne updated https://github.com/llvm/llvm-project/pull/125577

>From 35aeeb6e70b63999f610e479b4d11db20213551d Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Mon, 3 Feb 2025 15:47:01 -0500
Subject: [PATCH 1/2] [libc++] Provide sized deallocation declarations even
 when the compiler doesn't support sized deallocation

After ef804d8f9b4, we stopped providing the declaration of sized deallocation
functions unless the compiler provides support for the language feature.
In reality, we can still provide the declarations of global operator delete
for users who want to call these operators directly without going through
the compiler rewrite.
---
 libcxx/include/__new/global_new_delete.h      |  2 +-
 ...zed_delete.fno-sized-deallocation.pass.cpp | 27 +++++++++++++++++++
 2 files changed, 28 insertions(+), 1 deletion(-)
 create mode 100644 libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/sized_delete.fno-sized-deallocation.pass.cpp

diff --git a/libcxx/include/__new/global_new_delete.h b/libcxx/include/__new/global_new_delete.h
index 96510ab56b00b5..3d1f7b6f3d2409 100644
--- a/libcxx/include/__new/global_new_delete.h
+++ b/libcxx/include/__new/global_new_delete.h
@@ -25,7 +25,7 @@
 #  define _THROW_BAD_ALLOC
 #endif
 
-#if defined(__cpp_sized_deallocation) && __cpp_sized_deallocation >= 201309L
+#if _LIBCPP_STD_VER >= 14
 #  define _LIBCPP_HAS_SIZED_DEALLOCATION 1
 #else
 #  define _LIBCPP_HAS_SIZED_DEALLOCATION 0
diff --git a/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/sized_delete.fno-sized-deallocation.pass.cpp b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/sized_delete.fno-sized-deallocation.pass.cpp
new file mode 100644
index 00000000000000..daa0374fd28125
--- /dev/null
+++ b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/sized_delete.fno-sized-deallocation.pass.cpp
@@ -0,0 +1,27 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// Ensure that libc++ still provides the declaration of sized operator delete even
+// when sized deallocation support is disabled at the language level, since it should
+// still be valid to call these operators explicitly (as opposed to via a compiler
+// rewrite of a delete expression).
+
+// UNSUPPORTED: c++03, c++11
+
+// ADDITIONAL_COMPILE_FLAGS: -fno-sized-deallocation
+
+// Sized deallocation support was introduced in LLVM 11
+// XFAIL: using-built-library-before-llvm-11
+
+#include <new>
+
+int main(int, char**) {
+    void* p = ::operator new(10);
+    ::operator delete(p, 10);
+    return 0;
+}

>From e6864e9df0ada615f24eba42829986da527e50bb Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Tue, 4 Feb 2025 14:33:16 -0500
Subject: [PATCH 2/2] Format

---
 .../sized_delete.fno-sized-deallocation.pass.cpp            | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/sized_delete.fno-sized-deallocation.pass.cpp b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/sized_delete.fno-sized-deallocation.pass.cpp
index daa0374fd28125..f6145cb1eba741 100644
--- a/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/sized_delete.fno-sized-deallocation.pass.cpp
+++ b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/sized_delete.fno-sized-deallocation.pass.cpp
@@ -21,7 +21,7 @@
 #include <new>
 
 int main(int, char**) {
-    void* p = ::operator new(10);
-    ::operator delete(p, 10);
-    return 0;
+  void* p = ::operator new(10);
+  ::operator delete(p, 10);
+  return 0;
 }



More information about the libcxx-commits mailing list