diff options
author | Lua Team <team@lua.org> | 1996-05-14 12:00:00 +0000 |
---|---|---|
committer | repogen <> | 1996-05-14 12:00:00 +0000 |
commit | 721542976ebc89f2f8d17d19be7e4426570b69be (patch) | |
tree | 0c79a45c63aa89d6e4b8ac80931e46d74a72f8cb /src/mem.c | |
parent | 71754d2f6423fb9b6e87658e58bafc5470d53f65 (diff) | |
download | lua-github-2.4.tar.gz |
Lua 2.42.4
Diffstat (limited to 'src/mem.c')
-rw-r--r-- | src/mem.c | 45 |
1 files changed, 34 insertions, 11 deletions
@@ -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; +} + |