Skip to content

Commit c7ec6a0

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 c7ec6a0

File tree

4 files changed

+20
-54
lines changed

4 files changed

+20
-54
lines changed

include/rtps/communication/PacketInfo.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,7 @@ struct PacketInfo {
4545
PacketInfo() = default;
4646
~PacketInfo() = default;
4747

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

5450
PacketInfo &operator=(PacketInfo &&other) noexcept {
5551
copyTriviallyCopyable(other);

include/rtps/messages/MessageFactory.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,7 @@ void addSubMessageData(Buffer &buffer, const Buffer &filledPayload,
133133
serializeMessage(buffer, msg);
134134

135135
if (filledPayload.isValid()) {
136-
Buffer shallowCopy = filledPayload;
137-
buffer.append(std::move(shallowCopy));
136+
buffer.append(filledPayload);
138137
}
139138
}
140139

include/rtps/storages/PBufWrapper.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,28 +38,26 @@ struct PBufWrapper {
3838
explicit PBufWrapper(pbuf *bufferToWrap);
3939
explicit PBufWrapper(DataSize_t length);
4040

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);
41+
PBufWrapper(const PBufWrapper &other) = delete;
42+
PBufWrapper &operator=(const PBufWrapper &other) = delete;
4543

4644
PBufWrapper(PBufWrapper &&other) noexcept;
4745
PBufWrapper &operator=(PBufWrapper &&other) noexcept;
4846

4947
~PBufWrapper();
5048

51-
PBufWrapper deepCopy() const;
52-
5349
bool isValid() const;
5450

5551
bool append(const uint8_t *data, DataSize_t length);
5652

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

6157
bool reserve(DataSize_t length);
6258

59+
void destroy();
60+
6361
/// After calling this function, data is added starting from the beginning
6462
/// again. It does not revert reserve.
6563
void reset();

src/storages/PBufWrapper.cpp

Lines changed: 13 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -51,24 +51,11 @@ PBufWrapper::PBufWrapper(DataSize_t length)
5151
}
5252
}
5353

54-
// TODO: Uses copy assignment. Improvement possible
55-
PBufWrapper::PBufWrapper(const PBufWrapper &other) { *this = other; }
56-
5754
// TODO: Uses move assignment. Improvement possible
5855
PBufWrapper::PBufWrapper(PBufWrapper &&other) noexcept {
5956
*this = std::move(other);
6057
}
6158

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-
7259
PBufWrapper &PBufWrapper::operator=(PBufWrapper &&other) noexcept {
7360
copySimpleMembersAndResetBuffer(other);
7461

@@ -88,26 +75,17 @@ void PBufWrapper::copySimpleMembersAndResetBuffer(const PBufWrapper &other) {
8875
}
8976
}
9077

91-
PBufWrapper::~PBufWrapper() {
78+
void PBufWrapper::destroy()
79+
{
9280
if (firstElement != nullptr) {
9381
pbuf_free(firstElement);
82+
firstElement = nullptr;
9483
}
84+
m_freeSpace = 0;
9585
}
9686

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;
87+
PBufWrapper::~PBufWrapper() {
88+
destroy();
11189
}
11290

11391
bool PBufWrapper::isValid() const { return firstElement != nullptr; }
@@ -131,29 +109,24 @@ bool PBufWrapper::append(const uint8_t *data, DataSize_t length) {
131109
if (err != ERR_OK) {
132110
return false;
133111
}
134-
135112
m_freeSpace -= length;
136113
return true;
137114
}
138115

139-
void PBufWrapper::append(PBufWrapper &&other) {
140-
if (this == &other) {
141-
return;
142-
}
116+
void PBufWrapper::append(const PBufWrapper &other) {
143117
if (this->firstElement == nullptr) {
144-
*this = std::move(other);
118+
m_freeSpace = other.m_freeSpace;
119+
this->firstElement = other.firstElement;
120+
pbuf_ref(this->firstElement);
145121
return;
146122
}
147123

148-
m_freeSpace = other.m_freeSpace;
149-
pbuf *const newElement = other.firstElement;
150-
pbuf_cat(this->firstElement, newElement);
151-
152-
other.firstElement = nullptr;
124+
m_freeSpace += other.m_freeSpace;
125+
pbuf_chain(this->firstElement, other.firstElement);
153126
}
154127

155128
bool PBufWrapper::reserve(DataSize_t length) {
156-
auto additionalAllocation = length - m_freeSpace;
129+
int16_t additionalAllocation = length - m_freeSpace;
157130
if (additionalAllocation <= 0) {
158131
return true;
159132
}

0 commit comments

Comments
 (0)