diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-02-25 12:16:26 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-02-25 12:16:26 -0300 |
| commit | 26d1e21c89a481c2368ba934da8e192a164d8f99 (patch) | |
| tree | ea2eaaade79264b71d0e18fef75bca70fc1f4766 | |
| parent | 24a2c08145ecc9b5c528a46ba83bdd7b95dbdc02 (diff) | |
| download | lua-26d1e21c89a481c2368ba934da8e192a164d8f99.tar.gz lua-26d1e21c89a481c2368ba934da8e192a164d8f99.tar.bz2 lua-26d1e21c89a481c2368ba934da8e192a164d8f99.zip | |
new way to handle "growing" vectors
| -rw-r--r-- | lgc.c | 20 | ||||
| -rw-r--r-- | lmem.c | 47 | ||||
| -rw-r--r-- | lmem.h | 8 | ||||
| -rw-r--r-- | lparser.c | 31 | ||||
| -rw-r--r-- | ltm.c | 11 |
5 files changed, 51 insertions, 66 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lgc.c,v 1.19 1998/07/12 16:10:38 roberto Exp roberto $ | 2 | ** $Id: lgc.c,v 1.20 1999/01/22 18:08:03 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 | */ |
| @@ -29,23 +29,19 @@ static int markobject (TObject *o); | |||
| 29 | */ | 29 | */ |
| 30 | 30 | ||
| 31 | 31 | ||
| 32 | int luaC_ref (TObject *o, int lock) | 32 | int luaC_ref (TObject *o, int lock) { |
| 33 | { | ||
| 34 | int ref; | 33 | int ref; |
| 35 | if (ttype(o) == LUA_T_NIL) | 34 | if (ttype(o) == LUA_T_NIL) |
| 36 | ref = -1; /* special ref for nil */ | 35 | ref = -1; /* special ref for nil */ |
| 37 | else { | 36 | else { |
| 38 | for (ref=0; ref<L->refSize; ref++) | 37 | for (ref=0; ref<L->refSize; ref++) |
| 39 | if (L->refArray[ref].status == FREE) | 38 | if (L->refArray[ref].status == FREE) |
| 40 | goto found; | 39 | break; |
| 41 | /* no more empty spaces */ { | 40 | if (ref == L->refSize) { /* no more empty spaces? */ |
| 42 | int oldSize = L->refSize; | 41 | L->refArray = luaM_growvector(L->refArray, L->refSize, 1, struct ref, |
| 43 | L->refSize = luaM_growvector(&L->refArray, L->refSize, struct ref, | 42 | refEM, MAX_INT); |
| 44 | refEM, MAX_INT); | 43 | L->refSize++; |
| 45 | for (ref=oldSize; ref<L->refSize; ref++) | 44 | } |
| 46 | L->refArray[ref].status = FREE; | ||
| 47 | ref = oldSize; | ||
| 48 | } found: | ||
| 49 | L->refArray[ref].o = *o; | 45 | L->refArray[ref].o = *o; |
| 50 | L->refArray[ref].status = lock ? LOCK : HOLD; | 46 | L->refArray[ref].status = lock ? LOCK : HOLD; |
| 51 | } | 47 | } |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lmem.c,v 1.9 1999/01/22 18:08:57 roberto Exp roberto $ | 2 | ** $Id: lmem.c,v 1.10 1999/02/24 17:55:51 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 | */ |
| @@ -23,21 +23,36 @@ | |||
| 23 | #endif | 23 | #endif |
| 24 | 24 | ||
| 25 | 25 | ||
| 26 | #define MINSIZE 16 /* minimum size for "growing" vectors */ | ||
| 26 | 27 | ||
| 27 | #ifndef DEBUG | ||
| 28 | 28 | ||
| 29 | int luaM_growaux (void **block, unsigned long nelems, int size, | 29 | static unsigned long power2 (unsigned long n) { |
| 30 | unsigned long p = MINSIZE; | ||
| 31 | while (p<=n) p<<=1; | ||
| 32 | return p; | ||
| 33 | } | ||
| 34 | |||
| 35 | |||
| 36 | void *luaM_growaux (void *block, unsigned long nelems, int inc, int size, | ||
| 30 | char *errormsg, unsigned long limit) { | 37 | char *errormsg, unsigned long limit) { |
| 31 | if (nelems >= limit) | 38 | unsigned long newn = nelems+inc; |
| 32 | lua_error(errormsg); | 39 | if ((newn ^ nelems) > nelems) { /* cross a power of 2 boundary? */ |
| 33 | nelems = (nelems == 0) ? 32 : nelems*2; | 40 | if (newn >= limit) |
| 34 | if (nelems > limit) | 41 | lua_error(errormsg); |
| 35 | nelems = limit; | 42 | newn = power2(newn); |
| 36 | *block = luaM_realloc(*block, nelems*size); | 43 | if (newn > limit) |
| 37 | return (int)nelems; | 44 | newn = limit; |
| 45 | return luaM_realloc(block, newn*size); | ||
| 46 | } | ||
| 47 | else { | ||
| 48 | LUA_ASSERT(power2(nelems) == power2(newn), "bad arithmetic"); | ||
| 49 | return block; | ||
| 50 | } | ||
| 38 | } | 51 | } |
| 39 | 52 | ||
| 40 | 53 | ||
| 54 | #ifndef DEBUG | ||
| 55 | |||
| 41 | /* | 56 | /* |
| 42 | ** generic allocation routine. | 57 | ** generic allocation routine. |
| 43 | */ | 58 | */ |
| @@ -63,18 +78,6 @@ void *luaM_realloc (void *block, unsigned long size) { | |||
| 63 | #include <string.h> | 78 | #include <string.h> |
| 64 | 79 | ||
| 65 | 80 | ||
| 66 | int luaM_growaux (void **block, unsigned long nelems, int size, | ||
| 67 | char *errormsg, unsigned long limit) { | ||
| 68 | if (nelems >= limit) | ||
| 69 | lua_error(errormsg); | ||
| 70 | nelems = nelems+1; | ||
| 71 | if (nelems > limit) | ||
| 72 | nelems = limit; | ||
| 73 | *block = luaM_realloc(*block, nelems*size); | ||
| 74 | return (int)nelems; | ||
| 75 | } | ||
| 76 | |||
| 77 | |||
| 78 | #define HEADER (sizeof(double)) | 81 | #define HEADER (sizeof(double)) |
| 79 | 82 | ||
| 80 | #define MARK 55 | 83 | #define MARK 55 |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lmem.h,v 1.5 1997/12/17 20:48:58 roberto Exp roberto $ | 2 | ** $Id: lmem.h,v 1.6 1998/12/15 14:59:43 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 | */ |
| @@ -18,15 +18,15 @@ | |||
| 18 | #define memEM "not enough memory" | 18 | #define memEM "not enough memory" |
| 19 | 19 | ||
| 20 | void *luaM_realloc (void *oldblock, unsigned long size); | 20 | void *luaM_realloc (void *oldblock, unsigned long size); |
| 21 | int luaM_growaux (void **block, unsigned long nelems, int size, | 21 | void *luaM_growaux (void *block, unsigned long nelems, int inc, int size, |
| 22 | char *errormsg, unsigned long limit); | 22 | char *errormsg, unsigned long limit); |
| 23 | 23 | ||
| 24 | #define luaM_free(b) luaM_realloc((b), 0) | 24 | #define luaM_free(b) luaM_realloc((b), 0) |
| 25 | #define luaM_malloc(t) luaM_realloc(NULL, (t)) | 25 | #define luaM_malloc(t) luaM_realloc(NULL, (t)) |
| 26 | #define luaM_new(t) ((t *)luaM_malloc(sizeof(t))) | 26 | #define luaM_new(t) ((t *)luaM_malloc(sizeof(t))) |
| 27 | #define luaM_newvector(n,t) ((t *)luaM_malloc((n)*sizeof(t))) | 27 | #define luaM_newvector(n,t) ((t *)luaM_malloc((n)*sizeof(t))) |
| 28 | #define luaM_growvector(old,n,t,e,l) \ | 28 | #define luaM_growvector(old,nelems,inc,t,e,l) \ |
| 29 | (luaM_growaux((void**)old,n,sizeof(t),e,l)) | 29 | ((t *)luaM_growaux(old,nelems,inc,sizeof(t),e,l)) |
| 30 | #define luaM_reallocvector(v,n,t) ((t *)luaM_realloc(v,(n)*sizeof(t))) | 30 | #define luaM_reallocvector(v,n,t) ((t *)luaM_realloc(v,(n)*sizeof(t))) |
| 31 | 31 | ||
| 32 | 32 | ||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lparser.c,v 1.21 1999/02/24 15:37:19 roberto Exp roberto $ | 2 | ** $Id: lparser.c,v 1.22 1999/02/24 17:55:51 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 | */ |
| @@ -89,10 +89,7 @@ typedef struct FuncState { | |||
| 89 | int maxstacksize; /* maximum number of values on activation register */ | 89 | int maxstacksize; /* maximum number of values on activation register */ |
| 90 | int nlocalvar; /* number of active local variables */ | 90 | int nlocalvar; /* number of active local variables */ |
| 91 | int nupvalues; /* number of upvalues */ | 91 | int nupvalues; /* number of upvalues */ |
| 92 | int nvars; /* number of entries in f->locvars */ | 92 | int nvars; /* number of entries in f->locvars (-1 if no debug information) */ |
| 93 | int maxcode; /* size of f->code */ | ||
| 94 | int maxvars; /* size of f->locvars (-1 if no debug information) */ | ||
| 95 | int maxconsts; /* size of f->consts */ | ||
| 96 | int lastsetline; /* line where last SETLINE was issued */ | 93 | int lastsetline; /* line where last SETLINE was issued */ |
| 97 | vardesc upvalues[MAXUPVALUES]; /* upvalues */ | 94 | vardesc upvalues[MAXUPVALUES]; /* upvalues */ |
| 98 | TaggedString *localvar[MAXLOCALS]; /* store local variable names */ | 95 | TaggedString *localvar[MAXLOCALS]; /* store local variable names */ |
| @@ -134,9 +131,7 @@ static void var_or_func_tail (LexState *ls, vardesc *v); | |||
| 134 | 131 | ||
| 135 | 132 | ||
| 136 | static void check_pc (FuncState *fs, int n) { | 133 | static void check_pc (FuncState *fs, int n) { |
| 137 | if (fs->pc+n > fs->maxcode) | 134 | fs->f->code = luaM_growvector(fs->f->code, fs->pc, n, Byte, codeEM, MAX_INT); |
| 138 | fs->maxcode = luaM_growvector(&fs->f->code, fs->maxcode+n, | ||
| 139 | Byte, codeEM, MAX_INT); | ||
| 140 | } | 135 | } |
| 141 | 136 | ||
| 142 | 137 | ||
| @@ -221,9 +216,8 @@ static void code_constant (LexState *ls, int c) { | |||
| 221 | 216 | ||
| 222 | static int next_constant (FuncState *fs) { | 217 | static int next_constant (FuncState *fs) { |
| 223 | TProtoFunc *f = fs->f; | 218 | TProtoFunc *f = fs->f; |
| 224 | if (f->nconsts >= fs->maxconsts) | 219 | f->consts = luaM_growvector(f->consts, f->nconsts, 1, TObject, |
| 225 | fs->maxconsts = luaM_growvector(&f->consts, fs->maxconsts, TObject, | 220 | constantEM, MAX_ARG); |
| 226 | constantEM, MAX_ARG); | ||
| 227 | return f->nconsts++; | 221 | return f->nconsts++; |
| 228 | } | 222 | } |
| 229 | 223 | ||
| @@ -293,11 +287,9 @@ static void flush_list (LexState *ls, int m, int n) { | |||
| 293 | 287 | ||
| 294 | static void luaI_registerlocalvar (FuncState *fs, TaggedString *varname, | 288 | static void luaI_registerlocalvar (FuncState *fs, TaggedString *varname, |
| 295 | int line) { | 289 | int line) { |
| 296 | if (fs->maxvars != -1) { /* debug information? */ | 290 | if (fs->nvars != -1) { /* debug information? */ |
| 297 | TProtoFunc *f = fs->f; | 291 | TProtoFunc *f = fs->f; |
| 298 | if (fs->nvars >= fs->maxvars) | 292 | f->locvars = luaM_growvector(f->locvars, fs->nvars, 1, LocVar, "", MAX_INT); |
| 299 | fs->maxvars = luaM_growvector(&f->locvars, fs->maxvars, | ||
| 300 | LocVar, "", MAX_INT); | ||
| 301 | f->locvars[fs->nvars].varname = varname; | 293 | f->locvars[fs->nvars].varname = varname; |
| 302 | f->locvars[fs->nvars].line = line; | 294 | f->locvars[fs->nvars].line = line; |
| 303 | fs->nvars++; | 295 | fs->nvars++; |
| @@ -555,13 +547,8 @@ static void init_state (LexState *ls, FuncState *fs, TaggedString *filename) { | |||
| 555 | fs->f = f; | 547 | fs->f = f; |
| 556 | f->fileName = filename; | 548 | f->fileName = filename; |
| 557 | fs->pc = 0; | 549 | fs->pc = 0; |
| 558 | fs->maxcode = 0; | ||
| 559 | f->code = NULL; | 550 | f->code = NULL; |
| 560 | fs->maxconsts = 0; | 551 | fs->nvars = (L->debug) ? 0 : -1; /* flag no debug information? */ |
| 561 | if (L->debug) | ||
| 562 | fs->nvars = fs->maxvars = 0; | ||
| 563 | else | ||
| 564 | fs->maxvars = -1; /* flag no debug information */ | ||
| 565 | code_byte(fs, 0); /* to be filled with maxstacksize */ | 552 | code_byte(fs, 0); /* to be filled with maxstacksize */ |
| 566 | code_byte(fs, 0); /* to be filled with arg information */ | 553 | code_byte(fs, 0); /* to be filled with arg information */ |
| 567 | /* push function (to avoid GC) */ | 554 | /* push function (to avoid GC) */ |
| @@ -577,7 +564,7 @@ static void close_func (LexState *ls) { | |||
| 577 | f->code[0] = (Byte)fs->maxstacksize; | 564 | f->code[0] = (Byte)fs->maxstacksize; |
| 578 | f->code = luaM_reallocvector(f->code, fs->pc, Byte); | 565 | f->code = luaM_reallocvector(f->code, fs->pc, Byte); |
| 579 | f->consts = luaM_reallocvector(f->consts, f->nconsts, TObject); | 566 | f->consts = luaM_reallocvector(f->consts, f->nconsts, TObject); |
| 580 | if (fs->maxvars != -1) { /* debug information? */ | 567 | if (fs->nvars != -1) { /* debug information? */ |
| 581 | luaI_registerlocalvar(fs, NULL, -1); /* flag end of vector */ | 568 | luaI_registerlocalvar(fs, NULL, -1); /* flag end of vector */ |
| 582 | f->locvars = luaM_reallocvector(f->locvars, fs->nvars, LocVar); | 569 | f->locvars = luaM_reallocvector(f->locvars, fs->nvars, LocVar); |
| 583 | } | 570 | } |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ltm.c,v 1.20 1999/01/15 13:11:57 roberto Exp roberto $ | 2 | ** $Id: ltm.c,v 1.21 1999/02/04 18:59:31 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 | */ |
| @@ -58,9 +58,9 @@ static void init_entry (int tag) { | |||
| 58 | 58 | ||
| 59 | void luaT_init (void) { | 59 | void luaT_init (void) { |
| 60 | int t; | 60 | int t; |
| 61 | L->IMtable_size = NUM_TAGS*2; | ||
| 62 | L->last_tag = -(NUM_TAGS-1); | 61 | L->last_tag = -(NUM_TAGS-1); |
| 63 | L->IMtable = luaM_newvector(L->IMtable_size, struct IM); | 62 | L->IMtable = luaM_growvector(L->IMtable, 0, NUM_TAGS, |
| 63 | struct IM, memEM, MAX_INT); | ||
| 64 | for (t=L->last_tag; t<=0; t++) | 64 | for (t=L->last_tag; t<=0; t++) |
| 65 | init_entry(t); | 65 | init_entry(t); |
| 66 | } | 66 | } |
| @@ -68,9 +68,8 @@ void luaT_init (void) { | |||
| 68 | 68 | ||
| 69 | int lua_newtag (void) { | 69 | int lua_newtag (void) { |
| 70 | --L->last_tag; | 70 | --L->last_tag; |
| 71 | if ((-L->last_tag) >= L->IMtable_size) | 71 | L->IMtable = luaM_growvector(L->IMtable, -(L->last_tag), 1, |
| 72 | L->IMtable_size = luaM_growvector(&L->IMtable, L->IMtable_size, | 72 | struct IM, memEM, MAX_INT); |
| 73 | struct IM, memEM, MAX_INT); | ||
| 74 | init_entry(L->last_tag); | 73 | init_entry(L->last_tag); |
| 75 | return L->last_tag; | 74 | return L->last_tag; |
| 76 | } | 75 | } |
