new module for memory allocation

This commit is contained in:
Roberto Ierusalimschy 1994-11-16 15:39:16 -02:00
parent 94686ce585
commit 2b5bc5d1a8
9 changed files with 114 additions and 100 deletions

View File

@ -3,11 +3,11 @@
** TecCGraf - PUC-Rio ** TecCGraf - PUC-Rio
*/ */
char *rcs_fallback="$Id: fallback.c,v 1.4 1994/11/10 17:11:52 roberto Exp roberto $"; char *rcs_fallback="$Id: fallback.c,v 1.5 1994/11/10 17:36:54 roberto Exp roberto $";
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include "mem.h"
#include "fallback.h" #include "fallback.h"
#include "opcode.h" #include "opcode.h"
#include "inout.h" #include "inout.h"
@ -129,17 +129,12 @@ int lua_lock (lua_Object object)
if (lockArray == NULL) if (lockArray == NULL)
{ {
lockSize = 10; lockSize = 10;
lockArray = (Object *)malloc(lockSize); lockArray = newvector(lockSize, Object);
} }
else else
{ {
lockSize = 3*oldSize/2 + 5; lockSize = 3*oldSize/2 + 5;
lockArray = (Object *)realloc(lockArray, lockSize); lockArray = growvector(lockArray, lockSize, Object);
}
if (lockArray == NULL)
{
lockSize = 0;
lua_error("lock - not enough memory");
} }
for (i=oldSize; i<lockSize; i++) for (i=oldSize; i<lockSize; i++)
tag(&lockArray[i]) = LUA_T_NIL; tag(&lockArray[i]) = LUA_T_NIL;

24
hash.c
View File

@ -3,11 +3,9 @@
** hash manager for lua ** hash manager for lua
*/ */
char *rcs_hash="$Id: hash.c,v 2.15 1994/11/10 17:36:54 roberto Exp $"; char *rcs_hash="$Id: hash.c,v 2.16 1994/11/14 18:41:15 roberto Exp roberto $";
#include <string.h>
#include <stdlib.h>
#include "mem.h"
#include "opcode.h" #include "opcode.h"
#include "hash.h" #include "hash.h"
#include "inout.h" #include "inout.h"
@ -16,9 +14,6 @@ char *rcs_hash="$Id: hash.c,v 2.15 1994/11/10 17:36:54 roberto Exp $";
#define streq(s1,s2) (s1 == s2 || (*(s1) == *(s2) && strcmp(s1,s2)==0)) #define streq(s1,s2) (s1 == s2 || (*(s1) == *(s2) && strcmp(s1,s2)==0))
#define new(s) ((s *)malloc(sizeof(s)))
#define newvector(n,s) ((s *)calloc(n,sizeof(s)))
#define nhash(t) ((t)->nhash) #define nhash(t) ((t)->nhash)
#define nuse(t) ((t)->nuse) #define nuse(t) ((t)->nuse)
#define markarray(t) ((t)->mark) #define markarray(t) ((t)->mark)
@ -117,8 +112,6 @@ static Node *hashnodecreate (int nhash)
{ {
int i; int i;
Node *v = newvector (nhash, Node); Node *v = newvector (nhash, Node);
if (v == NULL)
lua_error ("not enough memory");
for (i=0; i<nhash; i++) for (i=0; i<nhash; i++)
tag(ref(&v[i])) = LUA_T_NIL; tag(ref(&v[i])) = LUA_T_NIL;
return v; return v;
@ -129,14 +122,9 @@ static Node *hashnodecreate (int nhash)
*/ */
static Hash *hashcreate (int nhash) static Hash *hashcreate (int nhash)
{ {
Hash *t = new (Hash); Hash *t = new(Hash);
if (t == NULL)
lua_error ("not enough memory");
nhash = redimension((int)((float)nhash/REHASH_LIMIT)); nhash = redimension((int)((float)nhash/REHASH_LIMIT));
nodevector(t) = hashnodecreate(nhash); nodevector(t) = hashnodecreate(nhash);
if (nodevector(t) == NULL)
lua_error ("not enough memory");
nhash(t) = nhash; nhash(t) = nhash;
nuse(t) = 0; nuse(t) = 0;
markarray(t) = 0; markarray(t) = 0;
@ -148,8 +136,8 @@ static Hash *hashcreate (int nhash)
*/ */
static void hashdelete (Hash *t) static void hashdelete (Hash *t)
{ {
free (nodevector(t)); luaI_free (nodevector(t));
free(t); luaI_free(t);
} }
@ -253,7 +241,7 @@ static void rehash (Hash *t)
if (tag(ref(n)) != LUA_T_NIL && tag(val(n)) != LUA_T_NIL) if (tag(ref(n)) != LUA_T_NIL && tag(val(n)) != LUA_T_NIL)
*node(t, present(t, ref(n))) = *n; /* copy old node to new hahs */ *node(t, present(t, ref(n))) = *n; /* copy old node to new hahs */
} }
free(vold); luaI_free(vold);
} }
/* /*

22
iolib.c
View File

@ -3,18 +3,16 @@
** Input/output library to LUA ** Input/output library to LUA
*/ */
char *rcs_iolib="$Id: iolib.c,v 1.14 1994/10/19 17:02:20 celes Exp roberto $"; char *rcs_iolib="$Id: iolib.c,v 1.15 1994/11/13 14:54:18 roberto Exp roberto $";
#include <stdlib.h>
#include <string.h>
#include <stdio.h> #include <stdio.h>
#include <ctype.h> #include <ctype.h>
#include <time.h>
#include <sys/stat.h> #include <sys/stat.h>
#ifdef __GNUC__ #include <string.h>
#include <floatingpoint.h> #include <time.h>
#endif #include <stdlib.h>
#include "mem.h"
#include "lua.h" #include "lua.h"
#include "lualib.h" #include "lualib.h"
@ -215,7 +213,6 @@ static void io_read (void)
} }
else else
{ {
char *ptr;
double d; double d;
ungetc (c, in); ungetc (c, in);
if (fscanf (in, "%s", s) != 1) if (fscanf (in, "%s", s) != 1)
@ -223,8 +220,7 @@ static void io_read (void)
lua_pushnil (); lua_pushnil ();
return; return;
} }
d = strtod (s, &ptr); if (sscanf(s, "%lf %*c", &d) == 1)
if (!(*ptr))
{ {
lua_pushnumber (d); lua_pushnumber (d);
return; return;
@ -327,20 +323,20 @@ static void io_readuntil (void)
else else
d = *lua_getstring(lo); d = *lua_getstring(lo);
s = calloc(n+1, sizeof(char)); s = newvector(n+1, char);
while((c = fgetc(in)) != EOF && c != d) while((c = fgetc(in)) != EOF && c != d)
{ {
if (m==n) if (m==n)
{ {
n *= 2; n *= 2;
s = realloc(s, (n+1)*sizeof(char)); s = growvector(s, n+1, char);
} }
s[m++] = c; s[m++] = c;
} }
if (c != EOF) ungetc(c,in); if (c != EOF) ungetc(c,in);
s[m] = 0; s[m] = 0;
lua_pushstring(s); lua_pushstring(s);
free(s); luaI_free(s);
} }

35
luamem.c Normal file
View File

@ -0,0 +1,35 @@
/*
** mem.c
** TecCGraf - PUC-Rio
*/
char *rcs_mem = "$Id: $";
#include <stdlib.h>
#include "mem.h"
#include "lua.h"
void luaI_free (void *block)
{
free(block);
}
void *luaI_malloc (unsigned long size)
{
void *block = malloc(size);
if (block == NULL)
lua_error("not enough memory");
return block;
}
void *luaI_realloc (void *oldblock, unsigned long size)
{
void *block = realloc(oldblock, size);
if (block == NULL)
lua_error("not enough memory");
return block;
}

23
luamem.h Normal file
View File

@ -0,0 +1,23 @@
/*
** mem.c
** memory manager for lua
** $Id: $
*/
#ifndef mem_h
#define mem_h
#ifndef NULL
#define NULL 0
#endif
void luaI_free (void *block);
void *luaI_malloc (unsigned long size);
void *luaI_realloc (void *oldblock, unsigned long size);
#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)))
#endif

View File

@ -3,17 +3,14 @@
** TecCGraf - PUC-Rio ** TecCGraf - PUC-Rio
*/ */
char *rcs_opcode="$Id: opcode.c,v 3.11 1994/11/13 16:17:04 roberto Exp $"; char *rcs_opcode="$Id: opcode.c,v 3.12 1994/11/16 16:03:48 roberto Exp roberto $";
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <setjmp.h> #include <setjmp.h>
#include <stdio.h>
#include <string.h>
#include <math.h> #include <math.h>
#ifdef __GNUC__
#include <floatingpoint.h>
#endif
#include "mem.h"
#include "opcode.h" #include "opcode.h"
#include "hash.h" #include "hash.h"
#include "inout.h" #include "inout.h"
@ -89,9 +86,7 @@ void lua_error (char *s)
static void lua_initstack (void) static void lua_initstack (void)
{ {
maxstack = STACK_BUFFER; maxstack = STACK_BUFFER;
stack = (Object *)calloc(maxstack, sizeof(Object)); stack = newvector(maxstack, Object);
if (stack == NULL)
lua_error("stack - not enough memory");
top = stack; top = stack;
} }
@ -108,9 +103,7 @@ static void lua_checkstack (Word n)
lua_initstack(); lua_initstack();
t = top-stack; t = top-stack;
maxstack *= 2; maxstack *= 2;
stack = (Object *)realloc(stack, maxstack*sizeof(Object)); stack = growvector(stack, maxstack, Object);
if (stack == NULL)
lua_error("stack - not enough memory");
top = stack + t; top = stack + t;
} }
} }
@ -126,16 +119,11 @@ static char *lua_strconc (char *l, char *r)
int nl = strlen(l); int nl = strlen(l);
int n = nl+strlen(r)+1; int n = nl+strlen(r)+1;
if (n > buffer_size) if (n > buffer_size)
{ {
buffer_size = n; buffer_size = n;
if (buffer != NULL) if (buffer != NULL)
free(buffer); luaI_free(buffer);
buffer = (char *)malloc(buffer_size); buffer = newvector(buffer_size, char);
if (buffer == NULL)
{
buffer_size = 0;
lua_error("concat - not enough memory");
}
} }
strcpy(buffer,l); strcpy(buffer,l);
strcpy(buffer+nl, r); strcpy(buffer+nl, r);
@ -149,11 +137,10 @@ static char *lua_strconc (char *l, char *r)
*/ */
static int lua_tonumber (Object *obj) static int lua_tonumber (Object *obj)
{ {
char c;
float t; float t;
if (tag(obj) != LUA_T_STRING) if (tag(obj) != LUA_T_STRING)
return 1; return 1;
else if (sscanf(svalue(obj), "%f %c",&t,&c) == 1) else if (sscanf(svalue(obj), "%f %*c",&t) == 1)
{ {
nvalue(obj) = t; nvalue(obj) = t;
tag(obj) = LUA_T_NUMBER; tag(obj) = LUA_T_NUMBER;
@ -353,7 +340,7 @@ static int do_protectedmain (void)
else else
status = 1; status = 1;
if (code) if (code)
free(code); luaI_free(code);
errorJmp = oldErr; errorJmp = oldErr;
CBase = oldCBase; CBase = oldCBase;
top = stack+CBase; top = stack+CBase;
@ -467,9 +454,9 @@ int lua_storesubscript (void)
lua_Object lua_createTable (int initSize) lua_Object lua_createTable (int initSize)
{ {
adjustC(0); adjustC(0);
tag(top) = LUA_T_ARRAY;
avalue(top) = lua_createarray(initSize);
top++; top++;
tag(top-1) = LUA_T_ARRAY;
avalue(top-1) = lua_createarray(initSize);
CBase++; /* incorporate object in the stack */ CBase++; /* incorporate object in the stack */
return Ref(top-1); return Ref(top-1);
} }
@ -540,7 +527,8 @@ void *lua_getuserdata (lua_Object object)
lua_Object lua_getlocked (int ref) lua_Object lua_getlocked (int ref)
{ {
adjustC(0); adjustC(0);
*(top++) = *luaI_getlocked(ref); *top = *luaI_getlocked(ref);
top++;
CBase++; /* incorporate object in the stack */ CBase++; /* incorporate object in the stack */
return Ref(top-1); return Ref(top-1);
} }
@ -552,7 +540,8 @@ lua_Object lua_getglobal (char *name)
{ {
int n = luaI_findsymbolbyname(name); int n = luaI_findsymbolbyname(name);
adjustC(0); adjustC(0);
*(top++) = s_object(n); *top = s_object(n);
top++;
CBase++; /* incorporate object in the stack */ CBase++; /* incorporate object in the stack */
return Ref(top-1); return Ref(top-1);
} }
@ -854,9 +843,9 @@ static int lua_execute (Byte *pc, int base)
{ {
CodeWord size; CodeWord size;
get_word(size,pc); get_word(size,pc);
tag(top) = LUA_T_ARRAY;
avalue(top) = lua_createarray(size.w);
top++; top++;
tag(top-1) = LUA_T_ARRAY;
avalue(top-1) = lua_createarray(size.w);
} }
break; break;

View File

@ -3,12 +3,12 @@
** String library to LUA ** String library to LUA
*/ */
char *rcs_strlib="$Id: strlib.c,v 1.3 1994/08/17 15:10:04 celes Exp roberto $"; char *rcs_strlib="$Id: strlib.c,v 1.4 1994/10/18 18:34:47 roberto Exp roberto $";
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <ctype.h> #include <ctype.h>
#include "mem.h"
#include "lua.h" #include "lua.h"
#include "lualib.h" #include "lualib.h"
@ -73,7 +73,7 @@ static void str_sub (void)
s[end] = 0; s[end] = 0;
lua_pushstring (&s[start-1]); lua_pushstring (&s[start-1]);
} }
free (s); luaI_free(s);
} }
/* /*
@ -94,7 +94,7 @@ static void str_lower (void)
c++; c++;
} }
lua_pushstring(s); lua_pushstring(s);
free(s); luaI_free(s);
} }
@ -116,7 +116,7 @@ static void str_upper (void)
c++; c++;
} }
lua_pushstring(s); lua_pushstring(s);
free(s); luaI_free(s);
} }

24
table.c
View File

@ -3,11 +3,11 @@
** Module to control static tables ** Module to control static tables
*/ */
char *rcs_table="$Id: table.c,v 2.17 1994/11/14 21:40:14 roberto Exp $"; char *rcs_table="$Id: table.c,v 2.18 1994/11/16 16:03:48 roberto Exp roberto $";
#include <stdlib.h>
#include <string.h> #include <string.h>
#include "mem.h"
#include "opcode.h" #include "opcode.h"
#include "tree.h" #include "tree.h"
#include "hash.h" #include "hash.h"
@ -16,7 +16,6 @@ char *rcs_table="$Id: table.c,v 2.17 1994/11/14 21:40:14 roberto Exp $";
#include "lua.h" #include "lua.h"
#include "fallback.h" #include "fallback.h"
#define streq(s1,s2) (s1[0]==s2[0]&&strcmp(s1+1,s2+1)==0)
#define BUFFER_BLOCK 256 #define BUFFER_BLOCK 256
@ -50,9 +49,7 @@ static void lua_initsymbol (void)
{ {
int n; int n;
lua_maxsymbol = BUFFER_BLOCK; lua_maxsymbol = BUFFER_BLOCK;
lua_table = (Symbol *) calloc(lua_maxsymbol, sizeof(Symbol)); lua_table = newvector(lua_maxsymbol, Symbol);
if (lua_table == NULL)
lua_error ("symbol table: not enough memory");
n = luaI_findsymbolbyname("next"); n = luaI_findsymbolbyname("next");
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_next; s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_next;
n = luaI_findsymbolbyname("nextvar"); n = luaI_findsymbolbyname("nextvar");
@ -80,9 +77,7 @@ static void lua_initsymbol (void)
void lua_initconstant (void) void lua_initconstant (void)
{ {
lua_maxconstant = BUFFER_BLOCK; lua_maxconstant = BUFFER_BLOCK;
lua_constant = (char **) calloc(lua_maxconstant, sizeof(char *)); lua_constant = newvector(lua_maxconstant, char *);
if (lua_constant == NULL)
lua_error ("constant table: not enough memory");
} }
@ -102,9 +97,7 @@ int luaI_findsymbol (TreeNode *t)
lua_maxsymbol *= 2; lua_maxsymbol *= 2;
if (lua_maxsymbol > MAX_WORD) if (lua_maxsymbol > MAX_WORD)
lua_error("symbol table overflow"); lua_error("symbol table overflow");
lua_table = (Symbol *)realloc(lua_table, lua_maxsymbol*sizeof(Symbol)); lua_table = growvector(lua_table, lua_maxsymbol, Symbol);
if (lua_table == NULL)
lua_error ("symbol table: not enough memory");
} }
t->varindex = lua_ntable; t->varindex = lua_ntable;
s_tag(lua_ntable) = LUA_T_NIL; s_tag(lua_ntable) = LUA_T_NIL;
@ -136,9 +129,7 @@ int luaI_findconstant (TreeNode *t)
lua_maxconstant *= 2; lua_maxconstant *= 2;
if (lua_maxconstant > MAX_WORD) if (lua_maxconstant > MAX_WORD)
lua_error("constant table overflow"); lua_error("constant table overflow");
lua_constant = (char**)realloc(lua_constant,lua_maxconstant*sizeof(char*)); lua_constant = growvector(lua_constant, lua_maxconstant, char*);
if (lua_constant == NULL)
lua_error ("constant table: not enough memory");
} }
t->constindex = lua_nconstant; t->constindex = lua_nconstant;
lua_constant[lua_nconstant] = t->str; lua_constant[lua_nconstant] = t->str;
@ -202,7 +193,6 @@ void lua_pack (void)
char *lua_createstring (char *s) char *lua_createstring (char *s)
{ {
if (s == NULL) return NULL; if (s == NULL) return NULL;
return lua_strcreate(s); return lua_strcreate(s);
} }
@ -226,7 +216,7 @@ char *lua_addfile (char *fn)
*/ */
int lua_delfile (void) int lua_delfile (void)
{ {
free(lua_file[--lua_nfile]); luaI_free(lua_file[--lua_nfile]);
return 1; return 1;
} }

14
tree.c
View File

@ -3,12 +3,12 @@
** TecCGraf - PUC-Rio ** TecCGraf - PUC-Rio
*/ */
char *rcs_tree="$Id: tree.c,v 1.4 1994/11/14 21:40:14 roberto Exp roberto $"; char *rcs_tree="$Id: tree.c,v 1.5 1994/11/16 16:03:48 roberto Exp roberto $";
#include <stdlib.h>
#include <string.h> #include <string.h>
#include "mem.h"
#include "lua.h" #include "lua.h"
#include "tree.h" #include "tree.h"
#include "table.h" #include "table.h"
@ -27,9 +27,7 @@ static TreeNode *tree_create (TreeNode **node, char *str, int *created)
{ {
if (*node == NULL) if (*node == NULL)
{ {
*node = (TreeNode *) malloc (sizeof(TreeNode)+strlen(str)); *node = (TreeNode *) luaI_malloc(sizeof(TreeNode)+strlen(str));
if (*node == NULL)
lua_error("not enough memory");
(*node)->left = (*node)->right = NULL; (*node)->left = (*node)->right = NULL;
strcpy((*node)->str, str); strcpy((*node)->str, str);
(*node)->varindex = (*node)->constindex = UNMARKED_STRING; (*node)->varindex = (*node)->constindex = UNMARKED_STRING;
@ -74,19 +72,19 @@ static TreeNode *lua_strfree (TreeNode *parent)
{ {
if (parent->left == NULL && parent->right == NULL) /* no child */ if (parent->left == NULL && parent->right == NULL) /* no child */
{ {
free (parent); luaI_free(parent);
return NULL; return NULL;
} }
else if (parent->left == NULL) /* only right child */ else if (parent->left == NULL) /* only right child */
{ {
TreeNode *p = parent->right; TreeNode *p = parent->right;
free (parent); luaI_free(parent);
return p; return p;
} }
else if (parent->right == NULL) /* only left child */ else if (parent->right == NULL) /* only left child */
{ {
TreeNode *p = parent->left; TreeNode *p = parent->left;
free (parent); luaI_free(parent);
return p; return p;
} }
else /* two children */ else /* two children */