CacheChangePool.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2016 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 CacheChangePool.h
  16. *
  17. */
  18. #ifndef _FASTDDS_RTPS_CACHECHANGEPOOL_H_
  19. #define _FASTDDS_RTPS_CACHECHANGEPOOL_H_
  20. #include <fastdds/rtps/resources/ResourceManagement.h>
  21. #include <vector>
  22. #include <functional>
  23. #include <cstdint>
  24. #include <cstddef>
  25. #include <mutex>
  26. namespace eprosima {
  27. namespace fastrtps{
  28. namespace rtps {
  29. struct CacheChange_t;
  30. /**
  31. * Class CacheChangePool, used by the HistoryCache to pre-reserve a number of CacheChange_t to avoid dynamically reserving memory in the middle of execution loops.
  32. * @ingroup COMMON_MODULE
  33. */
  34. class CacheChangePool {
  35. public:
  36. virtual ~CacheChangePool();
  37. /**
  38. * Constructor.
  39. * @param pool_size The initial pool size
  40. * @param payload_size The initial payload size associated with the pool.
  41. * @param max_pool_size Maximum payload size. If set to 0 the pool will keep reserving until something breaks.
  42. * @param memoryPolicy Memory management policy.
  43. */
  44. CacheChangePool(int32_t pool_size, uint32_t payload_size, int32_t max_pool_size, MemoryManagementPolicy_t memoryPolicy);
  45. /*!
  46. * @brief Reserves a CacheChange from the pool.
  47. * @param chan Returned pointer to the reserved CacheChange.
  48. * @param calculateSizeFunc Function that returns the size of the data which will go into the CacheChange.
  49. * This function is executed depending on the memory management policy (DYNAMIC_RESERVE_MEMORY_MODE and
  50. * PREALLOCATED_WITH_REALLOC_MEMORY_MODE)
  51. * @return True whether the CacheChange could be allocated. In other case returns false.
  52. */
  53. bool reserve_Cache(CacheChange_t** chan, const std::function<uint32_t()>& calculateSizeFunc);
  54. /*!
  55. * @brief Reserves a CacheChange from the pool.
  56. * @param chan Returned pointer to the reserved CacheChange.
  57. * @param dataSize Size of the data which will go into the CacheChange if it is necessary (on memory management
  58. * policy DYNAMIC_RESERVE_MEMORY_MODE and PREALLOCATED_WITH_REALLOC_MEMORY_MODE). In other case this variable is not used.
  59. * @return True whether the CacheChange could be allocated. In other case returns false.
  60. */
  61. bool reserve_Cache(CacheChange_t** chan, uint32_t dataSize);
  62. //!Release a Cache back to the pool.
  63. void release_Cache(CacheChange_t*);
  64. //!Get the size of the cache vector; all of them (reserved and not reserved).
  65. size_t get_allCachesSize(){return m_allCaches.size();}
  66. //!Get the number of frre caches.
  67. size_t get_freeCachesSize(){return m_freeCaches.size();}
  68. //!Get the initial payload size associated with the Pool.
  69. inline uint32_t getInitialPayloadSize(){return m_initial_payload_size;};
  70. private:
  71. //! Returns a CacheChange to the free caches pool
  72. void return_cache_to_pool(CacheChange_t* ch);
  73. uint32_t m_initial_payload_size;
  74. uint32_t m_payload_size;
  75. uint32_t m_pool_size;
  76. uint32_t m_max_pool_size;
  77. std::vector<CacheChange_t*> m_freeCaches;
  78. std::vector<CacheChange_t*> m_allCaches;
  79. bool allocateGroup(uint32_t pool_size);
  80. CacheChange_t* allocateSingle(uint32_t dataSize);
  81. MemoryManagementPolicy_t memoryMode;
  82. };
  83. }
  84. } /* namespace rtps */
  85. } /* namespace eprosima */
  86. #endif /* _FASTDDS_RTPS_CACHECHANGEPOOL_H_ */