From b5aad958a1f7c7e973c457b1c13ac4bc7e4eec77 Mon Sep 17 00:00:00 2001 From: BartolomeyKant Date: Tue, 4 Aug 2026 16:53:56 +0500 Subject: [PATCH 1/6] remove rc_ptr --- aether/aether_app.cpp | 4 +- aether/aether_app.h | 6 +- aether/aether_c/aether_capi.cpp | 18 +- aether/all.h | 1 - .../cloud_server_connection.cpp | 10 +- .../cloud_server_connection.h | 5 +- aether/config.h | 2 +- .../server_connection_manager.cpp | 13 +- .../server_connection_manager.h | 13 +- aether/events/event_deleter.cpp | 2 +- aether/events/event_deleter.h | 7 +- aether/events/events.h | 22 +- aether/ptr/ptr.h | 6 +- aether/ptr/rc_ptr.h | 306 ------------------ .../iserver_connection_factory.h | 5 +- .../a_b_message_exchange.cpp | 4 +- examples/benches/send_message_delays/main.cpp | 12 +- examples/common/aether_construct.h | 4 +- examples/common/aether_construct_esp_wifi.h | 18 +- examples/common/aether_construct_ethernet.h | 2 +- .../common/aether_construct_lora_module.h | 17 +- examples/common/aether_construct_modem.h | 27 +- tests/test-ptr/CMakeLists.txt | 3 - tests/test-ptr/main.cpp | 10 - tests/test-ptr/test-rc-ptr-bench.cpp | 96 ------ tests/test-ptr/test-rc-ptr.cpp | 194 ----------- tests/test-ptr/test-shared-ptr-bench.cpp | 96 ------ tools/registrator/registrator.cpp | 2 +- tools/registrator/registrator_action.cpp | 6 +- tools/registrator/registrator_action.h | 7 +- 30 files changed, 117 insertions(+), 801 deletions(-) delete mode 100644 aether/ptr/rc_ptr.h delete mode 100644 tests/test-ptr/test-rc-ptr-bench.cpp delete mode 100644 tests/test-ptr/test-rc-ptr.cpp delete mode 100644 tests/test-ptr/test-shared-ptr-bench.cpp diff --git a/aether/aether_app.cpp b/aether/aether_app.cpp index ef3f084a..00ff8031 100644 --- a/aether/aether_app.cpp +++ b/aether/aether_app.cpp @@ -323,11 +323,11 @@ void AetherAppContext::InitComponentContext() { #endif // AE_DISTILLATION } -RcPtr AetherApp::Construct(AetherAppContext context) { +std::unique_ptr AetherApp::Construct(AetherAppContext context) { // init all the components in context context.InitComponentContext(); - auto app = MakeRcPtr(); + auto app = std::unique_ptr{new AetherApp()}; app->aether_ = context.aether(); #if AE_DISTILLATION app->aether_->tele_statistics = context.tele_statistics_.Resolve(context); diff --git a/aether/aether_app.h b/aether/aether_app.h index d6e70f25..9c84f84a 100644 --- a/aether/aether_app.h +++ b/aether/aether_app.h @@ -24,9 +24,9 @@ #include "aether/common.h" #include "aether/config.h" +#include "aether/memory.h" #include "aether/obj/domain.h" #include "aether/ptr/ptr.h" -#include "aether/ptr/rc_ptr.h" #include "aether/actions/action.h" // IWYU pragma: keep #include "aether/events/events.h" // IWYU pragma: keep @@ -145,10 +145,8 @@ class AetherAppContext { * \brief The enter point to the Aethernet application world */ class AetherApp { - friend auto MakeRcPtr() noexcept; - public: - static RcPtr Construct(AetherAppContext context); + static std::unique_ptr Construct(AetherAppContext context); ~AetherApp(); diff --git a/aether/aether_c/aether_capi.cpp b/aether/aether_c/aether_capi.cpp index fd3fbbe0..f5d6ca80 100644 --- a/aether/aether_c/aether_capi.cpp +++ b/aether/aether_c/aether_capi.cpp @@ -21,36 +21,36 @@ #include -#include "aether/aether_c/extern_c.h" #include "aether/aether_c/c_errors.h" +#include "aether/aether_c/extern_c.h" +#include "aether/actions/actions_queue.h" #include "aether/memory.h" -#include "aether/ptr/ptr.h" #include "aether/obj/obj_ptr.h" +#include "aether/ptr/ptr.h" #include "aether/types/data_buffer.h" -#include "aether/actions/actions_queue.h" -#include "aether/client.h" #include "aether/aether_app.h" +#include "aether/client.h" // IWYU pragma: begin_keeps #include "aether/wifi/wifi_driver_types.h" #include "aether/domain_storage/domain_storage_factory.h" +#include "aether/domain_storage/file_system_std_storage.h" #include "aether/domain_storage/ram_domain_storage.h" #include "aether/domain_storage/spifs_domain_storage.h" -#include "aether/domain_storage/file_system_std_storage.h" #include "aether/adapter_registry.h" #include "aether/adapters/ethernet.h" -#include "aether/adapters/wifi_adapter.h" #include "aether/adapters/modem_adapter.h" +#include "aether/adapters/wifi_adapter.h" #include "aether/client_messages/p2p_message_stream.h" #include "aether/client_messages/p2p_message_stream_manager.h" // IWYU pragma: end_keeps -static ae::RcPtr aether_app; +static std::shared_ptr aether_app; struct AetherClient { ClientConfig config{}; @@ -81,7 +81,7 @@ ae::SelectClientAction& SelectClientImpl(AetherClient* client, void* user_data; }; - auto select_context = ae::MakeRcPtr( + auto select_context = std::make_shared( SelectContext{client, config->client_selected_cb, config->message_received_cb, config->user_data}); @@ -310,7 +310,7 @@ int AetherEnd() { default_client.reset(); } int exit_code = aether_app->IsExited() ? aether_app->ExitCode() : 0; - aether_app.Reset(); + aether_app.reset(); return exit_code; } diff --git a/aether/all.h b/aether/all.h index 6309df8f..20f5be39 100644 --- a/aether/all.h +++ b/aether/all.h @@ -41,7 +41,6 @@ #include "aether/obj/registry.h" #include "aether/ptr/ptr.h" #include "aether/ptr/ptr_view.h" -#include "aether/ptr/rc_ptr.h" #include "aether/domain_storage/domain_storage_factory.h" #include "aether/domain_storage/file_system_std_storage.h" diff --git a/aether/cloud_connections/cloud_server_connection.cpp b/aether/cloud_connections/cloud_server_connection.cpp index 422758b3..4cccca6c 100644 --- a/aether/cloud_connections/cloud_server_connection.cpp +++ b/aether/cloud_connections/cloud_server_connection.cpp @@ -33,7 +33,7 @@ void CloudServerConnection::SetPriority(std::size_t priority) { } void CloudServerConnection::Restream() { - if (client_connection_.get() != nullptr) { + if (client_connection_) { client_connection_->Restream(); } } @@ -44,15 +44,15 @@ void CloudServerConnection::SetQuarantine(bool value) { } bool CloudServerConnection::Connect() { - client_connection_.Reset(); + client_connection_.reset(); client_connection_ = connection_factory_->CreateConnection(server()); - return client_connection_.get() != nullptr; + return static_cast(client_connection_); } -void CloudServerConnection::Disconnect() { client_connection_.Reset(); } +void CloudServerConnection::Disconnect() { client_connection_.reset(); } ClientServerConnection* CloudServerConnection::client_connection() { - if (client_connection_.get() != nullptr) { + if (client_connection_) { return client_connection_.get(); } return nullptr; diff --git a/aether/cloud_connections/cloud_server_connection.h b/aether/cloud_connections/cloud_server_connection.h index 338583cc..1cfa4d18 100644 --- a/aether/cloud_connections/cloud_server_connection.h +++ b/aether/cloud_connections/cloud_server_connection.h @@ -17,8 +17,9 @@ #ifndef AETHER_CLOUD_CONNECTIONS_CLOUD_SERVER_CONNECTION_H_ #define AETHER_CLOUD_CONNECTIONS_CLOUD_SERVER_CONNECTION_H_ +#include + #include "aether/ptr/ptr_view.h" -#include "aether/ptr/rc_ptr.h" #include "aether/server_connections/client_server_connection.h" #include "aether/server_connections/iserver_connection_factory.h" @@ -49,7 +50,7 @@ class CloudServerConnection { private: PtrView server_; IServerConnectionFactory* connection_factory_; - RcPtr client_connection_; + std::shared_ptr client_connection_; std::size_t priority_; bool is_quarantined_; }; diff --git a/aether/config.h b/aether/config.h index 21a5605b..f9ef0d3c 100644 --- a/aether/config.h +++ b/aether/config.h @@ -56,7 +56,7 @@ // Maximum size in bytes for a single pending response pool element. #ifndef AE_API_PROTOCOL_PENDING_RESPONSE_MAX_SIZE -# define AE_API_PROTOCOL_PENDING_RESPONSE_MAX_SIZE 2 * sizeof(void*) +# define AE_API_PROTOCOL_PENDING_RESPONSE_MAX_SIZE 4 * sizeof(void*) #endif #ifndef AE_API_PROTOCOL_PENDING_RESPONSE_ALIGN diff --git a/aether/connection_manager/server_connection_manager.cpp b/aether/connection_manager/server_connection_manager.cpp index db1e713d..ff63c88d 100644 --- a/aether/connection_manager/server_connection_manager.cpp +++ b/aether/connection_manager/server_connection_manager.cpp @@ -24,9 +24,8 @@ ServerConnectionManager::ServerConnectionFactory::ServerConnectionFactory( ServerConnectionManager& server_connection_manager) : server_connection_manager_{&server_connection_manager} {} -RcPtr -ServerConnectionManager::ServerConnectionFactory::CreateConnection( - Ptr const& server) { +auto ServerConnectionManager::ServerConnectionFactory::CreateConnection( + Ptr const& server) -> std::shared_ptr { return server_connection_manager_->CreateConnection(server); } @@ -39,8 +38,8 @@ ServerConnectionManager::GetServerConnectionFactory() { return std::make_unique(*this); } -RcPtr ServerConnectionManager::CreateConnection( - Ptr const& server) { +auto ServerConnectionManager::CreateConnection(Ptr const& server) + -> std::shared_ptr { auto in_cache = FindInCache(server->server_id); if (in_cache) { return in_cache; @@ -51,7 +50,7 @@ RcPtr ServerConnectionManager::CreateConnection( assert(client); auto connection = - MakeRcPtr(ae_context_, client, server); + std::make_shared(ae_context_, client, server); // check updates server_update_subs_ += connection->stream_update_event().Subscribe( @@ -62,7 +61,7 @@ RcPtr ServerConnectionManager::CreateConnection( return connection; } -RcPtr ServerConnectionManager::FindInCache( +std::shared_ptr ServerConnectionManager::FindInCache( ServerId server_id) const { auto it = cached_connections_.find(server_id); if (it != cached_connections_.end()) { diff --git a/aether/connection_manager/server_connection_manager.h b/aether/connection_manager/server_connection_manager.h index d4976402..53d8a816 100644 --- a/aether/connection_manager/server_connection_manager.h +++ b/aether/connection_manager/server_connection_manager.h @@ -18,10 +18,10 @@ #define AETHER_CONNECTION_MANAGER_SERVER_CONNECTION_MANAGER_H_ #include +#include -#include "aether/ptr/ptr.h" -#include "aether/ptr/rc_ptr.h" #include "aether/ae_context.h" +#include "aether/ptr/ptr.h" #include "aether/ptr/ptr_view.h" #include "aether/server_connections/client_server_connection.h" #include "aether/server_connections/iserver_connection_factory.h" @@ -35,7 +35,7 @@ class ServerConnectionManager { explicit ServerConnectionFactory( ServerConnectionManager& server_connection_manager); - RcPtr CreateConnection( + std::shared_ptr CreateConnection( Ptr const& server) override; private: @@ -48,16 +48,17 @@ class ServerConnectionManager { std::unique_ptr GetServerConnectionFactory(); - RcPtr CreateConnection(Ptr const& server); + std::shared_ptr CreateConnection( + Ptr const& server); - RcPtr FindInCache(ServerId server_id) const; + std::shared_ptr FindInCache(ServerId server_id) const; private: void ServerUpdate(ServerId server_id); AeContext ae_context_; PtrView client_; - std::map> cached_connections_; + std::map> cached_connections_; MultiSubscription server_update_subs_; }; } // namespace ae diff --git a/aether/events/event_deleter.cpp b/aether/events/event_deleter.cpp index 19d27c2f..ba82aad8 100644 --- a/aether/events/event_deleter.cpp +++ b/aether/events/event_deleter.cpp @@ -18,7 +18,7 @@ namespace ae { EventHandlerDeleter::EventHandlerDeleter( - RcPtr const& event_handlers, + std::shared_ptr const& event_handlers, EventHandlersList::Index index) : event_handlers_{event_handlers}, index_{index} {} diff --git a/aether/events/event_deleter.h b/aether/events/event_deleter.h index 0dedf691..f799a74e 100644 --- a/aether/events/event_deleter.h +++ b/aether/events/event_deleter.h @@ -17,8 +17,9 @@ #ifndef AETHER_EVENTS_EVENT_DELETER_H_ #define AETHER_EVENTS_EVENT_DELETER_H_ +#include + #include "aether/common.h" -#include "aether/ptr/rc_ptr.h" #include "aether/events/event_list.h" namespace ae { @@ -28,7 +29,7 @@ namespace ae { */ class EventHandlerDeleter { public: - EventHandlerDeleter(RcPtr const& event_handlers, + EventHandlerDeleter(std::shared_ptr const& event_handlers, EventHandlersList::Index index); AE_CLASS_COPY_MOVE(EventHandlerDeleter) @@ -37,7 +38,7 @@ class EventHandlerDeleter { bool alive() const; private: - RcPtrView event_handlers_; + std::weak_ptr event_handlers_; typename EventHandlersList::Index index_; }; diff --git a/aether/events/events.h b/aether/events/events.h index 4922c232..0abe0060 100644 --- a/aether/events/events.h +++ b/aether/events/events.h @@ -17,16 +17,16 @@ #ifndef AETHER_EVENTS_EVENTS_H_ #define AETHER_EVENTS_EVENTS_H_ -#include -#include #include +#include +#include #include +#include -#include "aether/ptr/rc_ptr.h" -#include "aether/events/event_list.h" -#include "aether/events/event_handler.h" -#include "aether/events/event_deleter.h" #include "aether-miscpp/types/small_function.h" +#include "aether/events/event_deleter.h" +#include "aether/events/event_handler.h" +#include "aether/events/event_list.h" #include "aether/events/event_subscription.h" namespace ae { @@ -46,7 +46,7 @@ class Event { using Subscriber = EventSubscriber; using List = EventHandlersList; - explicit Event() : events_list_{MakeRcPtr()} {} + explicit Event() : events_list_{std::make_shared()} {} ~Event() = default; @@ -58,7 +58,8 @@ class Event { * subscriptions. */ void Emit(TArgs... args) { - EmitImpl(events_list_, std::forward(args)...); + std::shared_ptr list_life_extander = events_list_; + EmitImpl(list_life_extander, std::forward(args)...); } /** @@ -77,7 +78,8 @@ class Event { * Separate static emitter is used to handle self destruction while handler * invoking. */ - static void EmitImpl(RcPtr events_list, TArgs&&... args) { + static void EmitImpl(std::shared_ptr const& events_list, + TArgs&&... args) { auto iterate_list = events_list->Iterator(); for (auto it = std::begin(iterate_list); it != std::end(iterate_list); ++it) { @@ -87,7 +89,7 @@ class Event { } } - RcPtr events_list_; + std::shared_ptr events_list_; }; /** diff --git a/aether/ptr/ptr.h b/aether/ptr/ptr.h index 789872eb..7735a2b4 100644 --- a/aether/ptr/ptr.h +++ b/aether/ptr/ptr.h @@ -21,13 +21,11 @@ #include // IWYU pragma: begin_keeps -#include "aether/common.h" #include "aether/type_traits.h" -#include "aether/ptr/rc_ptr.h" -#include "aether/ptr/ref_tree.h" -#include "aether/ptr/ptr_management.h" #include "aether-miscpp/reflect/domain_visitor.h" +#include "aether/ptr/ptr_management.h" +#include "aether/ptr/ref_tree.h" // IWYU pragma: end_keeps namespace ae { diff --git a/aether/ptr/rc_ptr.h b/aether/ptr/rc_ptr.h deleted file mode 100644 index d5cb12f7..00000000 --- a/aether/ptr/rc_ptr.h +++ /dev/null @@ -1,306 +0,0 @@ -/* - * Copyright 2024 Aethernet Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef AETHER_PTR_RC_PTR_H_ -#define AETHER_PTR_RC_PTR_H_ - -#include -#include -#include -#include -#include - -#include "aether/mstream.h" -#include "aether-miscpp/types/aligned_storage.h" - -namespace ae { -// for most cases 2*uint8_t should be more than enough -// saving more space is impossible due to alignment in RcStorage -struct RefCounters { - std::atomic main_refs; - std::atomic weak_refs; -}; - -template -struct RcStorage { - public: - [[nodiscard]] auto* ptr() noexcept { return storage.ptr(); } - [[nodiscard]] auto* ptr() const noexcept { return storage.ptr(); } - - RefCounters ref_counters; - Storage storage; -}; - -template -class RcPtr { - template - friend class RcPtr; - - template - friend class RcPtrView; - - public: - RcPtr() noexcept = default; - RcPtr(std::nullptr_t) noexcept : RcPtr() {} // NOLINT - explicit RcPtr(RcStorage* rc_storage) noexcept : rc_storage_{rc_storage} { - Increment(); - } - ~RcPtr() { Reset(); } - - // Copying - RcPtr(RcPtr const& other) noexcept : RcPtr{other.rc_storage_} {} - - auto& operator=(RcPtr const& other) noexcept { - if (this != &other) { - Reset(); - rc_storage_ = other.rc_storage_; - Increment(); - } - return *this; - } - - // Moving - RcPtr(RcPtr&& other) noexcept : rc_storage_{other.rc_storage_} { - other.rc_storage_ = nullptr; - } - - auto& operator=(RcPtr&& other) noexcept { - if (this != &other) { - Reset(); - std::swap(rc_storage_, other.rc_storage_); - } - return *this; - } - - // Access - [[nodiscard]] T* get() { - return (rc_storage_ != nullptr) ? rc_storage_->ptr() : nullptr; - } - [[nodiscard]] T* get() const { - return (rc_storage_ != nullptr) ? rc_storage_->ptr() : nullptr; - } - - [[nodiscard]] T* operator->() noexcept { - assert((rc_storage_ != nullptr) && "Dereferencing a null pointer"); - return get(); - } - [[nodiscard]] T* operator->() const noexcept { - assert((rc_storage_ != nullptr) && "Dereferencing a null pointer"); - return get(); - } - [[nodiscard]] T& operator*() noexcept { - assert((rc_storage_ != nullptr) && "Dereferencing a null pointer"); - return *get(); - } - [[nodiscard]] T& operator*() const noexcept { - assert((rc_storage_ != nullptr) && "Dereferencing a null pointer"); - return *get(); - } - - explicit operator bool() const noexcept { return rc_storage_ != nullptr; } - - // Modifying - void Reset() { - if (rc_storage_ == nullptr) { - return; - } - - Decrement(); - if (rc_storage_->ref_counters.main_refs == 0) { - Destroy(); - if (rc_storage_->ref_counters.weak_refs == 0) { - Free(); - } - } - rc_storage_ = nullptr; - } - - private: - void Decrement() noexcept { rc_storage_->ref_counters.main_refs -= 1; } - void Increment() noexcept { - if (rc_storage_ == nullptr) { - return; - } - rc_storage_->ref_counters.main_refs += 1; - } - - void Destroy() { - // prevent cycled rcptrviews delete rc_storage - rc_storage_->ref_counters.weak_refs += 1; - // Call destructor on T - rc_storage_->ptr()->~T(); - rc_storage_->ref_counters.weak_refs -= 1; - } - - void Free() noexcept { - auto alloc = std::allocator>{}; - alloc.deallocate(rc_storage_, std::size_t{1}); - } - - RcStorage* rc_storage_{nullptr}; -}; - -/** - * \brief Construct an RcPtr - */ -template -auto MakeRcPtr(TArgs&&... args) noexcept { - auto alloc = std::allocator>{}; - auto* rc_storage = alloc.allocate(std::size_t{1}); - assert((rc_storage != nullptr) && "Bad alloc!"); - [[maybe_unused]] auto* constructed = - new (rc_storage->ptr()) T{std::forward(args)...}; - assert(constructed != nullptr && "Construction failed!"); - rc_storage->ref_counters.main_refs = 0; - rc_storage->ref_counters.weak_refs = 0; - return RcPtr{rc_storage}; -} - -template