mirror of
https://github.com/lua/lua.git
synced 2024-11-27 12:13:38 +08:00
new test function 'T.allocount' to restrict number of allocations
before a memory-allocation error
This commit is contained in:
parent
9fa1baf6de
commit
cc01d46247
21
ltests.c
21
ltests.c
@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ltests.c,v 2.232 2017/11/09 13:31:29 roberto Exp roberto $
|
||||
** $Id: ltests.c,v 2.233 2017/11/23 15:38:42 roberto Exp roberto $
|
||||
** Internal Module for Debugging of the Lua Implementation
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@ -102,7 +102,7 @@ typedef union Header {
|
||||
|
||||
|
||||
Memcontrol l_memcontrol =
|
||||
{0L, 0L, 0L, 0L, {0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L}};
|
||||
{0L, 0L, 0L, 0L, (~0L), {0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L}};
|
||||
|
||||
|
||||
static void freeblock (Memcontrol *mc, Header *block) {
|
||||
@ -141,7 +141,12 @@ void *debug_realloc (void *ud, void *b, size_t oldsize, size_t size) {
|
||||
freeblock(mc, block);
|
||||
return NULL;
|
||||
}
|
||||
else if (size > oldsize && mc->total+size-oldsize > mc->memlimit)
|
||||
if (mc->countlimit != ~0UL && size > oldsize) { /* count limit in use? */
|
||||
if (mc->countlimit == 0)
|
||||
return NULL; /* fake a memory allocation error */
|
||||
mc->countlimit--;
|
||||
}
|
||||
if (size > oldsize && mc->total+size-oldsize > mc->memlimit)
|
||||
return NULL; /* fake a memory allocation error */
|
||||
else {
|
||||
Header *newblock;
|
||||
@ -695,6 +700,15 @@ static int mem_query (lua_State *L) {
|
||||
}
|
||||
|
||||
|
||||
static int alloc_count (lua_State *L) {
|
||||
if (lua_isnone(L, 1))
|
||||
l_memcontrol.countlimit = ~0L;
|
||||
else
|
||||
l_memcontrol.countlimit = luaL_checkinteger(L, 1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int settrick (lua_State *L) {
|
||||
if (ttisnil(obj_at(L, 1)))
|
||||
l_Trick = NULL;
|
||||
@ -1673,6 +1687,7 @@ static const struct luaL_Reg tests_funcs[] = {
|
||||
{"testC", testC},
|
||||
{"makeCfunc", makeCfunc},
|
||||
{"totalmem", mem_query},
|
||||
{"alloccount", alloc_count},
|
||||
{"trick", settrick},
|
||||
{"udataval", udataval},
|
||||
{"unref", unref},
|
||||
|
3
ltests.h
3
ltests.h
@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ltests.h,v 2.52 2017/11/13 12:19:35 roberto Exp roberto $
|
||||
** $Id: ltests.h,v 2.53 2017/11/23 16:35:54 roberto Exp roberto $
|
||||
** Internal Header for Debugging of the Lua Implementation
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@ -57,6 +57,7 @@ typedef struct Memcontrol {
|
||||
unsigned long total;
|
||||
unsigned long maxmem;
|
||||
unsigned long memlimit;
|
||||
unsigned long countlimit;
|
||||
unsigned long objcount[LUA_NUMTAGS];
|
||||
} Memcontrol;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user