diff options
Diffstat (limited to 'lbuffer.c')
| -rw-r--r-- | lbuffer.c | 55 |
1 files changed, 21 insertions, 34 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lbuffer.c,v 1.4 1998/06/19 16:14:09 roberto Exp roberto $ | 2 | ** $Id: lbuffer.c,v 1.5 1998/12/28 13:44:54 roberto Exp roberto $ |
| 3 | ** Auxiliary functions for building Lua libraries | 3 | ** Auxiliary functions for building Lua libraries |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -16,70 +16,57 @@ | |||
| 16 | ** Auxiliary buffer | 16 | ** Auxiliary buffer |
| 17 | -------------------------------------------------------*/ | 17 | -------------------------------------------------------*/ |
| 18 | 18 | ||
| 19 | #define BUFF_STEP 32 | ||
| 20 | 19 | ||
| 21 | #define openspace(size) if (L->Mbuffnext+(size) > L->Mbuffsize) Openspace(size) | 20 | #define openspace(size) if (L->Mbuffnext+(size) > L->Mbuffsize) Openspace(size) |
| 22 | 21 | ||
| 23 | static void Openspace (int size) | 22 | static void Openspace (int size) { |
| 24 | { | ||
| 25 | lua_State *l = L; /* to optimize */ | 23 | lua_State *l = L; /* to optimize */ |
| 26 | int base = l->Mbuffbase-l->Mbuffer; | 24 | l->Mbuffsize = l->Mbuffnext+size; |
| 27 | l->Mbuffsize *= 2; | 25 | l->Mbuffer = luaM_growvector(l->Mbuffer, l->Mbuffnext, size, char, |
| 28 | if (l->Mbuffnext+size > l->Mbuffsize) /* still not big enough? */ | 26 | memEM, MAX_INT); |
| 29 | l->Mbuffsize = l->Mbuffnext+size; | ||
| 30 | l->Mbuffer = luaM_realloc(l->Mbuffer, l->Mbuffsize); | ||
| 31 | l->Mbuffbase = l->Mbuffer+base; | ||
| 32 | } | 27 | } |
| 33 | 28 | ||
| 34 | 29 | ||
| 35 | char *luaL_openspace (int size) | 30 | char *luaL_openspace (int size) { |
| 36 | { | ||
| 37 | openspace(size); | 31 | openspace(size); |
| 38 | return L->Mbuffer+L->Mbuffnext; | 32 | return L->Mbuffer+L->Mbuffnext; |
| 39 | } | 33 | } |
| 40 | 34 | ||
| 41 | 35 | ||
| 42 | void luaL_addchar (int c) | 36 | void luaL_addchar (int c) { |
| 43 | { | 37 | openspace(1); |
| 44 | openspace(BUFF_STEP); | ||
| 45 | L->Mbuffer[L->Mbuffnext++] = (char)c; | 38 | L->Mbuffer[L->Mbuffnext++] = (char)c; |
| 46 | } | 39 | } |
| 47 | 40 | ||
| 48 | 41 | ||
| 49 | void luaL_resetbuffer (void) | 42 | void luaL_resetbuffer (void) { |
| 50 | { | 43 | L->Mbuffnext = L->Mbuffbase; |
| 51 | L->Mbuffnext = L->Mbuffbase-L->Mbuffer; | ||
| 52 | } | 44 | } |
| 53 | 45 | ||
| 54 | 46 | ||
| 55 | void luaL_addsize (int n) | 47 | void luaL_addsize (int n) { |
| 56 | { | ||
| 57 | L->Mbuffnext += n; | 48 | L->Mbuffnext += n; |
| 58 | } | 49 | } |
| 59 | 50 | ||
| 60 | int luaL_getsize (void) | 51 | int luaL_getsize (void) { |
| 61 | { | 52 | return L->Mbuffnext-L->Mbuffbase; |
| 62 | return L->Mbuffnext-(L->Mbuffbase-L->Mbuffer); | ||
| 63 | } | 53 | } |
| 64 | 54 | ||
| 65 | int luaL_newbuffer (int size) | 55 | int luaL_newbuffer (int size) { |
| 66 | { | 56 | int old = L->Mbuffbase; |
| 67 | int old = L->Mbuffbase-L->Mbuffer; | ||
| 68 | openspace(size); | 57 | openspace(size); |
| 69 | L->Mbuffbase = L->Mbuffer+L->Mbuffnext; | 58 | L->Mbuffbase = L->Mbuffnext; |
| 70 | return old; | 59 | return old; |
| 71 | } | 60 | } |
| 72 | 61 | ||
| 73 | 62 | ||
| 74 | void luaL_oldbuffer (int old) | 63 | void luaL_oldbuffer (int old) { |
| 75 | { | 64 | L->Mbuffnext = L->Mbuffbase; |
| 76 | L->Mbuffnext = L->Mbuffbase-L->Mbuffer; | 65 | L->Mbuffbase = old; |
| 77 | L->Mbuffbase = L->Mbuffer+old; | ||
| 78 | } | 66 | } |
| 79 | 67 | ||
| 80 | 68 | ||
| 81 | char *luaL_buffer (void) | 69 | char *luaL_buffer (void) { |
| 82 | { | 70 | return L->Mbuffer+L->Mbuffbase; |
| 83 | return L->Mbuffbase; | ||
| 84 | } | 71 | } |
| 85 | 72 | ||
