Simple data structures / objects in plain C Snapshot
Classes | Typedefs | Functions
DBUF

buffer that is owner of stuff that grows dynamically as you add stuff to it; This object owns the memory that it is holding. it also has length/size fields, no assumption data includes zeros or not - any data (not just strings). More...

Classes

struct  tagDBUF

Typedefs

typedef struct tagDBUF DBUF

Functions

M_INLINE int DBUF_init (DBUF *buf, size_t init_size)
 initialise a buffer
M_INLINE void DBUF_reset (DBUF *buf)
M_INLINE void DBUF_free (DBUF *buf)
 free memory owned by a buffer
int DBUF_add (DBUF *buf, void *data, size_t data_size)
 add stuff to a buffer
M_INLINE void * DBUF_buffer (DBUF *buf)
M_INLINE size_t DBUF_size (DBUF *buf)

Detailed Description

buffer that is owner of stuff that grows dynamically as you add stuff to it; This object owns the memory that it is holding. it also has length/size fields, no assumption data includes zeros or not - any data (not just strings).


Typedef Documentation

typedef struct tagDBUF DBUF

Function Documentation

int DBUF_add ( DBUF buf,
void *  data,
size_t  data_size 
)

add stuff to a buffer

Definition at line 4 of file dbuf.c.

{
  size_t bs;
  uint8_t *nb;
  
  bs = buf->buf_size;
  if ( (buf->buf_used + data_size) > bs) {
    if (bs == 0) {
      bs = 1;
    }
    do {
      bs *= 2;
    } while( (buf->buf_used + data_size) > bs );

    nb = (uint8_t *) realloc( buf->buf, bs );
    if (!nb) {
      return -1;
    }
    buf->buf = nb;
    buf->buf_size = bs;
  }
  
  memcpy( buf->buf + buf->buf_used, data, data_size );
  buf->buf_size = bs;
  buf->buf_used += data_size;

  return 0; 
}
M_INLINE void* DBUF_buffer ( DBUF buf)

Definition at line 64 of file dbuf.h.

{
  return buf->buf;
}
M_INLINE void DBUF_free ( DBUF buf)

free memory owned by a buffer

Definition at line 50 of file dbuf.h.

                                   {
  if (buf->buf) {
    free(buf->buf); 
    buf->buf = 0;
    buf->buf_used = buf->buf_size = 0;
  }
}
M_INLINE int DBUF_init ( DBUF buf,
size_t  init_size 
)

initialise a buffer

Definition at line 29 of file dbuf.h.

{
  buf->buf =  0;
  if (init_size) {
    buf->buf = malloc( init_size );
    if (!buf->buf) {
      return -1;
    }
  }
  buf->buf_used = 0;
  buf->buf_size = init_size;
  return 0;
}
M_INLINE void DBUF_reset ( DBUF buf)

Definition at line 43 of file dbuf.h.

                                    {
  buf->buf_used = 0;
}
M_INLINE size_t DBUF_size ( DBUF buf)

Definition at line 69 of file dbuf.h.

{
  return buf->buf_used;
}