rogue
Loading...
Searching...
No Matches
Buffer.cpp
Go to the documentation of this file.
1
19
20#include <inttypes.h>
21
22#include <cstdio>
23#include <memory>
24
25#include "rogue/GeneralError.h"
28
30
32/*
33 * Pass owner, raw data buffer, and meta data
34 */
35ris::BufferPtr ris::Buffer::create(ris::PoolPtr source, void* data, uint32_t meta, uint32_t size, uint32_t alloc) {
36 ris::BufferPtr buff = std::make_shared<ris::Buffer>(source, data, meta, size, alloc);
37 return (buff);
38}
39
41/*
42 * Pass owner, raw data buffer, and meta data
43 */
44ris::Buffer::Buffer(ris::PoolPtr source, void* data, uint32_t meta, uint32_t size, uint32_t alloc) {
45 source_ = source;
46 data_ = reinterpret_cast<uint8_t*>(data);
47 meta_ = meta;
48 rawSize_ = size;
49 allocSize_ = alloc;
50 headRoom_ = 0;
51 tailRoom_ = 0;
52 payload_ = 0;
53}
54
56/*
57 * Owner return buffer method is called
58 */
59ris::Buffer::~Buffer() {
60 source_->retBuffer(data_, meta_, allocSize_);
61}
62
64void ris::Buffer::setFrame(ris::FramePtr frame) {
65 frame_ = frame;
66}
67
69uint32_t ris::Buffer::getMeta() {
70 return (meta_);
71}
72
74void ris::Buffer::setMeta(uint32_t meta) {
75 meta_ = meta;
76}
77
79void ris::Buffer::adjustHeader(int32_t value) {
80 // Decreasing header size
81 if (value < 0 && static_cast<uint32_t>(abs(value)) > headRoom_)
82 throw(rogue::GeneralError::create("Buffer::adjustHeader",
83 "Attempt to reduce header with size %" PRIu32 " by %" PRIi32,
84 headRoom_,
85 value));
86
87 // Increasing header size
88 if (value > 0 && static_cast<uint32_t>(value) > (rawSize_ - (headRoom_ + tailRoom_)))
89 throw(rogue::GeneralError::create("Buffer::adjustHeader",
90 "Attempt to increase header by %" PRIi32 " in buffer with size %" PRIu32,
91 value,
92 (rawSize_ - (headRoom_ + tailRoom_))));
93
94 // Make adjustment
95 headRoom_ += value;
96
97 // Payload can never be less than headroom
98 if (payload_ < headRoom_) payload_ = headRoom_;
99
100 ris::FramePtr tmpPtr;
101 if ((tmpPtr = frame_.lock())) tmpPtr->setSizeDirty();
102}
103
105void ris::Buffer::zeroHeader() {
106 headRoom_ = 0;
107
108 ris::FramePtr tmpPtr;
109 if ((tmpPtr = frame_.lock())) tmpPtr->setSizeDirty();
110}
111
113void ris::Buffer::adjustTail(int32_t value) {
114 // Decreasing tail size
115 if (value < 0 && static_cast<uint32_t>(abs(value)) > tailRoom_)
116 throw(rogue::GeneralError::create("Buffer::adjustTail",
117 "Attempt to reduce tail with size %" PRIu32 " by %" PRIi32,
118 tailRoom_,
119 value));
120
121 // Increasing tail size
122 if (value > 0 && static_cast<uint32_t>(value) > (rawSize_ - (headRoom_ + tailRoom_)))
123 throw(rogue::GeneralError::create("Buffer::adjustTail",
124 "Attempt to increase header by %" PRIi32 " in buffer with size %" PRIu32,
125 value,
126 (rawSize_ - (headRoom_ + tailRoom_))));
127
128 // Make adjustment
129 tailRoom_ += value;
130
131 ris::FramePtr tmpPtr;
132 if ((tmpPtr = frame_.lock())) tmpPtr->setSizeDirty();
133}
134
136void ris::Buffer::zeroTail() {
137 tailRoom_ = 0;
138
139 ris::FramePtr tmpPtr;
140 if ((tmpPtr = frame_.lock())) tmpPtr->setSizeDirty();
141}
142
143/*
144 * Get data pointer (begin iterator)
145 * Returns base + header size
146 */
147uint8_t* ris::Buffer::begin() {
148 return (data_ + headRoom_);
149}
150
151/*
152 * Get end data pointer (end iterator)
153 * This is the end of raw data buffer
154 */
155uint8_t* ris::Buffer::end() {
156 return (data_ + (rawSize_ - tailRoom_));
157}
158
159/*
160 * Get end payload pointer (end iterator)
161 * This is the end of payload data
162 */
163uint8_t* ris::Buffer::endPayload() {
164 return (data_ + payload_);
165}
166
167/*
168 * Get size of buffer that can hold
169 * payload data. This function
170 * returns the full buffer size minus
171 * the head and tail reservation.
172 */
173uint32_t ris::Buffer::getSize() {
174 return (rawSize_ - (headRoom_ + tailRoom_));
175}
176
177/*
178 * Get available size for payload
179 * This is the space remaining for payload
180 * minus the space reserved for the tail
181 */
182uint32_t ris::Buffer::getAvailable() {
183 uint32_t ret;
184
185 ret = rawSize_ - payload_;
186
187 // Subtract tail space, avoid overflow
188 if (ret < tailRoom_)
189 ret = 0;
190 else
191 ret -= tailRoom_;
192
193 return (ret);
194}
195
196/*
197 * Get real payload size without header
198 * This is the count of real data in the
199 * packet, minus the portion reserved for
200 * the head.
201 */
202uint32_t ris::Buffer::getPayload() {
203 return (payload_ - headRoom_);
204}
205
207void ris::Buffer::setPayload(uint32_t size) {
208 if (size > (rawSize_ - (headRoom_ + tailRoom_)))
209 throw(rogue::GeneralError::create("Buffer::setPayload",
210 "Attempt to set payload to size %" PRIu32 " in buffer with size %" PRIu32,
211 size,
212 (rawSize_ - (headRoom_ + tailRoom_))));
213
214 payload_ = size + headRoom_;
215
216 ris::FramePtr tmpPtr;
217 if ((tmpPtr = frame_.lock())) tmpPtr->setSizeDirty();
218}
219
220/*
221 * Set min payload size (not including header)
222 * Payload size is updated only if size > current size
223 */
224void ris::Buffer::minPayload(uint32_t size) {
225 if (size > getPayload()) setPayload(size);
226}
227
229void ris::Buffer::adjustPayload(int32_t value) {
230 if (value < 0 && static_cast<uint32_t>(abs(value)) > getPayload())
231 throw(rogue::GeneralError::create("Buffer::adjustPayload",
232 "Attempt to decrease payload by %" PRIi32 " in buffer with size %" PRIu32,
233 value,
234 getPayload()));
235
236 setPayload(getPayload() + value);
237}
238
240void ris::Buffer::setPayloadFull() {
241 payload_ = rawSize_ - tailRoom_;
242
243 ris::FramePtr tmpPtr;
244 if ((tmpPtr = frame_.lock())) tmpPtr->setSizeDirty();
245}
246
248void ris::Buffer::setPayloadEmpty() {
249 payload_ = headRoom_;
250
251 ris::FramePtr tmpPtr;
252 if ((tmpPtr = frame_.lock())) tmpPtr->setSizeDirty();
253}
254
256void ris::Buffer::debug(uint32_t idx) {
257 printf(" Buffer: %" PRIu32 ", AllocSize: %" PRIu32 ", RawSize: %" PRIu32 ", HeadRoom: %" PRIu32
258 ", TailRoom: %" PRIu32 ", Payload: %" PRIu32 "\n",
259 idx,
260 allocSize_,
261 rawSize_,
262 headRoom_,
263 tailRoom_,
264 payload_);
265}
static GeneralError create(std::string src, const char *fmt,...)
Creates a formatted error instance.
std::shared_ptr< rogue::interfaces::stream::Pool > PoolPtr
Shared pointer alias for Pool.
Definition Pool.h:249
std::shared_ptr< rogue::interfaces::stream::Buffer > BufferPtr
Shared pointer alias for Buffer.
Definition Buffer.h:270
std::shared_ptr< rogue::interfaces::stream::Frame > FramePtr
Shared pointer alias for Frame.
Definition Frame.h:549