aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2017-12-07 16:51:39 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2017-12-07 16:51:39 -0200
commitcc01d4624782e28c19b3c75ef7efda4b151dbf03 (patch)
tree63497e23ee54ab0a025f1a0a58e522866cd99ba5
parent9fa1baf6de64e3e9a3288c21e2da2a10bcd25cd4 (diff)
downloadlua-cc01d4624782e28c19b3c75ef7efda4b151dbf03.tar.gz
lua-cc01d4624782e28c19b3c75ef7efda4b151dbf03.tar.bz2
lua-cc01d4624782e28c19b3c75ef7efda4b151dbf03.zip
new test function 'T.allocount' to restrict number of allocations
before a memory-allocation error
-rw-r--r--ltests.c21
-rw-r--r--ltests.h3
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 @@
1/* 1/*
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 $
3** Internal Module for Debugging of the Lua Implementation 3** Internal Module for Debugging of the Lua Implementation
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -102,7 +102,7 @@ typedef union Header {
102 102
103 103
104Memcontrol l_memcontrol = 104Memcontrol 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}};
106 106
107 107
108static void freeblock (Memcontrol *mc, Header *block) { 108static void freeblock (Memcontrol *mc, Header *block) {
@@ -141,7 +141,12 @@ void *debug_realloc (void *ud, void *b, size_t oldsize, size_t size) {
141 freeblock(mc, block); 141 freeblock(mc, block);
142 return NULL; 142 return NULL;
143 } 143 }
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)
145 return NULL; /* fake a memory allocation error */ 150 return NULL; /* fake a memory allocation error */
146 else { 151 else {
147 Header *newblock; 152 Header *newblock;
@@ -695,6 +700,15 @@ static int mem_query (lua_State *L) {
695} 700}
696 701
697 702
703static 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
698static int settrick (lua_State *L) { 712static int settrick (lua_State *L) {
699 if (ttisnil(obj_at(L, 1))) 713 if (ttisnil(obj_at(L, 1)))
700 l_Trick = NULL; 714 l_Trick = NULL;
@@ -1673,6 +1687,7 @@ static const struct luaL_Reg tests_funcs[] = {
1673 {"testC", testC}, 1687 {"testC", testC},
1674 {"makeCfunc", makeCfunc}, 1688 {"makeCfunc", makeCfunc},
1675 {"totalmem", mem_query}, 1689 {"totalmem", mem_query},
1690 {"alloccount", alloc_count},
1676 {"trick", settrick}, 1691 {"trick", settrick},
1677 {"udataval", udataval}, 1692 {"udataval", udataval},
1678 {"unref", unref}, 1693 {"unref", unref},
diff --git a/ltests.h b/ltests.h
index 04aa91b3..25598c0e 100644
--- a/ltests.h
+++ b/ltests.h
@@ -1,5 +1,5 @@
1/* 1/*
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 $
3** Internal Header for Debugging of the Lua Implementation 3** Internal Header for Debugging of the Lua Implementation
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -57,6 +57,7 @@ typedef struct Memcontrol {
57 unsigned long total; 57 unsigned long total;
58 unsigned long maxmem; 58 unsigned long maxmem;
59 unsigned long memlimit; 59 unsigned long memlimit;
60 unsigned long countlimit;
60 unsigned long objcount[LUA_NUMTAGS]; 61 unsigned long objcount[LUA_NUMTAGS];
61} Memcontrol; 62} Memcontrol;
62 63