Skip to content

Commit a1de467

Browse files
committed
Refactor PBufWrapper move/copy implementations
PBuf reference counting is done inside LwIP. PBufWrapper should be transparent implementation without intrusive reference counting. This commit makes `PBufWrapper` class movable and non-copyable. Signed-off-by: Alexander Livenets <alexander.livenets@apex.ai>
1 parent b13cb87 commit a1de467

File tree

4 files changed

+32
-54
lines changed

4 files changed

+32
-54
lines changed

include/rtps/communication/PacketInfo.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ This file is part of embeddedRTPS.
2222
Author: i11 - Embedded Software, RWTH Aachen University
2323
*/
2424

25+
// Copyright 2023 Apex.AI, Inc.
26+
// All rights reserved.
27+
2528
#ifndef RTPS_PACKETINFO_H
2629
#define RTPS_PACKETINFO_H
2730

@@ -45,11 +48,7 @@ struct PacketInfo {
4548
PacketInfo() = default;
4649
~PacketInfo() = default;
4750

48-
PacketInfo &operator=(const PacketInfo &other) {
49-
copyTriviallyCopyable(other);
50-
this->buffer = other.buffer;
51-
return *this;
52-
}
51+
PacketInfo &operator=(const PacketInfo &other) = delete;
5352

5453
PacketInfo &operator=(PacketInfo &&other) noexcept {
5554
copyTriviallyCopyable(other);

include/rtps/messages/MessageFactory.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ This file is part of embeddedRTPS.
2222
Author: i11 - Embedded Software, RWTH Aachen University
2323
*/
2424

25+
// Copyright 2023 Apex.AI, Inc.
26+
// All rights reserved.
27+
2528
#ifndef RTPS_MESSAGEFACTORY_H
2629
#define RTPS_MESSAGEFACTORY_H
2730

@@ -133,8 +136,7 @@ void addSubMessageData(Buffer &buffer, const Buffer &filledPayload,
133136
serializeMessage(buffer, msg);
134137

135138
if (filledPayload.isValid()) {
136-
Buffer shallowCopy = filledPayload;
137-
buffer.append(std::move(shallowCopy));
139+
buffer.append(filledPayload);
138140
}
139141
}
140142

include/rtps/storages/PBufWrapper.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ This file is part of embeddedRTPS.
2222
Author: i11 - Embedded Software, RWTH Aachen University
2323
*/
2424

25+
// Copyright 2023 Apex.AI, Inc.
26+
// All rights reserved.
27+
2528
#ifndef RTPS_PBUFWRAPPER_H
2629
#define RTPS_PBUFWRAPPER_H
2730

@@ -38,28 +41,26 @@ struct PBufWrapper {
3841
explicit PBufWrapper(pbuf *bufferToWrap);
3942
explicit PBufWrapper(DataSize_t length);
4043

41-
// Shallow Copy. No copying of the underlying pbuf. Just another reference
42-
// like a shared pointer.
43-
PBufWrapper(const PBufWrapper &other);
44-
PBufWrapper &operator=(const PBufWrapper &other);
44+
PBufWrapper(const PBufWrapper &other) = delete;
45+
PBufWrapper &operator=(const PBufWrapper &other) = delete;
4546

4647
PBufWrapper(PBufWrapper &&other) noexcept;
4748
PBufWrapper &operator=(PBufWrapper &&other) noexcept;
4849

4950
~PBufWrapper();
5051

51-
PBufWrapper deepCopy() const;
52-
5352
bool isValid() const;
5453

5554
bool append(const uint8_t *data, DataSize_t length);
5655

5756
/// Note that unused reserved memory is now part of the wrapper. New calls to
5857
/// append(uint8_t*[...]) will continue behind the appended wrapper
59-
void append(PBufWrapper &&other);
58+
void append(const PBufWrapper &other);
6059

6160
bool reserve(DataSize_t length);
6261

62+
void destroy();
63+
6364
/// After calling this function, data is added starting from the beginning
6465
/// again. It does not revert reserve.
6566
void reset();

src/storages/PBufWrapper.cpp

Lines changed: 16 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ This file is part of embeddedRTPS.
2222
Author: i11 - Embedded Software, RWTH Aachen University
2323
*/
2424

25+
// Copyright 2023 Apex.AI, Inc.
26+
// All rights reserved.
27+
2528
#include "rtps/storages/PBufWrapper.h"
2629
#include "rtps/utils/Log.h"
2730

@@ -51,24 +54,11 @@ PBufWrapper::PBufWrapper(DataSize_t length)
5154
}
5255
}
5356

54-
// TODO: Uses copy assignment. Improvement possible
55-
PBufWrapper::PBufWrapper(const PBufWrapper &other) { *this = other; }
56-
5757
// TODO: Uses move assignment. Improvement possible
5858
PBufWrapper::PBufWrapper(PBufWrapper &&other) noexcept {
5959
*this = std::move(other);
6060
}
6161

62-
PBufWrapper &PBufWrapper::operator=(const PBufWrapper &other) {
63-
copySimpleMembersAndResetBuffer(other);
64-
65-
if (other.firstElement != nullptr) {
66-
pbuf_ref(other.firstElement);
67-
}
68-
firstElement = other.firstElement;
69-
return *this;
70-
}
71-
7262
PBufWrapper &PBufWrapper::operator=(PBufWrapper &&other) noexcept {
7363
copySimpleMembersAndResetBuffer(other);
7464

@@ -88,26 +78,17 @@ void PBufWrapper::copySimpleMembersAndResetBuffer(const PBufWrapper &other) {
8878
}
8979
}
9080

91-
PBufWrapper::~PBufWrapper() {
81+
void PBufWrapper::destroy()
82+
{
9283
if (firstElement != nullptr) {
9384
pbuf_free(firstElement);
85+
firstElement = nullptr;
9486
}
87+
m_freeSpace = 0;
9588
}
9689

97-
PBufWrapper PBufWrapper::deepCopy() const {
98-
PBufWrapper clone;
99-
clone.copySimpleMembersAndResetBuffer(*this);
100-
101-
// Decided not to use pbuf_clone because it prevents const
102-
clone.firstElement = pbuf_alloc(m_layer, this->firstElement->tot_len, m_type);
103-
if (clone.firstElement != nullptr) {
104-
if (pbuf_copy(clone.firstElement, this->firstElement) != ERR_OK) {
105-
PBUF_WRAP_LOG("PBufWrapper::deepCopy: Copy of pbuf failed");
106-
}
107-
} else {
108-
clone.m_freeSpace = 0;
109-
}
110-
return clone;
90+
PBufWrapper::~PBufWrapper() {
91+
destroy();
11192
}
11293

11394
bool PBufWrapper::isValid() const { return firstElement != nullptr; }
@@ -131,29 +112,24 @@ bool PBufWrapper::append(const uint8_t *data, DataSize_t length) {
131112
if (err != ERR_OK) {
132113
return false;
133114
}
134-
135115
m_freeSpace -= length;
136116
return true;
137117
}
138118

139-
void PBufWrapper::append(PBufWrapper &&other) {
140-
if (this == &other) {
141-
return;
142-
}
119+
void PBufWrapper::append(const PBufWrapper &other) {
143120
if (this->firstElement == nullptr) {
144-
*this = std::move(other);
121+
m_freeSpace = other.m_freeSpace;
122+
this->firstElement = other.firstElement;
123+
pbuf_ref(this->firstElement);
145124
return;
146125
}
147126

148-
m_freeSpace = other.m_freeSpace;
149-
pbuf *const newElement = other.firstElement;
150-
pbuf_cat(this->firstElement, newElement);
151-
152-
other.firstElement = nullptr;
127+
m_freeSpace += other.m_freeSpace;
128+
pbuf_chain(this->firstElement, other.firstElement);
153129
}
154130

155131
bool PBufWrapper::reserve(DataSize_t length) {
156-
auto additionalAllocation = length - m_freeSpace;
132+
int16_t additionalAllocation = length - m_freeSpace;
157133
if (additionalAllocation <= 0) {
158134
return true;
159135
}

0 commit comments

Comments
 (0)