foonathan_memory_helpers.hpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright 2019 Proyectos y Sistemas de Mantenimiento SL (eProsima).
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @file foonathan_memory_helpers.hpp
  16. *
  17. */
  18. #ifndef FASTRTPS_UTILS_COLLECTIONS_FOONATHAN_MEMORY_HELPERS_HPP_
  19. #define FASTRTPS_UTILS_COLLECTIONS_FOONATHAN_MEMORY_HELPERS_HPP_
  20. #include <foonathan/memory/memory_pool.hpp>
  21. #include <foonathan/memory/detail/debug_helpers.hpp>
  22. #include "ResourceLimitedContainerConfig.hpp"
  23. namespace eprosima {
  24. namespace fastrtps {
  25. /**
  26. * A helper to calculate the block size of a memory pool given the side of the node and
  27. * a resource limits configuration.
  28. *
  29. * @tparam MemoryPool memory_pool specialization
  30. *
  31. * @param node_size Size of the node for the memory pool
  32. * @param limits Resource limits configuration of the container
  33. *
  34. * @return the block size to pass to the memory pool constructor
  35. */
  36. template <typename MemoryPool>
  37. std::size_t memory_pool_block_size(
  38. std::size_t node_size,
  39. const ResourceLimitedContainerConfig& limits)
  40. {
  41. size_t num_elems = limits.increment > 0 ? limits.initial : limits.maximum;
  42. if (num_elems < 1u)
  43. {
  44. num_elems = 1u;
  45. }
  46. return num_elems
  47. * ((node_size > MemoryPool::min_node_size ? node_size : MemoryPool::min_node_size) // Room for elements
  48. * (foonathan::memory::detail::debug_fence_size ? 3 : 1)) // Room for debug info
  49. + foonathan::memory::detail::memory_block_stack::implementation_offset; // Room for padding
  50. }
  51. } // namespace fastrtps
  52. } // namespace eprosima
  53. #endif /* FASTRTPS_UTILS_COLLECTIONS_FOONATHAN_MEMORY_HELPERS_HPP_ */