1997-09-16 12:25:59 -07:00
|
|
|
/*
|
2001-02-20 10:15:33 -08:00
|
|
|
** $Id: lmem.c,v 1.46 2001/02/06 16:01:29 roberto Exp roberto $
|
1997-09-16 12:25:59 -07:00
|
|
|
** Interface to Memory Manager
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2000-06-12 06:52:05 -07:00
|
|
|
#include "lua.h"
|
|
|
|
|
2000-08-04 12:38:35 -07:00
|
|
|
#include "ldo.h"
|
1997-09-16 12:25:59 -07:00
|
|
|
#include "lmem.h"
|
1999-11-29 08:38:48 -08:00
|
|
|
#include "lobject.h"
|
1997-11-19 09:29:23 -08:00
|
|
|
#include "lstate.h"
|
1997-09-16 12:25:59 -07:00
|
|
|
|
|
|
|
|
1999-01-22 09:28:00 -08:00
|
|
|
|
2001-02-06 08:01:29 -08:00
|
|
|
#ifndef l_realloc
|
|
|
|
#define l_realloc(b,os,s) realloc(b,s)
|
|
|
|
#define l_free(b,s) free(b)
|
2000-10-11 09:47:50 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2000-12-26 10:46:09 -08:00
|
|
|
void *luaM_growaux (lua_State *L, void *block, int *size, int size_elems,
|
|
|
|
int limit, const char *errormsg) {
|
|
|
|
void *newblock;
|
|
|
|
int newsize = (*size)*2;
|
|
|
|
if (newsize < MINPOWER2)
|
|
|
|
newsize = MINPOWER2; /* minimum size */
|
|
|
|
else if (*size >= limit/2) { /* cannot double it? */
|
|
|
|
if (*size < limit - MINPOWER2) /* try something smaller... */
|
|
|
|
newsize = limit; /* still have at least MINPOWER2 free places */
|
2001-01-24 07:45:33 -08:00
|
|
|
else luaD_error(L, errormsg);
|
2000-12-26 10:46:09 -08:00
|
|
|
}
|
2001-02-20 10:15:33 -08:00
|
|
|
newblock = luaM_realloc(L, block, (lu_mem)(*size)*(lu_mem)size_elems,
|
|
|
|
(lu_mem)newsize*(lu_mem)size_elems);
|
2000-12-26 10:46:09 -08:00
|
|
|
*size = newsize; /* update only when everything else is OK */
|
|
|
|
return newblock;
|
2000-01-13 08:30:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** generic allocation routine.
|
|
|
|
*/
|
2001-02-20 10:15:33 -08:00
|
|
|
void *luaM_realloc (lua_State *L, void *block, lu_mem oldsize, lu_mem size) {
|
2000-01-13 08:30:47 -08:00
|
|
|
if (size == 0) {
|
2001-02-06 08:01:29 -08:00
|
|
|
l_free(block, oldsize); /* block may be NULL; that is OK for free */
|
2000-12-28 04:55:41 -08:00
|
|
|
block = NULL;
|
2000-01-13 08:30:47 -08:00
|
|
|
}
|
2000-05-24 06:54:49 -07:00
|
|
|
else if (size >= MAX_SIZET)
|
2001-01-24 07:45:33 -08:00
|
|
|
luaD_error(L, "memory allocation error: block too big");
|
2000-12-28 04:55:41 -08:00
|
|
|
else {
|
2001-02-06 08:01:29 -08:00
|
|
|
block = l_realloc(block, oldsize, size);
|
2000-12-28 04:55:41 -08:00
|
|
|
if (block == NULL) {
|
|
|
|
if (L)
|
|
|
|
luaD_breakrun(L, LUA_ERRMEM); /* break run without error message */
|
|
|
|
else return NULL; /* error before creating state! */
|
|
|
|
}
|
|
|
|
}
|
2001-01-19 05:20:30 -08:00
|
|
|
if (L && G(L)) {
|
|
|
|
G(L)->nblocks -= oldsize;
|
|
|
|
G(L)->nblocks += size;
|
2000-08-04 12:38:35 -07:00
|
|
|
}
|
2000-01-13 08:30:47 -08:00
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|