Skip to content

Commit 647c373

Browse files
authored
Loaning samples in DataWriter (#229)
Signed-off-by: Iker Luengo <ikerluengo@eprosima.com>
1 parent a0bdcbe commit 647c373

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

code/DDSCodeTester.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1870,6 +1870,38 @@ void dds_dataWriter_examples()
18701870
// but delete it at the end to avoid leaks
18711871
custom_type_support->deleteData(data);
18721872
//!--
1873+
1874+
{
1875+
//DDS_DATAWRITER_LOAN_SAMPLES
1876+
// Borrow a data instance
1877+
void* data = nullptr;
1878+
if (ReturnCode_t::RETCODE_OK == data_writer->loan_sample(data))
1879+
{
1880+
bool error = false;
1881+
1882+
// Fill the data values
1883+
// (...)
1884+
1885+
if (error)
1886+
{
1887+
// Return the loan without publishing
1888+
data_writer->discard_loan(data);
1889+
return;
1890+
}
1891+
1892+
// Publish the new value
1893+
if (data_writer->write(data, eprosima::fastrtps::rtps::InstanceHandle_t()) != ReturnCode_t::RETCODE_OK)
1894+
{
1895+
// Error
1896+
return;
1897+
}
1898+
}
1899+
1900+
// The data instance can be reused to publish new values,
1901+
// but delete it at the end to avoid leaks
1902+
custom_type_support->deleteData(data);
1903+
//!--
1904+
}
18731905
}
18741906
}
18751907

docs/03-exports/aliases-api.include

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@
5858
.. |DataWriter::get_qos-api| replace:: :cpp:func:`get_qos()<eprosima::fastdds::dds::DataWriter::get_qos>`
5959
.. |DataWriter::set_qos-api| replace:: :cpp:func:`DataWriter::set_qos()<eprosima::fastdds::dds::DataWriter::set_qos>`
6060
.. |DataWriter::write-api| replace:: :cpp:func:`write()<eprosima::fastdds::dds::DataWriter::write>`
61+
.. |DataWriter::loan_sample-api| replace:: :cpp:func:`loan_sample()<eprosima::fastdds::dds::DataWriter::loan_sample>`
62+
.. |DataWriter::discard_loan-api| replace:: :cpp:func:`discard_loan()<eprosima::fastdds::dds::DataWriter::discard_loan>`
6163
.. |DataWriterListener-api| replace:: :cpp:class:`DataWriterListener <eprosima::fastdds::dds::DataWriterListener>`
6264

6365
.. |DataWriter::get_offered_deadline_missed_status-api| replace:: :cpp:func:`get_offered_deadline_missed_status()<eprosima::fastdds::dds::DataWriter::get_offered_deadline_missed_status>`

docs/fastdds/dds_layer/publisher/dataWriter/publishingData.rst

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,37 @@ If ``max_blocking_time`` elapses before the DataWriter is able to store
4949
the modification without exceeding the limits, the write operation will fail and return ``TIMEOUT``.
5050

5151

52+
.. _dds_layer_publisher_write_loans:
5253

54+
Borrowing a data buffer
55+
-----------------------
56+
57+
When the user calls |DataWriter::write-api| with a new sample value,
58+
the data is copied from the given sample to the DataWriter's memory.
59+
For large data types this copy can consume significant time and memory resources.
60+
Instead, the DataWriter can loan a sample from its memory to the user,
61+
and the user can fill this sample with the required values.
62+
When |DataWriter::write-api| is called with such a loaned sample,
63+
the DataWriter does not copy its contents, as it already owns the buffer.
64+
65+
To use loaned data samples in publications, perform the following steps:
66+
67+
1. Get a reference to a loaned sample using |DataWriter::loan_sample-api|.
68+
2. Use the reference to build the data sample.
69+
3. Write the sample using |DataWriter::write-api|.
70+
71+
72+
Once |DataWriter::write-api| has been called with a loaned sample,
73+
the loan is considered returned, and it is not safe to make any
74+
changes on the contents of the sample.
75+
76+
If function |DataWriter::loan_sample-api| is called but the sample is never written,
77+
the loan must be returned to the DataWriter using |DataWriter::discard_loan-api|.
78+
Otherwise the DataWriter may run out of samples.
79+
80+
81+
.. literalinclude:: /../code/DDSCodeTester.cpp
82+
:language: c++
83+
:start-after: //DDS_DATAWRITER_LOAN_SAMPLES
84+
:end-before: //!
85+
:dedent: 8

0 commit comments

Comments
 (0)