Skip to content

Commit

Permalink
Custom Payload Pool example: avoid copy if owning buffer (#560)
Browse files Browse the repository at this point in the history
* Custom Payload Pool example: avoid copy if owning buffer

Signed-off-by: Juan Lopez Fernandez <juanlopez@eprosima.com>

* Revert partial change introduced in previous commit c3932ed to force payload owner to 'this' always

Signed-off-by: JesusPoderoso <jesuspoderoso@eprosima.com>

---------

Signed-off-by: Juan Lopez Fernandez <juanlopez@eprosima.com>
Signed-off-by: JesusPoderoso <jesuspoderoso@eprosima.com>
Co-authored-by: JesusPoderoso <jesuspoderoso@eprosima.com>
  • Loading branch information
juanlofer-eprosima and JesusPoderoso committed Sep 29, 2023
1 parent e2b3964 commit 632eff7
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions code/CodeTester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -934,31 +934,35 @@ void rtps_api_example_create_entities_with_custom_pool()

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

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

// Tell the CacheChange who needs to release its payload
cache_change.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;

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

return true;
}

bool release_payload(
CacheChange_t& cache_change) override
{
// Ensure precondition
assert(this == cache_change.payload_owner());
if (this != cache_change.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;
Expand Down

0 comments on commit 632eff7

Please sign in to comment.