Add docs to GBuf

This commit is contained in:
Nils O. Selåsdal
2012-11-08 20:03:38 +01:00
parent abf9a01fd8
commit 19469a4108
2 changed files with 55 additions and 12 deletions
+2 -2
View File
@@ -47,7 +47,7 @@ uc_gbuf_append(GBuf *buf,void *data,size_t len)
unsigned char *gbuf; unsigned char *gbuf;
if(buf->len - buf->used < len) if(buf->len - buf->used < len)
if(uc_grow_gbuf(buf, len + len/2)) if(uc_gbuf_grow(buf, len + len/2))
return -1; return -1;
gbuf = buf->buf; gbuf = buf->buf;
@@ -58,7 +58,7 @@ uc_gbuf_append(GBuf *buf,void *data,size_t len)
} }
int int
uc_grow_gbuf(GBuf *buf, size_t addlen) uc_gbuf_grow(GBuf *buf, size_t addlen)
{ {
void *tmp; void *tmp;
size_t newsz; size_t newsz;
+53 -10
View File
@@ -7,29 +7,72 @@
extern "C" { extern "C" {
#endif #endif
/** A GBuf is a growing reference counted buffer.
* The buffer is dynamically allocated, and can grow
* as you append data to it. Reference counting takes care
* of automatically freeing the GBuf and its buffer when it
* reaches 0
*/
typedef struct GBuf GBuf; typedef struct GBuf GBuf;
struct GBuf { struct GBuf {
/** Allocated buffer length.*/
size_t len; size_t len;
/** Used space in the buffer */
size_t used; size_t used;
/** Reference count */
size_t ref_cnt; size_t ref_cnt;
/** The data */
void *buf; void *buf;
}; };
GBuf* /** Allocatea new GBuf.
uc_new_gbuf(size_t initsz); *
* @param initsz Intial capacity of the GBuf
* @return New GBuf, or NULL if allocation fails,
* The returned GBuf will have ref_cnt=1
*/
GBuf* uc_new_gbuf(size_t initsz);
void /** Increment the reference count of the GBuf.
uc_gbuf_ref(GBuf *buf); *
* @param buf Bufffer
*/
void uc_gbuf_ref(GBuf *buf);
void /** Decrement the reference count of the GBuf, free it if ref_cnt reaches 0
uc_gbuf_unref(GBuf *buf); *
* @param buf Bufffer
*/
void uc_gbuf_unref(GBuf *buf);
int /** Append data to the GBuf.
uc_gbuf_append(GBuf *buf,void *data,size_t len); * Data is coped into the buffer, starting at &buf->data[used]
* The GBuf automatically grows if needed.
*
* @param buf buffer to append to
* @param data data to copy to the GBuf
* @param len length of @data to copy
* @return 0 on success, -1 on failure when allocation fails if he buffer
* needs to grow
*/
int uc_gbuf_append(GBuf *buf,void *data,size_t len);
int /** Expand the capacity of the GBuf.
uc_grow_gbuf(GBuf *buf, size_t addlen); *
* @param buf buffer to grow
* @return 0 on success, -1 on failure when allocation fails
*/
int uc_gbuf_grow(GBuf *buf, size_t addlen);
/** Remaining unised space of the GBuf.
* @param buff buffer
* @return length of unused space
*/
size_t uc_gbuf_remaining(const GBuf *buf)
{
return buf->len - buf->used;
}
#ifdef __cplusplus #ifdef __cplusplus
} }