Imported from ../lua-2.5.tar.gz.
[lua.git] / src / mem.c
blob4e3bcc522d7c8f71096a951ae8a6d8a8dd9bf2c9
1 /*
2 ** mem.c
3 ** TecCGraf - PUC-Rio
4 */
6 char *rcs_mem = "$Id: mem.c,v 1.13 1996/05/24 14:31:10 roberto Exp $";
8 #include <stdlib.h>
10 #include "mem.h"
11 #include "lua.h"
14 void luaI_free (void *block)
16 if (block)
18 *((int *)block) = -1; /* to catch errors */
19 free(block);
24 void *luaI_realloc (void *oldblock, unsigned long size)
26 void *block;
27 size_t s = (size_t)size;
28 if (s != size)
29 lua_error("Allocation Error: Block too big");
30 block = oldblock ? realloc(oldblock, s) : malloc(s);
31 if (block == NULL)
32 lua_error(memEM);
33 return block;
37 int luaI_growvector (void **block, unsigned long nelems, int size,
38 char *errormsg, unsigned long limit)
40 if (nelems >= limit)
41 lua_error(errormsg);
42 nelems = (nelems == 0) ? 20 : nelems*2;
43 if (nelems > limit)
44 nelems = limit;
45 *block = luaI_realloc(*block, nelems*size);
46 return (int)nelems;
50 void* luaI_buffer (unsigned long size)
52 static unsigned long buffsize = 0;
53 static char* buffer = NULL;
54 if (size > buffsize)
55 buffer = luaI_realloc(buffer, buffsize=size);
56 return buffer;