source: roaraudio/libroar/buffer.c @ 5011:512edfc544df

Last change on this file since 5011:512edfc544df was 5011:512edfc544df, checked in by phi, 11 years ago

Added memory corruption and double free detection to buffer API.

File size: 18.9 KB
Line 
1//buffer.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2011
5 *
6 *  This file is part of libroar a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  libroar is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 *  NOTE for everyone want's to change something and send patches:
25 *  read README and HACKING! There a addition information on
26 *  the license of this document you need to read before you send
27 *  any patches.
28 *
29 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
30 *  or libpulse*:
31 *  The libs libroaresd, libroararts and libroarpulse link this lib
32 *  and are therefore GPL. Because of this it may be illigal to use
33 *  them with any software that uses libesd, libartsc or libpulse*.
34 */
35
36#include "libroar.h"
37
38struct roar_buffer_ring {
39 size_t read_pos;
40 size_t write_pos;
41};
42
43struct roar_buffer {
44 size_t               refc;
45 size_t               len;
46 size_t               user_len;
47 int                  flags;
48 void               * data;
49 void               * user_data;
50 union {
51  void                    * vp;
52  int32_t                   i32;
53  struct roar_buffer_ring   ring;
54 }                    meta;
55 int                  type;
56 struct roar_buffer * next;
57};
58
59#define _ckbuf_free(m)  if ( buf == NULL || (m) ) { roar_err_set(ROAR_ERROR_FAULT); return -1; }
60#define _ckbuf(m)  _ckbuf_free((m)); if ( _ckmem_corruption(buf) == -1 ) return -1;
61
62static int inline _ckmem_corruption(struct roar_buffer * buf) {
63 int flags = buf->flags & (ROAR_BUFFER_FLAG_USEABLE|ROAR_BUFFER_FLAG_FREED);
64
65 ROAR_DBG("_ckmem_corruption(buf=%p{.flags=0x%.4x, ...} = ?", buf, buf->flags);
66
67 if ( flags == ROAR_BUFFER_FLAG_USEABLE ) {
68  // seems ok, continue with next check.
69 } else if ( flags == ROAR_BUFFER_FLAG_FREED ) {
70  roar_panic(ROAR_FATAL_ERROR_MEMORY_USED_AFTER_FREE, NULL);
71  roar_err_set(ROAR_ERROR_FAULT);
72  return -1;
73 } else {
74  roar_panic(ROAR_FATAL_ERROR_MEMORY_CORRUPTION, NULL);
75  roar_err_set(ROAR_ERROR_BADCKSUM);
76  return -1;
77 }
78
79 if ( buf->refc == 0 ) {
80  roar_panic(ROAR_FATAL_ERROR_MEMORY_CORRUPTION, NULL);
81 }
82
83 return 0;
84}
85
86int roar_buffer_new_data (struct roar_buffer ** buf, size_t len, void ** data) {
87 void * bufdata;
88 int err;
89
90 if ((bufdata = roar_mm_malloc(len)) == NULL) {
91  return -1;
92 }
93
94 if ( roar_buffer_new_no_ma(buf, len, bufdata) == -1 ) {
95  err = roar_error;
96  roar_mm_free(bufdata);
97  roar_err_set(err);
98  return -1;
99 }
100
101 if ( roar_buffer_set_flag(*buf, ROAR_BUFFER_FLAG_NOFREE, ROAR_BUFFER_RESET) == -1 ) {
102  err = roar_error;
103  roar_buffer_free(*buf);
104  roar_mm_free(bufdata);
105  roar_err_set(err);
106  return -1;
107 }
108
109 if ( data != NULL )
110  *data = bufdata;
111
112 return 0;
113}
114
115int roar_buffer_new_no_ma(struct roar_buffer ** buf, size_t len, void * data) { // no internal malloc
116 struct roar_buffer * new;
117
118 ROAR_DBG("buffer_new(buf=%p, len=%i) = ?", buf, len);
119
120 _ckbuf_free(data == NULL)
121
122 if ((new = roar_mm_malloc(sizeof(struct roar_buffer))) == NULL) {
123  *buf = NULL;
124  return -1;
125 }
126
127 new->refc      =  1;
128
129 new->data      = data;
130
131 new->flags     = ROAR_BUFFER_FLAG_NONE|ROAR_BUFFER_FLAG_NOFREE|ROAR_BUFFER_FLAG_USEABLE;
132 new->type      = -1;
133
134 new->user_data = new->data;
135
136 new->next      = NULL;
137
138 new->len       = len;
139 new->user_len  = len;
140 *buf           = new;
141
142 ROAR_DBG("buffer_new(buf=%p, len=%i): New buffer at %p", buf, len, new);
143
144 return 0;
145}
146
147int roar_buffer_ref      (struct roar_buffer *  buf) {
148 _ckbuf(0)
149
150 buf->refc++;
151
152 return 0;
153}
154
155int roar_buffer_unref     (struct roar_buffer * buf) {
156 struct roar_buffer * next, * cur;
157 int flags;
158
159 ROAR_DBG("roar_buffer_unref(buf=%p) = ?", buf);
160
161 _ckbuf_free(0)
162
163 ROAR_DBG("roar_buffer_unref(buf=%p) = ?", buf);
164
165 flags = buf->flags & (ROAR_BUFFER_FLAG_USEABLE|ROAR_BUFFER_FLAG_FREED);
166 if ( flags == ROAR_BUFFER_FLAG_FREED ) {
167  roar_panic(ROAR_FATAL_ERROR_MEMORY_DOUBLE_FREE, NULL);
168 }
169
170 _ckmem_corruption(buf);
171
172 ROAR_DBG("roar_buffer_unref(buf=%p) = ?", buf);
173
174 if ( buf->refc == 0 ) {
175  ROAR_WARN("roar_buffer_unref(buf=%p): Ref counter is wrong. assuming one.");
176  buf->refc = 1;
177  roar_panic(ROAR_FATAL_ERROR_MEMORY_CORRUPTION, NULL);
178 }
179
180 buf->refc--;
181
182 if ( buf->refc ) {
183  ROAR_DBG("roar_buffer_unref(buf=%p) = 0", buf);
184  return 0;
185 }
186
187 ROAR_DBG("roar_buffer_unref(buf=%p) = ?", buf);
188
189 cur = buf->next;
190 while (cur != NULL) {
191  flags = cur->flags & (ROAR_BUFFER_FLAG_USEABLE|ROAR_BUFFER_FLAG_FREED);
192  if ( flags == ROAR_BUFFER_FLAG_FREED ) {
193   roar_panic(ROAR_FATAL_ERROR_MEMORY_DOUBLE_FREE, NULL);
194  }
195
196  ROAR_DBG("roar_buffer_unref(buf=%p) = ?", buf);
197
198  _ckmem_corruption(cur);
199
200  if ( roar_buffer_get_flag(cur, ROAR_BUFFER_FLAG_NOFREE) != 1 )
201   roar_mm_free(cur->data);
202
203  cur->flags = ROAR_BUFFER_FLAG_FREED;
204  next = cur->next;
205  roar_mm_free(cur);
206  cur = next;
207 }
208
209 ROAR_DBG("roar_buffer_unref(buf=%p) = ?", buf);
210
211 if ( !(buf->flags & ROAR_BUFFER_FLAG_NOFREE) )
212  roar_mm_free(buf->data);
213
214 ROAR_DBG("roar_buffer_unref(buf=%p): setting flags = ROAR_BUFFER_FLAG_FREED", buf);
215 buf->flags = ROAR_BUFFER_FLAG_FREED;
216
217 roar_mm_free(buf);
218
219 ROAR_DBG("roar_buffer_unref(buf=%p) = 0", buf);
220 return 0;
221}
222
223int roar_buffer_delete   (struct roar_buffer * buf, struct roar_buffer ** next) {
224 if ( buf == NULL ) {
225  if ( next != NULL )
226   *next = NULL;
227  roar_err_set(ROAR_ERROR_FAULT);
228  return -1;
229 }
230
231 ROAR_DBG("buffer_delete(buf=%p, next=%p) = ?", buf, next);
232
233 if ( next != NULL )
234  *next = buf->next;
235
236 if ( roar_buffer_get_flag(buf, ROAR_BUFFER_FLAG_NOFREE) != 1 )
237  roar_mm_free(buf->data);
238
239 roar_mm_free(buf);
240
241 ROAR_DBG("buffer_delete(buf=%p, next=%p) = 0", buf, next);
242 return 0;
243}
244
245int roar_buffer_add      (struct roar_buffer * buf, struct roar_buffer *  next) {
246 unsigned int deep = 0;
247
248 _ckbuf(0)
249
250 ROAR_DBG("buffer_add(buf=%p, next=%p) = ?", buf, next);
251
252 if ( buf->flags & ROAR_BUFFER_FLAG_RING ) {
253  roar_err_set(ROAR_ERROR_INVAL);
254  return -1;
255 }
256
257 if ( buf == next ) {
258  ROAR_ERR("buffer_add(*): both pointer are of the same destination, This is a error in the application");
259  roar_err_set(ROAR_ERROR_INVAL);
260  return -1;
261 }
262
263 while ( buf->next != NULL ) {
264  ROAR_DBG("buffer_add(*): buf=%p, next=%p (len=%i)", buf, buf->next, buf->user_len);
265//  ROAR_DBG("buffer_add(): buf=%p, buf->next=%p", buf, buf->next);
266  buf = buf->next;
267  deep++;
268
269  if ( buf == next ) {
270   ROAR_ERR("buffer_add(*): Can not add buffer: loop detected at deep %u. This is a error in the application", deep);
271   // why don't we return here?
272  }
273 }
274
275 buf->next = next;
276
277 ROAR_DBG("buffer_add(*): adding buffer at deep %u", deep);
278
279 return 0;
280}
281
282int roar_buffer_clear_next (struct roar_buffer *  buf) {
283 _ckbuf(0)
284
285 buf->next = NULL;
286
287 return 0;
288}
289
290int roar_buffer_get_next (struct roar_buffer *  buf, struct roar_buffer ** next) {
291 _ckbuf(0)
292
293 *next = buf->next;
294
295 return 0;
296}
297
298int roar_buffer_ring_new (struct roar_buffer ** buf, size_t len, int free_running) {
299 struct roar_buffer * n;
300
301 _ckbuf_free(0)
302
303 if ( len == 0 ) {
304  roar_err_set(ROAR_ERROR_RANGE);
305  return -1;
306 }
307
308 // just to be sure:
309 *buf = NULL;
310
311 if ( roar_buffer_new(&n, len) == -1 )
312  return -1;
313
314 n->flags |= ROAR_BUFFER_FLAG_RING;
315
316 if ( free_running )
317  n->flags |= ROAR_BUFFER_FLAG_FREE_RUNNING;
318
319 n->meta.ring.read_pos  = 0;
320 n->meta.ring.write_pos = 0;
321
322 memset(n->data, 0, n->len);
323
324 *buf = n;
325
326 return 0;
327}
328
329int roar_buffer_get_data (struct roar_buffer *  buf, void   ** data) {
330 _ckbuf(0)
331
332 *data = buf->user_data;
333
334 return 0;
335}
336
337int roar_buffer_set_offset (struct roar_buffer *  buf, size_t off) {
338 _ckbuf(0)
339
340 if ( off > buf->user_len ) {
341  roar_err_set(ROAR_ERROR_RANGE);
342  return -1;
343 }
344
345 buf->user_len  -= off;
346 buf->user_data += off;
347
348 return 0;
349}
350
351int roar_buffer_shift_out (struct roar_buffer ** buf, void * data, size_t * len) {
352 size_t todo, cl;
353 struct roar_buffer * cur;
354 void * cd;
355
356 _ckbuf_free(len == NULL || data == NULL);
357
358 if ( *buf == NULL ) {
359  ROAR_DBG("roar_buffer_shift_out(buf=%p, data=%p, len={%lu}) = -1 // Invalid pointer to buffer ring", buf, data, (unsigned long)len);
360  roar_err_set(ROAR_ERROR_FAULT);
361  return -1;
362 }
363
364 todo = *len;
365 cur  = *buf;
366
367 *len = 0;
368
369 _ckmem_corruption(cur);
370
371 while (todo && cur != NULL) {
372  ROAR_DBG("roar_buffer_shift_out(*): todo=%u, cur=%p", (unsigned int) todo, cur);
373
374  if ( roar_buffer_get_len(cur, &cl) == -1 )
375   return -1;
376
377  if ( cl > todo ) {
378   if ( roar_buffer_get_data(cur, &cd) == -1 )
379    return -1;
380
381   cl = todo;
382
383   memcpy(data, cd, cl);
384   todo -= cl;
385   data += cl;
386   *len += cl;
387
388   if ( roar_buffer_set_offset(cur, cl) == -1 )
389    return -1;
390  } else {
391   if ( roar_buffer_get_data(cur, &cd) == -1 )
392    return -1;
393
394   memcpy(data, cd, cl);
395   todo -= cl;
396   data += cl;
397   *len += cl;
398
399   if ( roar_buffer_next(&cur) == -1 )
400    return -1;
401   _ckmem_corruption(cur);
402  }
403
404/*
405  if ( cur == NULL )
406   break;
407*/
408 }
409
410 *buf = cur;
411
412 return 0;
413}
414
415int roar_buffer_set_meta (struct roar_buffer * buf, void *  meta) {
416 _ckbuf(0)
417
418 buf->meta.vp = meta;
419
420 return 0;
421}
422
423int roar_buffer_get_meta (struct roar_buffer * buf, void ** meta) {
424 _ckbuf(meta == NULL)
425
426 *meta = buf->meta.vp;
427
428 return 0;
429}
430
431int roar_buffer_set_meta_i32(struct roar_buffer *  buf, int32_t    meta) {
432 _ckbuf(0)
433
434 buf->meta.i32 = meta;
435
436 return 0;
437}
438
439int roar_buffer_get_meta_i32(struct roar_buffer *  buf, int32_t *  meta) {
440 _ckbuf(meta == NULL)
441
442 *meta = buf->meta.i32;
443
444 return 0;
445}
446
447int roar_buffer_set_type    (struct roar_buffer *  buf, int        type) {
448 _ckbuf(0)
449
450 switch (type) {
451  case ROAR_VIO_DFT_RAW:
452  case ROAR_VIO_DFT_PACKET:
453  case ROAR_VIO_DFT_UNFRAMED:
454  case ROAR_VIO_DFT_OGG_PACKET:
455  case ROAR_VIO_DFT_OGG_PAGE:
456   break;
457  default:
458    roar_err_set(ROAR_ERROR_NOTSUP);
459    return -1;
460 }
461
462 buf->type = type;
463
464 return 0;
465}
466
467int roar_buffer_get_type    (struct roar_buffer *  buf, int     *  type) {
468 _ckbuf(type == NULL)
469
470 *type = buf->type;
471
472 return 0;
473}
474
475int roar_buffer_set_len  (struct roar_buffer *  buf, size_t    len) {
476 size_t   totlen;
477 void   * newbuf;
478
479 _ckbuf(0)
480
481 // handle specal case where user length is zero:
482 if ( len && !buf->user_len ) {
483  buf->user_data = buf->data;
484  buf->user_len  = buf->len;
485 }
486
487 if ( len > buf->user_len ) {
488  // we can only enlage a buffer if it's one of our own memory segments
489  if ( buf->flags & ROAR_BUFFER_FLAG_NOFREE )  {
490   roar_err_set(ROAR_ERROR_NOTSUP);
491   return -1;
492  }
493
494  totlen = (buf->len - buf->user_len) + len;
495  newbuf = roar_mm_realloc(buf->data, totlen);
496  if ( newbuf == NULL )
497   return -1;
498
499  buf->user_data = newbuf + (buf->user_data - buf->data);
500  buf->user_len = len;
501  buf->data = newbuf;
502  buf->len  = totlen;
503 } else {
504  buf->user_len = len;
505 }
506
507 return 0;
508}
509
510int roar_buffer_get_len  (struct roar_buffer *  buf, size_t *  len) {
511 _ckbuf(0)
512
513 *len = buf->user_len;
514
515 return 0;
516}
517
518int roar_buffer_set_flag (struct roar_buffer *  buf, int flag, int reset) {
519 _ckbuf(0)
520
521 buf->flags |= flag;
522
523 if ( reset )
524  buf->flags -= flag;
525
526 return 0;
527}
528
529int roar_buffer_get_flag (struct roar_buffer *  buf, int flag) {
530 _ckbuf(0)
531
532 return buf->flags & flag;
533}
534
535int roar_buffer_duplicate (struct roar_buffer *  buf, struct roar_buffer ** copy) {
536 struct roar_buffer *  cur = buf;
537 struct roar_buffer *  new;
538 void * od, * nd;
539 int err;
540
541 _ckbuf(copy == NULL)
542
543 *copy = NULL;
544
545 while (cur != NULL) {
546  if ( roar_buffer_new(&new, cur->user_len) == -1 ) {
547   err = roar_error;
548   roar_buffer_free(*copy);
549   roar_err_set(err);
550   return -1;
551  }
552
553  if ( *copy == NULL )
554   *copy = new;
555
556  roar_buffer_get_data(cur, &od);
557  roar_buffer_get_data(new, &nd);
558  memcpy(nd, od, cur->user_len);
559
560  roar_buffer_add(*copy, new);
561
562  cur = cur->next;
563 }
564
565 return 0;
566}
567
568int roar_buffer_ring_stats (struct roar_buffer *  buf, struct roar_buffer_stats * stats) {
569 _ckbuf(0)
570
571 stats->parts        = 0;
572 stats->bytes        = 0;
573 stats->memory_usage = 0;
574
575 while (buf != NULL ) {
576  stats->parts++;
577  stats->bytes        += buf->user_len;
578  stats->memory_usage += buf->len + sizeof(struct roar_buffer);
579  buf = buf->next;
580 }
581
582 return 0;
583}
584
585int roar_buffer_ring_read  (struct roar_buffer *  buf, void * data, size_t * len) {
586 struct roar_buffer_ring * ring;
587 size_t havelen;
588 size_t done;
589 size_t tmp;
590
591 _ckbuf(len == NULL)
592
593 if ( buf == NULL || len == NULL )
594  return -1;
595
596 if ( data == NULL && *len != 0 ) {
597  roar_err_set(ROAR_ERROR_FAULT);
598  return -1;
599 }
600
601 if ( !(buf->flags & ROAR_BUFFER_FLAG_RING) ) {
602  roar_err_set(ROAR_ERROR_TYPEMM);
603  return -1;
604 }
605
606 if ( *len == 0 )
607  return 0;
608
609 // we may handle this later:
610 if ( *len > buf->user_len ) {
611  roar_err_set(ROAR_ERROR_NOTSUP);
612  return -1;
613 }
614
615 if ( (buf->flags & ROAR_BUFFER_FLAG_FREE_RUNNING) ) {
616  if ( buf->meta.ring.read_pos >= buf->user_len )
617   buf->meta.ring.read_pos -= buf->user_len;
618
619  if ( (*len + buf->meta.ring.read_pos) > buf->user_len ) {
620   // wraped mode:
621   memcpy(data, buf->user_data+buf->meta.ring.read_pos, buf->user_len - buf->meta.ring.read_pos);
622   memcpy(data, buf->user_data, *len + buf->meta.ring.read_pos - buf->user_len);
623 
624   buf->meta.ring.read_pos += *len;
625   buf->meta.ring.read_pos -= buf->user_len;
626   return 0;
627  } else {
628   // unwarped mode:
629   memcpy(data, buf->user_data+buf->meta.ring.read_pos, *len);
630   buf->meta.ring.read_pos += *len;
631   return 0;
632  }
633 } else {
634  ring = &(buf->meta.ring);
635
636  if ( ring->read_pos == ring->write_pos ) {
637   *len = 0;
638   return 0;
639  } else if ( ring->read_pos < ring->write_pos ) {
640   havelen = ring->write_pos - ring->read_pos;
641
642   if ( havelen > *len )
643    havelen = *len;
644
645   memcpy(data, buf->user_data+ring->read_pos, havelen);
646   ring->read_pos += havelen;
647
648   *len = havelen;
649   return 0;
650  } else { // write_pos < read_pos
651   done = 0;
652
653   // first pass: use data up to end of buffer
654
655   havelen = buf->user_len - ring->read_pos;
656
657   if ( havelen > *len )
658    havelen = *len;
659
660   memcpy(data, buf->user_data+ring->read_pos, havelen);
661   ring->read_pos += havelen;
662
663   done += havelen;
664
665   if ( ring->read_pos == buf->user_len ) {
666    ring->read_pos = 0;
667   }
668
669   // second pass: use data from strat of buffer to write pointer
670
671   if ( *len > done ) {
672    tmp = *len - done;
673    if ( roar_buffer_ring_read(buf, data+done, &tmp) == 0 ) {
674     done += tmp;
675    }
676   }
677
678   *len = done;
679   return 0;
680  }
681 }
682
683 roar_err_set(ROAR_ERROR_UNKNOWN);
684 return -1;
685}
686
687int roar_buffer_ring_write (struct roar_buffer *  buf, void * data, size_t * len) {
688 struct roar_buffer_ring * ring;
689 size_t havelen;
690 size_t done;
691 size_t tmp;
692
693 ROAR_DBG("roar_buffer_ring_write(buf=%p, data=%p, len=%p) = ?", buf, data, len);
694
695 _ckbuf(len == NULL)
696
697 if ( data == NULL && *len != 0 ) {
698  roar_err_set(ROAR_ERROR_FAULT);
699  return -1;
700 }
701
702 if ( !(buf->flags & ROAR_BUFFER_FLAG_RING) ) {
703  roar_err_set(ROAR_ERROR_TYPEMM);
704  return -1;
705 }
706
707 if ( *len == 0 )
708  return 0;
709
710 // we may handle this later:
711 if ( *len > buf->user_len ) {
712  roar_err_set(ROAR_ERROR_NOTSUP);
713  return -1;
714 }
715
716 if ( (buf->flags & ROAR_BUFFER_FLAG_FREE_RUNNING) ) {
717  if ( buf->meta.ring.write_pos >= buf->user_len )
718   buf->meta.ring.write_pos -= buf->user_len;
719
720  if ( (*len + buf->meta.ring.write_pos) > buf->user_len ) {
721   // wraped mode:
722   memcpy(buf->user_data+buf->meta.ring.write_pos, data, buf->user_len - buf->meta.ring.write_pos);
723   memcpy(buf->user_data, data, *len + buf->meta.ring.write_pos - buf->user_len);
724
725   buf->meta.ring.write_pos += *len;
726   buf->meta.ring.write_pos -= buf->user_len;
727   return 0;
728  } else {
729   // unwarped mode:
730   memcpy(buf->user_data+buf->meta.ring.write_pos, data, *len);
731   buf->meta.ring.write_pos += *len;
732   return 0;
733  }
734 } else {
735  ring = &(buf->meta.ring);
736  done = 0;
737
738  ROAR_DBG("roar_buffer_ring_write(buf=%p, data=%p, len=%p): write_pos=%u, read_pos=%u, user_len=%u", buf, data, len, (unsigned int)ring->write_pos, (unsigned int)ring->read_pos, (unsigned int)buf->user_len);
739
740  if ( ring->write_pos >= ring->read_pos ) {
741   ROAR_DBG("roar_buffer_ring_write(buf=%p, data=%p, len=%p) = ?", buf, data, len);
742
743   havelen = buf->user_len - ring->write_pos;
744
745   if ( ring->read_pos == 0 )
746    havelen--;
747
748   if ( havelen > *len )
749    havelen = *len;
750
751   memcpy(buf->user_data+ring->write_pos, data, havelen);
752
753   done += havelen;
754   ring->write_pos += havelen;
755
756   if ( ring->write_pos == buf->user_len )
757    ring->write_pos = 0;
758
759   if ( *len > done && ring->read_pos != 0 ) {
760    tmp = *len - done;
761    if ( roar_buffer_ring_write(buf, data+done, &tmp) == 0 ) {
762     done += tmp;
763    }
764   }
765
766   *len = done;
767   ROAR_DBG("roar_buffer_ring_write(buf=%p, data=%p, len=%p) = 0", buf, data, len);
768   return 0;
769  } else {
770   ROAR_DBG("roar_buffer_ring_write(buf=%p, data=%p, len=%p) = ?", buf, data, len);
771
772   // test for buffer-is-full:
773   if ( (ring->write_pos + 1) == ring->read_pos ) {
774    *len = 0;
775    ROAR_DBG("roar_buffer_ring_write(buf=%p, data=%p, len=%p) = 0", buf, data, len);
776    return 0;
777   }
778
779   ROAR_DBG("roar_buffer_ring_write(buf=%p, data=%p, len=%p) = ?", buf, data, len);
780
781   havelen = ring->read_pos - ring->write_pos - 1;
782
783   if ( havelen > *len )
784    havelen = *len;
785
786   memcpy(buf->user_data+ring->write_pos, data, havelen);
787
788   ring->write_pos += havelen;
789   *len = havelen;
790
791   ROAR_DBG("roar_buffer_ring_write(buf=%p, data=%p, len=%p) = 0", buf, data, len);
792   return 0;
793  }
794 }
795
796 roar_err_set(ROAR_ERROR_UNKNOWN);
797 return -1;
798}
799
800int roar_buffer_ring_avail(struct roar_buffer *  buf, size_t * readlen, size_t * writelen) {
801 struct roar_buffer_ring * ring;
802 size_t have;
803
804 _ckbuf(0)
805
806 if ( !(buf->flags & ROAR_BUFFER_FLAG_RING) ) {
807  roar_err_set(ROAR_ERROR_TYPEMM);
808  return -1;
809 }
810
811 ring = &(buf->meta.ring);
812
813 ROAR_DBG("roar_buffer_ring_avail(buf=%p, readlen=%p, writelen=%p) = ?", buf, readlen, writelen);
814
815 if ( readlen != NULL ) {
816  have = 0;
817
818  if ( ring->write_pos >= ring->read_pos ) {
819   have  = ring->write_pos - ring->read_pos;
820  } else {
821   have  = buf->user_len - ring->read_pos;
822   have += ring->write_pos;
823   have -= 1;
824  }
825
826  *readlen = have;
827  ROAR_DBG("roar_buffer_ring_avail(buf=%p, readlen=%p, writelen=%p): readlen=%llu", buf, readlen, writelen, (unsigned long long int)have);
828 }
829
830 if ( writelen != NULL ) {
831  have = 0;
832
833  if ( ring->read_pos > ring->write_pos ) {
834   have  = ring->read_pos - ring->write_pos - 1;
835  } else {
836   have  = buf->user_len - ring->write_pos;
837   have += ring->read_pos;
838   have -= 1;
839  }
840
841  *writelen = have;
842  ROAR_DBG("roar_buffer_ring_avail(buf=%p, readlen=%p, writelen=%p): readlen=%llu", buf, readlen, writelen, (unsigned long long int)have);
843 }
844
845 ROAR_DBG("roar_buffer_ring_avail(buf=%p, readlen=%p, writelen=%p) = 0", buf, readlen, writelen);
846
847 return 0;
848}
849
850int roar_buffer_ring_reset(struct roar_buffer *  buf) {
851 _ckbuf(0)
852
853 if ( !(buf->flags & ROAR_BUFFER_FLAG_RING) ) {
854  roar_err_set(ROAR_ERROR_TYPEMM);
855  return -1;
856 }
857
858 buf->meta.ring.read_pos = buf->meta.ring.write_pos = 0;
859
860 return 0;
861}
862
863//ll
Note: See TracBrowser for help on using the repository browser.