From cc01d4624782e28c19b3c75ef7efda4b151dbf03 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 7 Dec 2017 16:51:39 -0200 Subject: new test function 'T.allocount' to restrict number of allocations before a memory-allocation error --- ltests.c | 21 ++++++++++++++++++--- ltests.h | 3 ++- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/ltests.c b/ltests.c index a2bbb49f..1f795b6c 100644 --- a/ltests.c +++ b/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}, diff --git a/ltests.h b/ltests.h index 04aa91b3..25598c0e 100644 --- a/ltests.h +++ b/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; -- cgit v1.2.3-55-g6feb