31 #include "ringbuffer.h"
41 if( !(rbuf->
buffer = (uint8_t *) malloc(
sizeof(uint8_t)*size)) ){
42 LOG(VB_GENERAL, LOG_ERR,
43 "Not enough memory for ringbuffer");
47 LOG(VB_GENERAL, LOG_ERR,
"Wrong size for ringbuffer");
57 if (size > (
int)(rbuf->
size)) {
58 uint8_t *tmpalloc = (uint8_t *) realloc(rbuf->
buffer,
59 sizeof(uint8_t)*
size);
65 unsigned int delta = size - rbuf->
size;
94 if (count <=0 )
return 0;
96 rest = rbuf->
size - pos;
101 LOG(VB_GENERAL, LOG_ERR,
102 "ringbuffer overflow %d<%d %d",
103 free, count, rbuf->
size);
108 memcpy (rbuf->
buffer+pos, data, rest);
110 memcpy (rbuf->
buffer, data+rest, count - rest);
113 memcpy (rbuf->
buffer+pos, data, count);
118 LOG(VB_GENERAL, LOG_ERR,
"Buffer empty %.2f%%",
126 unsigned int avail, pos, rest;
131 rest = rbuf->
size - pos ;
135 if ( avail < count ){
138 LOG(VB_GENERAL, LOG_ERR,
139 "ringbuffer peek underflow %d<%d %d %d",
146 memcpy(data, rbuf->
buffer+pos, count);
148 memcpy(data, rbuf->
buffer+pos, rest);
150 memcpy(data+rest, rbuf->
buffer, count - rest);
159 unsigned int avail, pos, rest;
164 rest = rbuf->
size - pos ;
168 if ( avail < count ){
171 LOG(VB_GENERAL, LOG_ERR,
172 "ringbuffer peek underflow %d<%d %d %d",
179 memcpy(rbuf->
buffer+pos, data, count);
181 memcpy(rbuf->
buffer+pos, data, rest);
183 memcpy(rbuf->
buffer, data+rest, count - rest);
192 int avail, pos, rest;
194 if (count <=0 )
return 0;
196 rest = rbuf->
size - pos;
199 if ( avail < count ){
202 LOG(VB_GENERAL, LOG_ERR,
203 "ringbuffer underflow %d<%d %d \n",
204 avail, count, rbuf->
size);
210 memcpy(data, rbuf->
buffer+pos, count);
213 memcpy(data, rbuf->
buffer+pos, rest);
215 memcpy(data+rest, rbuf->
buffer, count - rest);
220 LOG(VB_GENERAL, LOG_ERR,
"Buffer empty %.2f%%",
228 int avail, pos, rest;
230 if (count <=0 )
return -1;
232 rest = rbuf->
size - pos;
235 if ( avail < count ){
237 LOG(VB_GENERAL, LOG_ERR,
238 "ringbuffer skip underflow %d<%d %d %d\n",
250 LOG(VB_GENERAL, LOG_ERR,
"Buffer empty %.2f%%",
260 int free, pos, rest, rr;
262 if (count <=0 )
return 0;
264 rest = rbuf->
size - pos;
269 LOG(VB_GENERAL, LOG_ERR,
270 "ringbuffer overflow %d<%d %d %d\n",
276 rr = read (fd, rbuf->
buffer+pos, rest);
277 if (rr == rest && count - rest)
278 rr += read (fd, rbuf->
buffer, count - rest);
282 rr = read (fd, rbuf->
buffer+pos, count);
288 LOG(VB_GENERAL, LOG_ERR,
"Buffer empty %.2f%%",
298 int avail, pos, rest, rr;
300 if (count <=0 )
return -1;
302 rest = rbuf->
size - pos;
305 if ( avail < count ){
308 LOG(VB_GENERAL, LOG_ERR,
309 "ringbuffer underflow %d<%d %d %d",
317 if (rr == rest && count - rest)
329 LOG(VB_GENERAL, LOG_ERR,
"Buffer empty %.2f%%",
342 for (i=0; i<length; i+=16){
343 for (j=0; j < 8 && j+i<length; j++)
345 sprintf(temp,
"0x%02x ", (
int)(buf[i+j]));
346 strcat(buffer, temp);
353 for (j=8; j < 16 && j+i<length; j++)
355 sprintf(temp,
"0x%02x ", (
int)(buf[i+j]));
356 strcat(buffer, temp);
361 for (j=0; j < 16 && j+i<length; j++){
365 sprintf(temp,
"%c", buf[i+j]);
370 strcat(buffer, temp);
372 LOG(VB_GENERAL, LOG_INFO, buffer);
379 unsigned int avail, pos, rest;
384 rest = rbuf->
size - pos ;
388 if ( avail < count ){
391 LOG(VB_GENERAL, LOG_ERR,
392 "ringbuffer peek underflow %d<%d %d %d\n",
437 LOG(VB_GENERAL, LOG_INFO,
"add %d ",
dummy_space(dbuf));
445 LOG(VB_GENERAL, LOG_INFO,
" - %d = "%
d", size, dummy_space(dbuf));
450 int dummy_delete(dummy_buffer *dbuf, uint64_t time)
458 if (ring_peek(&dbuf->time_index,(uint8_t *) &rtime,
459 sizeof(uint64_t), 0)<0){
463 if (ptscmp(rtime,time) < 0){
464 ring_read(&dbuf->time_index,(uint8_t *) &rtime,
466 ring_read(&dbuf->data_index,(uint8_t *) &size,
472 LOG(VB_GENERAL, LOG_INFO, "delete %
d ", dummy_space(dbuf));
476 LOG(VB_GENERAL, LOG_INFO, " + %
d = %
d", dsize, dummy_space(dbuf));
482 static void dummy_print(dummy_buffer *dbuf)
487 int avail = ring_avail(&dbuf->time_index) / sizeof(uint64_t);
488 for(i = 0; i < avail; i++) {
489 ring_peek(&dbuf->time_index,(uint8_t *) &rtime,
490 sizeof(uint64_t), i * sizeof(uint64_t));
491 ring_peek(&dbuf->data_index,(uint8_t *) &size,
492 sizeof(uint32_t), i * sizeof(uint32_t));
494 LOG(VB_GENERAL, LOG_INFO, "%
d : %llu %u
", i,
495 (long long unsigned int)rtime, size);
497 LOG(VB_GENERAL, LOG_INFO, "Used: %
d Free: %
d data-free: %
d", avail,
498 1000-avail, dbuf->size - dbuf->fill);