Project
Loading...
Searching...
No Matches
PayLoadCont.h
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
12#ifndef ALICEO2_ITSMFT_PAYLOADCONT_H
13#define ALICEO2_ITSMFT_PAYLOADCONT_H
14
15#include <cstring>
16#include <vector>
17#include <functional>
18#include <Rtypes.h>
19
23
24namespace o2
25{
26namespace itsmft
27{
28
30{
31 // continous buffer for the payload, just a preallocated vector with current position and end pointer.
32 // Big endian is used.
33
34 public:
35 static constexpr size_t MinCapacity = 16;
36
38 PayLoadCont() = default;
39 PayLoadCont(size_t sz) { expand(sz); }
40 ~PayLoadCont() = default;
41
43
45
46 const uint8_t* data() const { return mBuffer.data(); }
47
49 void expand(size_t sz);
50
51 bool isEmpty() const { return mPtr >= mEnd; }
52
54 void clear()
55 {
56 mPtr = mBuffer.data();
57 mEnd = mPtr;
58 }
59
61 size_t getUnusedSize() const { return mEnd > mPtr ? mEnd - mPtr : 0; }
62
64 size_t getSize() const { return mEnd - mBuffer.data(); }
65
67 size_t getOffset() const { return mPtr - mBuffer.data(); }
68
70 size_t getCapacity() const { return mBuffer.size(); }
71
73 size_t getFreeCapacity() const { return mBuffer.size() - getSize(); }
74
76 void ensureFreeCapacity(size_t n)
77 {
78 if (getFreeCapacity() < n) {
79 expand(getCapacity() + 2 * n);
80 }
81 }
82
84 void fillFast(const uint8_t c, size_t n)
85 {
86 std::memset(mEnd, c, n);
87 mEnd += n;
88 }
89
91 void addFast(const uint8_t* ptr, size_t n)
92 {
93 std::memcpy(mEnd, ptr, n);
94 mEnd += n;
95 }
96
98 void addFast(uint8_t val) { *mEnd++ = val; }
99
101 void addFast(uint16_t val)
102 {
103 *mEnd++ = val >> 8;
104 *mEnd++ = 0xff & val;
105 }
106
108 void eraseFast(size_t n) { mEnd -= n; }
109
111 void erase(size_t n)
112 {
113 if (n > getSize()) {
114 clear();
115 } else {
116 eraseFast(n);
117 }
118 }
119
121 void fill(const uint8_t c, size_t n)
122 {
124 fillFast(c, n);
125 }
126
128 void add(const uint8_t* ptr, size_t n)
129 {
131 addFast(ptr, n);
132 }
133
135 void add(uint8_t val)
136 {
137 ensureFreeCapacity(sizeof(val));
138 addFast(val);
139 }
140
142 void add(uint16_t val)
143 {
144 ensureFreeCapacity(sizeof(val));
145 addFast(val);
146 }
147
149 void shrinkToSize(size_t sz)
150 {
151 mEnd = mPtr + sz;
152 }
153
155 uint8_t operator[](size_t i) const { return mBuffer[i]; }
156
158 uint8_t& operator[](size_t i) { return mBuffer[i]; }
159
161 bool current(uint8_t& v) const
162 {
163 if (mPtr < mEnd) {
164 v = *mPtr;
165 return true;
166 }
167 return false;
168 }
169
171 bool next(uint8_t& v)
172 {
173 if (mPtr < mEnd) {
174 v = *mPtr++;
175 return true;
176 }
177 return false;
178 }
179
181 bool next(uint16_t& v)
182 {
183 if (mPtr < mEnd - (sizeof(uint16_t) - 1)) {
184 v = (*mPtr++) << 8;
185 v |= (*mPtr++);
186 return true;
187 }
188 return false;
189 }
190
192 void rewind() { mPtr = mBuffer.data(); }
193
196 {
197 auto left = getUnusedSize();
198 if (left < getOffset()) {
199 std::memcpy(mBuffer.data(), mPtr, left); // there is no overlap
200 } else {
201 std::memmove(mBuffer.data(), mPtr, left); // there is an overlap
202 }
203 mPtr = mBuffer.data();
204 mEnd = mPtr + left;
205 }
206
208 // (attemtint to use all free capacity) using the method provided via getNext
209 size_t append(std::function<size_t(uint8_t*, size_t)> getNext)
210 {
212 auto nRead = getNext(mEnd, getFreeCapacity());
213 mEnd += nRead;
214 return nRead;
215 }
216
218 uint8_t* getPtr() { return mPtr; }
219 void setPtr(uint8_t* ptr) { mPtr = ptr; }
220
221 uint8_t* getEnd() { return mEnd; }
222 void setEnd(uint8_t* ptr) { mEnd = ptr; }
223
224 private:
225 std::vector<uint8_t> mBuffer;
226 uint8_t* mPtr = nullptr;
227 uint8_t* mEnd = nullptr;
228
229 ClassDefNV(PayLoadCont, 1);
230};
231} // namespace itsmft
232} // namespace o2
233
234#endif
int32_t i
uint32_t c
Definition RawData.h:2
TBranch * ptr
size_t getUnusedSize() const
get filled size
Definition PayLoadCont.h:61
size_t getOffset() const
booked capacity
Definition PayLoadCont.h:67
void ensureFreeCapacity(size_t n)
fill n bytes with given symbol w/o checking for the size
Definition PayLoadCont.h:76
static constexpr size_t MinCapacity
allocate buffer
Definition PayLoadCont.h:35
void eraseFast(size_t n)
erase n bytes
void shrinkToSize(size_t sz)
direct const access to value at a given slot, w/o checking for overflow
const uint8_t * data() const
increase the buffer size
Definition PayLoadCont.h:46
void moveUnusedToHead()
move unused data to the head and upload new chunk of data
void add(uint8_t val)
add new short to the buffer
void addFast(uint8_t val)
add new short to the buffer w/o checking for the size
Definition PayLoadCont.h:98
void addFast(uint16_t val)
erase n bytes w/o checking for the underflow
void rewind()
move all data between the mPtr and mEnd to the head of the buffer
uint8_t operator[](size_t i) const
direct access to value at a given slot, w/o checking for overflow
void add(const uint8_t *ptr, size_t n)
add new byte to the buffer
size_t getSize() const
get offset of the current ptr from the head
Definition PayLoadCont.h:64
PayLoadCont & operator=(const PayLoadCont &src)
uint8_t * getPtr()
direct write access
void fill(const uint8_t c, size_t n)
add n bytes to the buffer, expand if needed. no check for overlap
void add(uint16_t val)
shrink buffer to requested size, no check on over/under flow
void clear()
get unused size
Definition PayLoadCont.h:54
uint8_t & operator[](size_t i)
read current character value from buffer w/o stepping forward
void setEnd(uint8_t *ptr)
void addFast(const uint8_t *ptr, size_t n)
add new byte to the buffer w/o checking for the size
Definition PayLoadCont.h:91
size_t getCapacity() const
number of bytes still can accept w/o expanding the buffer
Definition PayLoadCont.h:70
bool next(uint8_t &v)
read short value from buffer
size_t getFreeCapacity() const
make sure buffer may accept at least n bytes
Definition PayLoadCont.h:73
bool isEmpty() const
make buffer empty w/o deallocating it
Definition PayLoadCont.h:51
bool current(uint8_t &v) const
read character value from buffer
void expand(size_t sz)
size_t append(std::function< size_t(uint8_t *, size_t)> getNext)
void fillFast(const uint8_t c, size_t n)
add n bytes to the buffer w/o checking for the size
Definition PayLoadCont.h:84
void erase(size_t n)
fill n bytes with given symbol
bool next(uint16_t &v)
move current pointer to the head
void setPtr(uint8_t *ptr)
GLdouble n
Definition glcorearb.h:1982
GLenum src
Definition glcorearb.h:1767
const GLdouble * v
Definition glcorearb.h:832
GLint left
Definition glcorearb.h:1979
GLuint GLfloat * val
Definition glcorearb.h:1582
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...