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