-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathPBufWrapper.cpp
163 lines (131 loc) · 4.4 KB
/
PBufWrapper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
The MIT License
Copyright (c) 2019 Lehrstuhl Informatik 11 - RWTH Aachen University
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE
This file is part of embeddedRTPS.
Author: i11 - Embedded Software, RWTH Aachen University
*/
// Copyright 2023 Apex.AI, Inc.
// All rights reserved.
#include "rtps/storages/PBufWrapper.h"
#include "rtps/utils/Log.h"
using rtps::PBufWrapper;
#if PBUF_WRAP_VERBOSE && RTPS_GLOBAL_VERBOSE
#include "rtps/utils/printutils.h"
#define PBUF_WRAP_LOG(...) \
if (true) { \
printf("[PBUF Wrapper] "); \
printf(__VA_ARGS__); \
printf("\n"); \
}
#else
#define PBUF_WRAP_LOG(...) //
#endif
PBufWrapper::PBufWrapper(pbuf *bufferToWrap) : firstElement(bufferToWrap) {
m_freeSpace = 0; // Assume it to be full
}
PBufWrapper::PBufWrapper(DataSize_t length)
: firstElement(pbuf_alloc(m_layer, length, m_type)) {
if (isValid()) {
m_freeSpace = length;
}
}
// TODO: Uses move assignment. Improvement possible
PBufWrapper::PBufWrapper(PBufWrapper &&other) noexcept {
*this = std::move(other);
}
PBufWrapper &PBufWrapper::operator=(PBufWrapper &&other) noexcept {
copySimpleMembersAndResetBuffer(other);
if (other.firstElement != nullptr) {
firstElement = other.firstElement;
other.firstElement = nullptr;
}
return *this;
}
void PBufWrapper::copySimpleMembersAndResetBuffer(const PBufWrapper &other) {
m_freeSpace = other.m_freeSpace;
if (firstElement != nullptr) {
pbuf_free(firstElement);
firstElement = nullptr;
}
}
void PBufWrapper::destroy()
{
if (firstElement != nullptr) {
pbuf_free(firstElement);
firstElement = nullptr;
}
m_freeSpace = 0;
}
PBufWrapper::~PBufWrapper() {
destroy();
}
bool PBufWrapper::isValid() const { return firstElement != nullptr; }
rtps::DataSize_t PBufWrapper::spaceLeft() const { return m_freeSpace; }
rtps::DataSize_t PBufWrapper::spaceUsed() const {
if (firstElement == nullptr) {
return 0;
}
return firstElement->tot_len - m_freeSpace;
}
bool PBufWrapper::append(const uint8_t *data, DataSize_t length) {
if (data == nullptr) {
return false;
}
err_t err = pbuf_take_at(firstElement, data, length, spaceUsed());
if (err != ERR_OK) {
return false;
}
m_freeSpace -= length;
return true;
}
void PBufWrapper::append(const PBufWrapper &other) {
if (this->firstElement == nullptr) {
m_freeSpace = other.m_freeSpace;
this->firstElement = other.firstElement;
pbuf_ref(this->firstElement);
return;
}
m_freeSpace += other.m_freeSpace;
pbuf_chain(this->firstElement, other.firstElement);
}
bool PBufWrapper::reserve(DataSize_t length) {
int16_t additionalAllocation = length - m_freeSpace;
if (additionalAllocation <= 0) {
return true;
}
return increaseSizeBy(additionalAllocation);
}
void PBufWrapper::reset() {
if (firstElement != nullptr) {
m_freeSpace = firstElement->tot_len;
}
}
bool PBufWrapper::increaseSizeBy(uint16_t length) {
pbuf *allocation = pbuf_alloc(m_layer, length, m_type);
if (allocation == nullptr) {
return false;
}
m_freeSpace += length;
if (firstElement == nullptr) {
firstElement = allocation;
} else {
pbuf_cat(firstElement, allocation);
}
return true;
}
#undef PBUF_WRAP_VERBOSE