Skip to content

Commit

Permalink
iox-eclipse-iceoryx#1218 Add workaround to ease CyclondDDS transition…
Browse files Browse the repository at this point in the history
… to the C binding using the heap
  • Loading branch information
elBoberido committed Mar 3, 2022
1 parent e837bcd commit 64cd6bf
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions iceoryx_binding_c/source/c_subscriber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ extern "C" {
#include "iceoryx_binding_c/subscriber.h"
}

/// @todo iox-#1221 remove this workaround needed for CycloneDDS due to our change to use the head for storage
struct SubscriberWithStoragePointer
{
void* subscriberStorage{nullptr};
cpp2c_Subscriber subscriber;
};

constexpr uint64_t SUBSCRIBER_OPTIONS_INIT_CHECK_CONSTANT = 543212345;

void iox_sub_options_init(iox_sub_options_t* options)
Expand Down Expand Up @@ -99,7 +106,14 @@ iox_sub_t iox_sub_init(iox_sub_storage_t* self,
subscriberOptions.requiresPublisherHistorySupport = options->requirePublisherHistorySupport;
}

auto* me = new cpp2c_Subscriber();
// this is required for CycloneDDS to limit the fallout of our change to use the heap for storage
// it should be removed with #1221
auto meWithStoragePointer = new SubscriberWithStoragePointer();
meWithStoragePointer->subscriberStorage = self;
auto me = &meWithStoragePointer->subscriber;
assert(reinterpret_cast<uint64_t>(me) - reinterpret_cast<uint64_t>(meWithStoragePointer) == sizeof(void*)
&& "Size mismatch for SubscriberWithStoragePointer!");

me->m_portData =
PoshRuntime::getInstance().getMiddlewareSubscriber(ServiceDescription{IdString_t(TruncateToCapacity, service),
IdString_t(TruncateToCapacity, instance),
Expand All @@ -114,7 +128,10 @@ void iox_sub_deinit(iox_sub_t const self)
{
iox::cxx::Expects(self != nullptr);

delete self;
auto addressOfSelf = reinterpret_cast<uint64_t>(self);
auto* selfWithStoragePointer = reinterpret_cast<SubscriberWithStoragePointer*>(addressOfSelf - sizeof(void*));

delete selfWithStoragePointer;
}

void iox_sub_subscribe(iox_sub_t const self)
Expand Down

0 comments on commit 64cd6bf

Please sign in to comment.