Simple data structures / objects in plain C Snapshot
dbuf.h
Go to the documentation of this file.
00001 #ifndef _DBUF_X_Y_Z_
00002 #define _DBUF_X_Y_Z_
00003 
00004 #ifdef  __cplusplus
00005 extern "C" {
00006 #endif
00007 
00008 
00009 #include <cutils/base.h>
00010 
00011 /**
00012  * @defgroup DBUF
00013  * @brief buffer that is owner  of stuff that grows dynamically as you add stuff to it;  
00014  * This object owns the memory that it is holding.
00015  * it also has length/size fields, no assumption data includes zeros or not - any data (not just strings).
00016  * @{
00017  */
00018 typedef struct tagDBUF {
00019 
00020   uint8_t *buf;
00021   size_t buf_used;
00022   size_t buf_size;
00023 
00024 } DBUF;
00025 
00026 /**
00027  * @brief initialise a buffer
00028  */
00029 M_INLINE int DBUF_init( DBUF *buf, size_t init_size )
00030 {
00031   buf->buf =  0;
00032   if (init_size) {
00033     buf->buf = malloc( init_size );
00034     if (!buf->buf) {
00035       return -1;
00036     }
00037   }
00038   buf->buf_used = 0;
00039   buf->buf_size = init_size;
00040   return 0;
00041 }
00042 
00043 M_INLINE void DBUF_reset(DBUF *buf) {
00044   buf->buf_used = 0;
00045 }
00046 
00047 /**
00048  * @brief free memory owned by a buffer
00049  */
00050 M_INLINE void DBUF_free(DBUF *buf) {
00051   if (buf->buf) {
00052     free(buf->buf); 
00053     buf->buf = 0;
00054     buf->buf_used = buf->buf_size = 0;
00055   }
00056 }
00057 
00058 /**
00059  * @brief add stuff to a buffer
00060  */
00061 int DBUF_add(  DBUF* buf, void *data, size_t data_size );
00062 
00063 
00064 M_INLINE  void * DBUF_buffer( DBUF *buf )
00065 {
00066   return buf->buf;
00067 }
00068 
00069 M_INLINE size_t DBUF_size( DBUF *buf )
00070 {
00071   return buf->buf_used;
00072 }
00073  
00074 /**
00075  * @}
00076  */
00077 
00078 #ifdef  __cplusplus
00079 }
00080 #endif
00081 
00082 
00083 #endif
00084