Fast RTPS  Version 2.1.0
Fast RTPS
foonathan_memory_helpers.hpp
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 
20 #ifndef FASTRTPS_UTILS_COLLECTIONS_FOONATHAN_MEMORY_HELPERS_HPP_
21 #define FASTRTPS_UTILS_COLLECTIONS_FOONATHAN_MEMORY_HELPERS_HPP_
22 
23 #include <foonathan/memory/memory_pool.hpp>
24 #include <foonathan/memory/detail/debug_helpers.hpp>
25 
26 #include "ResourceLimitedContainerConfig.hpp"
27 
28 namespace eprosima {
29 namespace fastrtps {
30 
42 template <typename MemoryPool>
44  std::size_t node_size,
45  const ResourceLimitedContainerConfig& limits)
46 {
47 #ifdef FOONATHAN_MEMORY_MEMORY_POOL_HAS_MIN_BLOCK_SIZE
48 
49  return MemoryPool::min_block_size(node_size, limits.initial ? limits.initial : 1);
50 
51 #else
52  size_t num_elems = limits.increment > 0 ? limits.initial : limits.maximum;
53  if (num_elems < 1u)
54  {
55  num_elems = 1u;
56  }
57 
58  return num_elems
59  * ((node_size > MemoryPool::min_node_size ? node_size : MemoryPool::min_node_size) // Room for elements
60  * (foonathan::memory::detail::debug_fence_size ? 3 : 1)) // Room for debug info
61  + foonathan::memory::detail::memory_block_stack::implementation_offset; // Room for padding
62 #endif // FOONATHAN_MEMORY_MEMORY_POOL_HAS_MIN_BLOCK_SIZE
63 }
64 
65 } // namespace fastrtps
66 } // namespace eprosima
67 
68 #endif /* FASTRTPS_UTILS_COLLECTIONS_FOONATHAN_MEMORY_HELPERS_HPP_ */
std::size_t memory_pool_block_size(std::size_t node_size, const ResourceLimitedContainerConfig &limits)
A helper to calculate the block size of a memory pool given the size of the node and a resource limit...
Definition: foonathan_memory_helpers.hpp:43
eProsima namespace.
Definition: LibrarySettingsAttributes.h:23
Specifies the configuration of a resource limited collection.
Definition: ResourceLimitedContainerConfig.hpp:34
size_t increment
Number of items to add when capacity limit is reached.
Definition: ResourceLimitedContainerConfig.hpp:51
size_t maximum
Maximum number of elements allowed in the collection.
Definition: ResourceLimitedContainerConfig.hpp:49
size_t initial
Number of elements to be preallocated in the collection.
Definition: ResourceLimitedContainerConfig.hpp:47