functions "luaI_free" and "luaI_realloc" (or macro "growvector") may be

called with NULL.
This commit is contained in:
Roberto Ierusalimschy
1996-02-22 17:34:33 -03:00
parent 05caf09a36
commit 8c1a9899d4
6 changed files with 33 additions and 57 deletions

View File

@@ -3,7 +3,7 @@
** TecCGraf - PUC-Rio ** 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 <stdio.h>
#include <string.h> #include <string.h>
@@ -132,16 +132,8 @@ int luaI_lock (Object *object)
} }
/* no more empty spaces */ /* no more empty spaces */
oldSize = lockSize; oldSize = lockSize;
if (lockArray == NULL) lockSize = (lockSize == 0) ? 10 : 3*lockSize/2 + 5;
{
lockSize = 10;
lockArray = newvector(lockSize, Object);
}
else
{
lockSize = 3*oldSize/2 + 5;
lockArray = growvector(lockArray, lockSize, Object); lockArray = growvector(lockArray, lockSize, Object);
}
for (i=oldSize; i<lockSize; i++) for (i=oldSize; i<lockSize; i++)
tag(&lockArray[i]) = LUA_T_NIL; tag(&lockArray[i]) = LUA_T_NIL;
lockArray[oldSize] = *object; lockArray[oldSize] = *object;

11
func.c
View File

@@ -42,7 +42,6 @@ void luaI_insertfunction (TFunc *f)
static void freefunc (TFunc *f) static void freefunc (TFunc *f)
{ {
luaI_free (f->code); luaI_free (f->code);
if (f->locvars)
luaI_free (f->locvars); luaI_free (f->locvars);
luaI_free (f); luaI_free (f);
} }
@@ -100,15 +99,9 @@ void lua_funcinfo (lua_Object func, char **filename, int *linedefined)
void luaI_registerlocalvar (TaggedString *varname, int line) void luaI_registerlocalvar (TaggedString *varname, int line)
{ {
if (numcurrvars >= maxcurrvars) if (numcurrvars >= maxcurrvars)
if (currvars == NULL)
{ {
maxcurrvars = LOCALVARINITSIZE; maxcurrvars = (maxcurrvars == 0) ? LOCALVARINITSIZE : maxcurrvars*2;
currvars = newvector (maxcurrvars, LocVar); currvars = growvector(currvars, maxcurrvars, LocVar);
}
else
{
maxcurrvars *= 2;
currvars = growvector (currvars, maxcurrvars, LocVar);
} }
currvars[numcurrvars].varname = varname; currvars[numcurrvars].varname = varname;
currvars[numcurrvars].line = line; currvars[numcurrvars].line = line;

View File

@@ -3,7 +3,7 @@
** TecCGraf - PUC-Rio ** 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 <stdlib.h>
#include <string.h> #include <string.h>
@@ -27,8 +27,11 @@ static void mem_error (void)
void luaI_free (void *block) void luaI_free (void *block)
{ {
if (block)
{
*((int *)block) = -1; /* to catch errors */ *((int *)block) = -1; /* to catch errors */
free(block); free(block);
}
} }
@@ -43,16 +46,10 @@ void *luaI_malloc (unsigned long size)
void *luaI_realloc (void *oldblock, 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) if (block == NULL)
mem_error(); mem_error();
return block; return block;
} }
char *luaI_strdup (char *str)
{
char *newstr = luaI_malloc(strlen(str)+1);
strcpy(newstr, str);
return newstr;
}

View File

@@ -1,7 +1,7 @@
/* /*
** mem.c ** mem.c
** memory manager for lua ** 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 #ifndef mem_h
@@ -15,8 +15,6 @@ void luaI_free (void *block);
void *luaI_malloc (unsigned long size); void *luaI_malloc (unsigned long size);
void *luaI_realloc (void *oldblock, 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 new(s) ((s *)luaI_malloc(sizeof(s)))
#define newvector(n,s) ((s *)luaI_malloc((n)*sizeof(s))) #define newvector(n,s) ((s *)luaI_malloc((n)*sizeof(s)))
#define growvector(old,n,s) ((s *)luaI_realloc(old,(n)*sizeof(s))) #define growvector(old,n,s) ((s *)luaI_realloc(old,(n)*sizeof(s)))

View File

@@ -3,7 +3,7 @@
** TecCGraf - PUC-Rio ** 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 <setjmp.h>
#include <stdlib.h> #include <stdlib.h>
@@ -135,7 +135,6 @@ static char *lua_strconc (char *l, char *r)
if (n > buffer_size) if (n > buffer_size)
{ {
buffer_size = n; buffer_size = n;
if (buffer != NULL)
luaI_free(buffer); luaI_free(buffer);
buffer = newvector(buffer_size, char); buffer = newvector(buffer_size, char);
} }
@@ -525,7 +524,6 @@ static int do_protectedmain (void)
adjustC(0); /* erase extra slot */ adjustC(0); /* erase extra slot */
} }
errorJmp = oldErr; errorJmp = oldErr;
if (tf.code)
luaI_free(tf.code); luaI_free(tf.code);
return status; return status;
} }

6
tree.c
View File

@@ -3,7 +3,7 @@
** TecCGraf - PUC-Rio ** 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> #include <string.h>
@@ -55,8 +55,7 @@ static void grow (stringtable *tb)
int i; int i;
for (i=0; i<newsize; i++) for (i=0; i<newsize; i++)
newhash[i] = NULL; newhash[i] = NULL;
if (tb->size > 0) /* rehash */
{ /* rehash */
tb->nuse = 0; tb->nuse = 0;
for (i=0; i<tb->size; i++) for (i=0; i<tb->size; i++)
if (tb->hash[i] != NULL && tb->hash[i] != &EMPTY) if (tb->hash[i] != NULL && tb->hash[i] != &EMPTY)
@@ -68,7 +67,6 @@ static void grow (stringtable *tb)
tb->nuse++; tb->nuse++;
} }
luaI_free(tb->hash); luaI_free(tb->hash);
}
tb->size = newsize; tb->size = newsize;
tb->hash = newhash; tb->hash = newhash;
} }