瀏覽代碼

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

Roberto Ierusalimschy 29 年之前
父節點
當前提交
8c1a9899d4
共有 6 個文件被更改,包括 33 次插入57 次删除
  1. 3 11
      fallback.c
  2. 5 12
      func.c
  3. 8 11
      luamem.c
  4. 1 3
      luamem.h
  5. 3 5
      opcode.c
  6. 13 15
      tree.c

+ 3 - 11
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;

+ 5 - 12
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++;

+ 8 - 11
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;
-}

+ 1 - 3
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)))

+ 3 - 5
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;
 }
 

+ 13 - 15
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;
 }