lua

A copy of the Lua development repository
Log | Files | Refs | README

commit 8c1a9899d4460aa19780919f4245c08d7ebba0e9
parent 05caf09a36cadaab401bc9a24e29e2cd6e4126d4
Author: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date:   Thu, 22 Feb 1996 17:34:13 -0300

functions "luaI_free" and "luaI_realloc" (or macro "growvector") may be
called with NULL.

Diffstat:
Mfallback.c | 14+++-----------
Mfunc.c | 17+++++------------
Mluamem.c | 19++++++++-----------
Mluamem.h | 4+---
Mopcode.c | 8+++-----
Mtree.c | 28+++++++++++++---------------
6 files changed, 33 insertions(+), 57 deletions(-)

diff --git 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 <stdio.h> #include <string.h> @@ -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; i<lockSize; i++) tag(&lockArray[i]) = LUA_T_NIL; lockArray[oldSize] = *object; diff --git a/func.c b/func.c @@ -42,8 +42,7 @@ void luaI_insertfunction (TFunc *f) static void freefunc (TFunc *f) { luaI_free (f->code); - 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 @@ -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 <stdlib.h> #include <string.h> @@ -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 @@ -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 @@ -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 <setjmp.h> #include <stdlib.h> @@ -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 @@ -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 <string.h> @@ -55,20 +55,18 @@ static void grow (stringtable *tb) int i; for (i=0; i<newsize; i++) newhash[i] = NULL; - if (tb->size > 0) - { /* rehash */ - tb->nuse = 0; - for (i=0; i<tb->size; 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; i<tb->size; 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; }