1997-09-16 23:25:59 +04:00
|
|
|
/*
|
2018-08-23 20:26:12 +03:00
|
|
|
** $Id: lmem.c $
|
1997-09-16 23:25:59 +04:00
|
|
|
** Interface to Memory Manager
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
2002-12-04 20:38:31 +03:00
|
|
|
#define lmem_c
|
2004-05-01 00:13:38 +04:00
|
|
|
#define LUA_CORE
|
2002-12-04 20:38:31 +03:00
|
|
|
|
2014-11-02 22:19:04 +03:00
|
|
|
#include "lprefix.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
2000-06-12 17:52:05 +04:00
|
|
|
#include "lua.h"
|
|
|
|
|
2002-05-15 22:57:44 +04:00
|
|
|
#include "ldebug.h"
|
2000-08-04 23:38:35 +04:00
|
|
|
#include "ldo.h"
|
2006-07-11 19:53:29 +04:00
|
|
|
#include "lgc.h"
|
1997-09-16 23:25:59 +04:00
|
|
|
#include "lmem.h"
|
1999-11-29 19:38:48 +03:00
|
|
|
#include "lobject.h"
|
1997-11-19 20:29:23 +03:00
|
|
|
#include "lstate.h"
|
1997-09-16 23:25:59 +04:00
|
|
|
|
|
|
|
|
2020-07-08 21:36:48 +03:00
|
|
|
#if defined(EMERGENCYGCTESTS)
|
2019-07-18 20:58:15 +03:00
|
|
|
/*
|
|
|
|
** First allocation will fail whenever not building initial state
|
|
|
|
** and not shrinking a block. (This fail will trigger 'tryagain' and
|
2019-12-30 17:45:08 +03:00
|
|
|
** a full GC cycle at every allocation.)
|
2019-07-18 20:58:15 +03:00
|
|
|
*/
|
|
|
|
static void *firsttry (global_State *g, void *block, size_t os, size_t ns) {
|
|
|
|
if (ttisnil(&g->nilvalue) && ns > os)
|
|
|
|
return NULL; /* fail */
|
|
|
|
else /* normal allocation */
|
|
|
|
return (*g->frealloc)(g->ud, block, os, ns);
|
|
|
|
}
|
2017-12-06 21:36:31 +03:00
|
|
|
#else
|
2019-07-18 20:58:15 +03:00
|
|
|
#define firsttry(g,block,os,ns) ((*g->frealloc)(g->ud, block, os, ns))
|
2017-12-06 21:36:31 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
1999-01-22 20:28:00 +03:00
|
|
|
|
2019-07-18 20:58:15 +03:00
|
|
|
|
|
|
|
|
2002-06-11 20:26:12 +04:00
|
|
|
/*
|
2003-10-03 00:31:17 +04:00
|
|
|
** About the realloc function:
|
2019-07-19 15:43:35 +03:00
|
|
|
** void *frealloc (void *ud, void *ptr, size_t osize, size_t nsize);
|
2014-10-25 15:50:46 +04:00
|
|
|
** ('osize' is the old size, 'nsize' is the new size)
|
2003-10-03 00:31:17 +04:00
|
|
|
**
|
2019-07-19 15:43:35 +03: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-03 00:31:17 +04:00
|
|
|
**
|
2019-07-19 15:43:35 +03: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-03 00:31:17 +04:00
|
|
|
**
|
2019-07-19 15:43:35 +03: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 20:26:12 +04:00
|
|
|
*/
|
2000-10-11 20:47:50 +04:00
|
|
|
|
2002-10-08 22:45:07 +04:00
|
|
|
|
2000-10-11 20:47:50 +04:00
|
|
|
|
2019-07-19 15:43:35 +03: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 23:13:33 +04:00
|
|
|
#define MINSIZEARRAY 4
|
|
|
|
|
|
|
|
|
2017-12-07 21:59:52 +03:00
|
|
|
void *luaM_growaux_ (lua_State *L, void *block, int nelems, int *psize,
|
2017-12-06 21:36:31 +03:00
|
|
|
int size_elems, int limit, const char *what) {
|
2000-12-26 21:46:09 +03:00
|
|
|
void *newblock;
|
2017-12-07 21:59:52 +03:00
|
|
|
int size = *psize;
|
|
|
|
if (nelems + 1 <= size) /* does one extra element still fit? */
|
2017-12-06 21:36:31 +03:00
|
|
|
return block; /* nothing to be done */
|
2017-12-07 21:59:52 +03:00
|
|
|
if (size >= limit / 2) { /* cannot double it? */
|
2018-05-30 17:25:52 +03:00
|
|
|
if (unlikely(size >= limit)) /* cannot grow even a little? */
|
2006-09-14 22:42:28 +04:00
|
|
|
luaG_runerror(L, "too many %s (limit is %d)", what, limit);
|
2017-12-07 21:59:52 +03:00
|
|
|
size = limit; /* still have at least one free place */
|
2003-11-27 21:18:37 +03:00
|
|
|
}
|
|
|
|
else {
|
2017-12-07 21:59:52 +03:00
|
|
|
size *= 2;
|
|
|
|
if (size < MINSIZEARRAY)
|
|
|
|
size = MINSIZEARRAY; /* minimum size */
|
2000-12-26 21:46:09 +03:00
|
|
|
}
|
2017-12-08 20:28:25 +03:00
|
|
|
lua_assert(nelems + 1 <= size && size <= limit);
|
2017-12-07 21:59:52 +03:00
|
|
|
/* 'limit' ensures that multiplication will not overflow */
|
2019-07-19 15:43:35 +03:00
|
|
|
newblock = luaM_saferealloc_(L, block, cast_sizet(*psize) * size_elems,
|
|
|
|
cast_sizet(size) * size_elems);
|
2017-12-07 21:59:52 +03:00
|
|
|
*psize = size; /* update only when everything else is OK */
|
2000-12-26 21:46:09 +03:00
|
|
|
return newblock;
|
2000-01-13 19:30:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-19 15:43:35 +03: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 21:36:31 +03:00
|
|
|
void *luaM_shrinkvector_ (lua_State *L, void *block, int *size,
|
|
|
|
int final_n, int size_elem) {
|
|
|
|
void *newblock;
|
2018-01-28 18:13:26 +03:00
|
|
|
size_t oldsize = cast_sizet((*size) * size_elem);
|
|
|
|
size_t newsize = cast_sizet(final_n * size_elem);
|
2017-12-06 21:36:31 +03:00
|
|
|
lua_assert(newsize <= oldsize);
|
2019-07-19 15:43:35 +03:00
|
|
|
newblock = luaM_saferealloc_(L, block, oldsize, newsize);
|
|
|
|
*size = final_n;
|
|
|
|
return newblock;
|
2017-12-06 21:36:31 +03:00
|
|
|
}
|
|
|
|
|
2019-07-19 15:43:35 +03:00
|
|
|
/* }================================================================== */
|
|
|
|
|
2017-12-06 21:36:31 +03:00
|
|
|
|
2011-11-30 16:42:49 +04:00
|
|
|
l_noret luaM_toobig (lua_State *L) {
|
2004-11-19 18:52:40 +03:00
|
|
|
luaG_runerror(L, "memory allocation error: block too big");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-06 21:36:31 +03:00
|
|
|
/*
|
|
|
|
** Free memory
|
|
|
|
*/
|
|
|
|
void luaM_free_ (lua_State *L, void *block, size_t osize) {
|
|
|
|
global_State *g = G(L);
|
2018-10-23 18:58:38 +03:00
|
|
|
lua_assert((osize == 0) == (block == NULL));
|
2017-12-06 21:36:31 +03:00
|
|
|
(*g->frealloc)(g->ud, block, osize, 0);
|
|
|
|
g->GCdebt -= osize;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-30 17:25:52 +03: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 15:08:10 +03:00
|
|
|
if (ttisnil(&g->nilvalue)) { /* is state fully build? */
|
2018-05-30 17:25:52 +03: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 18:52:40 +03:00
|
|
|
|
2000-01-13 19:30:47 +03:00
|
|
|
/*
|
2019-07-19 15:43:35 +03:00
|
|
|
** Generic allocation routine.
|
|
|
|
** If allocation fails while shrinking a block, do not try again; the
|
|
|
|
** GC shrinks some blocks and it is not reentrant.
|
2000-01-13 19:30:47 +03:00
|
|
|
*/
|
2017-12-08 20:28:25 +03:00
|
|
|
void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
|
2006-07-11 19:53:29 +04:00
|
|
|
void *newblock;
|
2003-10-03 00:31:17 +04:00
|
|
|
global_State *g = G(L);
|
2017-12-06 21:36:31 +03:00
|
|
|
lua_assert((osize == 0) == (block == NULL));
|
2019-07-18 20:58:15 +03:00
|
|
|
newblock = firsttry(g, block, osize, nsize);
|
2018-05-30 17:25:52 +03: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? */
|
2019-07-19 15:43:35 +03:00
|
|
|
return NULL; /* do not update 'GCdebt' */
|
2006-07-11 19:53:29 +04:00
|
|
|
}
|
|
|
|
lua_assert((nsize == 0) == (newblock == NULL));
|
2017-12-06 21:36:31 +03:00
|
|
|
g->GCdebt = (g->GCdebt + nsize) - osize;
|
2006-07-11 19:53:29 +04:00
|
|
|
return newblock;
|
2000-01-13 19:30:47 +03:00
|
|
|
}
|
|
|
|
|
2017-12-06 21:36:31 +03:00
|
|
|
|
2017-12-08 20:28:25 +03: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 17:25:52 +03:00
|
|
|
if (unlikely(newblock == NULL && nsize > 0)) /* allocation failed? */
|
2017-12-08 20:28:25 +03:00
|
|
|
luaM_error(L);
|
|
|
|
return newblock;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void *luaM_malloc_ (lua_State *L, size_t size, int tag) {
|
2017-12-06 21:36:31 +03:00
|
|
|
if (size == 0)
|
|
|
|
return NULL; /* that's all */
|
|
|
|
else {
|
|
|
|
global_State *g = G(L);
|
2019-07-18 20:58:15 +03:00
|
|
|
void *newblock = firsttry(g, NULL, tag, size);
|
2018-05-30 17:25:52 +03:00
|
|
|
if (unlikely(newblock == NULL)) {
|
|
|
|
newblock = tryagain(L, NULL, tag, size);
|
2017-12-06 21:36:31 +03:00
|
|
|
if (newblock == NULL)
|
2017-12-08 20:28:25 +03:00
|
|
|
luaM_error(L);
|
2017-12-06 21:36:31 +03:00
|
|
|
}
|
|
|
|
g->GCdebt += size;
|
|
|
|
return newblock;
|
|
|
|
}
|
|
|
|
}
|