From d0d826750f3f5eeb8d37de9448341d462944ebf4 Mon Sep 17 00:00:00 2001 From: "Romain Geissler @ Amadeus" Date: Tue, 12 Sep 2023 21:46:41 +0200 Subject: [PATCH] [C++] Stop using std::aligned_storage. (#34110) Indeed this is now deprecated since C++23. Fix #32848. --- include/grpcpp/server_context.h | 3 +-- src/core/lib/gprpp/manual_constructor.h | 3 +-- src/core/lib/gprpp/no_destruct.h | 2 +- src/core/lib/iomgr/event_engine_shims/endpoint.cc | 6 ++---- src/core/lib/promise/arena_promise.h | 6 +++++- test/core/gprpp/ref_counted_test.cc | 4 ++-- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/include/grpcpp/server_context.h b/include/grpcpp/server_context.h index 925316762fc..243d1492054 100644 --- a/include/grpcpp/server_context.h +++ b/include/grpcpp/server_context.h @@ -545,8 +545,7 @@ class ServerContextBase { const std::function func_; }; - typename std::aligned_storage::type - default_reactor_; + alignas(Reactor) char default_reactor_[sizeof(Reactor)]; std::atomic_bool default_reactor_used_{false}; std::atomic_bool marked_cancelled_{false}; diff --git a/src/core/lib/gprpp/manual_constructor.h b/src/core/lib/gprpp/manual_constructor.h index 09740e29551..6a3db686a42 100644 --- a/src/core/lib/gprpp/manual_constructor.h +++ b/src/core/lib/gprpp/manual_constructor.h @@ -25,7 +25,6 @@ #include -#include #include #include "src/core/lib/gprpp/construct_destruct.h" @@ -139,7 +138,7 @@ class ManualConstructor { void Destroy() { Destruct(get()); } private: - typename std::aligned_storage::type space_; + alignas(Type) char space_[sizeof(Type)]; }; } // namespace grpc_core diff --git a/src/core/lib/gprpp/no_destruct.h b/src/core/lib/gprpp/no_destruct.h index daf8b757770..8899c87477b 100644 --- a/src/core/lib/gprpp/no_destruct.h +++ b/src/core/lib/gprpp/no_destruct.h @@ -68,7 +68,7 @@ class NoDestruct { const T* get() const { return reinterpret_cast(&space_); } private: - typename std::aligned_storage::type space_; + alignas(T) char space_[sizeof(T)]; }; // Helper for when a program desires a single *process wide* instance of a diff --git a/src/core/lib/iomgr/event_engine_shims/endpoint.cc b/src/core/lib/iomgr/event_engine_shims/endpoint.cc index 20b6a158494..864a48b0e71 100644 --- a/src/core/lib/iomgr/event_engine_shims/endpoint.cc +++ b/src/core/lib/iomgr/event_engine_shims/endpoint.cc @@ -60,10 +60,8 @@ class EventEngineEndpointWrapper { struct grpc_event_engine_endpoint { grpc_endpoint base; EventEngineEndpointWrapper* wrapper; - std::aligned_storage::type - read_buffer; - std::aligned_storage::type - write_buffer; + alignas(SliceBuffer) char read_buffer[sizeof(SliceBuffer)]; + alignas(SliceBuffer) char write_buffer[sizeof(SliceBuffer)]; }; explicit EventEngineEndpointWrapper( diff --git a/src/core/lib/promise/arena_promise.h b/src/core/lib/promise/arena_promise.h index d6f0b288124..bdc5a484d8d 100644 --- a/src/core/lib/promise/arena_promise.h +++ b/src/core/lib/promise/arena_promise.h @@ -19,6 +19,7 @@ #include +#include #include #include #include @@ -34,7 +35,10 @@ namespace grpc_core { namespace arena_promise_detail { -using ArgType = std::aligned_storage_t; +struct ArgType { + alignas(std::max_align_t) char buffer[sizeof(void*)]; +}; + template T*& ArgAsPtr(ArgType* arg) { static_assert(sizeof(ArgType) >= sizeof(T**), diff --git a/test/core/gprpp/ref_counted_test.cc b/test/core/gprpp/ref_counted_test.cc index 0d58d1338e9..4d8761ecb1d 100644 --- a/test/core/gprpp/ref_counted_test.cc +++ b/test/core/gprpp/ref_counted_test.cc @@ -119,8 +119,8 @@ class ValueInExternalAllocation }; TEST(RefCounted, CallDtorUponUnref) { - std::aligned_storage::type storage; + alignas(ValueInExternalAllocation) char + storage[sizeof(ValueInExternalAllocation)]; RefCountedPtr value( new (&storage) ValueInExternalAllocation(5)); EXPECT_EQ(value->value(), 5);