Skip to content

Commit

Permalink
IPayloadPool refactor (#800)
Browse files Browse the repository at this point in the history
* Refs #21121: Update API

Signed-off-by: cferreiragonz <[email protected]>

* Refs #21121: Revision - remove getters/setter

Signed-off-by: cferreiragonz <[email protected]>

* Refs #21121: Make get_payload's first arg const

Signed-off-by: cferreiragonz <[email protected]>

* Refs #21121: Revision

Signed-off-by: cferreiragonz <[email protected]>

---------

Signed-off-by: cferreiragonz <[email protected]>
  • Loading branch information
cferreiragonz authored Jun 12, 2024
1 parent ac2911b commit 6802780
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 30 deletions.
43 changes: 21 additions & 22 deletions code/CodeTester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,64 +175,63 @@ void rtps_api_example_create_entities_with_custom_pool()
{
bool get_payload(
uint32_t size,
CacheChange_t& cache_change) override
SerializedPayload_t& payload) override
{
// Reserve new memory for the payload buffer
octet* payload = new octet[size];
octet* payload_buff = new octet[size];

// Assign the payload buffer to the CacheChange and update sizes
cache_change.serializedPayload.data = payload;
cache_change.serializedPayload.length = size;
cache_change.serializedPayload.max_size = size;
payload.data = payload_buff;
payload.length = size;
payload.max_size = size;

// Tell the CacheChange who needs to release its payload
cache_change.payload_owner(this);
payload.payload_owner = this;

return true;
}

bool get_payload(
SerializedPayload_t& data,
IPayloadPool*& /*data_owner*/,
CacheChange_t& cache_change)
const SerializedPayload_t& data,
SerializedPayload_t& payload)
{
// Reserve new memory for the payload buffer
octet* payload = new octet[data.length];
octet* payload_buff = new octet[data.length];

// Copy the data
memcpy(payload, data.data, data.length);
memcpy(payload_buff, data.data, data.length);

// Tell the CacheChange who needs to release its payload
cache_change.payload_owner(this);
payload.payload_owner = this;

// Assign the payload buffer to the CacheChange and update sizes
cache_change.serializedPayload.data = payload;
cache_change.serializedPayload.length = data.length;
cache_change.serializedPayload.max_size = data.length;
payload.data = payload_buff;
payload.length = data.length;
payload.max_size = data.length;

return true;
}

bool release_payload(
CacheChange_t& cache_change) override
SerializedPayload_t& payload) override
{
// Ensure precondition
if (this != cache_change.payload_owner())
if (this != payload.payload_owner)
{
std::cerr << "Trying to release a payload buffer allocated by a different PayloadPool." << std::endl;
return false;
}

// Dealloc the buffer of the payload
delete[] cache_change.serializedPayload.data;
delete[] payload.data;

// Reset sizes and pointers
cache_change.serializedPayload.data = nullptr;
cache_change.serializedPayload.length = 0;
cache_change.serializedPayload.max_size = 0;
payload.data = nullptr;
payload.length = 0;
payload.max_size = 0;

// Reset the owner of the payload
cache_change.payload_owner(nullptr);
payload.payload_owner = nullptr;

return true;
}
Expand Down
9 changes: 4 additions & 5 deletions code/DDSCodeTester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,21 +260,20 @@ class CustomPayloadPool : public eprosima::fastrtps::rtps::IPayloadPool
~CustomPayloadPool() = default;
bool get_payload(
unsigned int size,
eprosima::fastrtps::rtps::CacheChange_t& cache_change)
eprosima::fastrtps::rtps::SerializedPayload_t& payload)
{
return true;
}

bool get_payload(
eprosima::fastrtps::rtps::SerializedPayload_t& data,
eprosima::fastrtps::rtps::IPayloadPool*& data_owner,
eprosima::fastrtps::rtps::CacheChange_t& cache_change)
const eprosima::fastrtps::rtps::SerializedPayload_t& data,
eprosima::fastrtps::rtps::SerializedPayload_t& payload)
{
return true;
}

bool release_payload(
eprosima::fastrtps::rtps::CacheChange_t& cache_change)
eprosima::fastrtps::rtps::SerializedPayload_t& payload)
{
return true;
}
Expand Down
6 changes: 3 additions & 3 deletions docs/fastdds/rtps_layer/rtps_layer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,9 @@ IPayloadPool interface

* |IPayloadPool::get_payload-api| overload with SerializadPayload parameter:

Copies the given Payload data to a new Payload from the pool and ties it to the :class:`CacheChange_t` instance.
This overload also takes a pointer to the pool that owns the original Payload.
This allows certain optimizations, like sharing the Payload if the original one comes form this same pool,
Copies the given Payload data to a new Payload from the pool.
Each :cpp:member:`SerializedPayload_t` contains a pointer to the pool that allocated its data.
This allows certain certain optimizations, like sharing the Payload if the original one comes from this same pool,
therefore avoiding the copy operation.

* |IPayloadPool::release_payload-api|:
Expand Down

0 comments on commit 6802780

Please sign in to comment.