summaryrefslogtreecommitdiff
path: root/src/mem.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/mem.c')
-rw-r--r--src/mem.c45
1 files changed, 34 insertions, 11 deletions
diff --git a/src/mem.c b/src/mem.c
index 90369720..2e410876 100644
--- a/src/mem.c
+++ b/src/mem.c
@@ -3,18 +3,24 @@
** TecCGraf - PUC-Rio
*/
-char *rcs_mem = "$Id: mem.c,v 1.5 1995/02/06 19:34:03 roberto Exp $";
+char *rcs_mem = "$Id: mem.c,v 1.12 1996/05/06 16:59:00 roberto Exp $";
#include <stdlib.h>
-#include <string.h>
#include "mem.h"
#include "lua.h"
+
+#define mem_error() lua_error(memEM)
+
+
void luaI_free (void *block)
{
- *((int *)block) = -1; /* to catch errors */
- free(block);
+ if (block)
+ {
+ *((int *)block) = -1; /* to catch errors */
+ free(block);
+ }
}
@@ -22,23 +28,40 @@ void *luaI_malloc (unsigned long size)
{
void *block = malloc((size_t)size);
if (block == NULL)
- lua_error("not enough memory");
+ mem_error();
return block;
}
void *luaI_realloc (void *oldblock, unsigned long size)
{
- void *block = realloc(oldblock, (size_t)size);
+ void *block = oldblock ? realloc(oldblock, (size_t)size) :
+ malloc((size_t)size);
if (block == NULL)
- lua_error("not enough memory");
+ mem_error();
return block;
}
-char *luaI_strdup (char *str)
+int luaI_growvector (void **block, unsigned long nelems, int size,
+ char *errormsg, unsigned long limit)
{
- char *newstr = luaI_malloc(strlen(str)+1);
- strcpy(newstr, str);
- return newstr;
+ if (nelems >= limit)
+ lua_error(errormsg);
+ nelems = (nelems == 0) ? 20 : nelems*2;
+ if (nelems > limit)
+ nelems = limit;
+ *block = luaI_realloc(*block, nelems*size);
+ return (int) nelems;
}
+
+
+void* luaI_buffer (unsigned long size)
+{
+ static unsigned long buffsize = 0;
+ static char* buffer = NULL;
+ if (size > buffsize)
+ buffer = luaI_realloc(buffer, buffsize=size);
+ return buffer;
+}
+