Skip to content

Commit

Permalink
[cpp23] Remove usage of std::aligned_storage in nearby
Browse files Browse the repository at this point in the history
Deprecated in C++23. Details in https://crbug.com/388068052.

PiperOrigin-RevId: 722745132
  • Loading branch information
hai007 authored and copybara-github committed Feb 3, 2025
1 parent e76adea commit 97690c6
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 36 deletions.
5 changes: 2 additions & 3 deletions connections/implementation/analytics/throughput_recorder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ ThroughputRecorder::ThroughputRecorder(int64_t payload_id)
: payload_id_(payload_id) {}

ThroughputRecorderContainer& ThroughputRecorderContainer::GetInstance() {
static std::aligned_storage_t<sizeof(ThroughputRecorderContainer),
alignof(ThroughputRecorderContainer)>
storage;
alignas(ThroughputRecorderContainer) static char
storage[sizeof(ThroughputRecorderContainer)];
static ThroughputRecorderContainer* env =
new (&storage) ThroughputRecorderContainer();
return *env;
Expand Down
41 changes: 12 additions & 29 deletions connections/implementation/mediums/multiplex/multiplex_socket.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,10 @@ MultiplexSocket::MultiplexSocket(std::shared_ptr<MediumSocket> physical_socket)
absl::flat_hash_map<std::pair<std::string, Medium>,
MultiplexIncomingConnectionCb>&
MultiplexSocket::GetIncomingConnectionCallbacks() {
static std::aligned_storage_t<
sizeof(absl::flat_hash_map<std::pair<std::string, Medium>,
MultiplexIncomingConnectionCb>),
alignof(absl::flat_hash_map<std::pair<std::string, Medium>,
MultiplexIncomingConnectionCb>)>
storage;
static absl::flat_hash_map<std::pair<std::string, Medium>,
MultiplexIncomingConnectionCb>*
incoming_connection_callbacks =
new (&storage) absl::flat_hash_map<std::pair<std::string, Medium>,
MultiplexIncomingConnectionCb>();
using MapType = absl::flat_hash_map<std::pair<std::string, Medium>,
MultiplexIncomingConnectionCb>;
alignas(MapType) static char storage[sizeof(MapType)];
static MapType* incoming_connection_callbacks = new (&storage) MapType();

return *incoming_connection_callbacks;
}
Expand All @@ -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<sizeof(MultiplexSocket),
alignof(MultiplexSocket)>
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<sizeof(MultiplexSocket),
alignof(MultiplexSocket)>
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<sizeof(MultiplexSocket),
alignof(MultiplexSocket)>
storage_wlan;
alignas(
MultiplexSocket) static char storage_wlan[sizeof(MultiplexSocket)];
multiplex_incoming_socket =
new (&storage_wlan) MultiplexSocket(physical_socket);
break;
Expand Down Expand Up @@ -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<sizeof(MultiplexSocket),
alignof(MultiplexSocket)>
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<sizeof(MultiplexSocket),
alignof(MultiplexSocket)>
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<sizeof(MultiplexSocket),
alignof(MultiplexSocket)>
storage_wlan;
alignas(
MultiplexSocket) static char storage_wlan[sizeof(MultiplexSocket)];
multiplex_outgoing_socket =
new (&storage_wlan) MultiplexSocket(physical_socket);
break;
Expand Down
2 changes: 1 addition & 1 deletion internal/platform/implementation/windows/wifi_intel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ MurocDefs::INTEL_CALLBACK g_intel_event_cb_handle = {IntelEventHandler,
#endif

WifiIntel& WifiIntel::GetInstance() {
static std::aligned_storage_t<sizeof(WifiIntel), alignof(WifiIntel)> storage;
alignas(WifiIntel) static char storage[sizeof(WifiIntel)];
static WifiIntel* instance = new (&storage) WifiIntel();
return *instance;
}
Expand Down
4 changes: 1 addition & 3 deletions internal/platform/medium_environment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@
namespace nearby {

MediumEnvironment& MediumEnvironment::Instance() {
static std::aligned_storage_t<sizeof(MediumEnvironment),
alignof(MediumEnvironment)>
storage;
alignas(MediumEnvironment) static char storage[sizeof(MediumEnvironment)];
static MediumEnvironment* env = new (&storage) MediumEnvironment();
return *env;
}
Expand Down

0 comments on commit 97690c6

Please sign in to comment.