Skip to content

Commit

Permalink
[#210] Do not allocate for the C++ Sample
Browse files Browse the repository at this point in the history
  • Loading branch information
elBoberido committed Aug 19, 2024
1 parent 890413c commit ca7b14f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 21 deletions.
6 changes: 3 additions & 3 deletions iceoryx2-ffi/cxx/include/iox2/publisher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,12 @@ template <ServiceType S, typename Payload, typename UserHeader>
inline auto Publisher<S, Payload, UserHeader>::loan_uninit()
-> iox::expected<SampleMut<S, Payload, UserHeader>, PublisherLoanError> {
auto* ref_handle = iox2_cast_publisher_ref_h(m_handle);
iox2_sample_mut_h sample_handle {};
SampleMut<S, Payload, UserHeader> sample;

auto result = iox2_publisher_loan(ref_handle, nullptr, &sample_handle);
auto result = iox2_publisher_loan(ref_handle, &sample.m_sample, &sample.m_handle);

if (result == IOX2_OK) {
return iox::ok(SampleMut<S, Payload, UserHeader>(sample_handle));
return iox::ok(std::move(sample));
}

return iox::err(iox::into<PublisherLoanError>(result));
Expand Down
18 changes: 11 additions & 7 deletions iceoryx2-ffi/cxx/include/iox2/sample.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,14 @@ class Sample {
template <ServiceType, typename, typename>
friend class Subscriber;

explicit Sample(iox2_sample_h handle);
// The sample is defaulted since both members are initialized in Subscriber::receive
explicit Sample() = default;
void drop();

iox2_sample_t m_sample;
iox2_sample_h m_handle { nullptr };
};

template <ServiceType S, typename Payload, typename UserHeader>
inline Sample<S, Payload, UserHeader>::Sample(iox2_sample_h handle)
: m_handle { handle } {
}

template <ServiceType S, typename Payload, typename UserHeader>
inline void Sample<S, Payload, UserHeader>::drop() {
if (m_handle != nullptr) {
Expand All @@ -88,11 +85,18 @@ inline Sample<S, Payload, UserHeader>::Sample(Sample&& rhs) noexcept {
*this = std::move(rhs);
}

namespace internal {
extern "C" {
void iox2_sample_move(iox2_sample_t*, iox2_sample_t*, iox2_sample_h*);
}
} // namespace internal

template <ServiceType S, typename Payload, typename UserHeader>
inline auto Sample<S, Payload, UserHeader>::operator=(Sample&& rhs) noexcept -> Sample& {
if (this != &rhs) {
drop();
m_handle = std::move(rhs.m_handle);

internal::iox2_sample_move(&rhs.m_sample, &m_sample, &m_handle);
rhs.m_handle = nullptr;
}

Expand Down
18 changes: 11 additions & 7 deletions iceoryx2-ffi/cxx/include/iox2/sample_mut.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,14 @@ class SampleMut {
template <ServiceType ST, typename PayloadT, typename UserHeaderT>
friend auto send_sample(SampleMut<ST, PayloadT, UserHeaderT>&& sample) -> iox::expected<size_t, PublisherSendError>;

explicit SampleMut(iox2_sample_mut_h handle);
// The sample is defaulted since both members are initialized in Subscriber::receive
explicit SampleMut() = default;
void drop();

iox2_sample_mut_t m_sample;
iox2_sample_mut_h m_handle { nullptr };
};

template <ServiceType S, typename Payload, typename UserHeader>
inline SampleMut<S, Payload, UserHeader>::SampleMut(iox2_sample_mut_h handle)
: m_handle { handle } {
}

template <ServiceType S, typename Payload, typename UserHeader>
inline void SampleMut<S, Payload, UserHeader>::drop() {
if (m_handle != nullptr) {
Expand All @@ -124,11 +121,18 @@ inline SampleMut<S, Payload, UserHeader>::SampleMut(SampleMut&& rhs) noexcept {
*this = std::move(rhs);
}

namespace internal {
extern "C" {
void iox2_sample_mut_move(iox2_sample_mut_t*, iox2_sample_mut_t*, iox2_sample_mut_h*);
}
} // namespace internal

template <ServiceType S, typename Payload, typename UserHeader>
inline auto SampleMut<S, Payload, UserHeader>::operator=(SampleMut&& rhs) noexcept -> SampleMut& {
if (this != &rhs) {
drop();
m_handle = std::move(rhs.m_handle);

internal::iox2_sample_mut_move(&rhs.m_sample, &m_sample, &m_handle);
rhs.m_handle = nullptr;
}

Expand Down
9 changes: 5 additions & 4 deletions iceoryx2-ffi/cxx/include/iox2/subscriber.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,14 @@ template <ServiceType S, typename Payload, typename UserHeader>
inline auto Subscriber<S, Payload, UserHeader>::receive() const
-> iox::expected<iox::optional<Sample<S, Payload, UserHeader>>, SubscriberReceiveError> {
auto* ref_handle = iox2_cast_subscriber_ref_h(m_handle);

Sample<S, Payload, UserHeader> sample;
iox2_sample_h sample_handle {};
auto result = iox2_subscriber_receive(ref_handle, nullptr, &sample_handle);
auto result = iox2_subscriber_receive(ref_handle, &sample.m_sample, &sample.m_handle);

if (result == IOX2_OK) {
if (sample_handle != nullptr) {
return iox::ok(
iox::optional<Sample<S, Payload, UserHeader>>(Sample<S, Payload, UserHeader>(sample_handle)));
if (sample.m_handle != nullptr) {
return iox::ok(iox::optional<Sample<S, Payload, UserHeader>>(std::move(sample)));
}
return iox::ok(iox::optional<Sample<S, Payload, UserHeader>>(iox::nullopt));
}
Expand Down

0 comments on commit ca7b14f

Please sign in to comment.