diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1994-11-16 15:39:16 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1994-11-16 15:39:16 -0200 |
commit | 2b5bc5d1a81579a76c13e638de2592e2c39c73f0 (patch) | |
tree | 8294278f9fbd2565d3a2cd11642fed41982824bd | |
parent | 94686ce58554a80374eeff115ee5b87c184ed173 (diff) | |
download | lua-2b5bc5d1a81579a76c13e638de2592e2c39c73f0.tar.gz lua-2b5bc5d1a81579a76c13e638de2592e2c39c73f0.tar.bz2 lua-2b5bc5d1a81579a76c13e638de2592e2c39c73f0.zip |
new module for memory allocation
-rw-r--r-- | fallback.c | 13 | ||||
-rw-r--r-- | hash.c | 24 | ||||
-rw-r--r-- | iolib.c | 22 | ||||
-rw-r--r-- | luamem.c | 35 | ||||
-rw-r--r-- | luamem.h | 23 | ||||
-rw-r--r-- | opcode.c | 47 | ||||
-rw-r--r-- | strlib.c | 10 | ||||
-rw-r--r-- | table.c | 24 | ||||
-rw-r--r-- | tree.c | 14 |
9 files changed, 113 insertions, 99 deletions
@@ -3,11 +3,11 @@ | |||
3 | ** TecCGraf - PUC-Rio | 3 | ** TecCGraf - PUC-Rio |
4 | */ | 4 | */ |
5 | 5 | ||
6 | char *rcs_fallback="$Id: fallback.c,v 1.4 1994/11/10 17:11:52 roberto Exp roberto $"; | 6 | char *rcs_fallback="$Id: fallback.c,v 1.5 1994/11/10 17:36:54 roberto Exp roberto $"; |
7 | 7 | ||
8 | #include <stdio.h> | 8 | #include <stdio.h> |
9 | #include <stdlib.h> | ||
10 | 9 | ||
10 | #include "mem.h" | ||
11 | #include "fallback.h" | 11 | #include "fallback.h" |
12 | #include "opcode.h" | 12 | #include "opcode.h" |
13 | #include "inout.h" | 13 | #include "inout.h" |
@@ -129,17 +129,12 @@ int lua_lock (lua_Object object) | |||
129 | if (lockArray == NULL) | 129 | if (lockArray == NULL) |
130 | { | 130 | { |
131 | lockSize = 10; | 131 | lockSize = 10; |
132 | lockArray = (Object *)malloc(lockSize); | 132 | lockArray = newvector(lockSize, Object); |
133 | } | 133 | } |
134 | else | 134 | else |
135 | { | 135 | { |
136 | lockSize = 3*oldSize/2 + 5; | 136 | lockSize = 3*oldSize/2 + 5; |
137 | lockArray = (Object *)realloc(lockArray, lockSize); | 137 | lockArray = growvector(lockArray, lockSize, Object); |
138 | } | ||
139 | if (lockArray == NULL) | ||
140 | { | ||
141 | lockSize = 0; | ||
142 | lua_error("lock - not enough memory"); | ||
143 | } | 138 | } |
144 | for (i=oldSize; i<lockSize; i++) | 139 | for (i=oldSize; i<lockSize; i++) |
145 | tag(&lockArray[i]) = LUA_T_NIL; | 140 | tag(&lockArray[i]) = LUA_T_NIL; |
@@ -3,11 +3,9 @@ | |||
3 | ** hash manager for lua | 3 | ** hash manager for lua |
4 | */ | 4 | */ |
5 | 5 | ||
6 | char *rcs_hash="$Id: hash.c,v 2.15 1994/11/10 17:36:54 roberto Exp $"; | 6 | char *rcs_hash="$Id: hash.c,v 2.16 1994/11/14 18:41:15 roberto Exp roberto $"; |
7 | |||
8 | #include <string.h> | ||
9 | #include <stdlib.h> | ||
10 | 7 | ||
8 | #include "mem.h" | ||
11 | #include "opcode.h" | 9 | #include "opcode.h" |
12 | #include "hash.h" | 10 | #include "hash.h" |
13 | #include "inout.h" | 11 | #include "inout.h" |
@@ -16,9 +14,6 @@ char *rcs_hash="$Id: hash.c,v 2.15 1994/11/10 17:36:54 roberto Exp $"; | |||
16 | 14 | ||
17 | #define streq(s1,s2) (s1 == s2 || (*(s1) == *(s2) && strcmp(s1,s2)==0)) | 15 | #define streq(s1,s2) (s1 == s2 || (*(s1) == *(s2) && strcmp(s1,s2)==0)) |
18 | 16 | ||
19 | #define new(s) ((s *)malloc(sizeof(s))) | ||
20 | #define newvector(n,s) ((s *)calloc(n,sizeof(s))) | ||
21 | |||
22 | #define nhash(t) ((t)->nhash) | 17 | #define nhash(t) ((t)->nhash) |
23 | #define nuse(t) ((t)->nuse) | 18 | #define nuse(t) ((t)->nuse) |
24 | #define markarray(t) ((t)->mark) | 19 | #define markarray(t) ((t)->mark) |
@@ -117,8 +112,6 @@ static Node *hashnodecreate (int nhash) | |||
117 | { | 112 | { |
118 | int i; | 113 | int i; |
119 | Node *v = newvector (nhash, Node); | 114 | Node *v = newvector (nhash, Node); |
120 | if (v == NULL) | ||
121 | lua_error ("not enough memory"); | ||
122 | for (i=0; i<nhash; i++) | 115 | for (i=0; i<nhash; i++) |
123 | tag(ref(&v[i])) = LUA_T_NIL; | 116 | tag(ref(&v[i])) = LUA_T_NIL; |
124 | return v; | 117 | return v; |
@@ -129,14 +122,9 @@ static Node *hashnodecreate (int nhash) | |||
129 | */ | 122 | */ |
130 | static Hash *hashcreate (int nhash) | 123 | static Hash *hashcreate (int nhash) |
131 | { | 124 | { |
132 | Hash *t = new (Hash); | 125 | Hash *t = new(Hash); |
133 | if (t == NULL) | ||
134 | lua_error ("not enough memory"); | ||
135 | nhash = redimension((int)((float)nhash/REHASH_LIMIT)); | 126 | nhash = redimension((int)((float)nhash/REHASH_LIMIT)); |
136 | |||
137 | nodevector(t) = hashnodecreate(nhash); | 127 | nodevector(t) = hashnodecreate(nhash); |
138 | if (nodevector(t) == NULL) | ||
139 | lua_error ("not enough memory"); | ||
140 | nhash(t) = nhash; | 128 | nhash(t) = nhash; |
141 | nuse(t) = 0; | 129 | nuse(t) = 0; |
142 | markarray(t) = 0; | 130 | markarray(t) = 0; |
@@ -148,8 +136,8 @@ static Hash *hashcreate (int nhash) | |||
148 | */ | 136 | */ |
149 | static void hashdelete (Hash *t) | 137 | static void hashdelete (Hash *t) |
150 | { | 138 | { |
151 | free (nodevector(t)); | 139 | luaI_free (nodevector(t)); |
152 | free(t); | 140 | luaI_free(t); |
153 | } | 141 | } |
154 | 142 | ||
155 | 143 | ||
@@ -253,7 +241,7 @@ static void rehash (Hash *t) | |||
253 | if (tag(ref(n)) != LUA_T_NIL && tag(val(n)) != LUA_T_NIL) | 241 | if (tag(ref(n)) != LUA_T_NIL && tag(val(n)) != LUA_T_NIL) |
254 | *node(t, present(t, ref(n))) = *n; /* copy old node to new hahs */ | 242 | *node(t, present(t, ref(n))) = *n; /* copy old node to new hahs */ |
255 | } | 243 | } |
256 | free(vold); | 244 | luaI_free(vold); |
257 | } | 245 | } |
258 | 246 | ||
259 | /* | 247 | /* |
@@ -3,18 +3,16 @@ | |||
3 | ** Input/output library to LUA | 3 | ** Input/output library to LUA |
4 | */ | 4 | */ |
5 | 5 | ||
6 | char *rcs_iolib="$Id: iolib.c,v 1.14 1994/10/19 17:02:20 celes Exp roberto $"; | 6 | char *rcs_iolib="$Id: iolib.c,v 1.15 1994/11/13 14:54:18 roberto Exp roberto $"; |
7 | 7 | ||
8 | #include <stdlib.h> | ||
9 | #include <string.h> | ||
10 | #include <stdio.h> | 8 | #include <stdio.h> |
11 | #include <ctype.h> | 9 | #include <ctype.h> |
12 | #include <time.h> | ||
13 | #include <sys/stat.h> | 10 | #include <sys/stat.h> |
14 | #ifdef __GNUC__ | 11 | #include <string.h> |
15 | #include <floatingpoint.h> | 12 | #include <time.h> |
16 | #endif | 13 | #include <stdlib.h> |
17 | 14 | ||
15 | #include "mem.h" | ||
18 | #include "lua.h" | 16 | #include "lua.h" |
19 | #include "lualib.h" | 17 | #include "lualib.h" |
20 | 18 | ||
@@ -215,7 +213,6 @@ static void io_read (void) | |||
215 | } | 213 | } |
216 | else | 214 | else |
217 | { | 215 | { |
218 | char *ptr; | ||
219 | double d; | 216 | double d; |
220 | ungetc (c, in); | 217 | ungetc (c, in); |
221 | if (fscanf (in, "%s", s) != 1) | 218 | if (fscanf (in, "%s", s) != 1) |
@@ -223,8 +220,7 @@ static void io_read (void) | |||
223 | lua_pushnil (); | 220 | lua_pushnil (); |
224 | return; | 221 | return; |
225 | } | 222 | } |
226 | d = strtod (s, &ptr); | 223 | if (sscanf(s, "%lf %*c", &d) == 1) |
227 | if (!(*ptr)) | ||
228 | { | 224 | { |
229 | lua_pushnumber (d); | 225 | lua_pushnumber (d); |
230 | return; | 226 | return; |
@@ -327,20 +323,20 @@ static void io_readuntil (void) | |||
327 | else | 323 | else |
328 | d = *lua_getstring(lo); | 324 | d = *lua_getstring(lo); |
329 | 325 | ||
330 | s = calloc(n+1, sizeof(char)); | 326 | s = newvector(n+1, char); |
331 | while((c = fgetc(in)) != EOF && c != d) | 327 | while((c = fgetc(in)) != EOF && c != d) |
332 | { | 328 | { |
333 | if (m==n) | 329 | if (m==n) |
334 | { | 330 | { |
335 | n *= 2; | 331 | n *= 2; |
336 | s = realloc(s, (n+1)*sizeof(char)); | 332 | s = growvector(s, n+1, char); |
337 | } | 333 | } |
338 | s[m++] = c; | 334 | s[m++] = c; |
339 | } | 335 | } |
340 | if (c != EOF) ungetc(c,in); | 336 | if (c != EOF) ungetc(c,in); |
341 | s[m] = 0; | 337 | s[m] = 0; |
342 | lua_pushstring(s); | 338 | lua_pushstring(s); |
343 | free(s); | 339 | luaI_free(s); |
344 | } | 340 | } |
345 | 341 | ||
346 | 342 | ||
diff --git a/luamem.c b/luamem.c new file mode 100644 index 00000000..ddbd5e47 --- /dev/null +++ b/luamem.c | |||
@@ -0,0 +1,35 @@ | |||
1 | /* | ||
2 | ** mem.c | ||
3 | ** TecCGraf - PUC-Rio | ||
4 | */ | ||
5 | |||
6 | char *rcs_mem = "$Id: $"; | ||
7 | |||
8 | #include <stdlib.h> | ||
9 | |||
10 | #include "mem.h" | ||
11 | #include "lua.h" | ||
12 | |||
13 | void luaI_free (void *block) | ||
14 | { | ||
15 | free(block); | ||
16 | } | ||
17 | |||
18 | |||
19 | void *luaI_malloc (unsigned long size) | ||
20 | { | ||
21 | void *block = malloc(size); | ||
22 | if (block == NULL) | ||
23 | lua_error("not enough memory"); | ||
24 | return block; | ||
25 | } | ||
26 | |||
27 | |||
28 | void *luaI_realloc (void *oldblock, unsigned long size) | ||
29 | { | ||
30 | void *block = realloc(oldblock, size); | ||
31 | if (block == NULL) | ||
32 | lua_error("not enough memory"); | ||
33 | return block; | ||
34 | } | ||
35 | |||
diff --git a/luamem.h b/luamem.h new file mode 100644 index 00000000..c75dd211 --- /dev/null +++ b/luamem.h | |||
@@ -0,0 +1,23 @@ | |||
1 | /* | ||
2 | ** mem.c | ||
3 | ** memory manager for lua | ||
4 | ** $Id: $ | ||
5 | */ | ||
6 | |||
7 | #ifndef mem_h | ||
8 | #define mem_h | ||
9 | |||
10 | #ifndef NULL | ||
11 | #define NULL 0 | ||
12 | #endif | ||
13 | |||
14 | void luaI_free (void *block); | ||
15 | void *luaI_malloc (unsigned long size); | ||
16 | void *luaI_realloc (void *oldblock, unsigned long size); | ||
17 | |||
18 | #define new(s) ((s *)luaI_malloc(sizeof(s))) | ||
19 | #define newvector(n,s) ((s *)luaI_malloc((n)*sizeof(s))) | ||
20 | #define growvector(old,n,s) ((s *)luaI_realloc(old,(n)*sizeof(s))) | ||
21 | |||
22 | #endif | ||
23 | |||
@@ -3,17 +3,14 @@ | |||
3 | ** TecCGraf - PUC-Rio | 3 | ** TecCGraf - PUC-Rio |
4 | */ | 4 | */ |
5 | 5 | ||
6 | char *rcs_opcode="$Id: opcode.c,v 3.11 1994/11/13 16:17:04 roberto Exp $"; | 6 | char *rcs_opcode="$Id: opcode.c,v 3.12 1994/11/16 16:03:48 roberto Exp roberto $"; |
7 | 7 | ||
8 | #include <setjmp.h> | ||
8 | #include <stdio.h> | 9 | #include <stdio.h> |
9 | #include <stdlib.h> | ||
10 | #include <string.h> | 10 | #include <string.h> |
11 | #include <setjmp.h> | ||
12 | #include <math.h> | 11 | #include <math.h> |
13 | #ifdef __GNUC__ | ||
14 | #include <floatingpoint.h> | ||
15 | #endif | ||
16 | 12 | ||
13 | #include "mem.h" | ||
17 | #include "opcode.h" | 14 | #include "opcode.h" |
18 | #include "hash.h" | 15 | #include "hash.h" |
19 | #include "inout.h" | 16 | #include "inout.h" |
@@ -89,9 +86,7 @@ void lua_error (char *s) | |||
89 | static void lua_initstack (void) | 86 | static void lua_initstack (void) |
90 | { | 87 | { |
91 | maxstack = STACK_BUFFER; | 88 | maxstack = STACK_BUFFER; |
92 | stack = (Object *)calloc(maxstack, sizeof(Object)); | 89 | stack = newvector(maxstack, Object); |
93 | if (stack == NULL) | ||
94 | lua_error("stack - not enough memory"); | ||
95 | top = stack; | 90 | top = stack; |
96 | } | 91 | } |
97 | 92 | ||
@@ -108,9 +103,7 @@ static void lua_checkstack (Word n) | |||
108 | lua_initstack(); | 103 | lua_initstack(); |
109 | t = top-stack; | 104 | t = top-stack; |
110 | maxstack *= 2; | 105 | maxstack *= 2; |
111 | stack = (Object *)realloc(stack, maxstack*sizeof(Object)); | 106 | stack = growvector(stack, maxstack, Object); |
112 | if (stack == NULL) | ||
113 | lua_error("stack - not enough memory"); | ||
114 | top = stack + t; | 107 | top = stack + t; |
115 | } | 108 | } |
116 | } | 109 | } |
@@ -126,16 +119,11 @@ static char *lua_strconc (char *l, char *r) | |||
126 | int nl = strlen(l); | 119 | int nl = strlen(l); |
127 | int n = nl+strlen(r)+1; | 120 | int n = nl+strlen(r)+1; |
128 | if (n > buffer_size) | 121 | if (n > buffer_size) |
129 | { | 122 | { |
130 | buffer_size = n; | 123 | buffer_size = n; |
131 | if (buffer != NULL) | 124 | if (buffer != NULL) |
132 | free(buffer); | 125 | luaI_free(buffer); |
133 | buffer = (char *)malloc(buffer_size); | 126 | buffer = newvector(buffer_size, char); |
134 | if (buffer == NULL) | ||
135 | { | ||
136 | buffer_size = 0; | ||
137 | lua_error("concat - not enough memory"); | ||
138 | } | ||
139 | } | 127 | } |
140 | strcpy(buffer,l); | 128 | strcpy(buffer,l); |
141 | strcpy(buffer+nl, r); | 129 | strcpy(buffer+nl, r); |
@@ -149,11 +137,10 @@ static char *lua_strconc (char *l, char *r) | |||
149 | */ | 137 | */ |
150 | static int lua_tonumber (Object *obj) | 138 | static int lua_tonumber (Object *obj) |
151 | { | 139 | { |
152 | char c; | ||
153 | float t; | 140 | float t; |
154 | if (tag(obj) != LUA_T_STRING) | 141 | if (tag(obj) != LUA_T_STRING) |
155 | return 1; | 142 | return 1; |
156 | else if (sscanf(svalue(obj), "%f %c",&t,&c) == 1) | 143 | else if (sscanf(svalue(obj), "%f %*c",&t) == 1) |
157 | { | 144 | { |
158 | nvalue(obj) = t; | 145 | nvalue(obj) = t; |
159 | tag(obj) = LUA_T_NUMBER; | 146 | tag(obj) = LUA_T_NUMBER; |
@@ -353,7 +340,7 @@ static int do_protectedmain (void) | |||
353 | else | 340 | else |
354 | status = 1; | 341 | status = 1; |
355 | if (code) | 342 | if (code) |
356 | free(code); | 343 | luaI_free(code); |
357 | errorJmp = oldErr; | 344 | errorJmp = oldErr; |
358 | CBase = oldCBase; | 345 | CBase = oldCBase; |
359 | top = stack+CBase; | 346 | top = stack+CBase; |
@@ -467,9 +454,9 @@ int lua_storesubscript (void) | |||
467 | lua_Object lua_createTable (int initSize) | 454 | lua_Object lua_createTable (int initSize) |
468 | { | 455 | { |
469 | adjustC(0); | 456 | adjustC(0); |
457 | tag(top) = LUA_T_ARRAY; | ||
458 | avalue(top) = lua_createarray(initSize); | ||
470 | top++; | 459 | top++; |
471 | tag(top-1) = LUA_T_ARRAY; | ||
472 | avalue(top-1) = lua_createarray(initSize); | ||
473 | CBase++; /* incorporate object in the stack */ | 460 | CBase++; /* incorporate object in the stack */ |
474 | return Ref(top-1); | 461 | return Ref(top-1); |
475 | } | 462 | } |
@@ -540,7 +527,8 @@ void *lua_getuserdata (lua_Object object) | |||
540 | lua_Object lua_getlocked (int ref) | 527 | lua_Object lua_getlocked (int ref) |
541 | { | 528 | { |
542 | adjustC(0); | 529 | adjustC(0); |
543 | *(top++) = *luaI_getlocked(ref); | 530 | *top = *luaI_getlocked(ref); |
531 | top++; | ||
544 | CBase++; /* incorporate object in the stack */ | 532 | CBase++; /* incorporate object in the stack */ |
545 | return Ref(top-1); | 533 | return Ref(top-1); |
546 | } | 534 | } |
@@ -552,7 +540,8 @@ lua_Object lua_getglobal (char *name) | |||
552 | { | 540 | { |
553 | int n = luaI_findsymbolbyname(name); | 541 | int n = luaI_findsymbolbyname(name); |
554 | adjustC(0); | 542 | adjustC(0); |
555 | *(top++) = s_object(n); | 543 | *top = s_object(n); |
544 | top++; | ||
556 | CBase++; /* incorporate object in the stack */ | 545 | CBase++; /* incorporate object in the stack */ |
557 | return Ref(top-1); | 546 | return Ref(top-1); |
558 | } | 547 | } |
@@ -854,9 +843,9 @@ static int lua_execute (Byte *pc, int base) | |||
854 | { | 843 | { |
855 | CodeWord size; | 844 | CodeWord size; |
856 | get_word(size,pc); | 845 | get_word(size,pc); |
846 | tag(top) = LUA_T_ARRAY; | ||
847 | avalue(top) = lua_createarray(size.w); | ||
857 | top++; | 848 | top++; |
858 | tag(top-1) = LUA_T_ARRAY; | ||
859 | avalue(top-1) = lua_createarray(size.w); | ||
860 | } | 849 | } |
861 | break; | 850 | break; |
862 | 851 | ||
@@ -3,12 +3,12 @@ | |||
3 | ** String library to LUA | 3 | ** String library to LUA |
4 | */ | 4 | */ |
5 | 5 | ||
6 | char *rcs_strlib="$Id: strlib.c,v 1.3 1994/08/17 15:10:04 celes Exp roberto $"; | 6 | char *rcs_strlib="$Id: strlib.c,v 1.4 1994/10/18 18:34:47 roberto Exp roberto $"; |
7 | 7 | ||
8 | #include <stdlib.h> | ||
9 | #include <string.h> | 8 | #include <string.h> |
10 | #include <ctype.h> | 9 | #include <ctype.h> |
11 | 10 | ||
11 | #include "mem.h" | ||
12 | #include "lua.h" | 12 | #include "lua.h" |
13 | #include "lualib.h" | 13 | #include "lualib.h" |
14 | 14 | ||
@@ -73,7 +73,7 @@ static void str_sub (void) | |||
73 | s[end] = 0; | 73 | s[end] = 0; |
74 | lua_pushstring (&s[start-1]); | 74 | lua_pushstring (&s[start-1]); |
75 | } | 75 | } |
76 | free (s); | 76 | luaI_free(s); |
77 | } | 77 | } |
78 | 78 | ||
79 | /* | 79 | /* |
@@ -94,7 +94,7 @@ static void str_lower (void) | |||
94 | c++; | 94 | c++; |
95 | } | 95 | } |
96 | lua_pushstring(s); | 96 | lua_pushstring(s); |
97 | free(s); | 97 | luaI_free(s); |
98 | } | 98 | } |
99 | 99 | ||
100 | 100 | ||
@@ -116,7 +116,7 @@ static void str_upper (void) | |||
116 | c++; | 116 | c++; |
117 | } | 117 | } |
118 | lua_pushstring(s); | 118 | lua_pushstring(s); |
119 | free(s); | 119 | luaI_free(s); |
120 | } | 120 | } |
121 | 121 | ||
122 | 122 | ||
@@ -3,11 +3,11 @@ | |||
3 | ** Module to control static tables | 3 | ** Module to control static tables |
4 | */ | 4 | */ |
5 | 5 | ||
6 | char *rcs_table="$Id: table.c,v 2.17 1994/11/14 21:40:14 roberto Exp $"; | 6 | char *rcs_table="$Id: table.c,v 2.18 1994/11/16 16:03:48 roberto Exp roberto $"; |
7 | 7 | ||
8 | #include <stdlib.h> | ||
9 | #include <string.h> | 8 | #include <string.h> |
10 | 9 | ||
10 | #include "mem.h" | ||
11 | #include "opcode.h" | 11 | #include "opcode.h" |
12 | #include "tree.h" | 12 | #include "tree.h" |
13 | #include "hash.h" | 13 | #include "hash.h" |
@@ -16,7 +16,6 @@ char *rcs_table="$Id: table.c,v 2.17 1994/11/14 21:40:14 roberto Exp $"; | |||
16 | #include "lua.h" | 16 | #include "lua.h" |
17 | #include "fallback.h" | 17 | #include "fallback.h" |
18 | 18 | ||
19 | #define streq(s1,s2) (s1[0]==s2[0]&&strcmp(s1+1,s2+1)==0) | ||
20 | 19 | ||
21 | #define BUFFER_BLOCK 256 | 20 | #define BUFFER_BLOCK 256 |
22 | 21 | ||
@@ -50,9 +49,7 @@ static void lua_initsymbol (void) | |||
50 | { | 49 | { |
51 | int n; | 50 | int n; |
52 | lua_maxsymbol = BUFFER_BLOCK; | 51 | lua_maxsymbol = BUFFER_BLOCK; |
53 | lua_table = (Symbol *) calloc(lua_maxsymbol, sizeof(Symbol)); | 52 | lua_table = newvector(lua_maxsymbol, Symbol); |
54 | if (lua_table == NULL) | ||
55 | lua_error ("symbol table: not enough memory"); | ||
56 | n = luaI_findsymbolbyname("next"); | 53 | n = luaI_findsymbolbyname("next"); |
57 | s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_next; | 54 | s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_next; |
58 | n = luaI_findsymbolbyname("nextvar"); | 55 | n = luaI_findsymbolbyname("nextvar"); |
@@ -80,9 +77,7 @@ static void lua_initsymbol (void) | |||
80 | void lua_initconstant (void) | 77 | void lua_initconstant (void) |
81 | { | 78 | { |
82 | lua_maxconstant = BUFFER_BLOCK; | 79 | lua_maxconstant = BUFFER_BLOCK; |
83 | lua_constant = (char **) calloc(lua_maxconstant, sizeof(char *)); | 80 | lua_constant = newvector(lua_maxconstant, char *); |
84 | if (lua_constant == NULL) | ||
85 | lua_error ("constant table: not enough memory"); | ||
86 | } | 81 | } |
87 | 82 | ||
88 | 83 | ||
@@ -102,9 +97,7 @@ int luaI_findsymbol (TreeNode *t) | |||
102 | lua_maxsymbol *= 2; | 97 | lua_maxsymbol *= 2; |
103 | if (lua_maxsymbol > MAX_WORD) | 98 | if (lua_maxsymbol > MAX_WORD) |
104 | lua_error("symbol table overflow"); | 99 | lua_error("symbol table overflow"); |
105 | lua_table = (Symbol *)realloc(lua_table, lua_maxsymbol*sizeof(Symbol)); | 100 | lua_table = growvector(lua_table, lua_maxsymbol, Symbol); |
106 | if (lua_table == NULL) | ||
107 | lua_error ("symbol table: not enough memory"); | ||
108 | } | 101 | } |
109 | t->varindex = lua_ntable; | 102 | t->varindex = lua_ntable; |
110 | s_tag(lua_ntable) = LUA_T_NIL; | 103 | s_tag(lua_ntable) = LUA_T_NIL; |
@@ -136,9 +129,7 @@ int luaI_findconstant (TreeNode *t) | |||
136 | lua_maxconstant *= 2; | 129 | lua_maxconstant *= 2; |
137 | if (lua_maxconstant > MAX_WORD) | 130 | if (lua_maxconstant > MAX_WORD) |
138 | lua_error("constant table overflow"); | 131 | lua_error("constant table overflow"); |
139 | lua_constant = (char**)realloc(lua_constant,lua_maxconstant*sizeof(char*)); | 132 | lua_constant = growvector(lua_constant, lua_maxconstant, char*); |
140 | if (lua_constant == NULL) | ||
141 | lua_error ("constant table: not enough memory"); | ||
142 | } | 133 | } |
143 | t->constindex = lua_nconstant; | 134 | t->constindex = lua_nconstant; |
144 | lua_constant[lua_nconstant] = t->str; | 135 | lua_constant[lua_nconstant] = t->str; |
@@ -202,7 +193,6 @@ void lua_pack (void) | |||
202 | char *lua_createstring (char *s) | 193 | char *lua_createstring (char *s) |
203 | { | 194 | { |
204 | if (s == NULL) return NULL; | 195 | if (s == NULL) return NULL; |
205 | |||
206 | return lua_strcreate(s); | 196 | return lua_strcreate(s); |
207 | } | 197 | } |
208 | 198 | ||
@@ -226,7 +216,7 @@ char *lua_addfile (char *fn) | |||
226 | */ | 216 | */ |
227 | int lua_delfile (void) | 217 | int lua_delfile (void) |
228 | { | 218 | { |
229 | free(lua_file[--lua_nfile]); | 219 | luaI_free(lua_file[--lua_nfile]); |
230 | return 1; | 220 | return 1; |
231 | } | 221 | } |
232 | 222 | ||
@@ -3,12 +3,12 @@ | |||
3 | ** TecCGraf - PUC-Rio | 3 | ** TecCGraf - PUC-Rio |
4 | */ | 4 | */ |
5 | 5 | ||
6 | char *rcs_tree="$Id: tree.c,v 1.4 1994/11/14 21:40:14 roberto Exp roberto $"; | 6 | char *rcs_tree="$Id: tree.c,v 1.5 1994/11/16 16:03:48 roberto Exp roberto $"; |
7 | 7 | ||
8 | 8 | ||
9 | #include <stdlib.h> | ||
10 | #include <string.h> | 9 | #include <string.h> |
11 | 10 | ||
11 | #include "mem.h" | ||
12 | #include "lua.h" | 12 | #include "lua.h" |
13 | #include "tree.h" | 13 | #include "tree.h" |
14 | #include "table.h" | 14 | #include "table.h" |
@@ -27,9 +27,7 @@ static TreeNode *tree_create (TreeNode **node, char *str, int *created) | |||
27 | { | 27 | { |
28 | if (*node == NULL) | 28 | if (*node == NULL) |
29 | { | 29 | { |
30 | *node = (TreeNode *) malloc (sizeof(TreeNode)+strlen(str)); | 30 | *node = (TreeNode *) luaI_malloc(sizeof(TreeNode)+strlen(str)); |
31 | if (*node == NULL) | ||
32 | lua_error("not enough memory"); | ||
33 | (*node)->left = (*node)->right = NULL; | 31 | (*node)->left = (*node)->right = NULL; |
34 | strcpy((*node)->str, str); | 32 | strcpy((*node)->str, str); |
35 | (*node)->varindex = (*node)->constindex = UNMARKED_STRING; | 33 | (*node)->varindex = (*node)->constindex = UNMARKED_STRING; |
@@ -74,19 +72,19 @@ static TreeNode *lua_strfree (TreeNode *parent) | |||
74 | { | 72 | { |
75 | if (parent->left == NULL && parent->right == NULL) /* no child */ | 73 | if (parent->left == NULL && parent->right == NULL) /* no child */ |
76 | { | 74 | { |
77 | free (parent); | 75 | luaI_free(parent); |
78 | return NULL; | 76 | return NULL; |
79 | } | 77 | } |
80 | else if (parent->left == NULL) /* only right child */ | 78 | else if (parent->left == NULL) /* only right child */ |
81 | { | 79 | { |
82 | TreeNode *p = parent->right; | 80 | TreeNode *p = parent->right; |
83 | free (parent); | 81 | luaI_free(parent); |
84 | return p; | 82 | return p; |
85 | } | 83 | } |
86 | else if (parent->right == NULL) /* only left child */ | 84 | else if (parent->right == NULL) /* only left child */ |
87 | { | 85 | { |
88 | TreeNode *p = parent->left; | 86 | TreeNode *p = parent->left; |
89 | free (parent); | 87 | luaI_free(parent); |
90 | return p; | 88 | return p; |
91 | } | 89 | } |
92 | else /* two children */ | 90 | else /* two children */ |