42 rbuf->
buffer = (uint8_t *) malloc(
sizeof(uint8_t)*size);
43 if (rbuf->
buffer ==
nullptr)
45 LOG(VB_GENERAL, LOG_ERR,
46 "Not enough memory for ringbuffer");
50 LOG(VB_GENERAL, LOG_ERR,
"Wrong size for ringbuffer");
60 if (size > (
int)(rbuf->
size)) {
61 auto *tmpalloc = (uint8_t *) realloc(rbuf->
buffer,
62 sizeof(uint8_t)*size);
68 unsigned int delta = size - rbuf->
size;
94 if (count <=0 )
return 0;
96 int rest = rbuf->
size - pos;
101 LOG(VB_GENERAL, LOG_ERR,
102 QString(
"ringbuffer overflow %1<%2 %3")
103 .arg(free).arg(count).arg(rbuf->
size));
109 memcpy (rbuf->
buffer+pos, data, rest);
111 memcpy (rbuf->
buffer, data+rest, count - rest);
114 memcpy (rbuf->
buffer+pos, data, count);
119 LOG(VB_GENERAL, LOG_ERR, QString(
"Buffer empty %1%%")
129 unsigned int rest = rbuf->
size - pos ;
132 if ( avail < count ){
135 LOG(VB_GENERAL, LOG_ERR,
136 QString(
"ringbuffer peek underflow %1<%2 %3 %4")
137 .arg(avail).arg(count).arg(pos).arg(rbuf->
write_pos));
143 memcpy(data, rbuf->
buffer+pos, count);
145 memcpy(data, rbuf->
buffer+pos, rest);
147 memcpy(data+rest, rbuf->
buffer, count - rest);
155 return ring_peek(rbuf, data.data(), count, off);
168 unsigned int rest = rbuf->
size - pos ;
171 if ( avail < count ){
174 LOG(VB_GENERAL, LOG_ERR,
175 QString(
"ringbuffer peek underflow %1<%2 %3 %4")
176 .arg(avail).arg(count).arg(pos).arg(rbuf->
write_pos));
182 memcpy(rbuf->
buffer+pos, data, count);
184 memcpy(rbuf->
buffer+pos, data, rest);
186 memcpy(rbuf->
buffer, data+rest, count - rest);
194 return ring_poke(rbuf, data.data(), count, off);
204 if (count <=0 )
return 0;
206 int rest = rbuf->
size - pos;
209 if ( avail < count ){
212 LOG(VB_GENERAL, LOG_ERR,
213 QString(
"ringbuffer underflow %1<%2 %3 \n")
214 .arg(avail).arg(count).arg(rbuf->
size));
220 memcpy(data, rbuf->
buffer+pos, count);
223 memcpy(data, rbuf->
buffer+pos, rest);
225 memcpy(data+rest, rbuf->
buffer, count - rest);
230 LOG(VB_GENERAL, LOG_ERR, QString(
"Buffer empty %1%%")
237 if (count <=0 )
return -1;
239 int rest = rbuf->
size - pos;
242 if ( avail < count ){
244 LOG(VB_GENERAL, LOG_ERR,
245 QString(
"ringbuffer skip underflow %1<%2 %3 %4\n")
246 .arg(avail).arg(count).arg(pos).arg(rbuf->
write_pos));
257 LOG(VB_GENERAL, LOG_ERR, QString(
"Buffer empty %1%%")
268 if (count <=0 )
return 0;
270 int rest = rbuf->
size - pos;
275 LOG(VB_GENERAL, LOG_ERR,
276 QString(
"ringbuffer overflow %1<%2 %3 %4\n")
277 .arg(free).arg(count).arg(pos).arg(rbuf->
read_pos));
284 if (rr == rest && count - rest)
295 LOG(VB_GENERAL, LOG_ERR, QString(
"Buffer empty %.2f%%")
306 if (count <=0 )
return -1;
308 int rest = rbuf->
size - pos;
311 if ( avail < count ){
314 LOG(VB_GENERAL, LOG_ERR,
315 QString(
"ringbuffer underflow %1<%2 %3 %4")
316 .arg(avail).arg(count).arg(pos).arg(rbuf->
write_pos));
323 if (rr == rest && count - rest)
335 LOG(VB_GENERAL, LOG_ERR, QString(
"Buffer empty %1%%")
341 static void show(uint8_t *buf,
int length)
345 for (
int i=0; i<length; i+=16){
347 for (j=0; j < 8 && j+i<length; j++)
348 buffer += QString(
"0x%1 ").arg(buf[i+j],16,2,QChar(
'0'));
349 for (
int r=j; r<8; r++)
354 for (j=8; j < 16 && j+i<length; j++)
355 buffer += QString(
"0x%1 ").arg(buf[i+j],16,2,QChar(
'0'));
356 for (
int r=j; r<16; r++)
359 for (j=0; j < 16 && j+i<length; j++){
363 buffer += QString(QChar(buf[i+j]));
369 LOG(VB_GENERAL, LOG_INFO, buffer);
378 unsigned int rest = rbuf->
size - pos ;
381 if ( avail < count ){
384 LOG(VB_GENERAL, LOG_ERR,
385 QString(
"ringbuffer peek underflow %1<%2 %3 %4\n")
386 .arg(avail).arg(count).arg(pos).arg(rbuf->
write_pos));
430 LOG(VB_GENERAL, LOG_INFO, QString(
"add %1 ").arg(
dummy_space(dbuf)));
438 LOG(VB_GENERAL, LOG_INFO,
439 QString(
" - %1 = %2").arg(size).arg(
dummy_space(dbuf)));
453 sizeof(uint64_t), 0)<0){
457 if (
ptscmp(rtime,time) < 0){
468 LOG(VB_GENERAL, LOG_INFO, QString(
"delete %1 ").arg(
dummy_space(dbuf)));
472 LOG(VB_GENERAL, LOG_INFO,
473 QString(
" + %1 = %2").arg(dsize).arg(
dummy_space(dbuf)));
485 for (
int i = 0; i < avail; i++) {
487 sizeof(uint64_t), i *
sizeof(uint64_t));
489 sizeof(uint32_t), i *
sizeof(uint32_t));
491 LOG(VB_GENERAL, LOG_INFO, QString(
"%1 : %2 %3").arg(i)
492 .arg(rtime).arg(size));
494 LOG(VB_GENERAL, LOG_INFO, QString(
"Used: %d Free: %d data-free: %d")
495 .arg(avail).arg(1000-avail).arg(dbuf->
size - dbuf->
fill));