[libcxx-commits] [libcxx] [z/OS][libc++] Guard to be used only when `align_val_t` is available (PR #114396)

Zibi Sarbinowski via libcxx-commits libcxx-commits at lists.llvm.org
Wed Nov 6 06:28:32 PST 2024


https://github.com/zibi2 updated https://github.com/llvm/llvm-project/pull/114396

>From a83b5caf569b1aabfe347f5d84f6a2c9f518333e Mon Sep 17 00:00:00 2001
From: Zbigniew Sarbinowski <zibi at ca.ibm.com>
Date: Thu, 31 Oct 2024 12:08:18 +0000
Subject: [PATCH 1/5] Guard  to be used only when they are available

---
 libcxx/include/__utility/small_buffer.h | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/libcxx/include/__utility/small_buffer.h b/libcxx/include/__utility/small_buffer.h
index 70e068f89f62ed..6071bda4513657 100644
--- a/libcxx/include/__utility/small_buffer.h
+++ b/libcxx/include/__utility/small_buffer.h
@@ -61,6 +61,7 @@ class __small_buffer {
       return *std::launder(reinterpret_cast<_Stored**>(__buffer_));
   }
 
+#  ifdef _LIBCPP_HAS_LIBRARY_ALIGNED_ALLOCATION
   template <class _Stored>
   _LIBCPP_HIDE_FROM_ABI _Stored* __alloc() {
     if constexpr (__fits_in_buffer<_Stored>) {
@@ -72,11 +73,14 @@ class __small_buffer {
     }
   }
 
+#  endif // _LIBCPP_HAS_LIBRARY_ALIGNED_ALLOCATION
+#  ifdef _LIBCPP_HAS_LIBRARY_SIZED_DEALLOCATION
   template <class _Stored>
   _LIBCPP_HIDE_FROM_ABI void __dealloc() noexcept {
     if constexpr (!__fits_in_buffer<_Stored>)
       ::operator delete[](*reinterpret_cast<void**>(__buffer_), sizeof(_Stored), align_val_t{alignof(_Stored)});
   }
+#  endif // _LIBCPP_HAS_LIBRARY_SIZED_DEALLOCATION
 
   template <class _Stored, class... _Args>
   _LIBCPP_HIDE_FROM_ABI void __construct(_Args&&... __args) {

>From ff95378feeb7ef5790aa7d3f14f409d61d11300d Mon Sep 17 00:00:00 2001
From: Zbigniew Sarbinowski <zibi at ca.ibm.com>
Date: Thu, 31 Oct 2024 19:49:21 +0000
Subject: [PATCH 2/5] Add __libcpp_allocate and __libcpp_deallocate in the
 absense of align allocation and sized deallocation

---
 libcxx/include/__utility/small_buffer.h | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/libcxx/include/__utility/small_buffer.h b/libcxx/include/__utility/small_buffer.h
index 6071bda4513657..a99df30e80c794 100644
--- a/libcxx/include/__utility/small_buffer.h
+++ b/libcxx/include/__utility/small_buffer.h
@@ -61,26 +61,30 @@ class __small_buffer {
       return *std::launder(reinterpret_cast<_Stored**>(__buffer_));
   }
 
-#  ifdef _LIBCPP_HAS_LIBRARY_ALIGNED_ALLOCATION
   template <class _Stored>
   _LIBCPP_HIDE_FROM_ABI _Stored* __alloc() {
     if constexpr (__fits_in_buffer<_Stored>) {
       return std::launder(reinterpret_cast<_Stored*>(__buffer_));
     } else {
+#  ifdef _LIBCPP_HAS_LIBRARY_ALIGNED_ALLOCATION
       byte* __allocation = static_cast<byte*>(::operator new[](sizeof(_Stored), align_val_t{alignof(_Stored)}));
       std::construct_at(reinterpret_cast<byte**>(__buffer_), __allocation);
       return std::launder(reinterpret_cast<_Stored*>(__allocation));
+#  else
+      return static_cast<_Stored*>(std::__libcpp_allocate(_BufferSize* sizeof(byte), _LIBCPP_ALIGNOF(byte)));
+#  endif // _LIBCPP_HAS_LIBRARY_ALIGNED_ALLOCATION
     }
   }
 
-#  endif // _LIBCPP_HAS_LIBRARY_ALIGNED_ALLOCATION
-#  ifdef _LIBCPP_HAS_LIBRARY_SIZED_DEALLOCATION
   template <class _Stored>
   _LIBCPP_HIDE_FROM_ABI void __dealloc() noexcept {
     if constexpr (!__fits_in_buffer<_Stored>)
+#  ifdef _LIBCPP_HAS_LIBRARY_SIZED_DEALLOCATION
       ::operator delete[](*reinterpret_cast<void**>(__buffer_), sizeof(_Stored), align_val_t{alignof(_Stored)});
-  }
+#  else
+      std::__libcpp_deallocate((void*)__buffer_, _BufferSize * sizeof(byte), _LIBCPP_ALIGNOF(_Stored));
 #  endif // _LIBCPP_HAS_LIBRARY_SIZED_DEALLOCATION
+  }
 
   template <class _Stored, class... _Args>
   _LIBCPP_HIDE_FROM_ABI void __construct(_Args&&... __args) {

>From f05f1448144f12826c97fa4d09dd62718fd3923a Mon Sep 17 00:00:00 2001
From: Zbigniew Sarbinowski <zibi at ca.ibm.com>
Date: Thu, 31 Oct 2024 19:58:03 +0000
Subject: [PATCH 3/5] formattig

---
 libcxx/include/__utility/small_buffer.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libcxx/include/__utility/small_buffer.h b/libcxx/include/__utility/small_buffer.h
index a99df30e80c794..f7fedf4281dee2 100644
--- a/libcxx/include/__utility/small_buffer.h
+++ b/libcxx/include/__utility/small_buffer.h
@@ -71,7 +71,7 @@ class __small_buffer {
       std::construct_at(reinterpret_cast<byte**>(__buffer_), __allocation);
       return std::launder(reinterpret_cast<_Stored*>(__allocation));
 #  else
-      return static_cast<_Stored*>(std::__libcpp_allocate(_BufferSize* sizeof(byte), _LIBCPP_ALIGNOF(byte)));
+      return static_cast<_Stored*>(std::__libcpp_allocate(_BufferSize * sizeof(byte), _LIBCPP_ALIGNOF(byte)));
 #  endif // _LIBCPP_HAS_LIBRARY_ALIGNED_ALLOCATION
     }
   }

>From 315a9229c167f8b38d419e588abbbc37637b7c24 Mon Sep 17 00:00:00 2001
From: Zbigniew Sarbinowski <zibi at ca.ibm.com>
Date: Mon, 4 Nov 2024 20:13:51 +0000
Subject: [PATCH 4/5] Simplify __alloc() and __dealloc() in
 __utility/small_buffer.h

---
 libcxx/include/__utility/small_buffer.h | 14 ++------------
 1 file changed, 2 insertions(+), 12 deletions(-)

diff --git a/libcxx/include/__utility/small_buffer.h b/libcxx/include/__utility/small_buffer.h
index f7fedf4281dee2..71b725757a7f57 100644
--- a/libcxx/include/__utility/small_buffer.h
+++ b/libcxx/include/__utility/small_buffer.h
@@ -66,24 +66,14 @@ class __small_buffer {
     if constexpr (__fits_in_buffer<_Stored>) {
       return std::launder(reinterpret_cast<_Stored*>(__buffer_));
     } else {
-#  ifdef _LIBCPP_HAS_LIBRARY_ALIGNED_ALLOCATION
-      byte* __allocation = static_cast<byte*>(::operator new[](sizeof(_Stored), align_val_t{alignof(_Stored)}));
-      std::construct_at(reinterpret_cast<byte**>(__buffer_), __allocation);
-      return std::launder(reinterpret_cast<_Stored*>(__allocation));
-#  else
-      return static_cast<_Stored*>(std::__libcpp_allocate(_BufferSize * sizeof(byte), _LIBCPP_ALIGNOF(byte)));
-#  endif // _LIBCPP_HAS_LIBRARY_ALIGNED_ALLOCATION
+      return static_cast<_Stored*>(std::__libcpp_allocate(_BufferSize * sizeof(byte), alignof(_Stored)));
     }
   }
 
   template <class _Stored>
   _LIBCPP_HIDE_FROM_ABI void __dealloc() noexcept {
     if constexpr (!__fits_in_buffer<_Stored>)
-#  ifdef _LIBCPP_HAS_LIBRARY_SIZED_DEALLOCATION
-      ::operator delete[](*reinterpret_cast<void**>(__buffer_), sizeof(_Stored), align_val_t{alignof(_Stored)});
-#  else
-      std::__libcpp_deallocate((void*)__buffer_, _BufferSize * sizeof(byte), _LIBCPP_ALIGNOF(_Stored));
-#  endif // _LIBCPP_HAS_LIBRARY_SIZED_DEALLOCATION
+      std::__libcpp_deallocate((void*)__buffer_, _BufferSize * sizeof(byte), alignof(_Stored));
   }
 
   template <class _Stored, class... _Args>

>From af06112eef62d3c2a7b60102e7f5b7b51d3b979e Mon Sep 17 00:00:00 2001
From: Zbigniew Sarbinowski <zibi at ca.ibm.com>
Date: Wed, 6 Nov 2024 14:41:26 +0000
Subject: [PATCH 5/5] Correct previous simplification in
 __small_buffer::__alloc()

---
 libcxx/include/__utility/small_buffer.h | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/libcxx/include/__utility/small_buffer.h b/libcxx/include/__utility/small_buffer.h
index 71b725757a7f57..4aef5bb4241d46 100644
--- a/libcxx/include/__utility/small_buffer.h
+++ b/libcxx/include/__utility/small_buffer.h
@@ -66,7 +66,9 @@ class __small_buffer {
     if constexpr (__fits_in_buffer<_Stored>) {
       return std::launder(reinterpret_cast<_Stored*>(__buffer_));
     } else {
-      return static_cast<_Stored*>(std::__libcpp_allocate(_BufferSize * sizeof(byte), alignof(_Stored)));
+      byte* __allocation = static_cast<byte*>(std::__libcpp_allocate(sizeof(_Stored), alignof(_Stored)));
+      std::construct_at(reinterpret_cast<byte**>(__buffer_), __allocation);
+      return std::launder(reinterpret_cast<_Stored*>(__allocation));
     }
   }
 



More information about the libcxx-commits mailing list