@@ -22,6 +22,9 @@ This file is part of embeddedRTPS.
2222Author: 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
5858PBufWrapper::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-
7262PBufWrapper &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
11394bool 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
155131bool 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