Coverage Report

Created: 2025-07-03 06:58

/src/Fast-DDS/src/cpp/rtps/history/CacheChangePool.h
Line
Count
Source (jump to first uncovered line)
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
/**
16
 * @file CacheChangePool.h
17
 */
18
19
#ifndef RTPS_HISTORY_CACHECHANGEPOOL_H_
20
#define RTPS_HISTORY_CACHECHANGEPOOL_H_
21
22
#include <fastdds/rtps/attributes/ResourceManagement.hpp>
23
#include <fastdds/rtps/common/CacheChange.hpp>
24
#include <fastdds/rtps/history/IChangePool.hpp>
25
26
#include <rtps/history/PoolConfig.h>
27
28
#include <vector>
29
#include <algorithm>
30
#include <cstdint>
31
#include <cstddef>
32
33
namespace eprosima {
34
namespace fastdds {
35
namespace rtps {
36
37
/**
38
 * Class CacheChangePool, used by the HistoryCache to pre-reserve a number of CacheChange_t to avoid dynamically
39
 * reserving memory in the middle of execution loops.
40
 * @ingroup COMMON_MODULE
41
 */
42
class CacheChangePool : public IChangePool
43
{
44
public:
45
46
    virtual ~CacheChangePool();
47
48
    /**
49
     * Construct and initialize a CacheChangePool.
50
     *
51
     * @param config   Pool configuration (member @c payload_initial_size is not being used).
52
     * @param f        Functor to be called on all preallocated elements.
53
     */
54
    template<class UnaryFunction>
55
    CacheChangePool(
56
            const PoolConfig& config,
57
            UnaryFunction f)
58
0
    {
59
0
        init(config);
60
0
        std::for_each(all_caches_.begin(), all_caches_.end(), f);
61
0
    }
62
63
    /**
64
     * Construct and initialize a CacheChangePool.
65
     *
66
     * @param config   Pool configuration (member @c payload_initial_size is not being used).
67
     */
68
    CacheChangePool(
69
            const PoolConfig& config)
70
0
    {
71
0
        init(config);
72
0
    }
73
74
    bool reserve_cache(
75
            CacheChange_t*& cache_change) override;
76
77
    bool release_cache(
78
            CacheChange_t* cache_change) override;
79
80
    //!Get the size of the cache vector; all of them (reserved and not reserved).
81
    size_t get_allCachesSize()
82
0
    {
83
0
        return all_caches_.size();
84
0
    }
85
86
    //!Get the number of free caches.
87
    size_t get_freeCachesSize()
88
0
    {
89
0
        return free_caches_.size();
90
0
    }
91
92
protected:
93
94
    /**
95
     * Construct a CacheChangePool without initialization.
96
     */
97
0
    CacheChangePool() = default;
98
99
    void init(
100
            const PoolConfig& config);
101
102
    virtual CacheChange_t* create_change() const
103
0
    {
104
0
        return new CacheChange_t();
105
0
    }
106
107
    virtual void destroy_change(
108
            CacheChange_t* change) const
109
0
    {
110
0
        delete change;
111
0
    }
112
113
private:
114
115
    uint32_t current_pool_size_ = 0;
116
    uint32_t max_pool_size_ = 0;
117
    MemoryManagementPolicy_t memory_mode_ = MemoryManagementPolicy_t::PREALLOCATED_WITH_REALLOC_MEMORY_MODE;
118
119
    std::vector<CacheChange_t*> free_caches_;
120
    std::vector<CacheChange_t*> all_caches_;
121
122
    bool allocateGroup(
123
            uint32_t num_caches);
124
125
    CacheChange_t* allocateSingle();
126
127
    //! Returns a CacheChange to the free caches pool
128
    void return_cache_to_pool(
129
            CacheChange_t* ch);
130
131
};
132
133
} // namespace rtps
134
} // namespace fastdds
135
} // namespace eprosima
136
137
#endif /* RTPS_HISTORY_CACHECHANGEPOOL_H_ */