aboutsummaryrefslogtreecommitdiffstats
path: root/util-src/ringbuffer.c
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2017-01-23 19:34:14 +0100
committerKim Alvefur <zash@zash.se>2017-01-23 19:34:14 +0100
commit468d3db1a1eb87026d20303eeda6ff46d459df80 (patch)
treeea895b1073f2ff2de7a08fc01f12797ab58c7125 /util-src/ringbuffer.c
parent4c567124f67372cba0b688120eb726ab543b8bf1 (diff)
parentb54ba7235144f22208ce22ca31da705a62531935 (diff)
downloadprosody-468d3db1a1eb87026d20303eeda6ff46d459df80.tar.gz
prosody-468d3db1a1eb87026d20303eeda6ff46d459df80.zip
Merge 0.10->trunk
Diffstat (limited to 'util-src/ringbuffer.c')
-rw-r--r--util-src/ringbuffer.c28
1 files changed, 3 insertions, 25 deletions
diff --git a/util-src/ringbuffer.c b/util-src/ringbuffer.c
index d60a43d9..73a8616b 100644
--- a/util-src/ringbuffer.c
+++ b/util-src/ringbuffer.c
@@ -1,5 +1,4 @@
-
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
@@ -8,15 +7,12 @@
#include <lua.h>
#include <lauxlib.h>
-#define MIN(a, b) ((a)>(b)?(b):(a))
-#define MAX(a, b) ((a)>(b)?(a):(b))
-
typedef struct {
size_t rpos; /* read position */
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) {
@@ -76,7 +72,6 @@ int rb_find(lua_State* L) {
return 0;
}
-
int rb_read(lua_State* L) {
ringbuffer* b = luaL_checkudata(L, 1, "ringbuffer_mt");
int r = luaL_checkinteger(L, 2);
@@ -104,7 +99,6 @@ int rb_read(lua_State* L) {
return 1;
}
-
int rb_readuntil(lua_State* L) {
size_t l, m;
ringbuffer* b = luaL_checkudata(L, 1, "ringbuffer_mt");
@@ -169,16 +163,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);
@@ -186,16 +176,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);
@@ -205,8 +185,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 */
{