summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lapi.c3
-rw-r--r--lcode.c24
-rw-r--r--ldo.c26
-rw-r--r--lfunc.c56
-rw-r--r--lfunc.h3
-rw-r--r--lgc.c17
-rw-r--r--lmem.c71
-rw-r--r--lmem.h22
-rw-r--r--lobject.c5
-rw-r--r--lobject.h14
-rw-r--r--lparser.c55
-rw-r--r--lparser.h15
-rw-r--r--lstate.c23
-rw-r--r--lstring.c35
-rw-r--r--lstring.h15
-rw-r--r--ltable.c15
-rw-r--r--ltests.c4
-rw-r--r--ltm.c6
-rw-r--r--lundump.c16
-rw-r--r--lvm.c6
20 files changed, 207 insertions, 224 deletions
diff --git a/lapi.c b/lapi.c
index b1b7d0ff..661d5ba5 100644
--- a/lapi.c
+++ b/lapi.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lapi.c,v 1.112 2000/12/04 18:33:40 roberto Exp roberto $ 2** $Id: lapi.c,v 1.113 2000/12/26 18:46:09 roberto Exp roberto $
3** Lua API 3** Lua API
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -362,7 +362,6 @@ LUA_API int lua_ref (lua_State *L, int lock) {
362 else { /* no more free places */ 362 else { /* no more free places */
363 luaM_growvector(L, L->refArray, L->nref, L->sizeref, struct Ref, 363 luaM_growvector(L, L->refArray, L->nref, L->sizeref, struct Ref,
364 MAX_INT, "reference table overflow"); 364 MAX_INT, "reference table overflow");
365 L->nblocks += sizeof(struct Ref);
366 ref = L->nref++; 365 ref = L->nref++;
367 } 366 }
368 L->refArray[ref].o = *(L->top-1); 367 L->refArray[ref].o = *(L->top-1);
diff --git a/lcode.c b/lcode.c
index 685ce748..bd73dc57 100644
--- a/lcode.c
+++ b/lcode.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lcode.c,v 1.53 2000/12/04 18:33:40 roberto Exp roberto $ 2** $Id: lcode.c,v 1.54 2000/12/26 18:46:09 roberto Exp roberto $
3** Code generator for Lua 3** Code generator for Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -102,14 +102,14 @@ void luaK_kstr (LexState *ls, int c) {
102static int number_constant (FuncState *fs, lua_Number r) { 102static int number_constant (FuncState *fs, lua_Number r) {
103 /* check whether `r' has appeared within the last LOOKBACKNUMS entries */ 103 /* check whether `r' has appeared within the last LOOKBACKNUMS entries */
104 Proto *f = fs->f; 104 Proto *f = fs->f;
105 int c = f->nknum; 105 int c = fs->nknum;
106 int lim = c < LOOKBACKNUMS ? 0 : c-LOOKBACKNUMS; 106 int lim = c < LOOKBACKNUMS ? 0 : c-LOOKBACKNUMS;
107 while (--c >= lim) 107 while (--c >= lim)
108 if (f->knum[c] == r) return c; 108 if (f->knum[c] == r) return c;
109 /* not found; create a new entry */ 109 /* not found; create a new entry */
110 luaM_growvector(fs->L, f->knum, f->nknum, fs->sizeknum, lua_Number, 110 luaM_growvector(fs->L, f->knum, fs->nknum, f->sizeknum, lua_Number,
111 MAXARG_U, "constant table overflow"); 111 MAXARG_U, "constant table overflow");
112 c = f->nknum++; 112 c = fs->nknum++;
113 f->knum[c] = r; 113 f->knum[c] = r;
114 return c; 114 return c;
115} 115}
@@ -424,13 +424,13 @@ static void codelineinfo (FuncState *fs) {
424 LexState *ls = fs->ls; 424 LexState *ls = fs->ls;
425 if (ls->lastline > fs->lastline) { 425 if (ls->lastline > fs->lastline) {
426 if (ls->lastline > fs->lastline+1) { 426 if (ls->lastline > fs->lastline+1) {
427 luaM_growvector(fs->L, f->lineinfo, f->nlineinfo, fs->sizelineinfo, int, 427 luaM_growvector(fs->L, f->lineinfo, fs->nlineinfo, f->sizelineinfo, int,
428 MAX_INT, "line info overflow"); 428 MAX_INT, "line info overflow");
429 f->lineinfo[f->nlineinfo++] = -(ls->lastline - (fs->lastline+1)); 429 f->lineinfo[fs->nlineinfo++] = -(ls->lastline - (fs->lastline+1));
430 } 430 }
431 luaM_growvector(fs->L, f->lineinfo, f->nlineinfo, fs->sizelineinfo, int, 431 luaM_growvector(fs->L, f->lineinfo, fs->nlineinfo, f->sizelineinfo, int,
432 MAX_INT, "line info overflow"); 432 MAX_INT, "line info overflow");
433 f->lineinfo[f->nlineinfo++] = fs->pc; 433 f->lineinfo[fs->nlineinfo++] = fs->pc;
434 fs->lastline = ls->lastline; 434 fs->lastline = ls->lastline;
435 } 435 }
436} 436}
@@ -447,6 +447,7 @@ int luaK_code1 (FuncState *fs, OpCode o, int arg1) {
447 447
448 448
449int luaK_code2 (FuncState *fs, OpCode o, int arg1, int arg2) { 449int luaK_code2 (FuncState *fs, OpCode o, int arg1, int arg2) {
450 Proto *f;
450 Instruction i = previous_instruction(fs); 451 Instruction i = previous_instruction(fs);
451 int delta = (int)luaK_opproperties[o].push - (int)luaK_opproperties[o].pop; 452 int delta = (int)luaK_opproperties[o].push - (int)luaK_opproperties[o].pop;
452 int optm = 0; /* 1 when there is an optimization */ 453 int optm = 0; /* 1 when there is an optimization */
@@ -629,9 +630,10 @@ int luaK_code2 (FuncState *fs, OpCode o, int arg1, int arg2) {
629 break; 630 break;
630 } 631 }
631 } 632 }
633 f = fs->f;
632 luaK_deltastack(fs, delta); 634 luaK_deltastack(fs, delta);
633 if (optm) { /* optimize: put instruction in place of last one */ 635 if (optm) { /* optimize: put instruction in place of last one */
634 fs->f->code[fs->pc-1] = i; /* change previous instruction */ 636 f->code[fs->pc-1] = i; /* change previous instruction */
635 return fs->pc-1; /* do not generate new instruction */ 637 return fs->pc-1; /* do not generate new instruction */
636 } 638 }
637 /* else build new instruction */ 639 /* else build new instruction */
@@ -643,9 +645,9 @@ int luaK_code2 (FuncState *fs, OpCode o, int arg1, int arg2) {
643 } 645 }
644 codelineinfo(fs); 646 codelineinfo(fs);
645 /* put new instruction in code array */ 647 /* put new instruction in code array */
646 luaM_growvector(fs->L, fs->f->code, fs->pc, fs->sizecode, Instruction, 648 luaM_growvector(fs->L, f->code, fs->pc, f->sizecode, Instruction,
647 MAX_INT, "code size overflow"); 649 MAX_INT, "code size overflow");
648 fs->f->code[fs->pc] = i; 650 f->code[fs->pc] = i;
649 return fs->pc++; 651 return fs->pc++;
650} 652}
651 653
diff --git a/ldo.c b/ldo.c
index 140cff45..01485165 100644
--- a/ldo.c
+++ b/ldo.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldo.c,v 1.109 2000/10/30 12:38:50 roberto Exp roberto $ 2** $Id: ldo.c,v 1.110 2000/11/24 17:39:56 roberto Exp roberto $
3** Stack and Call structure of Lua 3** Stack and Call structure of Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -28,38 +28,40 @@
28 28
29 29
30/* space to handle stack overflow errors */ 30/* space to handle stack overflow errors */
31#define EXTRA_STACK (2*LUA_MINSTACK) 31#define EXTRA_STACK (2*LUA_MINSTACK)
32
33
34static void restore_stack_limit (lua_State *L) {
35 StkId limit = L->stack+(L->stacksize-EXTRA_STACK)-1;
36 if (L->top < limit)
37 L->stack_last = limit;
38}
32 39
33 40
34void luaD_init (lua_State *L, int stacksize) { 41void luaD_init (lua_State *L, int stacksize) {
35 L->stack = luaM_newvector(L, stacksize+EXTRA_STACK, TObject); 42 stacksize += EXTRA_STACK;
36 L->nblocks += stacksize*sizeof(TObject); 43 L->stack = luaM_newvector(L, stacksize, TObject);
37 L->stack_last = L->stack+(stacksize-1);
38 L->stacksize = stacksize; 44 L->stacksize = stacksize;
39 L->Cbase = L->top = L->stack; 45 L->Cbase = L->top = L->stack;
46 restore_stack_limit(L);
40} 47}
41 48
42 49
43void luaD_checkstack (lua_State *L, int n) { 50void luaD_checkstack (lua_State *L, int n) {
44 if (L->stack_last - L->top <= n) { /* stack overflow? */ 51 if (L->stack_last - L->top <= n) { /* stack overflow? */
45 if (L->stack_last-L->stack > (L->stacksize-1)) { 52 if (L->stack_last == L->stack+L->stacksize-1) {
46 /* overflow while handling overflow */ 53 /* overflow while handling overflow */
47 luaD_breakrun(L, LUA_ERRERR); /* break run without error message */ 54 luaD_breakrun(L, LUA_ERRERR); /* break run without error message */
48 } 55 }
49 else { 56 else {
50 L->stack_last += EXTRA_STACK; /* to be used by error message */ 57 L->stack_last += EXTRA_STACK; /* to be used by error message */
58 LUA_ASSERT(L->stack_last == L->stack+L->stacksize-1, "wrong stack limit");
51 lua_error(L, "stack overflow"); 59 lua_error(L, "stack overflow");
52 } 60 }
53 } 61 }
54} 62}
55 63
56 64
57static void restore_stack_limit (lua_State *L) {
58 if (L->top - L->stack < L->stacksize - 1)
59 L->stack_last = L->stack + (L->stacksize-1);
60}
61
62
63/* 65/*
64** Adjust stack. Set top to base+extra, pushing NILs if needed. 66** Adjust stack. Set top to base+extra, pushing NILs if needed.
65** (we cannot add base+extra unless we are sure it fits in the stack; 67** (we cannot add base+extra unless we are sure it fits in the stack;
diff --git a/lfunc.c b/lfunc.c
index ad1abfc4..14ef5e15 100644
--- a/lfunc.c
+++ b/lfunc.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lfunc.c,v 1.34 2000/10/30 12:20:29 roberto Exp roberto $ 2** $Id: lfunc.c,v 1.35 2000/12/04 18:33:40 roberto Exp roberto $
3** Auxiliary functions to manipulate prototypes and closures 3** Auxiliary functions to manipulate prototypes and closures
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -18,13 +18,11 @@
18 18
19 19
20Closure *luaF_newclosure (lua_State *L, int nelems) { 20Closure *luaF_newclosure (lua_State *L, int nelems) {
21 int size = sizeclosure(nelems); 21 Closure *c = (Closure *)luaM_malloc(L, sizeclosure(nelems));
22 Closure *c = (Closure *)luaM_malloc(L, size);
23 c->next = L->rootcl; 22 c->next = L->rootcl;
24 L->rootcl = c; 23 L->rootcl = c;
25 c->mark = c; 24 c->mark = c;
26 c->nupvalues = nelems; 25 c->nupvalues = nelems;
27 L->nblocks += size;
28 return c; 26 return c;
29} 27}
30 28
@@ -32,20 +30,20 @@ Closure *luaF_newclosure (lua_State *L, int nelems) {
32Proto *luaF_newproto (lua_State *L) { 30Proto *luaF_newproto (lua_State *L) {
33 Proto *f = luaM_new(L, Proto); 31 Proto *f = luaM_new(L, Proto);
34 f->knum = NULL; 32 f->knum = NULL;
35 f->nknum = 0; 33 f->sizeknum = 0;
36 f->kstr = NULL; 34 f->kstr = NULL;
37 f->nkstr = 0; 35 f->sizekstr = 0;
38 f->kproto = NULL; 36 f->kproto = NULL;
39 f->nkproto = 0; 37 f->sizekproto = 0;
40 f->code = NULL; 38 f->code = NULL;
41 f->ncode = 0; 39 f->sizecode = 0;
42 f->numparams = 0; 40 f->numparams = 0;
43 f->is_vararg = 0; 41 f->is_vararg = 0;
44 f->maxstacksize = 0; 42 f->maxstacksize = 0;
45 f->marked = 0; 43 f->marked = 0;
46 f->lineinfo = NULL; 44 f->lineinfo = NULL;
47 f->nlineinfo = 0; 45 f->sizelocvars = 0;
48 f->nlocvars = 0; 46 f->sizelineinfo = 0;
49 f->locvars = NULL; 47 f->locvars = NULL;
50 f->lineDefined = 0; 48 f->lineDefined = 0;
51 f->source = NULL; 49 f->source = NULL;
@@ -55,39 +53,19 @@ Proto *luaF_newproto (lua_State *L) {
55} 53}
56 54
57 55
58static size_t protosize (Proto *f) {
59 return sizeof(Proto)
60 + f->nknum*sizeof(lua_Number)
61 + f->nkstr*sizeof(TString *)
62 + f->nkproto*sizeof(Proto *)
63 + f->ncode*sizeof(Instruction)
64 + f->nlocvars*sizeof(struct LocVar)
65 + f->nlineinfo*sizeof(int);
66}
67
68
69void luaF_protook (lua_State *L, Proto *f, int pc) {
70 f->ncode = pc; /* signal that proto was properly created */
71 L->nblocks += protosize(f);
72}
73
74
75void luaF_freeproto (lua_State *L, Proto *f) { 56void luaF_freeproto (lua_State *L, Proto *f) {
76 if (f->ncode > 0) /* function was properly created? */ 57 luaM_freearray(L, f->code, f->sizecode, Instruction);
77 L->nblocks -= protosize(f); 58 luaM_freearray(L, f->locvars, f->sizelocvars, struct LocVar);
78 luaM_free(L, f->code); 59 luaM_freearray(L, f->kstr, f->sizekstr, TString *);
79 luaM_free(L, f->locvars); 60 luaM_freearray(L, f->knum, f->sizeknum, lua_Number);
80 luaM_free(L, f->kstr); 61 luaM_freearray(L, f->kproto, f->sizekproto, Proto *);
81 luaM_free(L, f->knum); 62 luaM_freearray(L, f->lineinfo, f->sizelineinfo, int);
82 luaM_free(L, f->kproto); 63 luaM_freelem(L, f, Proto);
83 luaM_free(L, f->lineinfo);
84 luaM_free(L, f);
85} 64}
86 65
87 66
88void luaF_freeclosure (lua_State *L, Closure *c) { 67void luaF_freeclosure (lua_State *L, Closure *c) {
89 L->nblocks -= sizeclosure(c->nupvalues); 68 luaM_free(L, c, sizeclosure(c->nupvalues));
90 luaM_free(L, c);
91} 69}
92 70
93 71
@@ -97,7 +75,7 @@ void luaF_freeclosure (lua_State *L, Closure *c) {
97*/ 75*/
98const char *luaF_getlocalname (const Proto *f, int local_number, int pc) { 76const char *luaF_getlocalname (const Proto *f, int local_number, int pc) {
99 int i; 77 int i;
100 for (i = 0; i<f->nlocvars && f->locvars[i].startpc <= pc; i++) { 78 for (i = 0; i<f->sizelocvars && f->locvars[i].startpc <= pc; i++) {
101 if (pc < f->locvars[i].endpc) { /* is variable active? */ 79 if (pc < f->locvars[i].endpc) { /* is variable active? */
102 local_number--; 80 local_number--;
103 if (local_number == 0) 81 if (local_number == 0)
diff --git a/lfunc.h b/lfunc.h
index 3a9bc990..b78578a6 100644
--- a/lfunc.h
+++ b/lfunc.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lfunc.h,v 1.12 2000/06/26 19:28:31 roberto Exp roberto $ 2** $Id: lfunc.h,v 1.13 2000/09/29 12:42:13 roberto Exp roberto $
3** Auxiliary functions to manipulate prototypes and closures 3** Auxiliary functions to manipulate prototypes and closures
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -13,7 +13,6 @@
13 13
14 14
15Proto *luaF_newproto (lua_State *L); 15Proto *luaF_newproto (lua_State *L);
16void luaF_protook (lua_State *L, Proto *f, int pc);
17Closure *luaF_newclosure (lua_State *L, int nelems); 16Closure *luaF_newclosure (lua_State *L, int nelems);
18void luaF_freeproto (lua_State *L, Proto *f); 17void luaF_freeproto (lua_State *L, Proto *f);
19void luaF_freeclosure (lua_State *L, Closure *c); 18void luaF_freeclosure (lua_State *L, Closure *c);
diff --git a/lgc.c b/lgc.c
index 7b35ee03..8c63570a 100644
--- a/lgc.c
+++ b/lgc.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lgc.c,v 1.73 2000/11/24 17:39:56 roberto Exp roberto $ 2** $Id: lgc.c,v 1.74 2000/12/26 18:46:09 roberto Exp roberto $
3** Garbage Collector 3** Garbage Collector
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -37,11 +37,11 @@ static void protomark (Proto *f) {
37 int i; 37 int i;
38 f->marked = 1; 38 f->marked = 1;
39 strmark(f->source); 39 strmark(f->source);
40 for (i=0; i<f->nkstr; i++) 40 for (i=0; i<f->sizekstr; i++)
41 strmark(f->kstr[i]); 41 strmark(f->kstr[i]);
42 for (i=0; i<f->nkproto; i++) 42 for (i=0; i<f->sizekproto; i++)
43 protomark(f->kproto[i]); 43 protomark(f->kproto[i]);
44 for (i=0; i<f->nlocvars; i++) /* mark local-variable names */ 44 for (i=0; i<f->sizelocvars; i++) /* mark local-variable names */
45 strmark(f->locvars[i].varname); 45 strmark(f->locvars[i].varname);
46 } 46 }
47} 47}
@@ -248,8 +248,7 @@ static void collectstrings (lua_State *L, int all) {
248 else { /* collect */ 248 else { /* collect */
249 *p = next->nexthash; 249 *p = next->nexthash;
250 L->strt.nuse--; 250 L->strt.nuse--;
251 L->nblocks -= sizestring(next->len); 251 luaM_free(L, next, sizestring(next->len));
252 luaM_free(L, next);
253 } 252 }
254 } 253 }
255 } 254 }
@@ -273,7 +272,6 @@ static void collectudata (lua_State *L, int all) {
273 *p = next->nexthash; 272 *p = next->nexthash;
274 next->nexthash = L->TMtable[tag].collected; /* chain udata */ 273 next->nexthash = L->TMtable[tag].collected; /* chain udata */
275 L->TMtable[tag].collected = next; 274 L->TMtable[tag].collected = next;
276 L->nblocks -= sizestring(next->len);
277 L->udt.nuse--; 275 L->udt.nuse--;
278 } 276 }
279 } 277 }
@@ -286,9 +284,8 @@ static void collectudata (lua_State *L, int all) {
286static void checkMbuffer (lua_State *L) { 284static void checkMbuffer (lua_State *L) {
287 if (L->Mbuffsize > MINBUFFER*2) { /* is buffer too big? */ 285 if (L->Mbuffsize > MINBUFFER*2) { /* is buffer too big? */
288 size_t newsize = L->Mbuffsize/2; /* still larger than MINBUFFER */ 286 size_t newsize = L->Mbuffsize/2; /* still larger than MINBUFFER */
289 L->nblocks -= (L->Mbuffsize - newsize)*sizeof(char); 287 luaM_reallocvector(L, L->Mbuffer, L->Mbuffsize, newsize, char);
290 L->Mbuffsize = newsize; 288 L->Mbuffsize = newsize;
291 luaM_reallocvector(L, L->Mbuffer, newsize, char);
292 } 289 }
293} 290}
294 291
@@ -320,7 +317,7 @@ static void callgcTMudata (lua_State *L) {
320 L->TMtable[tag].collected = udata->nexthash; /* remove it from list */ 317 L->TMtable[tag].collected = udata->nexthash; /* remove it from list */
321 tsvalue(&o) = udata; 318 tsvalue(&o) = udata;
322 callgcTM(L, &o); 319 callgcTM(L, &o);
323 luaM_free(L, udata); 320 luaM_free(L, udata, sizeudata(udata->len));
324 } 321 }
325 } 322 }
326} 323}
diff --git a/lmem.c b/lmem.c
index 830f6f1d..3d34e313 100644
--- a/lmem.c
+++ b/lmem.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lmem.c,v 1.40 2000/11/24 17:39:56 roberto Exp roberto $ 2** $Id: lmem.c,v 1.41 2000/12/26 18:46:09 roberto Exp roberto $
3** Interface to Memory Manager 3** Interface to Memory Manager
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -29,15 +29,14 @@
29#include <limits.h> 29#include <limits.h>
30#include <string.h> 30#include <string.h>
31 31
32#define realloc(b, s) debug_realloc(b, s) 32#define basicrealloc(b, os, s) debug_realloc(b, os, s)
33#define malloc(b) debug_realloc(NULL, b) 33#define basicfree(b, s) debug_realloc(b, s, 0)
34#define free(b) debug_realloc(b, 0)
35 34
36 35
37/* ensures maximum alignment for HEADER */ 36/* ensures maximum alignment for HEADER */
38#define HEADER (sizeof(union L_Umaxalign)) 37#define HEADER (sizeof(union L_Umaxalign))
39 38
40#define MARKSIZE 16 39#define MARKSIZE 32
41#define MARK 0x55 /* 01010101 (a nice pattern) */ 40#define MARK 0x55 /* 01010101 (a nice pattern) */
42 41
43 42
@@ -55,8 +54,6 @@ static void *checkblock (void *block) {
55 int i; 54 int i;
56 for (i=0;i<MARKSIZE;i++) 55 for (i=0;i<MARKSIZE;i++)
57 assert(*(((char *)b)+HEADER+size+i) == MARK+i); /* corrupted block? */ 56 assert(*(((char *)b)+HEADER+size+i) == MARK+i); /* corrupted block? */
58 memdebug_numblocks--;
59 memdebug_total -= size;
60 return b; 57 return b;
61} 58}
62 59
@@ -66,12 +63,15 @@ static void freeblock (void *block) {
66 size_t size = *blocksize(block); 63 size_t size = *blocksize(block);
67 block = checkblock(block); 64 block = checkblock(block);
68 memset(block, -1, size+HEADER+MARKSIZE); /* erase block */ 65 memset(block, -1, size+HEADER+MARKSIZE); /* erase block */
69 (free)(block); /* free original block */ 66 free(block); /* free original block */
67 memdebug_numblocks--;
68 memdebug_total -= size;
70 } 69 }
71} 70}
72 71
73 72
74static void *debug_realloc (void *block, size_t size) { 73static void *debug_realloc (void *block, size_t oldsize, size_t size) {
74 assert((oldsize == 0) ? block == NULL : oldsize == *blocksize(block));
75 if (size == 0) { 75 if (size == 0) {
76 freeblock(block); 76 freeblock(block);
77 return NULL; 77 return NULL;
@@ -79,19 +79,22 @@ static void *debug_realloc (void *block, size_t size) {
79 else if (memdebug_total+size > memdebug_memlimit) 79 else if (memdebug_total+size > memdebug_memlimit)
80 return NULL; /* to test memory allocation errors */ 80 return NULL; /* to test memory allocation errors */
81 else { 81 else {
82 size_t realsize = HEADER+size+MARKSIZE; 82 char *newblock;
83 char *newblock = (char *)(malloc)(realsize); /* alloc a new block */
84 int i; 83 int i;
84 size_t realsize = HEADER+size+MARKSIZE;
85 if (realsize < size) return NULL; /* overflow! */ 85 if (realsize < size) return NULL; /* overflow! */
86 newblock = (char *)malloc(realsize); /* alloc a new block */
86 if (newblock == NULL) return NULL; 87 if (newblock == NULL) return NULL;
88 if (oldsize > size) oldsize = size;
87 if (block) { 89 if (block) {
88 size_t oldsize = *blocksize(block);
89 if (oldsize > size) oldsize = size;
90 memcpy(newblock+HEADER, block, oldsize); 90 memcpy(newblock+HEADER, block, oldsize);
91 freeblock(block); /* erase (and check) old copy */ 91 freeblock(block); /* erase (and check) old copy */
92 } 92 }
93 /* initialize new part of the block with something `weird' */
94 memset(newblock+HEADER+oldsize, -MARK, size-oldsize);
93 memdebug_total += size; 95 memdebug_total += size;
94 if (memdebug_total > memdebug_maxmem) memdebug_maxmem = memdebug_total; 96 if (memdebug_total > memdebug_maxmem)
97 memdebug_maxmem = memdebug_total;
95 memdebug_numblocks++; 98 memdebug_numblocks++;
96 *(size_t *)newblock = size; 99 *(size_t *)newblock = size;
97 for (i=0;i<MARKSIZE;i++) 100 for (i=0;i<MARKSIZE;i++)
@@ -102,20 +105,26 @@ static void *debug_realloc (void *block, size_t size) {
102 105
103 106
104/* }====================================================================== */ 107/* }====================================================================== */
105#endif
106
107 108
109#else
110/* no debug */
108 111
109/* 112/*
110** Real ISO (ANSI) systems do not need these tests; 113** Real ISO (ANSI) systems do not need these tests;
111** but some systems (Sun OS) are not that ISO... 114** but some systems (Sun OS) are not that ISO...
112*/ 115*/
113#ifdef OLD_ANSI 116#ifdef OLD_ANSI
114#define realloc(b,s) ((b) == NULL ? malloc(s) : (realloc)(b, s)) 117#define basicrealloc(b,os,s) ((b) == NULL ? malloc(s) : realloc(b, s))
115#define free(b) if (b) (free)(b) 118#define basicfree(b,s) if (b) free(b)
119#else
120#define basicrealloc(b,os,s) realloc(b,s)
121#define basicfree(b,s) free(b)
122
116#endif 123#endif
117 124
118 125
126#endif
127
119void *luaM_growaux (lua_State *L, void *block, int *size, int size_elems, 128void *luaM_growaux (lua_State *L, void *block, int *size, int size_elems,
120 int limit, const char *errormsg) { 129 int limit, const char *errormsg) {
121 void *newblock; 130 void *newblock;
@@ -127,7 +136,8 @@ void *luaM_growaux (lua_State *L, void *block, int *size, int size_elems,
127 newsize = limit; /* still have at least MINPOWER2 free places */ 136 newsize = limit; /* still have at least MINPOWER2 free places */
128 else lua_error(L, errormsg); 137 else lua_error(L, errormsg);
129 } 138 }
130 newblock = luaM_realloc(L, block, (luint32)newsize*(luint32)size_elems); 139 newblock = luaM_realloc(L, block, (luint32)(*size)*(luint32)size_elems,
140 (luint32)newsize*(luint32)size_elems);
131 *size = newsize; /* update only when everything else is OK */ 141 *size = newsize; /* update only when everything else is OK */
132 return newblock; 142 return newblock;
133} 143}
@@ -136,20 +146,25 @@ void *luaM_growaux (lua_State *L, void *block, int *size, int size_elems,
136/* 146/*
137** generic allocation routine. 147** generic allocation routine.
138*/ 148*/
139void *luaM_realloc (lua_State *L, void *block, luint32 size) { 149void *luaM_realloc (lua_State *L, void *block, luint32 oldsize, luint32 size) {
140 if (size == 0) { 150 if (size == 0) {
141 free(block); /* block may be NULL; that is OK for free */ 151 basicfree(block, oldsize); /* block may be NULL; that is OK for free */
142 return NULL; 152 block = NULL;
143 } 153 }
144 else if (size >= MAX_SIZET) 154 else if (size >= MAX_SIZET)
145 lua_error(L, "memory allocation error: block too big"); 155 lua_error(L, "memory allocation error: block too big");
146 block = realloc(block, size); 156 else {
147 if (block == NULL) { 157 block = basicrealloc(block, oldsize, size);
148 if (L) 158 if (block == NULL) {
149 luaD_breakrun(L, LUA_ERRMEM); /* break run without error message */ 159 if (L)
150 else return NULL; /* error before creating state! */ 160 luaD_breakrun(L, LUA_ERRMEM); /* break run without error message */
161 else return NULL; /* error before creating state! */
162 }
163 }
164 if (L) {
165 L->nblocks -= oldsize;
166 L->nblocks += size;
151 } 167 }
152 return block; 168 return block;
153} 169}
154 170
155
diff --git a/lmem.h b/lmem.h
index c2ee8213..d33473c7 100644
--- a/lmem.h
+++ b/lmem.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lmem.h,v 1.17 2000/11/24 17:39:56 roberto Exp roberto $ 2** $Id: lmem.h,v 1.18 2000/12/26 18:46:09 roberto Exp roberto $
3** Interface to Memory Manager 3** Interface to Memory Manager
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -13,21 +13,29 @@
13#include "llimits.h" 13#include "llimits.h"
14#include "lua.h" 14#include "lua.h"
15 15
16void *luaM_realloc (lua_State *L, void *oldblock, luint32 size); 16void *luaM_realloc (lua_State *L, void *oldblock, luint32 oldsize,
17 luint32 size);
18
17void *luaM_growaux (lua_State *L, void *block, int *size, int size_elem, 19void *luaM_growaux (lua_State *L, void *block, int *size, int size_elem,
18 int limit, const char *errormsg); 20 int limit, const char *errormsg);
19 21
20#define luaM_free(L, b) luaM_realloc(L, (b), 0) 22#define luaM_free(L, b, s) luaM_realloc(L, (b), (s), 0)
21#define luaM_malloc(L, t) luaM_realloc(L, NULL, (t)) 23#define luaM_freelem(L, b, t) luaM_realloc(L, (b), sizeof(t), 0)
24#define luaM_freearray(L, b, n, t) luaM_realloc(L, (b), \
25 ((luint32)(n)*(luint32)sizeof(t)), 0)
26
27#define luaM_malloc(L, t) luaM_realloc(L, NULL, 0, (t))
22#define luaM_new(L, t) ((t *)luaM_malloc(L, sizeof(t))) 28#define luaM_new(L, t) ((t *)luaM_malloc(L, sizeof(t)))
23#define luaM_newvector(L, n,t) ((t *)luaM_malloc(L, (n)*(luint32)sizeof(t))) 29#define luaM_newvector(L, n,t) ((t *)luaM_malloc(L, \
30 (luint32)(n)*(luint32)sizeof(t)))
24 31
25#define luaM_growvector(L,v,nelems,size,t,limit,e) \ 32#define luaM_growvector(L,v,nelems,size,t,limit,e) \
26 if (((nelems)+1) > (size)) \ 33 if (((nelems)+1) > (size)) \
27 ((v)=(t *)luaM_growaux(L,v,&(size),sizeof(t),limit,e)) 34 ((v)=(t *)luaM_growaux(L,v,&(size),sizeof(t),limit,e))
28 35
29#define luaM_reallocvector(L, v,n,t) \ 36#define luaM_reallocvector(L, v,oldn,n,t) \
30 ((v)=(t *)luaM_realloc(L, v,(n)*(luint32)sizeof(t))) 37 ((v)=(t *)luaM_realloc(L, v,(luint32)(oldn)*(luint32)sizeof(t), \
38 (luint32)(n)*(luint32)sizeof(t)))
31 39
32 40
33#ifdef LUA_DEBUG 41#ifdef LUA_DEBUG
diff --git a/lobject.c b/lobject.c
index 5f9876c0..a2fd786b 100644
--- a/lobject.c
+++ b/lobject.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lobject.c,v 1.56 2000/11/24 17:39:56 roberto Exp roberto $ 2** $Id: lobject.c,v 1.57 2000/12/04 18:33:40 roberto Exp roberto $
3** Some generic functions over Lua objects 3** Some generic functions over Lua objects
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -57,8 +57,7 @@ int luaO_equalObj (const TObject *t1, const TObject *t2) {
57 57
58char *luaO_openspace (lua_State *L, size_t n) { 58char *luaO_openspace (lua_State *L, size_t n) {
59 if (n > L->Mbuffsize) { 59 if (n > L->Mbuffsize) {
60 luaM_reallocvector(L, L->Mbuffer, n, char); 60 luaM_reallocvector(L, L->Mbuffer, L->Mbuffsize, n, char);
61 L->nblocks += (n - L->Mbuffsize)*sizeof(char);
62 L->Mbuffsize = n; 61 L->Mbuffsize = n;
63 } 62 }
64 return L->Mbuffer; 63 return L->Mbuffer;
diff --git a/lobject.h b/lobject.h
index 916cc4d1..903833f0 100644
--- a/lobject.h
+++ b/lobject.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lobject.h,v 1.83 2000/11/24 17:39:56 roberto Exp roberto $ 2** $Id: lobject.h,v 1.84 2000/12/04 18:33:40 roberto Exp roberto $
3** Type definitions for Lua objects 3** Type definitions for Lua objects
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -102,13 +102,13 @@ typedef struct TString {
102*/ 102*/
103typedef struct Proto { 103typedef struct Proto {
104 lua_Number *knum; /* numbers used by the function */ 104 lua_Number *knum; /* numbers used by the function */
105 int nknum; /* size of `knum' */ 105 int sizeknum; /* size of `knum' */
106 struct TString **kstr; /* strings used by the function */ 106 struct TString **kstr; /* strings used by the function */
107 int nkstr; /* size of `kstr' */ 107 int sizekstr; /* size of `kstr' */
108 struct Proto **kproto; /* functions defined inside the function */ 108 struct Proto **kproto; /* functions defined inside the function */
109 int nkproto; /* size of `kproto' */ 109 int sizekproto; /* size of `kproto' */
110 Instruction *code; 110 Instruction *code;
111 int ncode; /* size of `code'; when 0 means an incomplete `Proto' */ 111 int sizecode;
112 short numparams; 112 short numparams;
113 short is_vararg; 113 short is_vararg;
114 short maxstacksize; 114 short maxstacksize;
@@ -116,9 +116,9 @@ typedef struct Proto {
116 struct Proto *next; 116 struct Proto *next;
117 /* debug information */ 117 /* debug information */
118 int *lineinfo; /* map from opcodes to source lines */ 118 int *lineinfo; /* map from opcodes to source lines */
119 int nlineinfo; /* size of `lineinfo' */ 119 int sizelineinfo; /* size of `lineinfo' */
120 int nlocvars;
121 struct LocVar *locvars; /* information about local variables */ 120 struct LocVar *locvars; /* information about local variables */
121 int sizelocvars;
122 int lineDefined; 122 int lineDefined;
123 TString *source; 123 TString *source;
124} Proto; 124} Proto;
diff --git a/lparser.c b/lparser.c
index f4e4c551..fe4a1586 100644
--- a/lparser.c
+++ b/lparser.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lparser.c,v 1.119 2000/12/04 18:33:40 roberto Exp roberto $ 2** $Id: lparser.c,v 1.120 2000/12/26 18:46:09 roberto Exp roberto $
3** LL(1) Parser and code generator for Lua 3** LL(1) Parser and code generator for Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -120,10 +120,10 @@ static void check_match (LexState *ls, int what, int who, int where) {
120static int string_constant (FuncState *fs, TString *s) { 120static int string_constant (FuncState *fs, TString *s) {
121 Proto *f = fs->f; 121 Proto *f = fs->f;
122 int c = s->u.s.constindex; 122 int c = s->u.s.constindex;
123 if (c >= f->nkstr || f->kstr[c] != s) { 123 if (c >= fs->nkstr || f->kstr[c] != s) {
124 luaM_growvector(fs->L, f->kstr, f->nkstr, fs->sizekstr, TString *, 124 luaM_growvector(fs->L, f->kstr, fs->nkstr, f->sizekstr, TString *,
125 MAXARG_U, "constant table overflow"); 125 MAXARG_U, "constant table overflow");
126 c = f->nkstr++; 126 c = fs->nkstr++;
127 f->kstr[c] = s; 127 f->kstr[c] = s;
128 s->u.s.constindex = c; /* hint for next time */ 128 s->u.s.constindex = c; /* hint for next time */
129 } 129 }
@@ -151,11 +151,12 @@ static int checkname (LexState *ls) {
151 151
152 152
153static int luaI_registerlocalvar (LexState *ls, TString *varname) { 153static int luaI_registerlocalvar (LexState *ls, TString *varname) {
154 Proto *f = ls->fs->f; 154 FuncState *fs = ls->fs;
155 luaM_growvector(ls->L, f->locvars, f->nlocvars, ls->fs->sizelocvars, 155 Proto *f = fs->f;
156 luaM_growvector(ls->L, f->locvars, fs->nlocvars, f->sizelocvars,
156 LocVar, MAX_INT, ""); 157 LocVar, MAX_INT, "");
157 f->locvars[f->nlocvars].varname = varname; 158 f->locvars[fs->nlocvars].varname = varname;
158 return f->nlocvars++; 159 return fs->nlocvars++;
159} 160}
160 161
161 162
@@ -295,10 +296,10 @@ static void pushclosure (LexState *ls, FuncState *func) {
295 int i; 296 int i;
296 for (i=0; i<func->nupvalues; i++) 297 for (i=0; i<func->nupvalues; i++)
297 luaK_tostack(ls, &func->upvalues[i], 1); 298 luaK_tostack(ls, &func->upvalues[i], 1);
298 luaM_growvector(ls->L, f->kproto, f->nkproto, fs->sizekproto, Proto *, 299 luaM_growvector(ls->L, f->kproto, fs->nkproto, f->sizekproto, Proto *,
299 MAXARG_A, "constant table overflow"); 300 MAXARG_A, "constant table overflow");
300 f->kproto[f->nkproto++] = func->f; 301 f->kproto[fs->nkproto++] = func->f;
301 luaK_code2(fs, OP_CLOSURE, f->nkproto-1, func->nupvalues); 302 luaK_code2(fs, OP_CLOSURE, fs->nkproto-1, func->nupvalues);
302} 303}
303 304
304 305
@@ -313,12 +314,11 @@ static void open_func (LexState *ls, FuncState *fs) {
313 fs->lasttarget = 0; 314 fs->lasttarget = 0;
314 fs->jlt = NO_JUMP; 315 fs->jlt = NO_JUMP;
315 fs->stacklevel = 0; 316 fs->stacklevel = 0;
316 fs->sizekstr = 0; 317 fs->nkstr = 0;
317 fs->sizekproto = 0; 318 fs->nkproto = 0;
318 fs->sizeknum = 0; 319 fs->nknum = 0;
319 fs->sizelineinfo = 0; 320 fs->nlineinfo = 0;
320 fs->sizecode = 0; 321 fs->nlocvars = 0;
321 fs->sizelocvars = 0;
322 fs->nactloc = 0; 322 fs->nactloc = 0;
323 fs->nupvalues = 0; 323 fs->nupvalues = 0;
324 fs->lastline = 0; 324 fs->lastline = 0;
@@ -337,15 +337,20 @@ static void close_func (LexState *ls) {
337 Proto *f = fs->f; 337 Proto *f = fs->f;
338 luaK_code0(fs, OP_END); 338 luaK_code0(fs, OP_END);
339 luaK_getlabel(fs); /* close eventual list of pending jumps */ 339 luaK_getlabel(fs); /* close eventual list of pending jumps */
340 luaM_reallocvector(L, f->code, fs->pc, Instruction);
341 luaM_reallocvector(L, f->kstr, f->nkstr, TString *);
342 luaM_reallocvector(L, f->knum, f->nknum, lua_Number);
343 luaM_reallocvector(L, f->kproto, f->nkproto, Proto *);
344 removelocalvars(ls, fs->nactloc); 340 removelocalvars(ls, fs->nactloc);
345 luaM_reallocvector(L, f->locvars, f->nlocvars, LocVar); 341 luaM_reallocvector(L, f->code, f->sizecode, fs->pc, Instruction);
346 luaM_reallocvector(L, f->lineinfo, f->nlineinfo+1, int); 342 f->sizecode = fs->pc;
347 f->lineinfo[f->nlineinfo++] = MAX_INT; /* end flag */ 343 luaM_reallocvector(L, f->kstr, f->sizekstr, fs->nkstr, TString *);
348 luaF_protook(L, f, fs->pc); /* proto is ok now */ 344 f->sizekstr = fs->nkstr;
345 luaM_reallocvector(L, f->knum, f->sizeknum, fs->nknum, lua_Number);
346 f->sizeknum = fs->nknum;
347 luaM_reallocvector(L, f->kproto, f->sizekproto, fs->nkproto, Proto *);
348 f->sizekproto = fs->nkproto;
349 luaM_reallocvector(L, f->locvars, f->sizelocvars, fs->nlocvars, LocVar);
350 f->sizelocvars = fs->nlocvars;
351 luaM_reallocvector(L, f->lineinfo, f->sizelineinfo, fs->nlineinfo+1, int);
352 f->lineinfo[fs->nlineinfo++] = MAX_INT; /* end flag */
353 f->sizelineinfo = fs->nlineinfo;
349 ls->fs = fs->prev; 354 ls->fs = fs->prev;
350 LUA_ASSERT(fs->bl == NULL, "wrong list end"); 355 LUA_ASSERT(fs->bl == NULL, "wrong list end");
351} 356}
diff --git a/lparser.h b/lparser.h
index 9c34a861..933934c9 100644
--- a/lparser.h
+++ b/lparser.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lparser.h,v 1.27 2000/11/30 18:50:47 roberto Exp roberto $ 2** $Id: lparser.h,v 1.28 2000/12/26 18:46:09 roberto Exp roberto $
3** LL(1) Parser and code generator for Lua 3** LL(1) Parser and code generator for Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -41,16 +41,15 @@ typedef struct FuncState {
41 struct FuncState *prev; /* enclosing function */ 41 struct FuncState *prev; /* enclosing function */
42 struct LexState *ls; /* lexical state */ 42 struct LexState *ls; /* lexical state */
43 struct lua_State *L; /* copy of the Lua state */ 43 struct lua_State *L; /* copy of the Lua state */
44 int pc; /* next position to code */ 44 int pc; /* next position to code (equivalent to `ncode') */
45 int lasttarget; /* `pc' of last `jump target' */ 45 int lasttarget; /* `pc' of last `jump target' */
46 int jlt; /* list of jumps to `lasttarget' */ 46 int jlt; /* list of jumps to `lasttarget' */
47 int stacklevel; /* number of values on activation register */ 47 int stacklevel; /* number of values on activation register */
48 int sizekstr; /* size of array `kstr' */ 48 int nkstr; /* number of elements in `kstr' */
49 int sizekproto; /* size of array `kproto' */ 49 int nkproto; /* number of elements in `kproto' */
50 int sizeknum; /* size of array `knum' */ 50 int nknum; /* number of elements in `knum' */
51 int sizelineinfo; /* size of array `lineinfo' */ 51 int nlineinfo; /* number of elements in `lineinfo' */
52 int sizecode; /* size of array `code' */ 52 int nlocvars; /* number of elements in `locvars' */
53 int sizelocvars; /* size of array `locvars' */
54 int nactloc; /* number of active local variables */ 53 int nactloc; /* number of active local variables */
55 int nupvalues; /* number of upvalues */ 54 int nupvalues; /* number of upvalues */
56 int lastline; /* line where last `lineinfo' was generated */ 55 int lastline; /* line where last `lineinfo' was generated */
diff --git a/lstate.c b/lstate.c
index 90ee5cb3..7ed79b50 100644
--- a/lstate.c
+++ b/lstate.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstate.c,v 1.48 2000/10/30 16:29:59 roberto Exp roberto $ 2** $Id: lstate.c,v 1.49 2000/12/26 18:46:09 roberto Exp roberto $
3** Global State 3** Global State
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -58,8 +58,8 @@ static void f_luaopen (lua_State *L, void *ud) {
58#ifdef LUA_DEBUG 58#ifdef LUA_DEBUG
59 luaB_opentests(L); 59 luaB_opentests(L);
60 if (lua_state == NULL) lua_state = L; /* keep first state to be opened */ 60 if (lua_state == NULL) lua_state = L; /* keep first state to be opened */
61#endif
62 LUA_ASSERT(lua_gettop(L) == 0, "wrong API stack"); 61 LUA_ASSERT(lua_gettop(L) == 0, "wrong API stack");
62#endif
63} 63}
64 64
65 65
@@ -67,10 +67,10 @@ LUA_API lua_State *lua_open (int stacksize) {
67 lua_State *L = luaM_new(NULL, lua_State); 67 lua_State *L = luaM_new(NULL, lua_State);
68 if (L == NULL) return NULL; /* memory allocation error */ 68 if (L == NULL) return NULL; /* memory allocation error */
69 L->stack = NULL; 69 L->stack = NULL;
70 L->stacksize = 0;
70 L->strt.size = L->udt.size = 0; 71 L->strt.size = L->udt.size = 0;
71 L->strt.nuse = L->udt.nuse = 0; 72 L->strt.nuse = L->udt.nuse = 0;
72 L->strt.hash = NULL; 73 L->strt.hash = L->udt.hash = NULL;
73 L->udt.hash = NULL;
74 L->Mbuffer = NULL; 74 L->Mbuffer = NULL;
75 L->Mbuffsize = 0; 75 L->Mbuffsize = 0;
76 L->rootproto = NULL; 76 L->rootproto = NULL;
@@ -106,17 +106,12 @@ LUA_API void lua_close (lua_State *L) {
106 LUA_ASSERT(L->rootcl == NULL, "list should be empty"); 106 LUA_ASSERT(L->rootcl == NULL, "list should be empty");
107 LUA_ASSERT(L->roottable == NULL, "list should be empty"); 107 LUA_ASSERT(L->roottable == NULL, "list should be empty");
108 luaS_freeall(L); 108 luaS_freeall(L);
109 if (L->stack) 109 luaM_freearray(L, L->stack, L->stacksize, TObject);
110 L->nblocks -= (L->stack_last - L->stack + 1)*sizeof(TObject); 110 luaM_freearray(L, L->TMtable, L->sizeTM, struct TM);
111 luaM_free(L, L->stack); 111 luaM_freearray(L, L->refArray, L->sizeref, struct Ref);
112 L->nblocks -= L->ntag*sizeof(struct TM); 112 luaM_freearray(L, L->Mbuffer, L->Mbuffsize, char);
113 luaM_free(L, L->TMtable);
114 L->nblocks -= (L->nref)*sizeof(struct Ref);
115 luaM_free(L, L->refArray);
116 L->nblocks -= (L->Mbuffsize)*sizeof(char);
117 luaM_free(L, L->Mbuffer);
118 LUA_ASSERT(L->nblocks == sizeof(lua_State), "wrong count for nblocks"); 113 LUA_ASSERT(L->nblocks == sizeof(lua_State), "wrong count for nblocks");
119 luaM_free(L, L); 114 luaM_freelem(L, L, lua_State);
120 LUA_ASSERT(L != lua_state || memdebug_numblocks == 0, "memory leak!"); 115 LUA_ASSERT(L != lua_state || memdebug_numblocks == 0, "memory leak!");
121 LUA_ASSERT(L != lua_state || memdebug_total == 0,"memory leak!"); 116 LUA_ASSERT(L != lua_state || memdebug_total == 0,"memory leak!");
122} 117}
diff --git a/lstring.c b/lstring.c
index 05e5bbaa..c8cd17b1 100644
--- a/lstring.c
+++ b/lstring.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstring.c,v 1.46 2000/11/24 17:39:56 roberto Exp roberto $ 2** $Id: lstring.c,v 1.47 2000/12/22 16:57:46 roberto Exp roberto $
3** String table (keeps all strings handled by Lua) 3** String table (keeps all strings handled by Lua)
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -15,32 +15,18 @@
15#include "lstring.h" 15#include "lstring.h"
16 16
17 17
18/*
19** type equivalent to TString, but with maximum alignment requirements
20*/
21union L_UTString {
22 TString ts;
23 union L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */
24};
25
26
27 18
28void luaS_init (lua_State *L) { 19void luaS_init (lua_State *L) {
29 L->strt.hash = luaM_newvector(L, 1, TString *); 20 luaS_resize(L, &L->strt, MINPOWER2);
30 L->udt.hash = luaM_newvector(L, 1, TString *); 21 luaS_resize(L, &L->udt, MINPOWER2);
31 L->nblocks += 2*sizeof(TString *);
32 L->strt.size = L->udt.size = 1;
33 L->strt.nuse = L->udt.nuse = 0;
34 L->strt.hash[0] = L->udt.hash[0] = NULL;
35} 22}
36 23
37 24
38void luaS_freeall (lua_State *L) { 25void luaS_freeall (lua_State *L) {
39 LUA_ASSERT(L->strt.nuse==0, "non-empty string table"); 26 LUA_ASSERT(L->strt.nuse==0, "non-empty string table");
40 L->nblocks -= (L->strt.size + L->udt.size)*sizeof(TString *); 27 luaM_freearray(L, L->strt.hash, L->strt.size, TString *);
41 luaM_free(L, L->strt.hash);
42 LUA_ASSERT(L->udt.nuse==0, "non-empty udata table"); 28 LUA_ASSERT(L->udt.nuse==0, "non-empty udata table");
43 luaM_free(L, L->udt.hash); 29 luaM_freearray(L, L->udt.hash, L->udt.size, TString *);
44} 30}
45 31
46 32
@@ -71,9 +57,7 @@ void luaS_resize (lua_State *L, stringtable *tb, int newsize) {
71 p = next; 57 p = next;
72 } 58 }
73 } 59 }
74 luaM_free(L, tb->hash); 60 luaM_freearray(L, tb->hash, tb->size, TString *);
75 L->nblocks -= tb->size*sizeof(TString *);
76 L->nblocks += newsize*sizeof(TString *);
77 tb->size = newsize; 61 tb->size = newsize;
78 tb->hash = newhash; 62 tb->hash = newhash;
79} 63}
@@ -106,23 +90,20 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
106 ts->u.s.constindex = 0; 90 ts->u.s.constindex = 0;
107 memcpy(ts->str, str, l); 91 memcpy(ts->str, str, l);
108 ts->str[l] = 0; /* ending 0 */ 92 ts->str[l] = 0; /* ending 0 */
109 L->nblocks += sizestring(l);
110 newentry(L, &L->strt, ts, h1); /* insert it on table */ 93 newentry(L, &L->strt, ts, h1); /* insert it on table */
111 return ts; 94 return ts;
112} 95}
113 96
114 97
115TString *luaS_newudata (lua_State *L, size_t s, void *udata) { 98TString *luaS_newudata (lua_State *L, size_t s, void *udata) {
116 union L_UTString *uts = (union L_UTString *)luaM_malloc(L, 99 union L_UTString *uts = (union L_UTString *)luaM_malloc(L, sizeudata(s));
117 (luint32)sizeof(union L_UTString)+s);
118 TString *ts = &uts->ts; 100 TString *ts = &uts->ts;
119 ts->marked = 0; 101 ts->marked = 0;
120 ts->nexthash = NULL; 102 ts->nexthash = NULL;
121 ts->len = s; 103 ts->len = s;
122 ts->u.d.tag = 0; 104 ts->u.d.tag = 0;
123 ts->u.d.value = (udata == NULL) ? uts+1 : udata; 105 ts->u.d.value = (udata == NULL) ? uts+1 : udata;
124 L->nblocks += sizestring(s); 106 /* insert it on table */
125 /* insert it on table */
126 newentry(L, &L->udt, ts, IntPoint(ts->u.d.value) & (L->udt.size-1)); 107 newentry(L, &L->udt, ts, IntPoint(ts->u.d.value) & (L->udt.size-1));
127 return ts; 108 return ts;
128} 109}
diff --git a/lstring.h b/lstring.h
index af733b74..330d80e1 100644
--- a/lstring.h
+++ b/lstring.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstring.h,v 1.24 2000/10/30 17:49:19 roberto Exp roberto $ 2** $Id: lstring.h,v 1.25 2000/11/24 17:39:56 roberto Exp roberto $
3** String table (keep all strings handled by Lua) 3** String table (keep all strings handled by Lua)
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -12,6 +12,17 @@
12#include "lstate.h" 12#include "lstate.h"
13 13
14 14
15
16/*
17** type equivalent to TString, but with maximum alignment requirements
18*/
19union L_UTString {
20 TString ts;
21 union L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */
22};
23
24
25
15/* 26/*
16** any TString with mark>=FIXMARK is never collected. 27** any TString with mark>=FIXMARK is never collected.
17** Marks>=RESERVEDMARK are used to identify reserved words. 28** Marks>=RESERVEDMARK are used to identify reserved words.
@@ -23,6 +34,8 @@
23#define sizestring(l) ((lint32)sizeof(TString) + \ 34#define sizestring(l) ((lint32)sizeof(TString) + \
24 ((lint32)(l+1)-TSPACK)*(lint32)sizeof(char)) 35 ((lint32)(l+1)-TSPACK)*(lint32)sizeof(char))
25 36
37#define sizeudata(l) ((luint32)sizeof(union L_UTString)+(l))
38
26 39
27void luaS_init (lua_State *L); 40void luaS_init (lua_State *L);
28void luaS_resize (lua_State *L, stringtable *tb, int newsize); 41void luaS_resize (lua_State *L, stringtable *tb, int newsize);
diff --git a/ltable.c b/ltable.c
index 82f47808..1895029c 100644
--- a/ltable.c
+++ b/ltable.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltable.c,v 1.60 2000/12/04 18:33:40 roberto Exp roberto $ 2** $Id: ltable.c,v 1.61 2000/12/22 16:57:46 roberto Exp roberto $
3** Lua tables (hash) 3** Lua tables (hash)
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -27,9 +27,6 @@
27#include "ltable.h" 27#include "ltable.h"
28 28
29 29
30#define gcsize(L, n) (sizeof(Hash)+(n)*sizeof(Node))
31
32
33 30
34#define TagDefault LUA_TTABLE 31#define TagDefault LUA_TTABLE
35 32
@@ -167,8 +164,6 @@ static void setnodevector (lua_State *L, Hash *t, luint32 size) {
167 ttype(&t->node[i].key) = ttype(&t->node[i].val) = LUA_TNIL; 164 ttype(&t->node[i].key) = ttype(&t->node[i].val) = LUA_TNIL;
168 t->node[i].next = NULL; 165 t->node[i].next = NULL;
169 } 166 }
170 L->nblocks -= gcsize(L, t->size); /* old size */
171 L->nblocks += gcsize(L, size); /* new size */
172 t->size = size; 167 t->size = size;
173 t->firstfree = &t->node[size-1]; /* first free position to be used */ 168 t->firstfree = &t->node[size-1]; /* first free position to be used */
174} 169}
@@ -181,7 +176,6 @@ Hash *luaH_new (lua_State *L, int size) {
181 L->roottable = t; 176 L->roottable = t;
182 t->mark = t; 177 t->mark = t;
183 t->size = 0; 178 t->size = 0;
184 L->nblocks += gcsize(L, 0);
185 t->node = NULL; 179 t->node = NULL;
186 setnodevector(L, t, luaO_power2(size)); 180 setnodevector(L, t, luaO_power2(size));
187 return t; 181 return t;
@@ -189,9 +183,8 @@ Hash *luaH_new (lua_State *L, int size) {
189 183
190 184
191void luaH_free (lua_State *L, Hash *t) { 185void luaH_free (lua_State *L, Hash *t) {
192 L->nblocks -= gcsize(L, t->size); 186 luaM_freearray(L, t->node, t->size, Node);
193 luaM_free(L, t->node); 187 luaM_freelem(L, t, Hash);
194 luaM_free(L, t);
195} 188}
196 189
197 190
@@ -226,7 +219,7 @@ static void rehash (lua_State *L, Hash *t) {
226 if (ttype(&old->val) != LUA_TNIL) 219 if (ttype(&old->val) != LUA_TNIL)
227 *luaH_set(L, t, &old->key) = old->val; 220 *luaH_set(L, t, &old->key) = old->val;
228 } 221 }
229 luaM_free(L, nold); /* free old array */ 222 luaM_freearray(L, nold, oldsize, Node); /* free old array */
230} 223}
231 224
232 225
diff --git a/ltests.c b/ltests.c
index a80a7977..ae0c71fc 100644
--- a/ltests.c
+++ b/ltests.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltests.c,v 1.53 2000/10/30 16:29:59 roberto Exp roberto $ 2** $Id: ltests.c,v 1.54 2000/10/31 13:10:24 roberto Exp roberto $
3** Internal Module for Debugging of the Lua Implementation 3** Internal Module for Debugging of the Lua Implementation
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -116,7 +116,7 @@ static int liststrings (lua_State *L) {
116 1, "Lua function expected"); 116 1, "Lua function expected");
117 p = clvalue(luaA_index(L, 1))->f.l; 117 p = clvalue(luaA_index(L, 1))->f.l;
118 lua_newtable(L); 118 lua_newtable(L);
119 for (i=0; i<p->nkstr; i++) { 119 for (i=0; i<p->sizekstr; i++) {
120 lua_pushnumber(L, i+1); 120 lua_pushnumber(L, i+1);
121 lua_pushstring(L, p->kstr[i]->str); 121 lua_pushstring(L, p->kstr[i]->str);
122 lua_settable(L, -3); 122 lua_settable(L, -3);
diff --git a/ltm.c b/ltm.c
index 852dfd13..78cebeb2 100644
--- a/ltm.c
+++ b/ltm.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltm.c,v 1.57 2000/11/30 18:50:47 roberto Exp roberto $ 2** $Id: ltm.c,v 1.58 2000/12/26 18:46:09 roberto Exp roberto $
3** Tag methods 3** Tag methods
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -75,9 +75,8 @@ static void init_entry (lua_State *L, int tag) {
75 75
76void luaT_init (lua_State *L) { 76void luaT_init (lua_State *L) {
77 int t; 77 int t;
78 L->TMtable = luaM_newvector(L, NUM_TAGS+2, struct TM);
78 L->sizeTM = NUM_TAGS+2; 79 L->sizeTM = NUM_TAGS+2;
79 L->TMtable = luaM_newvector(L, L->sizeTM, struct TM);
80 L->nblocks += NUM_TAGS*sizeof(struct TM);
81 L->ntag = NUM_TAGS; 80 L->ntag = NUM_TAGS;
82 for (t=0; t<L->ntag; t++) 81 for (t=0; t<L->ntag; t++)
83 init_entry(L, t); 82 init_entry(L, t);
@@ -87,7 +86,6 @@ void luaT_init (lua_State *L) {
87LUA_API int lua_newtag (lua_State *L) { 86LUA_API int lua_newtag (lua_State *L) {
88 luaM_growvector(L, L->TMtable, L->ntag, L->sizeTM, struct TM, 87 luaM_growvector(L, L->TMtable, L->ntag, L->sizeTM, struct TM,
89 MAX_INT, "tag table overflow"); 88 MAX_INT, "tag table overflow");
90 L->nblocks += sizeof(struct TM);
91 init_entry(L, L->ntag); 89 init_entry(L, L->ntag);
92 return L->ntag++; 90 return L->ntag++;
93} 91}
diff --git a/lundump.c b/lundump.c
index b367fe36..04d062a0 100644
--- a/lundump.c
+++ b/lundump.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lundump.c,v 1.34 2000/11/07 12:44:44 roberto Exp roberto $ 2** $Id: lundump.c,v 1.35 2000/12/04 18:33:40 roberto Exp roberto $
3** load bytecodes from files 3** load bytecodes from files
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -104,17 +104,17 @@ static TString* LoadString (lua_State* L, ZIO* Z, int swap)
104 104
105static void LoadCode (lua_State* L, Proto* tf, ZIO* Z, int swap) 105static void LoadCode (lua_State* L, Proto* tf, ZIO* Z, int swap)
106{ 106{
107 int size=LoadInt(L,Z,swap); 107 int size;
108 tf->sizecode=size=LoadInt(L,Z,swap);
108 tf->code=luaM_newvector(L,size,Instruction); 109 tf->code=luaM_newvector(L,size,Instruction);
109 LoadVector(L,tf->code,size,sizeof(*tf->code),Z,swap); 110 LoadVector(L,tf->code,size,sizeof(*tf->code),Z,swap);
110 if (tf->code[size-1]!=OP_END) luaO_verror(L,"bad code in `%.99s'",ZNAME(Z)); 111 if (tf->code[size-1]!=OP_END) luaO_verror(L,"bad code in `%.99s'",ZNAME(Z));
111 luaF_protook(L,tf,size);
112} 112}
113 113
114static void LoadLocals (lua_State* L, Proto* tf, ZIO* Z, int swap) 114static void LoadLocals (lua_State* L, Proto* tf, ZIO* Z, int swap)
115{ 115{
116 int i,n; 116 int i,n;
117 tf->nlocvars=n=LoadInt(L,Z,swap); 117 tf->sizelocvars=n=LoadInt(L,Z,swap);
118 tf->locvars=luaM_newvector(L,n,LocVar); 118 tf->locvars=luaM_newvector(L,n,LocVar);
119 for (i=0; i<n; i++) 119 for (i=0; i<n; i++)
120 { 120 {
@@ -127,7 +127,7 @@ static void LoadLocals (lua_State* L, Proto* tf, ZIO* Z, int swap)
127static void LoadLines (lua_State* L, Proto* tf, ZIO* Z, int swap) 127static void LoadLines (lua_State* L, Proto* tf, ZIO* Z, int swap)
128{ 128{
129 int n; 129 int n;
130 tf->nlineinfo=n=LoadInt(L,Z,swap); 130 tf->sizelineinfo=n=LoadInt(L,Z,swap);
131 tf->lineinfo=luaM_newvector(L,n,int); 131 tf->lineinfo=luaM_newvector(L,n,int);
132 LoadVector(L,tf->lineinfo,n,sizeof(*tf->lineinfo),Z,swap); 132 LoadVector(L,tf->lineinfo,n,sizeof(*tf->lineinfo),Z,swap);
133} 133}
@@ -137,14 +137,14 @@ static Proto* LoadFunction (lua_State* L, ZIO* Z, int swap);
137static void LoadConstants (lua_State* L, Proto* tf, ZIO* Z, int swap) 137static void LoadConstants (lua_State* L, Proto* tf, ZIO* Z, int swap)
138{ 138{
139 int i,n; 139 int i,n;
140 tf->nkstr=n=LoadInt(L,Z,swap); 140 tf->sizekstr=n=LoadInt(L,Z,swap);
141 tf->kstr=luaM_newvector(L,n,TString*); 141 tf->kstr=luaM_newvector(L,n,TString*);
142 for (i=0; i<n; i++) 142 for (i=0; i<n; i++)
143 tf->kstr[i]=LoadString(L,Z,swap); 143 tf->kstr[i]=LoadString(L,Z,swap);
144 tf->nknum=n=LoadInt(L,Z,swap); 144 tf->sizeknum=n=LoadInt(L,Z,swap);
145 tf->knum=luaM_newvector(L,n,lua_Number); 145 tf->knum=luaM_newvector(L,n,lua_Number);
146 LoadVector(L,tf->knum,n,sizeof(*tf->knum),Z,swap); 146 LoadVector(L,tf->knum,n,sizeof(*tf->knum),Z,swap);
147 tf->nkproto=n=LoadInt(L,Z,swap); 147 tf->sizekproto=n=LoadInt(L,Z,swap);
148 tf->kproto=luaM_newvector(L,n,Proto*); 148 tf->kproto=luaM_newvector(L,n,Proto*);
149 for (i=0; i<n; i++) 149 for (i=0; i<n; i++)
150 tf->kproto[i]=LoadFunction(L,Z,swap); 150 tf->kproto[i]=LoadFunction(L,Z,swap);
diff --git a/lvm.c b/lvm.c
index d49ed9e4..141d88f6 100644
--- a/lvm.c
+++ b/lvm.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lvm.c,v 1.147 2000/11/24 17:39:56 roberto Exp roberto $ 2** $Id: lvm.c,v 1.148 2000/12/04 18:33:40 roberto Exp roberto $
3** Lua virtual machine 3** Lua virtual machine
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -35,7 +35,7 @@
35** Extra stack size to run a function: 35** Extra stack size to run a function:
36** TAG_LINE(1), NAME(1), TM calls(3) (plus some extra...) 36** TAG_LINE(1), NAME(1), TM calls(3) (plus some extra...)
37*/ 37*/
38#define EXTRA_STACK 8 38#define EXTRA_FSTACK 8
39 39
40 40
41 41
@@ -355,7 +355,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
355 TString **const kstr = tf->kstr; 355 TString **const kstr = tf->kstr;
356 const lua_Hook linehook = L->linehook; 356 const lua_Hook linehook = L->linehook;
357 infovalue(base-1)->pc = &pc; 357 infovalue(base-1)->pc = &pc;
358 luaD_checkstack(L, tf->maxstacksize+EXTRA_STACK); 358 luaD_checkstack(L, tf->maxstacksize+EXTRA_FSTACK);
359 if (tf->is_vararg) /* varargs? */ 359 if (tf->is_vararg) /* varargs? */
360 adjust_varargs(L, base, tf->numparams); 360 adjust_varargs(L, base, tf->numparams);
361 else 361 else