lua/luamem.c

71 lines
1.3 KiB
C
Raw Normal View History

1994-11-16 09:39:16 -08:00
/*
** mem.c
** TecCGraf - PUC-Rio
*/
char *rcs_mem = "$Id: mem.c,v 1.10 1996/03/21 16:31:32 roberto Exp roberto $";
1994-11-16 09:39:16 -08:00
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
1994-11-16 09:39:16 -08:00
#include "mem.h"
#include "lua.h"
#include "table.h"
1996-03-21 08:33:47 -08:00
#define mem_error() lua_error(memEM)
1994-11-16 09:39:16 -08:00
void luaI_free (void *block)
{
if (block)
{
*((int *)block) = -1; /* to catch errors */
free(block);
}
1994-11-16 09:39:16 -08:00
}
void *luaI_malloc (unsigned long size)
{
void *block = malloc((size_t)size);
1994-11-16 09:39:16 -08:00
if (block == NULL)
mem_error();
1994-11-16 09:39:16 -08:00
return block;
}
void *luaI_realloc (void *oldblock, unsigned long size)
{
void *block = oldblock ? realloc(oldblock, (size_t)size) :
malloc((size_t)size);
1994-11-16 09:39:16 -08:00
if (block == NULL)
mem_error();
1994-11-16 09:39:16 -08:00
return block;
}
1996-03-14 07:55:49 -08:00
1996-03-21 08:33:47 -08:00
int luaI_growvector (void **block, unsigned long nelems, int size,
char *errormsg, unsigned long limit)
1996-03-21 08:33:47 -08:00
{
if (nelems >= limit)
lua_error(errormsg);
1996-03-21 08:33:47 -08:00
nelems = (nelems == 0) ? 20 : nelems*2;
if (nelems > limit)
nelems = limit;
*block = luaI_realloc(*block, nelems*size);
return (int) nelems;
}
1996-03-14 07:55:49 -08:00
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;
}