aboutsummaryrefslogtreecommitdiffstats
path: root/util-src/ringbuffer.c
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2017-01-22 09:23:10 +0100
committerKim Alvefur <zash@zash.se>2017-01-22 09:23:10 +0100
commitc8e4e17b468657f997ad03e0baa91f205b71213c (patch)
tree5f6205ae3b551a92ce8b18ba4297e1b5a2c6afdc /util-src/ringbuffer.c
parent26d03a9a1af39684490dcc02b36f092db92cb4cc (diff)
downloadprosody-c8e4e17b468657f997ad03e0baa91f205b71213c.tar.gz
prosody-c8e4e17b468657f997ad03e0baa91f205b71213c.zip
util.ringbuffer: Allocate buffer itself as part of userdata (simpler, single allocation, no need for __gc)
Diffstat (limited to 'util-src/ringbuffer.c')
-rw-r--r--util-src/ringbuffer.c22
1 files changed, 3 insertions, 19 deletions
diff --git a/util-src/ringbuffer.c b/util-src/ringbuffer.c
index 950bda2b..f7487a88 100644
--- a/util-src/ringbuffer.c
+++ b/util-src/ringbuffer.c
@@ -13,7 +13,7 @@ typedef struct {
size_t wpos; /* write position */
size_t alen; /* allocated size */
size_t blen; /* current content size */
- char* buffer;
+ char buffer[];
} ringbuffer;
char readchar(ringbuffer* b) {
@@ -166,16 +166,12 @@ int rb_free(lua_State* L) {
int rb_new(lua_State* L) {
size_t size = luaL_optinteger(L, 1, sysconf(_SC_PAGESIZE));
- ringbuffer* b = lua_newuserdata(L, sizeof(ringbuffer));
+ ringbuffer *b = lua_newuserdata(L, sizeof(ringbuffer) + size);
+
b->rpos = 0;
b->wpos = 0;
b->alen = size;
b->blen = 0;
- b->buffer = malloc(size);
-
- if(b->buffer == NULL) {
- return 0;
- }
luaL_getmetatable(L, "ringbuffer_mt");
lua_setmetatable(L, -2);
@@ -183,16 +179,6 @@ int rb_new(lua_State* L) {
return 1;
}
-int rb_gc(lua_State* L) {
- ringbuffer* b = luaL_checkudata(L, 1, "ringbuffer_mt");
-
- if(b->buffer != NULL) {
- free(b->buffer);
- }
-
- return 0;
-}
-
int luaopen_util_ringbuffer(lua_State* L) {
#if (LUA_VERSION_NUM > 501)
luaL_checkversion(L);
@@ -202,8 +188,6 @@ int luaopen_util_ringbuffer(lua_State* L) {
lua_setfield(L, -2, "__tostring");
lua_pushcfunction(L, rb_length);
lua_setfield(L, -2, "__len");
- lua_pushcfunction(L, rb_gc);
- lua_setfield(L, -2, "__gc");
lua_newtable(L); /* __index */
{