Simple data structures / objects in plain C Snapshot
dbuf.c
Go to the documentation of this file.
00001 #include "dbuf.h"
00002 #include <string.h>
00003 
00004 int DBUF_add(  DBUF* buf, void *data, size_t data_size )
00005 {
00006   size_t bs;
00007   uint8_t *nb;
00008   
00009   bs = buf->buf_size;
00010   if ( (buf->buf_used + data_size) > bs) {
00011     if (bs == 0) {
00012       bs = 1;
00013     }
00014     do {
00015       bs *= 2;
00016     } while( (buf->buf_used + data_size) > bs );
00017 
00018     nb = (uint8_t *) realloc( buf->buf, bs );
00019     if (!nb) {
00020       return -1;
00021     }
00022     buf->buf = nb;
00023     buf->buf_size = bs;
00024   }
00025   
00026   memcpy( buf->buf + buf->buf_used, data, data_size );
00027   buf->buf_size = bs;
00028   buf->buf_used += data_size;
00029 
00030   return 0; 
00031 }
00032 
00033