Commit cde7ad26 authored by seny's avatar seny
Browse files

Remove inline storage

parent 7069e9a8
...@@ -27,18 +27,16 @@ ...@@ -27,18 +27,16 @@
#define MAXSTACK 1000 /* Arbitrary stack size limit to check for recursion */ #define MAXSTACK 1000 /* Arbitrary stack size limit to check for recursion */
typedef struct { typedef struct {
char *buf, data[1024]; char *buf;
size_t pos, size; size_t pos, size;
} Box; } Box;
static char *resizeBox(lua_State *L, Box *box, size_t size) { static char *resizeBox(lua_State *L, Box *box, size_t size) {
void *ud; void *ud;
lua_Alloc allocf = lua_getallocf(L, &ud); lua_Alloc allocf = lua_getallocf(L, &ud);
int dyn = box->buf != box->data; /* Dynamically allocated? */ char *buf = allocf(ud, box->buf, box->size, size);
char *buf = dyn ? allocf(ud, box->buf, box->size, size) : allocf(ud, 0, 0, size);
if (!size) return 0; if (!size) return 0;
if (!buf) luaL_error(L, "cannot allocate buffer"); if (!buf) luaL_error(L, "cannot allocate buffer");
if (!dyn) memcpy(buf, box->buf, box->pos);
box->buf = buf; box->buf = buf;
box->size = size; box->size = size;
return buf; return buf;
...@@ -51,14 +49,15 @@ static int m__gc(lua_State *L) { ...@@ -51,14 +49,15 @@ static int m__gc(lua_State *L) {
static Box *newBox(lua_State *L) { static Box *newBox(lua_State *L) {
Box *box = lua_newuserdata(L, sizeof *box); Box *box = lua_newuserdata(L, sizeof *box);
box->buf = box->data; box->buf = 0;
box->pos = 0; box->pos = 0;
box->size = sizeof box->data; box->size = 0;
if (luaL_newmetatable(L, MODNAME)) { if (luaL_newmetatable(L, MODNAME)) {
lua_pushcfunction(L, m__gc); lua_pushcfunction(L, m__gc);
lua_setfield(L, -2, "__gc"); lua_setfield(L, -2, "__gc");
} }
lua_setmetatable(L, -2); lua_setmetatable(L, -2);
resizeBox(L, box, 100);
return box; return box;
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment