1997-09-16 12:25:59 -07:00
|
|
|
/*
|
2018-06-18 05:08:10 -07:00
|
|
|
** $Id: lmem.c,v 1.97 2018/05/30 14:25:52 roberto Exp roberto $
|
1997-09-16 12:25:59 -07:00
|
|
|
** Interface to Memory Manager
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
2002-12-04 09:38:31 -08:00
|
|
|
#define lmem_c
|
2004-04-30 13:13:38 -07:00
|
|
|
#define LUA_CORE
|
2002-12-04 09:38:31 -08:00
|
|
|
|
2014-11-02 11:19:04 -08:00
|
|
|
#include "lprefix.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
2000-06-12 06:52:05 -07:00
|
|
|
#include "lua.h"
|
|
|
|
|
2002-05-15 11:57:44 -07:00
|
|
|
#include "ldebug.h"
|
2000-08-04 12:38:35 -07:00
|
|
|
#include "ldo.h"
|
2006-07-11 08:53:29 -07:00
|
|
|
#include "lgc.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
|
|
|
|
|
|
|
|
2017-12-06 10:36:31 -08:00
|
|
|
#if defined(HARDMEMTESTS)
|
|
|
|
#define hardtest(L,os,s) /* force a GC whenever possible */ \
|
|
|
|
if ((s) > (os) && (G(L))->gcrunning) luaC_fullgc(L, 1);
|
|
|
|
#else
|
|
|
|
#define hardtest(L,os,s) ((void)0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
1999-01-22 09:28:00 -08:00
|
|
|
|
2002-06-11 09:26:12 -07:00
|
|
|
/*
|
2003-10-02 13:31:17 -07:00
|
|
|
** About the realloc function:
|
2005-02-23 09:30:22 -08:00
|
|
|
** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize);
|
2014-10-25 04:50:46 -07:00
|
|
|
** ('osize' is the old size, 'nsize' is the new size)
|
2003-10-02 13:31:17 -07:00
|
|
|
**
|
2014-10-25 04:50:46 -07:00
|
|
|
** * frealloc(ud, NULL, x, s) creates a new block of size 's' (no
|
2009-12-17 07:46:44 -08:00
|
|
|
** matter 'x').
|
2003-10-02 13:31:17 -07:00
|
|
|
**
|
2014-10-25 04:50:46 -07:00
|
|
|
** * frealloc(ud, p, x, 0) frees the block 'p'
|
2009-12-17 07:46:44 -08:00
|
|
|
** (in this specific case, frealloc must return NULL);
|
2005-02-23 09:30:22 -08:00
|
|
|
** particularly, frealloc(ud, NULL, 0, 0) does nothing
|
2014-11-02 11:33:33 -08:00
|
|
|
** (which is equivalent to free(NULL) in ISO C)
|
2003-10-02 13:31:17 -07:00
|
|
|
**
|
2005-02-23 09:30:22 -08:00
|
|
|
** frealloc returns NULL if it cannot create or reallocate the area
|
2003-10-02 13:31:17 -07:00
|
|
|
** (any reallocation to an equal or smaller size cannot fail!)
|
2002-06-11 09:26:12 -07:00
|
|
|
*/
|
2000-10-11 09:47:50 -07:00
|
|
|
|
2002-10-08 11:45:07 -07:00
|
|
|
|
2000-10-11 09:47:50 -07:00
|
|
|
|
2001-10-25 12:13:33 -07:00
|
|
|
#define MINSIZEARRAY 4
|
|
|
|
|
|
|
|
|
2017-12-07 10:59:52 -08:00
|
|
|
void *luaM_growaux_ (lua_State *L, void *block, int nelems, int *psize,
|
2017-12-06 10:36:31 -08:00
|
|
|
int size_elems, int limit, const char *what) {
|
2000-12-26 10:46:09 -08:00
|
|
|
void *newblock;
|
2017-12-07 10:59:52 -08:00
|
|
|
int size = *psize;
|
|
|
|
if (nelems + 1 <= size) /* does one extra element still fit? */
|
2017-12-06 10:36:31 -08:00
|
|
|
return block; /* nothing to be done */
|
2017-12-07 10:59:52 -08:00
|
|
|
if (size >= limit / 2) { /* cannot double it? */
|
2018-05-30 07:25:52 -07:00
|
|
|
if (unlikely(size >= limit)) /* cannot grow even a little? */
|
2006-09-14 11:42:28 -07:00
|
|
|
luaG_runerror(L, "too many %s (limit is %d)", what, limit);
|
2017-12-07 10:59:52 -08:00
|
|
|
size = limit; /* still have at least one free place */
|
2003-11-27 10:18:37 -08:00
|
|
|
}
|
|
|
|
else {
|
2017-12-07 10:59:52 -08:00
|
|
|
size *= 2;
|
|
|
|
if (size < MINSIZEARRAY)
|
|
|
|
size = MINSIZEARRAY; /* minimum size */
|
2000-12-26 10:46:09 -08:00
|
|
|
}
|
2017-12-08 09:28:25 -08:00
|
|
|
lua_assert(nelems + 1 <= size && size <= limit);
|
2017-12-07 10:59:52 -08:00
|
|
|
/* 'limit' ensures that multiplication will not overflow */
|
2018-01-28 07:13:26 -08:00
|
|
|
newblock = luaM_realloc_(L, block, cast_sizet(*psize) * size_elems,
|
|
|
|
cast_sizet(size) * size_elems);
|
2018-05-30 07:25:52 -07:00
|
|
|
if (unlikely(newblock == NULL))
|
2017-12-08 09:28:25 -08:00
|
|
|
luaM_error(L);
|
2017-12-07 10:59:52 -08:00
|
|
|
*psize = size; /* update only when everything else is OK */
|
2000-12-26 10:46:09 -08:00
|
|
|
return newblock;
|
2000-01-13 08:30:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-06 10:36:31 -08:00
|
|
|
void *luaM_shrinkvector_ (lua_State *L, void *block, int *size,
|
|
|
|
int final_n, int size_elem) {
|
|
|
|
global_State *g = G(L);
|
|
|
|
void *newblock;
|
2018-01-28 07:13:26 -08:00
|
|
|
size_t oldsize = cast_sizet((*size) * size_elem);
|
|
|
|
size_t newsize = cast_sizet(final_n * size_elem);
|
2017-12-06 10:36:31 -08:00
|
|
|
lua_assert(newsize <= oldsize);
|
|
|
|
newblock = (*g->frealloc)(g->ud, block, oldsize, newsize);
|
2018-05-30 07:25:52 -07:00
|
|
|
if (unlikely(newblock == NULL && final_n > 0)) /* allocation failed? */
|
2017-12-11 04:27:48 -08:00
|
|
|
luaM_error(L);
|
2017-12-06 10:36:31 -08:00
|
|
|
else {
|
|
|
|
g->GCdebt += newsize - oldsize;
|
|
|
|
*size = final_n;
|
|
|
|
return newblock;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-30 04:42:49 -08:00
|
|
|
l_noret luaM_toobig (lua_State *L) {
|
2004-11-19 07:52:40 -08:00
|
|
|
luaG_runerror(L, "memory allocation error: block too big");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-06 10:36:31 -08:00
|
|
|
/*
|
|
|
|
** Free memory
|
|
|
|
*/
|
|
|
|
void luaM_free_ (lua_State *L, void *block, size_t osize) {
|
|
|
|
global_State *g = G(L);
|
|
|
|
lua_assert((block == 0) == (block == NULL));
|
|
|
|
(*g->frealloc)(g->ud, block, osize, 0);
|
|
|
|
g->GCdebt -= osize;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-30 07:25:52 -07:00
|
|
|
/*
|
|
|
|
** In case of allocation fail, this function will call the GC to try
|
|
|
|
** to free some memory and then try the allocation again.
|
|
|
|
** (It should not be called when shrinking a block, because then the
|
|
|
|
** interpreter may be in the middle of a collection step.)
|
|
|
|
*/
|
|
|
|
static void *tryagain (lua_State *L, void *block,
|
|
|
|
size_t osize, size_t nsize) {
|
|
|
|
global_State *g = G(L);
|
2018-06-18 05:08:10 -07:00
|
|
|
if (ttisnil(&g->nilvalue)) { /* is state fully build? */
|
2018-05-30 07:25:52 -07:00
|
|
|
luaC_fullgc(L, 1); /* try to free some memory... */
|
|
|
|
return (*g->frealloc)(g->ud, block, osize, nsize); /* try again */
|
|
|
|
}
|
|
|
|
else return NULL; /* cannot free any memory without a full state */
|
|
|
|
}
|
|
|
|
|
2004-11-19 07:52:40 -08:00
|
|
|
|
2000-01-13 08:30:47 -08:00
|
|
|
/*
|
|
|
|
** generic allocation routine.
|
|
|
|
*/
|
2017-12-08 09:28:25 -08:00
|
|
|
void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
|
2006-07-11 08:53:29 -07:00
|
|
|
void *newblock;
|
2003-10-02 13:31:17 -07:00
|
|
|
global_State *g = G(L);
|
2017-12-06 10:36:31 -08:00
|
|
|
lua_assert((osize == 0) == (block == NULL));
|
|
|
|
hardtest(L, osize, nsize);
|
2006-07-11 08:53:29 -07:00
|
|
|
newblock = (*g->frealloc)(g->ud, block, osize, nsize);
|
2018-05-30 07:25:52 -07:00
|
|
|
if (unlikely(newblock == NULL && nsize > 0)) {
|
|
|
|
if (nsize > osize) /* not shrinking a block? */
|
|
|
|
newblock = tryagain(L, block, osize, nsize);
|
|
|
|
if (newblock == NULL) /* still no memory? */
|
2017-12-08 09:28:25 -08:00
|
|
|
return NULL;
|
2006-07-11 08:53:29 -07:00
|
|
|
}
|
|
|
|
lua_assert((nsize == 0) == (newblock == NULL));
|
2017-12-06 10:36:31 -08:00
|
|
|
g->GCdebt = (g->GCdebt + nsize) - osize;
|
2006-07-11 08:53:29 -07:00
|
|
|
return newblock;
|
2000-01-13 08:30:47 -08:00
|
|
|
}
|
|
|
|
|
2017-12-06 10:36:31 -08:00
|
|
|
|
2017-12-08 09:28:25 -08:00
|
|
|
void *luaM_saferealloc_ (lua_State *L, void *block, size_t osize,
|
|
|
|
size_t nsize) {
|
|
|
|
void *newblock = luaM_realloc_(L, block, osize, nsize);
|
2018-05-30 07:25:52 -07:00
|
|
|
if (unlikely(newblock == NULL && nsize > 0)) /* allocation failed? */
|
2017-12-08 09:28:25 -08:00
|
|
|
luaM_error(L);
|
|
|
|
return newblock;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void *luaM_malloc_ (lua_State *L, size_t size, int tag) {
|
2017-12-06 10:36:31 -08:00
|
|
|
hardtest(L, 0, size);
|
|
|
|
if (size == 0)
|
|
|
|
return NULL; /* that's all */
|
|
|
|
else {
|
|
|
|
global_State *g = G(L);
|
|
|
|
void *newblock = (*g->frealloc)(g->ud, NULL, tag, size);
|
2018-05-30 07:25:52 -07:00
|
|
|
if (unlikely(newblock == NULL)) {
|
|
|
|
newblock = tryagain(L, NULL, tag, size);
|
2017-12-06 10:36:31 -08:00
|
|
|
if (newblock == NULL)
|
2017-12-08 09:28:25 -08:00
|
|
|
luaM_error(L);
|
2017-12-06 10:36:31 -08:00
|
|
|
}
|
|
|
|
g->GCdebt += size;
|
|
|
|
return newblock;
|
|
|
|
}
|
|
|
|
}
|