aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-12-23 17:24:36 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-12-23 17:24:36 -0200
commit1bf762ba3877eb27d7c30bbbbc774f1634062800 (patch)
tree573439db73259c7ff083fb5418a8207a8cc5ed51
parent541e722360168c78a61b8b79f4cd234252ffb6cb (diff)
downloadlua-1bf762ba3877eb27d7c30bbbbc774f1634062800.tar.gz
lua-1bf762ba3877eb27d7c30bbbbc774f1634062800.tar.bz2
lua-1bf762ba3877eb27d7c30bbbbc774f1634062800.zip
Generic buffer facilities for Lua (and libraries)
-rw-r--r--lbuffer.c81
1 files changed, 81 insertions, 0 deletions
diff --git a/lbuffer.c b/lbuffer.c
new file mode 100644
index 00000000..04820736
--- /dev/null
+++ b/lbuffer.c
@@ -0,0 +1,81 @@
1/*
2** $Id: $
3** Auxiliar functions for building Lua libraries
4** See Copyright Notice in lua.h
5*/
6
7
8#include <stdio.h>
9
10#include "lauxlib.h"
11#include "lmem.h"
12#include "lstate.h"
13
14
15/*-------------------------------------------------------
16** Auxiliar buffer
17-------------------------------------------------------*/
18
19#define BUFF_STEP 32
20
21#define openspace(size) if (L->Mbuffnext+(size) > L->Mbuffsize) Openspace(size)
22
23static void Openspace (int size)
24{
25 LState *l = L; /* to optimize */
26 int base = l->Mbuffbase-l->Mbuffer;
27 l->Mbuffsize *= 2;
28 if (l->Mbuffnext+size > l->Mbuffsize) /* still not big enough? */
29 l->Mbuffsize = l->Mbuffnext+size;
30 l->Mbuffer = luaM_realloc(l->Mbuffer, l->Mbuffsize);
31 l->Mbuffbase = l->Mbuffer+base;
32}
33
34
35char *luaL_openspace (int size)
36{
37 openspace(size);
38 return L->Mbuffer+L->Mbuffnext;
39}
40
41
42void luaL_addchar (int c)
43{
44 openspace(BUFF_STEP);
45 L->Mbuffer[L->Mbuffnext++] = c;
46}
47
48
49void luaL_resetbuffer (void)
50{
51 L->Mbuffnext = L->Mbuffbase-L->Mbuffer;
52}
53
54
55void luaL_addsize (int n)
56{
57 L->Mbuffnext += n;
58}
59
60
61int luaL_newbuffer (int size)
62{
63 int old = L->Mbuffbase-L->Mbuffer;
64 openspace(size);
65 L->Mbuffbase = L->Mbuffer+L->Mbuffnext;
66 return old;
67}
68
69
70void luaL_oldbuffer (int old)
71{
72 L->Mbuffnext = L->Mbuffbase-L->Mbuffer;
73 L->Mbuffbase = L->Mbuffer+old;
74}
75
76
77char *luaL_buffer (void)
78{
79 return L->Mbuffbase;
80}
81