Browse Source

Add a new liBuffer alloc function for long-term buffers

personal/stbuehler/wip
Stefan Bühler 12 years ago
parent
commit
26ebaa3bd3
  1. 5
      include/lighttpd/buffer.h
  2. 30
      src/common/buffer.c

5
include/lighttpd/buffer.h

@ -15,7 +15,12 @@ struct liBuffer {
};
/* shared buffer; free memory after last reference is released */
/** create new buffer: optimized for short-term buffers which will be released soon, uses mempool */
LI_API liBuffer* li_buffer_new(gsize max_size);
/** create new buffer; optimized for long-term buffers, uses g_slice_alloc */
LI_API liBuffer* li_buffer_new_slice(gsize max_size);
LI_API void li_buffer_acquire(liBuffer *buf);
LI_API void li_buffer_release(liBuffer *buf);

30
src/common/buffer.c

@ -8,12 +8,26 @@ static void _buffer_init(liBuffer *buf, gsize alloc_size) {
buf->addr = buf->mptr.data;
}
static void _buffer_init_slice(liBuffer *buf, gsize alloc_size) {
buf->alloc_size = alloc_size;
buf->used = 0;
buf->mptr.data = NULL;
buf->addr = g_slice_alloc(alloc_size);
}
static void _buffer_destroy(liBuffer *buf) {
if (!buf || NULL == buf->addr) return;
mempool_free(buf->mptr, buf->alloc_size);
buf->addr = NULL;
buf->mptr.data = NULL; buf->mptr.priv_data = NULL;
buf->used = buf->alloc_size = 0;
if (NULL == buf->mptr.data) {
g_slice_free1(buf->alloc_size, buf->addr);
} else {
mempool_free(buf->mptr, buf->alloc_size);
buf->addr = NULL;
buf->mptr.data = NULL; buf->mptr.priv_data = NULL;
buf->used = buf->alloc_size = 0;
}
g_slice_free(liBuffer, buf);
}
@ -24,12 +38,18 @@ liBuffer* li_buffer_new(gsize max_size) {
return buf;
}
liBuffer* li_buffer_new_slice(gsize max_size) {
liBuffer *buf = g_slice_new0(liBuffer);
_buffer_init_slice(buf, max_size);
buf->refcount = 1;
return buf;
}
void li_buffer_release(liBuffer *buf) {
if (!buf) return;
assert(g_atomic_int_get(&buf->refcount) > 0);
if (g_atomic_int_dec_and_test(&buf->refcount)) {
_buffer_destroy(buf);
g_slice_free(liBuffer, buf);
}
}

Loading…
Cancel
Save