Initial import of libucore

This commit is contained in:
Nils O. Selåsdal
2012-10-29 23:07:32 +01:00
commit ed3866e49a
79 changed files with 6181 additions and 0 deletions
+77
View File
@@ -0,0 +1,77 @@
#include <stdlib.h>
#include <string.h>
#include "ucore_buffer.h"
GBuf*
uc_new_gbuf(size_t initsz)
{
GBuf *p;
p = malloc(sizeof *p);
if(p == NULL)
return NULL;
p->used = 0;
p->len = initsz;
p->buf = NULL;
p->ref_cnt = 1;
if(initsz != 0 && (p->buf = malloc(initsz)) == NULL) {
free(p);
return NULL;
}
return p;
}
void
uc_gbuf_ref(GBuf *buf)
{
buf->ref_cnt++;
}
void
uc_gbuf_unref(GBuf *buf)
{
buf->ref_cnt--;
if(buf->ref_cnt == 0) {
free(buf->buf);
free(buf);
}
}
int
uc_gbuf_append(GBuf *buf,void *data,size_t len)
{
unsigned char *d = data;
unsigned char *gbuf;
if(buf->len - buf->used < len)
if(uc_grow_gbuf(buf, len + len/2))
return -1;
gbuf = buf->buf;
memcpy(gbuf + buf->used,d,len);
buf->used += len;
return 0;
}
int
uc_grow_gbuf(GBuf *buf, size_t addlen)
{
void *tmp;
size_t newsz;
newsz = buf->len + addlen;
tmp = realloc(buf->buf,newsz);
if(tmp == NULL)
return -1;
buf->buf = tmp;
buf->len = newsz;
return 0;
}