From 2b5bc5d1a81579a76c13e638de2592e2c39c73f0 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 16 Nov 1994 15:39:16 -0200 Subject: new module for memory allocation --- fallback.c | 13 ++++--------- hash.c | 24 ++++++------------------ iolib.c | 22 +++++++++------------- luamem.c | 35 +++++++++++++++++++++++++++++++++++ luamem.h | 23 +++++++++++++++++++++++ opcode.c | 47 ++++++++++++++++++----------------------------- strlib.c | 10 +++++----- table.c | 24 +++++++----------------- tree.c | 14 ++++++-------- 9 files changed, 113 insertions(+), 99 deletions(-) create mode 100644 luamem.c create mode 100644 luamem.h diff --git a/fallback.c b/fallback.c index b9584032..5690be64 100644 --- a/fallback.c +++ b/fallback.c @@ -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 -#include +#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 -#include +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 -#include #include #include -#include #include -#ifdef __GNUC__ -#include -#endif +#include +#include +#include +#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); } diff --git a/luamem.c b/luamem.c new file mode 100644 index 00000000..ddbd5e47 --- /dev/null +++ b/luamem.c @@ -0,0 +1,35 @@ +/* +** mem.c +** TecCGraf - PUC-Rio +*/ + +char *rcs_mem = "$Id: $"; + +#include + +#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; +} + diff --git a/luamem.h b/luamem.h new file mode 100644 index 00000000..c75dd211 --- /dev/null +++ b/luamem.h @@ -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 + diff --git a/opcode.c b/opcode.c index 7f5f8bd6..b6955aee 100644 --- a/opcode.c +++ b/opcode.c @@ -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 #include -#include #include -#include #include -#ifdef __GNUC__ -#include -#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; diff --git a/strlib.c b/strlib.c index 0cd9104b..a5d44da9 100644 --- a/strlib.c +++ b/strlib.c @@ -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 #include #include +#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); } diff --git a/table.c b/table.c index d37b7b03..e52fcd4a 100644 --- a/table.c +++ b/table.c @@ -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 #include +#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; } diff --git a/tree.c b/tree.c index eee5a204..6e1c0b2a 100644 --- a/tree.c +++ b/tree.c @@ -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 #include +#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 */ -- cgit v1.2.3-55-g6feb