lua

A copy of the Lua development repository
Log | Files | Refs | README

commit cc01d4624782e28c19b3c75ef7efda4b151dbf03
parent 9fa1baf6de64e3e9a3288c21e2da2a10bcd25cd4
Author: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date:   Thu,  7 Dec 2017 16:51:12 -0200

new test function 'T.allocount' to restrict number of allocations
before a memory-allocation error

Diffstat:
Mltests.c | 21++++++++++++++++++---
Mltests.h | 3++-
2 files changed, 20 insertions(+), 4 deletions(-)

diff --git 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 @@ -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;