1994-11-16 20:39:16 +03:00
|
|
|
/*
|
|
|
|
** mem.c
|
|
|
|
** TecCGraf - PUC-Rio
|
|
|
|
*/
|
|
|
|
|
1996-02-04 19:59:12 +03:00
|
|
|
char *rcs_mem = "$Id: mem.c,v 1.6 1996/01/22 14:15:13 roberto Exp roberto $";
|
1994-11-16 20:39:16 +03:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
1995-02-06 22:34:03 +03:00
|
|
|
#include <string.h>
|
1996-01-22 17:15:13 +03:00
|
|
|
#include <stdio.h>
|
1994-11-16 20:39:16 +03:00
|
|
|
|
|
|
|
#include "mem.h"
|
|
|
|
#include "lua.h"
|
1996-01-22 17:15:13 +03:00
|
|
|
#include "table.h"
|
|
|
|
|
|
|
|
static void mem_error (void)
|
|
|
|
{
|
|
|
|
Long recovered = luaI_collectgarbage(); /* try to collect garbage */
|
|
|
|
if (recovered)
|
|
|
|
lua_error("not enough memory");
|
|
|
|
else
|
|
|
|
{ /* if there is no garbage then must exit */
|
1996-02-04 19:59:12 +03:00
|
|
|
fprintf(stderr, "lua error: memory overflow - unable to recover\n");
|
1996-01-22 17:15:13 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
1994-11-16 20:39:16 +03:00
|
|
|
|
|
|
|
void luaI_free (void *block)
|
|
|
|
{
|
1994-11-16 21:09:11 +03:00
|
|
|
*((int *)block) = -1; /* to catch errors */
|
1994-11-16 20:39:16 +03:00
|
|
|
free(block);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void *luaI_malloc (unsigned long size)
|
|
|
|
{
|
1994-12-21 00:20:36 +03:00
|
|
|
void *block = malloc((size_t)size);
|
1994-11-16 20:39:16 +03:00
|
|
|
if (block == NULL)
|
1996-01-22 17:15:13 +03:00
|
|
|
mem_error();
|
1994-11-16 20:39:16 +03:00
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void *luaI_realloc (void *oldblock, unsigned long size)
|
|
|
|
{
|
1994-12-21 00:20:36 +03:00
|
|
|
void *block = realloc(oldblock, (size_t)size);
|
1994-11-16 20:39:16 +03:00
|
|
|
if (block == NULL)
|
1996-01-22 17:15:13 +03:00
|
|
|
mem_error();
|
1994-11-16 20:39:16 +03:00
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
1995-01-14 18:40:26 +03:00
|
|
|
|
|
|
|
char *luaI_strdup (char *str)
|
|
|
|
{
|
|
|
|
char *newstr = luaI_malloc(strlen(str)+1);
|
|
|
|
strcpy(newstr, str);
|
|
|
|
return newstr;
|
|
|
|
}
|