aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1996-02-22 17:34:33 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1996-02-22 17:34:33 -0300
commit8c1a9899d4460aa19780919f4245c08d7ebba0e9 (patch)
tree685817907f64133330011eb7db845830c9327a3e
parent05caf09a36cadaab401bc9a24e29e2cd6e4126d4 (diff)
downloadlua-8c1a9899d4460aa19780919f4245c08d7ebba0e9.tar.gz
lua-8c1a9899d4460aa19780919f4245c08d7ebba0e9.tar.bz2
lua-8c1a9899d4460aa19780919f4245c08d7ebba0e9.zip
functions "luaI_free" and "luaI_realloc" (or macro "growvector") may be
called with NULL.
-rw-r--r--fallback.c14
-rw-r--r--func.c17
-rw-r--r--luamem.c19
-rw-r--r--luamem.h4
-rw-r--r--opcode.c8
-rw-r--r--tree.c28
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 @@
3** TecCGraf - PUC-Rio 3** TecCGraf - PUC-Rio
4*/ 4*/
5 5
6char *rcs_fallback="$Id: fallback.c,v 1.18 1996/01/30 15:25:23 roberto Exp roberto $"; 6char *rcs_fallback="$Id: fallback.c,v 1.19 1996/02/08 19:08:34 roberto Exp roberto $";
7 7
8#include <stdio.h> 8#include <stdio.h>
9#include <string.h> 9#include <string.h>
@@ -132,16 +132,8 @@ int luaI_lock (Object *object)
132 } 132 }
133 /* no more empty spaces */ 133 /* no more empty spaces */
134 oldSize = lockSize; 134 oldSize = lockSize;
135 if (lockArray == NULL) 135 lockSize = (lockSize == 0) ? 10 : 3*lockSize/2 + 5;
136 { 136 lockArray = growvector(lockArray, lockSize, Object);
137 lockSize = 10;
138 lockArray = newvector(lockSize, Object);
139 }
140 else
141 {
142 lockSize = 3*oldSize/2 + 5;
143 lockArray = growvector(lockArray, lockSize, Object);
144 }
145 for (i=oldSize; i<lockSize; i++) 137 for (i=oldSize; i<lockSize; i++)
146 tag(&lockArray[i]) = LUA_T_NIL; 138 tag(&lockArray[i]) = LUA_T_NIL;
147 lockArray[oldSize] = *object; 139 lockArray[oldSize] = *object;
diff --git a/func.c b/func.c
index 6a02c78d..8180cd39 100644
--- a/func.c
+++ b/func.c
@@ -42,8 +42,7 @@ void luaI_insertfunction (TFunc *f)
42static void freefunc (TFunc *f) 42static void freefunc (TFunc *f)
43{ 43{
44 luaI_free (f->code); 44 luaI_free (f->code);
45 if (f->locvars) 45 luaI_free (f->locvars);
46 luaI_free (f->locvars);
47 luaI_free (f); 46 luaI_free (f);
48} 47}
49 48
@@ -100,16 +99,10 @@ void lua_funcinfo (lua_Object func, char **filename, int *linedefined)
100void luaI_registerlocalvar (TaggedString *varname, int line) 99void luaI_registerlocalvar (TaggedString *varname, int line)
101{ 100{
102 if (numcurrvars >= maxcurrvars) 101 if (numcurrvars >= maxcurrvars)
103 if (currvars == NULL) 102 {
104 { 103 maxcurrvars = (maxcurrvars == 0) ? LOCALVARINITSIZE : maxcurrvars*2;
105 maxcurrvars = LOCALVARINITSIZE; 104 currvars = growvector(currvars, maxcurrvars, LocVar);
106 currvars = newvector (maxcurrvars, LocVar); 105 }
107 }
108 else
109 {
110 maxcurrvars *= 2;
111 currvars = growvector (currvars, maxcurrvars, LocVar);
112 }
113 currvars[numcurrvars].varname = varname; 106 currvars[numcurrvars].varname = varname;
114 currvars[numcurrvars].line = line; 107 currvars[numcurrvars].line = line;
115 numcurrvars++; 108 numcurrvars++;
diff --git a/luamem.c b/luamem.c
index d040f31d..ef4eae29 100644
--- a/luamem.c
+++ b/luamem.c
@@ -3,7 +3,7 @@
3** TecCGraf - PUC-Rio 3** TecCGraf - PUC-Rio
4*/ 4*/
5 5
6char *rcs_mem = "$Id: mem.c,v 1.6 1996/01/22 14:15:13 roberto Exp roberto $"; 6char *rcs_mem = "$Id: mem.c,v 1.7 1996/02/04 16:59:12 roberto Exp roberto $";
7 7
8#include <stdlib.h> 8#include <stdlib.h>
9#include <string.h> 9#include <string.h>
@@ -27,8 +27,11 @@ static void mem_error (void)
27 27
28void luaI_free (void *block) 28void luaI_free (void *block)
29{ 29{
30 *((int *)block) = -1; /* to catch errors */ 30 if (block)
31 free(block); 31 {
32 *((int *)block) = -1; /* to catch errors */
33 free(block);
34 }
32} 35}
33 36
34 37
@@ -43,16 +46,10 @@ void *luaI_malloc (unsigned long size)
43 46
44void *luaI_realloc (void *oldblock, unsigned long size) 47void *luaI_realloc (void *oldblock, unsigned long size)
45{ 48{
46 void *block = realloc(oldblock, (size_t)size); 49 void *block = oldblock ? realloc(oldblock, (size_t)size) :
50 malloc((size_t)size);
47 if (block == NULL) 51 if (block == NULL)
48 mem_error(); 52 mem_error();
49 return block; 53 return block;
50} 54}
51 55
52
53char *luaI_strdup (char *str)
54{
55 char *newstr = luaI_malloc(strlen(str)+1);
56 strcpy(newstr, str);
57 return newstr;
58}
diff --git a/luamem.h b/luamem.h
index 168a09d0..06687efc 100644
--- a/luamem.h
+++ b/luamem.h
@@ -1,7 +1,7 @@
1/* 1/*
2** mem.c 2** mem.c
3** memory manager for lua 3** memory manager for lua
4** $Id: mem.h,v 1.1 1994/11/16 17:38:08 roberto Stab roberto $ 4** $Id: mem.h,v 1.2 1995/01/13 22:11:12 roberto Exp roberto $
5*/ 5*/
6 6
7#ifndef mem_h 7#ifndef mem_h
@@ -15,8 +15,6 @@ void luaI_free (void *block);
15void *luaI_malloc (unsigned long size); 15void *luaI_malloc (unsigned long size);
16void *luaI_realloc (void *oldblock, unsigned long size); 16void *luaI_realloc (void *oldblock, unsigned long size);
17 17
18char *luaI_strdup (char *str);
19
20#define new(s) ((s *)luaI_malloc(sizeof(s))) 18#define new(s) ((s *)luaI_malloc(sizeof(s)))
21#define newvector(n,s) ((s *)luaI_malloc((n)*sizeof(s))) 19#define newvector(n,s) ((s *)luaI_malloc((n)*sizeof(s)))
22#define growvector(old,n,s) ((s *)luaI_realloc(old,(n)*sizeof(s))) 20#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 @@
3** TecCGraf - PUC-Rio 3** TecCGraf - PUC-Rio
4*/ 4*/
5 5
6char *rcs_opcode="$Id: opcode.c,v 3.56 1996/02/08 17:03:20 roberto Exp roberto $"; 6char *rcs_opcode="$Id: opcode.c,v 3.57 1996/02/12 18:32:40 roberto Exp roberto $";
7 7
8#include <setjmp.h> 8#include <setjmp.h>
9#include <stdlib.h> 9#include <stdlib.h>
@@ -135,8 +135,7 @@ static char *lua_strconc (char *l, char *r)
135 if (n > buffer_size) 135 if (n > buffer_size)
136 { 136 {
137 buffer_size = n; 137 buffer_size = n;
138 if (buffer != NULL) 138 luaI_free(buffer);
139 luaI_free(buffer);
140 buffer = newvector(buffer_size, char); 139 buffer = newvector(buffer_size, char);
141 } 140 }
142 strcpy(buffer,l); 141 strcpy(buffer,l);
@@ -525,8 +524,7 @@ static int do_protectedmain (void)
525 adjustC(0); /* erase extra slot */ 524 adjustC(0); /* erase extra slot */
526 } 525 }
527 errorJmp = oldErr; 526 errorJmp = oldErr;
528 if (tf.code) 527 luaI_free(tf.code);
529 luaI_free(tf.code);
530 return status; 528 return status;
531} 529}
532 530
diff --git a/tree.c b/tree.c
index 8f0c08b4..fa708c9d 100644
--- a/tree.c
+++ b/tree.c
@@ -3,7 +3,7 @@
3** TecCGraf - PUC-Rio 3** TecCGraf - PUC-Rio
4*/ 4*/
5 5
6char *rcs_tree="$Id: tree.c,v 1.17 1996/02/14 13:35:51 roberto Exp roberto $"; 6char *rcs_tree="$Id: tree.c,v 1.18 1996/02/14 19:11:09 roberto Exp roberto $";
7 7
8 8
9#include <string.h> 9#include <string.h>
@@ -55,20 +55,18 @@ static void grow (stringtable *tb)
55 int i; 55 int i;
56 for (i=0; i<newsize; i++) 56 for (i=0; i<newsize; i++)
57 newhash[i] = NULL; 57 newhash[i] = NULL;
58 if (tb->size > 0) 58 /* rehash */
59 { /* rehash */ 59 tb->nuse = 0;
60 tb->nuse = 0; 60 for (i=0; i<tb->size; i++)
61 for (i=0; i<tb->size; i++) 61 if (tb->hash[i] != NULL && tb->hash[i] != &EMPTY)
62 if (tb->hash[i] != NULL && tb->hash[i] != &EMPTY) 62 {
63 { 63 int h = tb->hash[i]->hash%newsize;
64 int h = tb->hash[i]->hash%newsize; 64 while (newhash[h])
65 while (newhash[h]) 65 h = (h+1)%newsize;
66 h = (h+1)%newsize; 66 newhash[h] = tb->hash[i];
67 newhash[h] = tb->hash[i]; 67 tb->nuse++;
68 tb->nuse++; 68 }
69 } 69 luaI_free(tb->hash);
70 luaI_free(tb->hash);
71 }
72 tb->size = newsize; 70 tb->size = newsize;
73 tb->hash = newhash; 71 tb->hash = newhash;
74} 72}