diff --git a/src/cpp/rtps/transport/shared_mem/SharedMemManager.hpp b/src/cpp/rtps/transport/shared_mem/SharedMemManager.hpp index 4dbd05171d1..2e64de6a324 100644 --- a/src/cpp/rtps/transport/shared_mem/SharedMemManager.hpp +++ b/src/cpp/rtps/transport/shared_mem/SharedMemManager.hpp @@ -19,10 +19,13 @@ #include #include -#include +#include +#include -#include -#include +#include "rtps/transport/shared_mem/SharedMemGlobal.hpp" +#include "utils/collections/node_size_helpers.hpp" +#include "utils/shared_memory/RobustSharedLock.hpp" +#include "utils/shared_memory/SharedMemWatchdog.hpp" namespace eprosima { namespace fastdds { @@ -366,7 +369,12 @@ class SharedMemManager : uint32_t payload_size, uint32_t max_allocations, const std::string& domain_name) - : segment_id_() + : buffer_node_list_allocator_( + buffer_node_list_helper::node_size, + buffer_node_list_helper::min_pool_size(max_allocations)) + , free_buffers_(buffer_node_list_allocator_) + , allocated_buffers_(buffer_node_list_allocator_) + , segment_id_() , overflows_count_(0) { generate_segment_id_and_name(domain_name); @@ -468,7 +476,6 @@ class SharedMemManager : throw std::runtime_error("alloc_buffer: out of memory"); } - // TODO(Adolfo) : Dynamic allocation. Use foonathan to convert it to static allocation allocated_buffers_.push_back(buffer_node); } catch (const std::exception&) @@ -502,9 +509,15 @@ class SharedMemManager : std::unique_ptr segment_name_lock_; - // TODO(Adolfo) : Dynamic allocations. Use foonathan to convert it to static allocation - std::list free_buffers_; - std::list allocated_buffers_; + using buffer_node_list_helper = + utilities::collections::list_size_helper; + + using pool_allocator_t = + foonathan::memory::memory_pool; + pool_allocator_t buffer_node_list_allocator_; + + foonathan::memory::list free_buffers_; + foonathan::memory::list allocated_buffers_; std::mutex alloc_mutex_; std::shared_ptr segment_; diff --git a/src/cpp/utils/collections/impl/node-sizes/foonathan/list_node_size_impl.hpp b/src/cpp/utils/collections/impl/node-sizes/foonathan/list_node_size_impl.hpp new file mode 100644 index 00000000000..ca9b2b51ad0 --- /dev/null +++ b/src/cpp/utils/collections/impl/node-sizes/foonathan/list_node_size_impl.hpp @@ -0,0 +1,27 @@ +// Copyright 2023 Proyectos y Sistemas de Mantenimiento SL (eProsima). +// +// 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. + +/** + * @file list_node_size_impl.hpp + */ + +#ifndef SRC_CPP_UTILS_COLLECTIONS_IMPL_FOONATHAN_LIST_NODE_SIZE_IMPL_HPP_ +#define SRC_CPP_UTILS_COLLECTIONS_IMPL_FOONATHAN_LIST_NODE_SIZE_IMPL_HPP_ + +template +struct list_node_size : foonathan::memory::list_node_size::value_type> +{ +}; + +#endif /* SRC_CPP_UTILS_COLLECTIONS_IMPL_FOONATHAN_LIST_NODE_SIZE_IMPL_HPP_ */ diff --git a/src/cpp/utils/collections/impl/node-sizes/list_node_size_impl.hpp b/src/cpp/utils/collections/impl/node-sizes/list_node_size_impl.hpp new file mode 100644 index 00000000000..901c3d6a53b --- /dev/null +++ b/src/cpp/utils/collections/impl/node-sizes/list_node_size_impl.hpp @@ -0,0 +1,34 @@ +// Copyright 2023 Proyectos y Sistemas de Mantenimiento SL (eProsima). +// +// 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. + +/** + * @file list_node_size_impl.hpp + */ + +#ifndef SRC_CPP_UTILS_COLLECTIONS_IMPL_LIST_NODE_SIZE_IMPL_HPP_ +#define SRC_CPP_UTILS_COLLECTIONS_IMPL_LIST_NODE_SIZE_IMPL_HPP_ + +#include "./config.hpp" + +#if defined(USE_FOONATHAN_NODE_SIZES) +#include "foonathan/list_node_size_impl.hpp" +#elif defined(USE_STD_NODE_SIZES) +#include "std/list_node_size_impl.hpp" +#elif defined(USE_CUSTOM_NODE_SIZES) +#include "custom/list_node_size_impl.hpp" +#else +#error "Don't now which list_node_size implementation to use" +#endif // NODE SIZE CONFIG + +#endif /* SRC_CPP_UTILS_COLLECTIONS_IMPL_LIST_NODE_SIZE_IMPL_HPP_ */ diff --git a/src/cpp/utils/collections/node_size_helpers.hpp b/src/cpp/utils/collections/node_size_helpers.hpp index e1d51b7cce8..0f6834fd838 100644 --- a/src/cpp/utils/collections/node_size_helpers.hpp +++ b/src/cpp/utils/collections/node_size_helpers.hpp @@ -35,6 +35,7 @@ namespace detail { namespace fm = foonathan::memory; // Include implementations for node size helpers +#include "impl/node-sizes/list_node_size_impl.hpp" #include "impl/node-sizes/map_node_size_impl.hpp" #include "impl/node-sizes/set_node_size_impl.hpp" #include "impl/node-sizes/unordered_map_node_size_impl.hpp" @@ -100,6 +101,10 @@ struct pool_size_helper } // namespace detail +template +struct list_size_helper : public detail::pool_size_helper::value> +{ +}; template struct map_size_helper : public detail::pool_size_helper::value> diff --git a/test/unittest/transport/CMakeLists.txt b/test/unittest/transport/CMakeLists.txt index 73b2de89057..f73dfe82909 100644 --- a/test/unittest/transport/CMakeLists.txt +++ b/test/unittest/transport/CMakeLists.txt @@ -16,6 +16,7 @@ if(WIN32) add_definitions( -D_WIN32_WINNT=0x0601 -D_CRT_SECURE_NO_WARNINGS + -DNOMINMAX ) endif() @@ -422,6 +423,7 @@ if(IS_THIRDPARTY_BOOST_OK) $<$:${ANDROID_IFADDRS_INCLUDE_DIR}> ) target_link_libraries(SharedMemTests + foonathan_memory fastcdr GTest::gtest ${MOCKS}