From 502343b40230dfb00efc37bdbaa5c5576f3a5aa5 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 17 Dec 1997 18:48:58 -0200 Subject: new scheme for buffers, centralized in auxlib. --- lauxlib.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) (limited to 'lauxlib.c') diff --git a/lauxlib.c b/lauxlib.c index ed893f70..ce6c2c78 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.c,v 1.4 1997/11/21 19:00:46 roberto Exp roberto $ +** $Id: lauxlib.c,v 1.5 1997/12/09 13:35:19 roberto Exp roberto $ ** Auxiliar functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -10,6 +10,8 @@ #include #include "lauxlib.h" +#include "lmem.h" +#include "lstate.h" #include "lua.h" #include "luadebug.h" @@ -99,3 +101,70 @@ void luaL_verror (char *fmt, ...) } +/*------------------------------------------------------- +** Auxiliar buffer +-------------------------------------------------------*/ + +#define BUFF_STEP 32 + +#define openspace(size) if (L->Mbuffnext+(size) > L->Mbuffsize) Openspace(size) + +static void Openspace (int size) +{ + LState *l = L; /* to optimize */ + int base = l->Mbuffbase-l->Mbuffer; + l->Mbuffsize *= 2; + if (l->Mbuffnext+size > l->Mbuffsize) /* still not big enough? */ + l->Mbuffsize = l->Mbuffnext+size; + l->Mbuffer = luaM_realloc(l->Mbuffer, l->Mbuffsize); + l->Mbuffbase = l->Mbuffer+base; +} + + +char *luaL_openspace (int size) +{ + openspace(size); + return L->Mbuffer+L->Mbuffnext; +} + + +void luaL_addchar (int c) +{ + openspace(BUFF_STEP); + L->Mbuffer[L->Mbuffnext++] = c; +} + + +void luaL_resetbuffer (void) +{ + L->Mbuffnext = L->Mbuffbase-L->Mbuffer; +} + + +void luaL_addsize (int n) +{ + L->Mbuffnext += n; +} + + +int luaL_newbuffer (int size) +{ + int old = L->Mbuffbase-L->Mbuffer; + openspace(size); + L->Mbuffbase = L->Mbuffer+L->Mbuffnext; + return old; +} + + +void luaL_oldbuffer (int old) +{ + L->Mbuffnext = L->Mbuffbase-L->Mbuffer; + L->Mbuffbase = L->Mbuffer+old; +} + + +char *luaL_buffer (void) +{ + return L->Mbuffbase; +} + -- cgit v1.2.3-55-g6feb