Coverage Report

Created: 2025-06-13 06:46

/src/Fast-DDS/src/cpp/rtps/builtin/data/ProxyHashTables.hpp
Line
Count
Source (jump to first uncovered line)
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
/**
16
 * @file ProxyHashTables.hpp
17
 *
18
 */
19
20
#ifndef _FASTDDS_RTPS_BUILTIN_DATA_PROXYHASHTABLES_HPP_
21
#define _FASTDDS_RTPS_BUILTIN_DATA_PROXYHASHTABLES_HPP_
22
23
#include <fastdds/rtps/common/Guid.hpp>
24
25
#include <foonathan/memory/container.hpp>
26
#include <foonathan/memory/memory_pool.hpp>
27
#include <foonathan/memory/segregator.hpp>
28
29
#include "utils/collections/node_size_helpers.hpp"
30
31
#include <unordered_map>
32
33
namespace eprosima {
34
namespace fastdds {
35
namespace rtps {
36
37
// Static allocation ancillary for proxies
38
39
namespace detail {
40
41
template<
42
    std::size_t node_size,
43
    class RawAllocator = foonathan::memory::default_allocator>
44
class node_segregator
45
{
46
public:
47
48
    using allocator_type = foonathan::memory::memory_pool<foonathan::memory::node_pool, RawAllocator>;
49
    using size_helper = utilities::collections::detail::pool_size_helper<node_size>;
50
51
    node_segregator(
52
            std::size_t nodes_to_allocate,
53
            const bool& flag)
54
0
        : block_size_(size_helper::template min_pool_size<allocator_type>(nodes_to_allocate))
55
0
        , node_allocator_(new allocator_type(node_size, block_size_))
56
0
        , initialization_is_done_(flag)
57
0
    {
58
0
    }
59
60
    node_segregator(
61
            node_segregator&& s)
62
0
        : block_size_(s.block_size_)
63
0
        , node_allocator_(s.node_allocator_)
64
0
        , initialization_is_done_(s.initialization_is_done_)
65
0
    {
66
0
        s.node_allocator_ = nullptr;
67
0
    }
68
69
    node_segregator(
70
            const node_segregator& s)
71
        : block_size_(s.block_size_)
72
        , node_allocator_(new allocator_type(node_size, s.block_size_))
73
        , initialization_is_done_(s.initialization_is_done_)
74
    {
75
    }
76
77
    ~node_segregator()
78
0
    {
79
0
        if (node_allocator_)
80
0
        {
81
0
            delete node_allocator_;
82
0
        }
83
0
    }
84
85
    allocator_type& get_allocator() const noexcept
86
0
    {
87
0
        return *node_allocator_;
88
0
    }
89
90
    bool use_allocate_node(
91
            std::size_t size,
92
            std::size_t) noexcept
93
0
    {
94
0
        return initialization_is_done_ && size == node_size;
95
0
    }
96
97
    bool use_allocate_array(
98
            std::size_t,
99
            std::size_t,
100
            std::size_t) noexcept
101
0
    {
102
0
        return false;
103
0
    }
104
105
private:
106
107
    std::size_t block_size_ = 0;
108
    allocator_type* node_allocator_ = nullptr;
109
    const bool& initialization_is_done_;
110
};
111
112
template<
113
    std::size_t node_size,
114
    class RawAllocator = foonathan::memory::new_allocator>
115
class binary_node_segregator
116
    : public foonathan::memory::binary_segregator<node_segregator<node_size>, RawAllocator>
117
{
118
public:
119
120
    using segregator = node_segregator<node_size>;
121
    using base_class = foonathan::memory::binary_segregator<segregator, RawAllocator>;
122
    using is_stateful = std::true_type;
123
    using is_shared = std::false_type;
124
125
    binary_node_segregator(
126
            std::size_t nodes_to_allocate)
127
0
        : base_class(segregator(nodes_to_allocate, initialized_), RawAllocator())
128
0
        , initialized_(false)
129
0
    {
130
0
    }
131
132
    void has_been_initialized()
133
0
    {
134
0
        initialized_ = true;
135
0
    }
136
137
    void is_being_destroyed()
138
0
    {
139
0
        initialized_ = false;
140
0
    }
141
142
private:
143
144
    bool initialized_;
145
};
146
147
} // namespace detail
148
149
template<class Proxy>
150
class ProxyHashTable
151
    : protected detail::binary_node_segregator<
152
        utilities::collections::unordered_map_size_helper<EntityId_t, Proxy*>::node_size>
153
    , public foonathan::memory::unordered_map<
154
        EntityId_t,
155
        Proxy*,
156
        detail::binary_node_segregator<
157
            utilities::collections::unordered_map_size_helper<EntityId_t, Proxy*>::node_size>
158
        >
159
{
160
public:
161
162
    using allocator_type = detail::binary_node_segregator<
163
        utilities::collections::unordered_map_size_helper<EntityId_t, Proxy*>::node_size>;
164
    using base_class = foonathan::memory::unordered_map<EntityId_t, Proxy*, allocator_type>;
165
166
    explicit ProxyHashTable(
167
            const ResourceLimitedContainerConfig& r)
168
0
        : allocator_type(r.initial ? r.initial : 1u)
169
0
        , base_class(
170
0
            r.initial ? r.initial : 1u,
171
0
            std::hash<EntityId_t>(),
172
0
            std::equal_to<EntityId_t>(),
173
0
            *static_cast<allocator_type*>(this))
174
0
    {
175
        // notify the pool that fixed allocations may start
176
0
        allocator_type::has_been_initialized();
177
0
    }
Unexecuted instantiation: eprosima::fastdds::rtps::ProxyHashTable<eprosima::fastdds::rtps::ReaderProxyData>::ProxyHashTable(eprosima::fastdds::ResourceLimitedContainerConfig const&)
Unexecuted instantiation: eprosima::fastdds::rtps::ProxyHashTable<eprosima::fastdds::rtps::WriterProxyData>::ProxyHashTable(eprosima::fastdds::ResourceLimitedContainerConfig const&)
178
179
    ~ProxyHashTable()
180
0
    {
181
0
        base_class::clear();
182
0
        allocator_type::is_being_destroyed();
183
0
    }
Unexecuted instantiation: eprosima::fastdds::rtps::ProxyHashTable<eprosima::fastdds::rtps::ReaderProxyData>::~ProxyHashTable()
Unexecuted instantiation: eprosima::fastdds::rtps::ProxyHashTable<eprosima::fastdds::rtps::WriterProxyData>::~ProxyHashTable()
184
185
};
186
187
} // namespace rtps
188
} // namespace fastdds
189
} // namespace eprosima
190
191
#endif // _FASTDDS_RTPS_BUILTIN_DATA_PROXYHASHTABLES_HPP_