From 8c1a9899d4460aa19780919f4245c08d7ebba0e9 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 22 Feb 1996 17:34:33 -0300 Subject: functions "luaI_free" and "luaI_realloc" (or macro "growvector") may be called with NULL. --- fallback.c | 14 +++----------- func.c | 17 +++++------------ luamem.c | 19 ++++++++----------- luamem.h | 4 +--- opcode.c | 8 +++----- tree.c | 28 +++++++++++++--------------- 6 files changed, 33 insertions(+), 57 deletions(-) diff --git a/fallback.c b/fallback.c index 00eb458a..62732f6b 100644 --- a/fallback.c +++ b/fallback.c @@ -3,7 +3,7 @@ ** TecCGraf - PUC-Rio */ -char *rcs_fallback="$Id: fallback.c,v 1.18 1996/01/30 15:25:23 roberto Exp roberto $"; +char *rcs_fallback="$Id: fallback.c,v 1.19 1996/02/08 19:08:34 roberto Exp roberto $"; #include #include @@ -132,16 +132,8 @@ int luaI_lock (Object *object) } /* no more empty spaces */ oldSize = lockSize; - if (lockArray == NULL) - { - lockSize = 10; - lockArray = newvector(lockSize, Object); - } - else - { - lockSize = 3*oldSize/2 + 5; - lockArray = growvector(lockArray, lockSize, Object); - } + lockSize = (lockSize == 0) ? 10 : 3*lockSize/2 + 5; + lockArray = growvector(lockArray, lockSize, Object); for (i=oldSize; icode); - if (f->locvars) - luaI_free (f->locvars); + luaI_free (f->locvars); luaI_free (f); } @@ -100,16 +99,10 @@ void lua_funcinfo (lua_Object func, char **filename, int *linedefined) void luaI_registerlocalvar (TaggedString *varname, int line) { if (numcurrvars >= maxcurrvars) - if (currvars == NULL) - { - maxcurrvars = LOCALVARINITSIZE; - currvars = newvector (maxcurrvars, LocVar); - } - else - { - maxcurrvars *= 2; - currvars = growvector (currvars, maxcurrvars, LocVar); - } + { + maxcurrvars = (maxcurrvars == 0) ? LOCALVARINITSIZE : maxcurrvars*2; + currvars = growvector(currvars, maxcurrvars, LocVar); + } currvars[numcurrvars].varname = varname; currvars[numcurrvars].line = line; numcurrvars++; diff --git a/luamem.c b/luamem.c index d040f31d..ef4eae29 100644 --- a/luamem.c +++ b/luamem.c @@ -3,7 +3,7 @@ ** TecCGraf - PUC-Rio */ -char *rcs_mem = "$Id: mem.c,v 1.6 1996/01/22 14:15:13 roberto Exp roberto $"; +char *rcs_mem = "$Id: mem.c,v 1.7 1996/02/04 16:59:12 roberto Exp roberto $"; #include #include @@ -27,8 +27,11 @@ static void mem_error (void) void luaI_free (void *block) { - *((int *)block) = -1; /* to catch errors */ - free(block); + if (block) + { + *((int *)block) = -1; /* to catch errors */ + free(block); + } } @@ -43,16 +46,10 @@ void *luaI_malloc (unsigned long size) void *luaI_realloc (void *oldblock, unsigned long size) { - void *block = realloc(oldblock, (size_t)size); + void *block = oldblock ? realloc(oldblock, (size_t)size) : + malloc((size_t)size); if (block == NULL) mem_error(); return block; } - -char *luaI_strdup (char *str) -{ - char *newstr = luaI_malloc(strlen(str)+1); - strcpy(newstr, str); - return newstr; -} diff --git a/luamem.h b/luamem.h index 168a09d0..06687efc 100644 --- a/luamem.h +++ b/luamem.h @@ -1,7 +1,7 @@ /* ** mem.c ** memory manager for lua -** $Id: mem.h,v 1.1 1994/11/16 17:38:08 roberto Stab roberto $ +** $Id: mem.h,v 1.2 1995/01/13 22:11:12 roberto Exp roberto $ */ #ifndef mem_h @@ -15,8 +15,6 @@ void luaI_free (void *block); void *luaI_malloc (unsigned long size); void *luaI_realloc (void *oldblock, unsigned long size); -char *luaI_strdup (char *str); - #define new(s) ((s *)luaI_malloc(sizeof(s))) #define newvector(n,s) ((s *)luaI_malloc((n)*sizeof(s))) #define growvector(old,n,s) ((s *)luaI_realloc(old,(n)*sizeof(s))) diff --git a/opcode.c b/opcode.c index 2f77cad4..c11cda3f 100644 --- a/opcode.c +++ b/opcode.c @@ -3,7 +3,7 @@ ** TecCGraf - PUC-Rio */ -char *rcs_opcode="$Id: opcode.c,v 3.56 1996/02/08 17:03:20 roberto Exp roberto $"; +char *rcs_opcode="$Id: opcode.c,v 3.57 1996/02/12 18:32:40 roberto Exp roberto $"; #include #include @@ -135,8 +135,7 @@ static char *lua_strconc (char *l, char *r) if (n > buffer_size) { buffer_size = n; - if (buffer != NULL) - luaI_free(buffer); + luaI_free(buffer); buffer = newvector(buffer_size, char); } strcpy(buffer,l); @@ -525,8 +524,7 @@ static int do_protectedmain (void) adjustC(0); /* erase extra slot */ } errorJmp = oldErr; - if (tf.code) - luaI_free(tf.code); + luaI_free(tf.code); return status; } diff --git a/tree.c b/tree.c index 8f0c08b4..fa708c9d 100644 --- a/tree.c +++ b/tree.c @@ -3,7 +3,7 @@ ** TecCGraf - PUC-Rio */ -char *rcs_tree="$Id: tree.c,v 1.17 1996/02/14 13:35:51 roberto Exp roberto $"; +char *rcs_tree="$Id: tree.c,v 1.18 1996/02/14 19:11:09 roberto Exp roberto $"; #include @@ -55,20 +55,18 @@ static void grow (stringtable *tb) int i; for (i=0; isize > 0) - { /* rehash */ - tb->nuse = 0; - for (i=0; isize; i++) - if (tb->hash[i] != NULL && tb->hash[i] != &EMPTY) - { - int h = tb->hash[i]->hash%newsize; - while (newhash[h]) - h = (h+1)%newsize; - newhash[h] = tb->hash[i]; - tb->nuse++; - } - luaI_free(tb->hash); - } + /* rehash */ + tb->nuse = 0; + for (i=0; isize; i++) + if (tb->hash[i] != NULL && tb->hash[i] != &EMPTY) + { + int h = tb->hash[i]->hash%newsize; + while (newhash[h]) + h = (h+1)%newsize; + newhash[h] = tb->hash[i]; + tb->nuse++; + } + luaI_free(tb->hash); tb->size = newsize; tb->hash = newhash; } -- cgit v1.2.3-55-g6feb