/src/Fast-DDS/src/cpp/fastdds/subscriber/DataReaderImpl/DataReaderLoanManager.hpp
Line | Count | Source |
1 | | // Copyright 2021 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 DataReaderLoanManager.hpp |
17 | | */ |
18 | | |
19 | | #ifndef _FASTDDS_SUBSCRIBER_DATAREADERIMPL_DATAREADERLOANMANAGER_HPP_ |
20 | | #define _FASTDDS_SUBSCRIBER_DATAREADERIMPL_DATAREADERLOANMANAGER_HPP_ |
21 | | |
22 | | #include <algorithm> |
23 | | #include <cassert> |
24 | | |
25 | | #include <fastdds/dds/core/LoanableCollection.hpp> |
26 | | #include <fastdds/dds/core/LoanableTypedCollection.hpp> |
27 | | #include <fastdds/dds/core/ReturnCode.hpp> |
28 | | #include <fastdds/dds/subscriber/SampleInfo.hpp> |
29 | | #include <fastdds/dds/subscriber/qos/DataReaderQos.hpp> |
30 | | |
31 | | #include <fastdds/utils/collections/ResourceLimitedContainerConfig.hpp> |
32 | | #include <fastdds/utils/collections/ResourceLimitedVector.hpp> |
33 | | |
34 | | namespace eprosima { |
35 | | namespace fastdds { |
36 | | namespace dds { |
37 | | namespace detail { |
38 | | |
39 | | struct DataReaderLoanManager |
40 | | { |
41 | | using SampleInfoSeq = LoanableTypedCollection<SampleInfo>; |
42 | | |
43 | | explicit DataReaderLoanManager( |
44 | | const DataReaderQos& qos) |
45 | 0 | : max_samples_(qos.reader_resource_limits().max_samples_per_read) |
46 | 0 | , free_loans_(qos.reader_resource_limits().outstanding_reads_allocation) |
47 | 0 | , used_loans_(qos.reader_resource_limits().outstanding_reads_allocation) |
48 | 0 | { |
49 | 0 | for (size_t n = 0; n < qos.reader_resource_limits().outstanding_reads_allocation.initial; ++n) |
50 | 0 | { |
51 | 0 | OutstandingLoanItem tmp; |
52 | 0 | tmp.data_values = new LoanableCollection::element_type[max_samples_]; |
53 | 0 | tmp.sample_infos = new LoanableCollection::element_type[max_samples_]; |
54 | 0 | OutstandingLoanItem* result = free_loans_.push_back(tmp); |
55 | 0 | static_cast<void>(result); |
56 | 0 | assert(result != nullptr); |
57 | 0 | } |
58 | 0 | } |
59 | | |
60 | | ~DataReaderLoanManager() |
61 | 0 | { |
62 | 0 | for (OutstandingLoanItem& it : free_loans_) |
63 | 0 | { |
64 | 0 | delete[] it.data_values; |
65 | 0 | delete[] it.sample_infos; |
66 | 0 | } |
67 | 0 | } |
68 | | |
69 | | bool has_outstanding_loans() const |
70 | 0 | { |
71 | 0 | return !used_loans_.empty(); |
72 | 0 | } |
73 | | |
74 | | ReturnCode_t get_loan( |
75 | | LoanableCollection& data_values, |
76 | | SampleInfoSeq& sample_infos) |
77 | 0 | { |
78 | 0 | OutstandingLoanItem* result = nullptr; |
79 | |
|
80 | 0 | if (free_loans_.empty()) |
81 | 0 | { |
82 | 0 | OutstandingLoanItem tmp; |
83 | 0 | result = used_loans_.push_back(tmp); |
84 | 0 | if (nullptr == result) |
85 | 0 | { |
86 | 0 | return RETCODE_OUT_OF_RESOURCES; |
87 | 0 | } |
88 | | |
89 | 0 | result->data_values = new LoanableCollection::element_type[max_samples_]; |
90 | 0 | result->sample_infos = new LoanableCollection::element_type[max_samples_]; |
91 | 0 | } |
92 | 0 | else |
93 | 0 | { |
94 | 0 | result = used_loans_.push_back(free_loans_.back()); |
95 | 0 | static_cast<void>(result); |
96 | 0 | assert(result != nullptr); |
97 | 0 | free_loans_.pop_back(); |
98 | 0 | } |
99 | | |
100 | 0 | data_values.loan(result->data_values, max_samples_, 0); |
101 | 0 | sample_infos.loan(result->sample_infos, max_samples_, 0); |
102 | 0 | return RETCODE_OK; |
103 | 0 | } |
104 | | |
105 | | ReturnCode_t return_loan( |
106 | | LoanableCollection& data_values, |
107 | | SampleInfoSeq& sample_infos) |
108 | 0 | { |
109 | 0 | OutstandingLoanItem tmp; |
110 | 0 | tmp.data_values = const_cast<LoanableCollection::element_type*>(data_values.buffer()); |
111 | 0 | tmp.sample_infos = const_cast<LoanableCollection::element_type*>(sample_infos.buffer()); |
112 | |
|
113 | 0 | if (!used_loans_.remove(tmp)) |
114 | 0 | { |
115 | 0 | return RETCODE_PRECONDITION_NOT_MET; |
116 | 0 | } |
117 | | |
118 | 0 | OutstandingLoanItem* result = free_loans_.push_back(tmp); |
119 | 0 | static_cast<void>(result); |
120 | 0 | assert(result != nullptr); |
121 | 0 | return RETCODE_OK; |
122 | 0 | } |
123 | | |
124 | | private: |
125 | | |
126 | | struct OutstandingLoanItem |
127 | | { |
128 | | LoanableCollection::element_type* data_values = nullptr; |
129 | | LoanableCollection::element_type* sample_infos = nullptr; |
130 | | |
131 | | bool operator == ( |
132 | | const OutstandingLoanItem& other) const |
133 | 0 | { |
134 | 0 | return other.data_values == data_values && other.sample_infos == sample_infos; |
135 | 0 | } |
136 | | |
137 | | }; |
138 | | |
139 | | using collection_type = eprosima::fastdds::ResourceLimitedVector<OutstandingLoanItem>; |
140 | | |
141 | | int32_t max_samples_ = 0; |
142 | | collection_type free_loans_; |
143 | | collection_type used_loans_; |
144 | | }; |
145 | | |
146 | | } /* namespace detail */ |
147 | | } /* namespace dds */ |
148 | | } /* namespace fastdds */ |
149 | | } /* namespace eprosima */ |
150 | | |
151 | | #endif // _FASTDDS_SUBSCRIBER_DATAREADERIMPL_DATAREADERLOANMANAGER_HPP_ |