Skip to content

Commit cc01d46

Browse files
committed
new test function 'T.allocount' to restrict number of allocations
before a memory-allocation error
1 parent 9fa1baf commit cc01d46

2 files changed

Lines changed: 20 additions & 4 deletions

File tree

ltests.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: ltests.c,v 2.232 2017/11/09 13:31:29 roberto Exp roberto $
2+
** $Id: ltests.c,v 2.233 2017/11/23 15:38:42 roberto Exp roberto $
33
** Internal Module for Debugging of the Lua Implementation
44
** See Copyright Notice in lua.h
55
*/
@@ -102,7 +102,7 @@ typedef union Header {
102102

103103

104104
Memcontrol l_memcontrol =
105-
{0L, 0L, 0L, 0L, {0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L}};
105+
{0L, 0L, 0L, 0L, (~0L), {0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L}};
106106

107107

108108
static void freeblock (Memcontrol *mc, Header *block) {
@@ -141,7 +141,12 @@ void *debug_realloc (void *ud, void *b, size_t oldsize, size_t size) {
141141
freeblock(mc, block);
142142
return NULL;
143143
}
144-
else if (size > oldsize && mc->total+size-oldsize > mc->memlimit)
144+
if (mc->countlimit != ~0UL && size > oldsize) { /* count limit in use? */
145+
if (mc->countlimit == 0)
146+
return NULL; /* fake a memory allocation error */
147+
mc->countlimit--;
148+
}
149+
if (size > oldsize && mc->total+size-oldsize > mc->memlimit)
145150
return NULL; /* fake a memory allocation error */
146151
else {
147152
Header *newblock;
@@ -695,6 +700,15 @@ static int mem_query (lua_State *L) {
695700
}
696701

697702

703+
static int alloc_count (lua_State *L) {
704+
if (lua_isnone(L, 1))
705+
l_memcontrol.countlimit = ~0L;
706+
else
707+
l_memcontrol.countlimit = luaL_checkinteger(L, 1);
708+
return 0;
709+
}
710+
711+
698712
static int settrick (lua_State *L) {
699713
if (ttisnil(obj_at(L, 1)))
700714
l_Trick = NULL;
@@ -1673,6 +1687,7 @@ static const struct luaL_Reg tests_funcs[] = {
16731687
{"testC", testC},
16741688
{"makeCfunc", makeCfunc},
16751689
{"totalmem", mem_query},
1690+
{"alloccount", alloc_count},
16761691
{"trick", settrick},
16771692
{"udataval", udataval},
16781693
{"unref", unref},

ltests.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: ltests.h,v 2.52 2017/11/13 12:19:35 roberto Exp roberto $
2+
** $Id: ltests.h,v 2.53 2017/11/23 16:35:54 roberto Exp roberto $
33
** Internal Header for Debugging of the Lua Implementation
44
** See Copyright Notice in lua.h
55
*/
@@ -57,6 +57,7 @@ typedef struct Memcontrol {
5757
unsigned long total;
5858
unsigned long maxmem;
5959
unsigned long memlimit;
60+
unsigned long countlimit;
6061
unsigned long objcount[LUA_NUMTAGS];
6162
} Memcontrol;
6263

0 commit comments

Comments
 (0)