1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
/*
** $Id: lbuffer.cpp 905 2008-07-20 21:08:22Z aquadran $
** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h
*/
#include "lauxlib.h"
#include "lmem.h"
#include "lstate.h"
/*-------------------------------------------------------
** Auxiliary buffer
-------------------------------------------------------*/
#define BUFF_STEP 32
#define openspace(size) if (L->Mbuffnext+(size) > L->Mbuffsize) Openspace(size)
static void Openspace (int32 size)
{
lua_State *l = L; /* to optimize */
int32 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 = (char *)luaM_realloc(l->Mbuffer, l->Mbuffsize);
l->Mbuffbase = l->Mbuffer+base;
}
char *luaL_openspace (int32 size)
{
openspace(size);
return L->Mbuffer+L->Mbuffnext;
}
void luaL_addchar (int32 c)
{
openspace(BUFF_STEP);
L->Mbuffer[L->Mbuffnext++] = c;
}
void luaL_resetbuffer (void)
{
L->Mbuffnext = L->Mbuffbase-L->Mbuffer;
}
void luaL_addsize (int32 n)
{
L->Mbuffnext += n;
}
int32 luaL_getsize (void)
{
return L->Mbuffnext-(L->Mbuffbase-L->Mbuffer);
}
int32 luaL_newbuffer (int32 size)
{
int32 old = L->Mbuffbase-L->Mbuffer;
openspace(size);
L->Mbuffbase = L->Mbuffer+L->Mbuffnext;
return old;
}
void luaL_oldbuffer (int32 old)
{
L->Mbuffnext = L->Mbuffbase-L->Mbuffer;
L->Mbuffbase = L->Mbuffer+old;
}
char *luaL_buffer (void)
{
return L->Mbuffbase;
}
|