1997-09-16 12:25:59 -07:00
|
|
|
/*
|
2018-08-23 10:26:12 -07:00
|
|
|
** $Id: lmem.c $
|
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
|
|
|
|
|
|
|
|
2020-07-08 11:36:48 -07:00
|
|
|
#if defined(EMERGENCYGCTESTS)
|
2019-07-18 10:58:15 -07:00
|
|
|
/*
|
2021-02-26 06:41:02 -08:00
|
|
|
** First allocation will fail whenever not building initial state.
|
|
|
|
** (This fail will trigger 'tryagain' and a full GC cycle at every
|
|
|
|
** allocation.)
|
2019-07-18 10:58:15 -07:00
|
|
|
*/
|
|
|
|
static void *firsttry (global_State *g, void *block, size_t os, size_t ns) {
|
2021-02-26 06:41:02 -08:00
|
|
|
if (completestate(g) && ns > 0) /* frees never fail */
|
2019-07-18 10:58:15 -07:00
|
|
|
return NULL; /* fail */
|
|
|
|
else /* normal allocation */
|
|
|
|
return (*g->frealloc)(g->ud, block, os, ns);
|
|
|
|
}
|
2017-12-06 10:36:31 -08:00
|
|
|
#else
|
2019-07-18 10:58:15 -07:00
|
|
|
#define firsttry(g,block,os,ns) ((*g->frealloc)(g->ud, block, os, ns))
|
2017-12-06 10:36:31 -08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
1999-01-22 09:28:00 -08:00
|
|
|
|
2019-07-18 10:58:15 -07:00
|
|
|
|
|
|
|
|
2002-06-11 09:26:12 -07:00
|
|
|
/*
|
2003-10-02 13:31:17 -07:00
|
|
|
** About the realloc function:
|
2019-07-19 05:43:35 -07: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
|
|
|
**
|
2019-07-19 05:43:35 -07:00
|
|
|
** - frealloc(ud, p, x, 0) frees the block 'p' and returns NULL.
|
|
|
|
** Particularly, frealloc(ud, NULL, 0, 0) does nothing,
|
|
|
|
** which is equivalent to free(NULL) in ISO C.
|
2003-10-02 13:31:17 -07:00
|
|
|
**
|
2019-07-19 05:43:35 -07:00
|
|
|
** - frealloc(ud, NULL, x, s) creates a new block of size 's'
|
|
|
|
** (no matter 'x'). Returns NULL if it cannot create the new block.
|
2003-10-02 13:31:17 -07:00
|
|
|
**
|
2019-07-19 05:43:35 -07:00
|
|
|
** - otherwise, frealloc(ud, b, x, y) reallocates the block 'b' from
|
|
|
|
** size 'x' to size 'y'. Returns NULL if it cannot reallocate the
|
|
|
|
** block to the new size.
|
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
|
|
|
|
2019-07-19 05:43:35 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
** {==================================================================
|
|
|
|
** Functions to allocate/deallocate arrays for the Parser
|
|
|
|
** ===================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Minimum size for arrays during parsing, to avoid overhead of
|
|
|
|
** reallocating to size 1, then 2, and then 4. All these arrays
|
|
|
|
** will be reallocated to exact sizes or erased when parsing ends.
|
|
|
|
*/
|
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? */
|
2021-02-24 06:14:44 -08:00
|
|
|
if (l_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 */
|
2019-07-19 05:43:35 -07:00
|
|
|
newblock = luaM_saferealloc_(L, block, cast_sizet(*psize) * size_elems,
|
|
|
|
cast_sizet(size) * size_elems);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-19 05:43:35 -07:00
|
|
|
/*
|
|
|
|
** In prototypes, the size of the array is also its number of
|
|
|
|
** elements (to save memory). So, if it cannot shrink an array
|
|
|
|
** to its number of elements, the only option is to raise an
|
|
|
|
** error.
|
|
|
|
*/
|
2017-12-06 10:36:31 -08:00
|
|
|
void *luaM_shrinkvector_ (lua_State *L, void *block, int *size,
|
|
|
|
int final_n, int size_elem) {
|
|
|
|
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);
|
2019-07-19 05:43:35 -07:00
|
|
|
newblock = luaM_saferealloc_(L, block, oldsize, newsize);
|
|
|
|
*size = final_n;
|
|
|
|
return newblock;
|
2017-12-06 10:36:31 -08:00
|
|
|
}
|
|
|
|
|
2019-07-19 05:43:35 -07:00
|
|
|
/* }================================================================== */
|
|
|
|
|
2017-12-06 10:36:31 -08:00
|
|
|
|
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);
|
2018-10-23 08:58:38 -07:00
|
|
|
lua_assert((osize == 0) == (block == NULL));
|
2017-12-06 10:36:31 -08:00
|
|
|
(*g->frealloc)(g->ud, block, osize, 0);
|
|
|
|
g->GCdebt -= osize;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-30 07:25:52 -07:00
|
|
|
/*
|
2021-02-26 06:41:02 -08:00
|
|
|
** In case of allocation fail, this function will do an emergency
|
|
|
|
** collection to free some memory and then try the allocation again.
|
|
|
|
** The GC should not be called while state is not fully built, as the
|
|
|
|
** collector is not yet fully initialized. Also, it should not be called
|
|
|
|
** when 'gcstopem' is true, because then the interpreter is in the
|
|
|
|
** middle of a collection step.
|
2018-05-30 07:25:52 -07:00
|
|
|
*/
|
|
|
|
static void *tryagain (lua_State *L, void *block,
|
|
|
|
size_t osize, size_t nsize) {
|
|
|
|
global_State *g = G(L);
|
2021-02-26 06:41:02 -08:00
|
|
|
if (completestate(g) && !g->gcstopem) {
|
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
|
|
|
/*
|
2019-07-19 05:43:35 -07:00
|
|
|
** Generic allocation routine.
|
2000-01-13 08:30:47 -08:00
|
|
|
*/
|
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));
|
2019-07-18 10:58:15 -07:00
|
|
|
newblock = firsttry(g, block, osize, nsize);
|
2021-02-24 06:14:44 -08:00
|
|
|
if (l_unlikely(newblock == NULL && nsize > 0)) {
|
2021-02-26 06:41:02 -08:00
|
|
|
newblock = tryagain(L, block, osize, nsize);
|
2018-05-30 07:25:52 -07:00
|
|
|
if (newblock == NULL) /* still no memory? */
|
2019-07-19 05:43:35 -07:00
|
|
|
return NULL; /* do not update 'GCdebt' */
|
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);
|
2021-02-24 06:14:44 -08:00
|
|
|
if (l_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
|
|
|
if (size == 0)
|
|
|
|
return NULL; /* that's all */
|
|
|
|
else {
|
|
|
|
global_State *g = G(L);
|
2019-07-18 10:58:15 -07:00
|
|
|
void *newblock = firsttry(g, NULL, tag, size);
|
2021-02-24 06:14:44 -08:00
|
|
|
if (l_unlikely(newblock == NULL)) {
|
2018-05-30 07:25:52 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|