Skip to content

Commit

Permalink
Make shm manager optional
Browse files Browse the repository at this point in the history
Signed-off-by: Yadunund <[email protected]>
  • Loading branch information
Yadunund committed Jan 19, 2024
1 parent 804e915 commit 02fee09
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
5 changes: 3 additions & 2 deletions rmw_zenoh_cpp/src/detail/rmw_data_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<zc_owned_shm_manager_t> shm_manager;

z_owned_subscriber_t graph_subscriber;

Expand Down
13 changes: 9 additions & 4 deletions rmw_zenoh_cpp/src/rmw_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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");
Expand All @@ -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<GuardCondition *>(context->impl->graph_guard_condition->data)->~GuardCondition(),
GuardCondition, );
Expand Down
10 changes: 6 additions & 4 deletions rmw_zenoh_cpp/src/rmw_zenoh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 02fee09

Please sign in to comment.