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
*/
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 <stdlib.h>
#include "mem.h"
#include "fallback.h"
#include "opcode.h"
#include "inout.h"
@ -129,17 +129,12 @@ int lua_lock (lua_Object object)
if (lockArray == NULL)
{
lockSize = 10;
lockArray = (Object *)malloc(lockSize);
lockArray = newvector(lockSize, Object);
}
else
{
lockSize = 3*oldSize/2 + 5;
lockArray = (Object *)realloc(lockArray, lockSize);
}
if (lockArray == NULL)
{
lockSize = 0;
lua_error("lock - not enough memory");
lockArray = growvector(lockArray, lockSize, Object);
}
for (i=oldSize; i<lockSize; i++)
tag(&lockArray[i]) = LUA_T_NIL;

24
hash.c
View File

@ -3,11 +3,9 @@
** hash manager for lua
*/
char *rcs_hash="$Id: hash.c,v 2.15 1994/11/10 17:36:54 roberto Exp $";
#include <string.h>
#include <stdlib.h>
char *rcs_hash="$Id: hash.c,v 2.16 1994/11/14 18:41:15 roberto Exp roberto $";
#include "mem.h"
#include "opcode.h"
#include "hash.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 new(s) ((s *)malloc(sizeof(s)))
#define newvector(n,s) ((s *)calloc(n,sizeof(s)))
#define nhash(t) ((t)->nhash)
#define nuse(t) ((t)->nuse)
#define markarray(t) ((t)->mark)
@ -117,8 +112,6 @@ static Node *hashnodecreate (int nhash)
{
int i;
Node *v = newvector (nhash, Node);
if (v == NULL)
lua_error ("not enough memory");
for (i=0; i<nhash; i++)
tag(ref(&v[i])) = LUA_T_NIL;
return v;
@ -129,14 +122,9 @@ static Node *hashnodecreate (int nhash)
*/
static Hash *hashcreate (int nhash)
{
Hash *t = new (Hash);
if (t == NULL)
lua_error ("not enough memory");
Hash *t = new(Hash);
nhash = redimension((int)((float)nhash/REHASH_LIMIT));
nodevector(t) = hashnodecreate(nhash);
if (nodevector(t) == NULL)
lua_error ("not enough memory");
nhash(t) = nhash;
nuse(t) = 0;
markarray(t) = 0;
@ -148,8 +136,8 @@ static Hash *hashcreate (int nhash)
*/
static void hashdelete (Hash *t)
{
free (nodevector(t));
free(t);
luaI_free (nodevector(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)
*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
*/
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 <ctype.h>
#include <time.h>
#include <sys/stat.h>
#ifdef __GNUC__
#include <floatingpoint.h>
#endif
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include "mem.h"
#include "lua.h"
#include "lualib.h"
@ -215,7 +213,6 @@ static void io_read (void)
}
else
{
char *ptr;
double d;
ungetc (c, in);
if (fscanf (in, "%s", s) != 1)
@ -223,8 +220,7 @@ static void io_read (void)
lua_pushnil ();
return;
}
d = strtod (s, &ptr);
if (!(*ptr))
if (sscanf(s, "%lf %*c", &d) == 1)
{
lua_pushnumber (d);
return;
@ -327,20 +323,20 @@ static void io_readuntil (void)
else
d = *lua_getstring(lo);
s = calloc(n+1, sizeof(char));
s = newvector(n+1, char);
while((c = fgetc(in)) != EOF && c != d)
{
if (m==n)
{
n *= 2;
s = realloc(s, (n+1)*sizeof(char));
s = growvector(s, n+1, char);
}
s[m++] = c;
}
if (c != EOF) ungetc(c,in);
s[m] = 0;
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
*/
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 <stdio.h>
#include <string.h>
#include <math.h>
#ifdef __GNUC__
#include <floatingpoint.h>
#endif
#include "mem.h"
#include "opcode.h"
#include "hash.h"
#include "inout.h"
@ -89,9 +86,7 @@ void lua_error (char *s)
static void lua_initstack (void)
{
maxstack = STACK_BUFFER;
stack = (Object *)calloc(maxstack, sizeof(Object));
if (stack == NULL)
lua_error("stack - not enough memory");
stack = newvector(maxstack, Object);
top = stack;
}
@ -108,9 +103,7 @@ static void lua_checkstack (Word n)
lua_initstack();
t = top-stack;
maxstack *= 2;
stack = (Object *)realloc(stack, maxstack*sizeof(Object));
if (stack == NULL)
lua_error("stack - not enough memory");
stack = growvector(stack, maxstack, Object);
top = stack + t;
}
}
@ -126,16 +119,11 @@ static char *lua_strconc (char *l, char *r)
int nl = strlen(l);
int n = nl+strlen(r)+1;
if (n > buffer_size)
{
{
buffer_size = n;
if (buffer != NULL)
free(buffer);
buffer = (char *)malloc(buffer_size);
if (buffer == NULL)
{
buffer_size = 0;
lua_error("concat - not enough memory");
}
luaI_free(buffer);
buffer = newvector(buffer_size, char);
}
strcpy(buffer,l);
strcpy(buffer+nl, r);
@ -149,11 +137,10 @@ static char *lua_strconc (char *l, char *r)
*/
static int lua_tonumber (Object *obj)
{
char c;
float t;
if (tag(obj) != LUA_T_STRING)
return 1;
else if (sscanf(svalue(obj), "%f %c",&t,&c) == 1)
else if (sscanf(svalue(obj), "%f %*c",&t) == 1)
{
nvalue(obj) = t;
tag(obj) = LUA_T_NUMBER;
@ -353,7 +340,7 @@ static int do_protectedmain (void)
else
status = 1;
if (code)
free(code);
luaI_free(code);
errorJmp = oldErr;
CBase = oldCBase;
top = stack+CBase;
@ -467,9 +454,9 @@ int lua_storesubscript (void)
lua_Object lua_createTable (int initSize)
{
adjustC(0);
tag(top) = LUA_T_ARRAY;
avalue(top) = lua_createarray(initSize);
top++;
tag(top-1) = LUA_T_ARRAY;
avalue(top-1) = lua_createarray(initSize);
CBase++; /* incorporate object in the stack */
return Ref(top-1);
}
@ -540,7 +527,8 @@ void *lua_getuserdata (lua_Object object)
lua_Object lua_getlocked (int ref)
{
adjustC(0);
*(top++) = *luaI_getlocked(ref);
*top = *luaI_getlocked(ref);
top++;
CBase++; /* incorporate object in the stack */
return Ref(top-1);
}
@ -552,7 +540,8 @@ lua_Object lua_getglobal (char *name)
{
int n = luaI_findsymbolbyname(name);
adjustC(0);
*(top++) = s_object(n);
*top = s_object(n);
top++;
CBase++; /* incorporate object in the stack */
return Ref(top-1);
}
@ -854,9 +843,9 @@ static int lua_execute (Byte *pc, int base)
{
CodeWord size;
get_word(size,pc);
tag(top) = LUA_T_ARRAY;
avalue(top) = lua_createarray(size.w);
top++;
tag(top-1) = LUA_T_ARRAY;
avalue(top-1) = lua_createarray(size.w);
}
break;

View File

@ -3,12 +3,12 @@
** 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 <ctype.h>
#include "mem.h"
#include "lua.h"
#include "lualib.h"
@ -73,7 +73,7 @@ static void str_sub (void)
s[end] = 0;
lua_pushstring (&s[start-1]);
}
free (s);
luaI_free(s);
}
/*
@ -94,7 +94,7 @@ static void str_lower (void)
c++;
}
lua_pushstring(s);
free(s);
luaI_free(s);
}
@ -116,7 +116,7 @@ static void str_upper (void)
c++;
}
lua_pushstring(s);
free(s);
luaI_free(s);
}

24
table.c
View File

@ -3,11 +3,11 @@
** 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 "mem.h"
#include "opcode.h"
#include "tree.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 "fallback.h"
#define streq(s1,s2) (s1[0]==s2[0]&&strcmp(s1+1,s2+1)==0)
#define BUFFER_BLOCK 256
@ -50,9 +49,7 @@ static void lua_initsymbol (void)
{
int n;
lua_maxsymbol = BUFFER_BLOCK;
lua_table = (Symbol *) calloc(lua_maxsymbol, sizeof(Symbol));
if (lua_table == NULL)
lua_error ("symbol table: not enough memory");
lua_table = newvector(lua_maxsymbol, Symbol);
n = luaI_findsymbolbyname("next");
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_next;
n = luaI_findsymbolbyname("nextvar");
@ -80,9 +77,7 @@ static void lua_initsymbol (void)
void lua_initconstant (void)
{
lua_maxconstant = BUFFER_BLOCK;
lua_constant = (char **) calloc(lua_maxconstant, sizeof(char *));
if (lua_constant == NULL)
lua_error ("constant table: not enough memory");
lua_constant = newvector(lua_maxconstant, char *);
}
@ -102,9 +97,7 @@ int luaI_findsymbol (TreeNode *t)
lua_maxsymbol *= 2;
if (lua_maxsymbol > MAX_WORD)
lua_error("symbol table overflow");
lua_table = (Symbol *)realloc(lua_table, lua_maxsymbol*sizeof(Symbol));
if (lua_table == NULL)
lua_error ("symbol table: not enough memory");
lua_table = growvector(lua_table, lua_maxsymbol, Symbol);
}
t->varindex = lua_ntable;
s_tag(lua_ntable) = LUA_T_NIL;
@ -136,9 +129,7 @@ int luaI_findconstant (TreeNode *t)
lua_maxconstant *= 2;
if (lua_maxconstant > MAX_WORD)
lua_error("constant table overflow");
lua_constant = (char**)realloc(lua_constant,lua_maxconstant*sizeof(char*));
if (lua_constant == NULL)
lua_error ("constant table: not enough memory");
lua_constant = growvector(lua_constant, lua_maxconstant, char*);
}
t->constindex = lua_nconstant;
lua_constant[lua_nconstant] = t->str;
@ -202,7 +193,6 @@ void lua_pack (void)
char *lua_createstring (char *s)
{
if (s == NULL) return NULL;
return lua_strcreate(s);
}
@ -226,7 +216,7 @@ char *lua_addfile (char *fn)
*/
int lua_delfile (void)
{
free(lua_file[--lua_nfile]);
luaI_free(lua_file[--lua_nfile]);
return 1;
}

14
tree.c
View File

@ -3,12 +3,12 @@
** 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 "mem.h"
#include "lua.h"
#include "tree.h"
#include "table.h"
@ -27,9 +27,7 @@ static TreeNode *tree_create (TreeNode **node, char *str, int *created)
{
if (*node == NULL)
{
*node = (TreeNode *) malloc (sizeof(TreeNode)+strlen(str));
if (*node == NULL)
lua_error("not enough memory");
*node = (TreeNode *) luaI_malloc(sizeof(TreeNode)+strlen(str));
(*node)->left = (*node)->right = NULL;
strcpy((*node)->str, str);
(*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 */
{
free (parent);
luaI_free(parent);
return NULL;
}
else if (parent->left == NULL) /* only right child */
{
TreeNode *p = parent->right;
free (parent);
luaI_free(parent);
return p;
}
else if (parent->right == NULL) /* only left child */
{
TreeNode *p = parent->left;
free (parent);
luaI_free(parent);
return p;
}
else /* two children */