From e99c91211d38e5ab9791b6625693ff09b51393a0 Mon Sep 17 00:00:00 2001 From: hai007 Date: Wed, 29 Jan 2025 16:03:19 -0800 Subject: [PATCH] [cpp23] Remove usage of std::aligned_storage in nearby Deprecated in C++23. Details in https://crbug.com/388068052. PiperOrigin-RevId: 721140337 --- .../analytics/throughput_recorder.cc | 5 +-- .../mediums/multiplex/multiplex_socket.cc | 41 ++++++------------- .../implementation/windows/wifi_intel.cc | 2 +- internal/platform/medium_environment.cc | 4 +- 4 files changed, 16 insertions(+), 36 deletions(-) diff --git a/connections/implementation/analytics/throughput_recorder.cc b/connections/implementation/analytics/throughput_recorder.cc index 62d7ac2105..54b31d0552 100644 --- a/connections/implementation/analytics/throughput_recorder.cc +++ b/connections/implementation/analytics/throughput_recorder.cc @@ -43,9 +43,8 @@ ThroughputRecorder::ThroughputRecorder(int64_t payload_id) : payload_id_(payload_id) {} ThroughputRecorderContainer& ThroughputRecorderContainer::GetInstance() { - static std::aligned_storage_t - storage; + alignas(ThroughputRecorderContainer) static char + storage[sizeof(ThroughputRecorderContainer)]; static ThroughputRecorderContainer* env = new (&storage) ThroughputRecorderContainer(); return *env; diff --git a/connections/implementation/mediums/multiplex/multiplex_socket.cc b/connections/implementation/mediums/multiplex/multiplex_socket.cc index cb33894674..cd9ea191f1 100644 --- a/connections/implementation/mediums/multiplex/multiplex_socket.cc +++ b/connections/implementation/mediums/multiplex/multiplex_socket.cc @@ -95,17 +95,10 @@ MultiplexSocket::MultiplexSocket(std::shared_ptr physical_socket) absl::flat_hash_map, MultiplexIncomingConnectionCb>& MultiplexSocket::GetIncomingConnectionCallbacks() { - static std::aligned_storage_t< - sizeof(absl::flat_hash_map, - MultiplexIncomingConnectionCb>), - alignof(absl::flat_hash_map, - MultiplexIncomingConnectionCb>)> - storage; - static absl::flat_hash_map, - MultiplexIncomingConnectionCb>* - incoming_connection_callbacks = - new (&storage) absl::flat_hash_map, - MultiplexIncomingConnectionCb>(); + using MapType = absl::flat_hash_map, + MultiplexIncomingConnectionCb>; + alignas(MapType) static char storage[sizeof(MapType)]; + static MapType* incoming_connection_callbacks = new (&storage) MapType(); return *incoming_connection_callbacks; } @@ -121,23 +114,18 @@ MultiplexSocket* MultiplexSocket::CreateIncomingSocket( static MultiplexSocket* multiplex_incoming_socket = nullptr; switch (physical_socket->GetMedium()) { case Medium::BLUETOOTH: - static std::aligned_storage_t - storage_bt; + alignas(MultiplexSocket) static char storage_bt[sizeof(MultiplexSocket)]; multiplex_incoming_socket = new (&storage_bt) MultiplexSocket(physical_socket); break; case Medium::BLE: - static std::aligned_storage_t - storage_ble; + alignas(MultiplexSocket) static char storage_ble[sizeof(MultiplexSocket)]; multiplex_incoming_socket = new (&storage_ble) MultiplexSocket(physical_socket); break; case Medium::WIFI_LAN: - static std::aligned_storage_t - storage_wlan; + alignas( + MultiplexSocket) static char storage_wlan[sizeof(MultiplexSocket)]; multiplex_incoming_socket = new (&storage_wlan) MultiplexSocket(physical_socket); break; @@ -169,23 +157,18 @@ MultiplexSocket* MultiplexSocket::CreateOutgoingSocket( static MultiplexSocket* multiplex_outgoing_socket = nullptr; switch (physical_socket->GetMedium()) { case Medium::BLUETOOTH: - static std::aligned_storage_t - storage_bt; + alignas(MultiplexSocket) static char storage_bt[sizeof(MultiplexSocket)]; multiplex_outgoing_socket = new (&storage_bt) MultiplexSocket(physical_socket); break; case Medium::BLE: - static std::aligned_storage_t - storage_ble; + alignas(MultiplexSocket) static char storage_ble[sizeof(MultiplexSocket)]; multiplex_outgoing_socket = new (&storage_ble) MultiplexSocket(physical_socket); break; case Medium::WIFI_LAN: - static std::aligned_storage_t - storage_wlan; + alignas( + MultiplexSocket) static char storage_wlan[sizeof(MultiplexSocket)]; multiplex_outgoing_socket = new (&storage_wlan) MultiplexSocket(physical_socket); break; diff --git a/internal/platform/implementation/windows/wifi_intel.cc b/internal/platform/implementation/windows/wifi_intel.cc index 737dfc01d9..20daf8c05c 100644 --- a/internal/platform/implementation/windows/wifi_intel.cc +++ b/internal/platform/implementation/windows/wifi_intel.cc @@ -137,7 +137,7 @@ MurocDefs::INTEL_CALLBACK g_intel_event_cb_handle = {IntelEventHandler, #endif WifiIntel& WifiIntel::GetInstance() { - static std::aligned_storage_t storage; + alignas(WifiIntel) static char storage[sizeof(WifiIntel)]; static WifiIntel* instance = new (&storage) WifiIntel(); return *instance; } diff --git a/internal/platform/medium_environment.cc b/internal/platform/medium_environment.cc index 885a7a22af..1451865530 100644 --- a/internal/platform/medium_environment.cc +++ b/internal/platform/medium_environment.cc @@ -52,9 +52,7 @@ namespace nearby { MediumEnvironment& MediumEnvironment::Instance() { - static std::aligned_storage_t - storage; + alignas(MediumEnvironment) static char storage[sizeof(MediumEnvironment)]; static MediumEnvironment* env = new (&storage) MediumEnvironment(); return *env; }