From 02fee0987da5107fe031954e6ec1387294884641 Mon Sep 17 00:00:00 2001 From: Yadunund Date: Fri, 19 Jan 2024 14:23:50 +0800 Subject: [PATCH] Make shm manager optional Signed-off-by: Yadunund --- rmw_zenoh_cpp/src/detail/rmw_data_types.hpp | 5 +++-- rmw_zenoh_cpp/src/rmw_init.cpp | 13 +++++++++---- rmw_zenoh_cpp/src/rmw_zenoh.cpp | 10 ++++++---- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/rmw_zenoh_cpp/src/detail/rmw_data_types.hpp b/rmw_zenoh_cpp/src/detail/rmw_data_types.hpp index e8893eaf..a328a534 100644 --- a/rmw_zenoh_cpp/src/detail/rmw_data_types.hpp +++ b/rmw_zenoh_cpp/src/detail/rmw_data_types.hpp @@ -45,8 +45,9 @@ struct rmw_context_impl_s // An owned session. z_owned_session_t session; - // The SHM manager. - zc_owned_shm_manager_t shm_manager; + // An optional SHM manager that is initialized of SHM is enabled in the + // zenoh session config. + std::optional shm_manager; z_owned_subscriber_t graph_subscriber; diff --git a/rmw_zenoh_cpp/src/rmw_init.cpp b/rmw_zenoh_cpp/src/rmw_init.cpp index fbfb9e52..c86d2ca4 100644 --- a/rmw_zenoh_cpp/src/rmw_init.cpp +++ b/rmw_zenoh_cpp/src/rmw_init.cpp @@ -195,14 +195,18 @@ rmw_init(const rmw_init_options_t * options, rmw_context_t * context) z_loan(context->impl->session), idstr, SHM_BUFFER_SIZE_MB * 1024 * 1024); - if (!zc_shm_manager_check(&context->impl->shm_manager)) { + if (!context->impl->shm_manager.has_value() || + !zc_shm_manager_check(&context->impl->shm_manager.value())) + { RMW_SET_ERROR_MSG("Unable to create shm manager."); return RMW_RET_ERROR; } } auto free_shm_manager = rcpputils::make_scope_exit( [context]() { - z_drop(z_move(context->impl->shm_manager)); + if (context->impl->shm_manager.has_value()) { + z_drop(z_move(context->impl->shm_manager.value())); + } }); // Initialize the guard condition. @@ -341,6 +345,9 @@ rmw_shutdown(rmw_context_t * context) return RMW_RET_INCORRECT_RMW_IMPLEMENTATION); z_undeclare_subscriber(z_move(context->impl->graph_subscriber)); + if (context->impl->shm_manager.has_value()) { + z_drop(z_move(context->impl->shm_manager.value())); + } // Close the zenoh session if (z_close(z_move(context->impl->session)) < 0) { RMW_SET_ERROR_MSG("Error while closing zenoh session"); @@ -349,8 +356,6 @@ rmw_shutdown(rmw_context_t * context) const rcutils_allocator_t * allocator = &context->options.allocator; - z_drop(z_move(context->impl->shm_manager)); - RMW_TRY_DESTRUCTOR( static_cast(context->impl->graph_guard_condition->data)->~GuardCondition(), GuardCondition, ); diff --git a/rmw_zenoh_cpp/src/rmw_zenoh.cpp b/rmw_zenoh_cpp/src/rmw_zenoh.cpp index b5bd5eac..1432f4b2 100644 --- a/rmw_zenoh_cpp/src/rmw_zenoh.cpp +++ b/rmw_zenoh_cpp/src/rmw_zenoh.cpp @@ -878,13 +878,15 @@ rmw_publish( zc_owned_shmbuf_t shmbuf; // Get memory from SHM buffer if available. - if (zc_shm_manager_check(&publisher_data->context->impl->shm_manager)) { + if (publisher_data->context->impl->shm_manager.has_value() && + zc_shm_manager_check(&publisher_data->context->impl->shm_manager.value())) + { shmbuf = zc_shm_alloc( - &publisher_data->context->impl->shm_manager, + &publisher_data->context->impl->shm_manager.value(), max_data_length); if (!z_check(shmbuf)) { - zc_shm_gc(&publisher_data->context->impl->shm_manager); - shmbuf = zc_shm_alloc(&publisher_data->context->impl->shm_manager, max_data_length); + zc_shm_gc(&publisher_data->context->impl->shm_manager.value()); + shmbuf = zc_shm_alloc(&publisher_data->context->impl->shm_manager.value(), max_data_length); if (!z_check(shmbuf)) { // TODO(Yadunund): Should we revert to regular allocation and not return an error? RMW_SET_ERROR_MSG("Failed to allocate a SHM buffer, even after GCing");