Index: libroar/buffer.c
===================================================================
--- libroar/buffer.c	(revision 4903)
+++ libroar/buffer.c	(revision 4905)
@@ -57,6 +57,9 @@
 };
 
+#define _ckbuf(m)  if ( buf == NULL || (m) ) { roar_err_set(ROAR_ERROR_FAULT); return -1; }
+
 int roar_buffer_new_data (struct roar_buffer ** buf, size_t len, void ** data) {
  void * bufdata;
+ int err;
 
  if ((bufdata = roar_mm_malloc(len)) == NULL) {
@@ -65,11 +68,15 @@
 
  if ( roar_buffer_new_no_ma(buf, len, bufdata) == -1 ) {
+  err = roar_error;
   roar_mm_free(bufdata);
+  roar_err_set(err);
   return -1;
  }
 
  if ( roar_buffer_set_flag(*buf, ROAR_BUFFER_FLAG_NOFREE, ROAR_BUFFER_RESET) == -1 ) {
+  err = roar_error;
   roar_buffer_free(*buf);
   roar_mm_free(bufdata);
+  roar_err_set(err);
   return -1;
  }
@@ -86,6 +93,5 @@
  ROAR_DBG("buffer_new(buf=%p, len=%i) = ?", buf, len);
 
- if ( buf == NULL || data == NULL )
-  return -1;
+ _ckbuf(data == NULL)
 
  if ((new = roar_mm_malloc(sizeof(struct roar_buffer))) == NULL) {
@@ -115,6 +121,5 @@
 
 int roar_buffer_ref      (struct roar_buffer *  buf) {
- if ( buf == NULL )
-  return -1;
+ _ckbuf(0)
 
  buf->refc++;
@@ -126,6 +131,5 @@
  struct roar_buffer * next;
 
- if ( buf == NULL )
-  return -1;
+ _ckbuf(0)
 
  if ( buf->refc == 0 ) {
@@ -159,4 +163,5 @@
   if ( next != NULL )
    *next = NULL;
+  roar_err_set(ROAR_ERROR_FAULT);
   return -1;
  }
@@ -179,14 +184,16 @@
  unsigned int deep = 0;
 
- if ( buf == NULL )
-  return -1;
+ _ckbuf(0)
 
  ROAR_DBG("buffer_add(buf=%p, next=%p) = ?", buf, next);
 
- if ( buf->flags & ROAR_BUFFER_FLAG_RING )
-  return -1;
+ if ( buf->flags & ROAR_BUFFER_FLAG_RING ) {
+  roar_err_set(ROAR_ERROR_INVAL);
+  return -1;
+ }
 
  if ( buf == next ) {
   ROAR_ERR("buffer_add(*): both pointer are of the same destination, This is a error in the application");
+  roar_err_set(ROAR_ERROR_INVAL);
   return -1;
  }
@@ -200,4 +207,5 @@
   if ( buf == next ) {
    ROAR_ERR("buffer_add(*): Can not add buffer: loop detected at deep %u. This is a error in the application", deep);
+   // why don't we return here?
   }
  }
@@ -211,6 +219,5 @@
 
 int roar_buffer_clear_next (struct roar_buffer *  buf) {
- if ( buf == NULL )
-  return -1;
+ _ckbuf(0)
 
  buf->next = NULL;
@@ -220,6 +227,5 @@
 
 int roar_buffer_get_next (struct roar_buffer *  buf, struct roar_buffer ** next) {
- if ( buf == NULL )
-  return -1;
+ _ckbuf(0)
 
  *next = buf->next;
@@ -231,6 +237,10 @@
  struct roar_buffer * n;
 
- if ( buf == NULL || len == 0 )
-  return -1;
+ _ckbuf(0)
+
+ if ( len == 0 ) {
+  roar_err_set(ROAR_ERROR_RANGE);
+  return -1;
+ }
 
  // just to be sure:
@@ -256,6 +266,5 @@
 
 int roar_buffer_get_data (struct roar_buffer *  buf, void   ** data) {
- if ( buf == NULL )
-  return -1;
+ _ckbuf(0)
 
  *data = buf->user_data;
@@ -265,6 +274,10 @@
 
 int roar_buffer_set_offset (struct roar_buffer *  buf, size_t off) {
- if ( buf == NULL )
-  return -1;
+ _ckbuf(0)
+
+ if ( off > buf->user_len ) {
+  roar_err_set(ROAR_ERROR_RANGE);
+  return -1;
+ }
 
  buf->user_len  -= off;
@@ -279,11 +292,9 @@
  void * cd;
 
- if ( len == NULL || buf == NULL || data == NULL ) {
-  ROAR_DBG("roar_buffer_shift_out(buf=%p, data=%p, len={%lu}) = -1 // Invalid input", buf, data, (unsigned long)*len);
-  return -1;
- }
+ _ckbuf(len == NULL || data == NULL);
 
  if ( *buf == NULL ) {
   ROAR_DBG("roar_buffer_shift_out(buf=%p, data=%p, len={%lu}) = -1 // Invalid pointer to buffer ring", buf, data, (unsigned long)len);
+  roar_err_set(ROAR_ERROR_FAULT);
   return -1;
  }
@@ -338,6 +349,5 @@
 
 int roar_buffer_set_meta (struct roar_buffer * buf, void *  meta) {
- if ( buf == NULL )
-  return -1;
+ _ckbuf(0)
 
  buf->meta.vp = meta;
@@ -347,6 +357,5 @@
 
 int roar_buffer_get_meta (struct roar_buffer * buf, void ** meta) {
- if ( buf == NULL || meta == NULL )
-  return -1;
+ _ckbuf(meta == NULL)
 
  *meta = buf->meta.vp;
@@ -356,6 +365,5 @@
 
 int roar_buffer_set_meta_i32(struct roar_buffer *  buf, int32_t    meta) {
- if ( buf == NULL )
-  return -1;
+ _ckbuf(0)
 
  buf->meta.i32 = meta;
@@ -365,6 +373,5 @@
 
 int roar_buffer_get_meta_i32(struct roar_buffer *  buf, int32_t *  meta) {
- if ( buf == NULL || meta == NULL )
-  return -1;
+ _ckbuf(meta == NULL)
 
  *meta = buf->meta.i32;
@@ -374,6 +381,5 @@
 
 int roar_buffer_set_type    (struct roar_buffer *  buf, int        type) {
- if ( buf == NULL )
-  return -1;
+ _ckbuf(0)
 
  switch (type) {
@@ -383,4 +389,5 @@
    break;
   default:
+    roar_err_set(ROAR_ERROR_NOTSUP);
     return -1;
  }
@@ -392,6 +399,5 @@
 
 int roar_buffer_get_type    (struct roar_buffer *  buf, int     *  type) {
- if ( buf == NULL || type == NULL )
-  return -1;
+ _ckbuf(type == NULL)
 
  *type = buf->type;
@@ -404,6 +410,5 @@
  void   * newbuf;
 
- if ( buf == NULL )
-  return -1;
+ _ckbuf(0)
 
  // handle specal case where user length is zero:
@@ -415,6 +420,8 @@
  if ( len > buf->user_len ) {
   // we can only enlage a buffer if it's one of our own memory segments
-  if ( buf->flags & ROAR_BUFFER_FLAG_NOFREE )
+  if ( buf->flags & ROAR_BUFFER_FLAG_NOFREE )  {
+   roar_err_set(ROAR_ERROR_NOTSUP);
    return -1;
+  }
 
   totlen = buf->len - buf->user_len + len;
@@ -433,6 +440,5 @@
 
 int roar_buffer_get_len  (struct roar_buffer *  buf, size_t *  len) {
- if ( buf == NULL )
-  return -1;
+ _ckbuf(0)
 
  *len = buf->user_len;
@@ -442,6 +448,5 @@
 
 int roar_buffer_set_flag (struct roar_buffer *  buf, int flag, int reset) {
- if ( buf == NULL )
-  return -1;
+ _ckbuf(0)
 
  buf->flags |= flag;
@@ -454,6 +459,5 @@
 
 int roar_buffer_get_flag (struct roar_buffer *  buf, int flag) {
- if ( buf == NULL )
-  return -1;
+ _ckbuf(0)
 
  return buf->flags & flag;
@@ -464,10 +468,15 @@
  struct roar_buffer *  new;
  void * od, * nd;
+ int err;
+
+ _ckbuf(copy == NULL)
 
  *copy = NULL;
 
- while (cur) {
+ while (cur != NULL) {
   if ( roar_buffer_new(&new, cur->user_len) == -1 ) {
+   err = roar_error;
    roar_buffer_free(*copy);
+   roar_err_set(err);
    return -1;
   }
@@ -484,10 +493,10 @@
   cur = cur->next;
  }
+
  return 0;
 }
 
 int roar_buffer_ring_stats (struct roar_buffer *  buf, struct roar_buffer_stats * stats) {
- if ( buf == NULL )
-  return -1;
+ _ckbuf(0)
 
  stats->parts        = 0;
@@ -495,5 +504,5 @@
  stats->memory_usage = 0;
 
- while (buf) {
+ while (buf != NULL ) {
   stats->parts++;
   stats->bytes        += buf->user_len;
@@ -511,12 +520,18 @@
  size_t tmp;
 
+ _ckbuf(len == NULL)
+
  if ( buf == NULL || len == NULL )
   return -1;
 
- if ( data == NULL && *len != 0 )
-  return -1;
-
- if ( !(buf->flags & ROAR_BUFFER_FLAG_RING) )
-  return -1;
+ if ( data == NULL && *len != 0 ) {
+  roar_err_set(ROAR_ERROR_FAULT);
+  return -1;
+ }
+
+ if ( !(buf->flags & ROAR_BUFFER_FLAG_RING) ) {
+  roar_err_set(ROAR_ERROR_TYPEMM);
+  return -1;
+ }
 
  if ( *len == 0 )
@@ -524,6 +539,8 @@
 
  // we may handle this later:
- if ( *len > buf->user_len )
-  return -1;
+ if ( *len > buf->user_len ) {
+  roar_err_set(ROAR_ERROR_NOTSUP);
+  return -1;
+ }
 
  if ( (buf->flags & ROAR_BUFFER_FLAG_FREE_RUNNING) ) {
@@ -593,8 +610,7 @@
    return 0;
   }
-
-  return -1;
- }
-
+ }
+
+ roar_err_set(ROAR_ERROR_UNKNOWN);
  return -1;
 }
@@ -608,12 +624,15 @@
  ROAR_DBG("roar_buffer_ring_write(buf=%p, data=%p, len=%p) = ?", buf, data, len);
 
- if ( buf == NULL || len == NULL )
-  return -1;
-
- if ( data == NULL && *len != 0 )
-  return -1;
-
- if ( !(buf->flags & ROAR_BUFFER_FLAG_RING) )
-  return -1;
+ _ckbuf(len == NULL)
+
+ if ( data == NULL && *len != 0 ) {
+  roar_err_set(ROAR_ERROR_FAULT);
+  return -1;
+ }
+
+ if ( !(buf->flags & ROAR_BUFFER_FLAG_RING) ) {
+  roar_err_set(ROAR_ERROR_TYPEMM);
+  return -1;
+ }
 
  if ( *len == 0 )
@@ -621,6 +640,8 @@
 
  // we may handle this later:
- if ( *len > buf->user_len )
-  return -1;
+ if ( *len > buf->user_len ) {
+  roar_err_set(ROAR_ERROR_NOTSUP);
+  return -1;
+ }
 
  if ( (buf->flags & ROAR_BUFFER_FLAG_FREE_RUNNING) ) {
@@ -702,8 +723,7 @@
    return 0;
   }
-
-  return -1;
- }
-
+ }
+
+ roar_err_set(ROAR_ERROR_UNKNOWN);
  return -1;
 }
@@ -713,9 +733,10 @@
  size_t have;
 
- if ( buf == NULL )
-  return -1;
-
- if ( !(buf->flags & ROAR_BUFFER_FLAG_RING) )
-  return -1;
+ _ckbuf(0)
+
+ if ( !(buf->flags & ROAR_BUFFER_FLAG_RING) ) {
+  roar_err_set(ROAR_ERROR_TYPEMM);
+  return -1;
+ }
 
  ring = &(buf->meta.ring);
@@ -759,9 +780,10 @@
 
 int roar_buffer_ring_reset(struct roar_buffer *  buf) {
- if ( buf == NULL )
-  return -1;
-
- if ( !(buf->flags & ROAR_BUFFER_FLAG_RING) )
-  return -1;
+ _ckbuf(0)
+
+ if ( !(buf->flags & ROAR_BUFFER_FLAG_RING) ) {
+  roar_err_set(ROAR_ERROR_TYPEMM);
+  return -1;
+ }
 
  buf->meta.ring.read_pos = buf->meta.ring.write_pos = 0;
