1
0
mirror of https://github.com/lua/lua.git synced 2025-01-21 05:53:03 +08:00
lua/luamem.c

71 lines
1.3 KiB
C
Raw Normal View History

1994-11-16 15:39:16 -02: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 15:39:16 -02:00
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
1994-11-16 15:39:16 -02:00
#include "mem.h"
#include "lua.h"
#include "table.h"
1996-03-21 13:33:47 -03:00
#define mem_error() lua_error(memEM)
1994-11-16 15:39:16 -02:00
void luaI_free (void *block)
{
if (block)
{
*((int *)block) = -1; /* to catch errors */
free(block);
}
1994-11-16 15:39:16 -02:00
}
void *luaI_malloc (unsigned long size)
{
void *block = malloc((size_t)size);
1994-11-16 15:39:16 -02:00
if (block == NULL)
mem_error();
1994-11-16 15:39:16 -02: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 15:39:16 -02:00
if (block == NULL)
mem_error();
1994-11-16 15:39:16 -02:00
return block;
}
1996-03-14 12:55:49 -03:00
1996-03-21 13:33:47 -03:00
int luaI_growvector (void **block, unsigned long nelems, int size,
char *errormsg, unsigned long limit)
1996-03-21 13:33:47 -03:00
{
if (nelems >= limit)
lua_error(errormsg);
1996-03-21 13:33:47 -03:00
nelems = (nelems == 0) ? 20 : nelems*2;
if (nelems > limit)
nelems = limit;
*block = luaI_realloc(*block, nelems*size);
return (int) nelems;
}
1996-03-14 12:55:49 -03: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;
}