浏览代码

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

Roberto Ierusalimschy 7 年之前
父节点
当前提交
cc01d46247
共有 2 个文件被更改,包括 20 次插入4 次删除
  1. 18 3
      ltests.c
  2. 2 1
      ltests.h

+ 18 - 3
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
 ** Internal Module for Debugging of the Lua Implementation
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -102,7 +102,7 @@ typedef union Header {
 
 
 
 
 Memcontrol l_memcontrol =
 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) {
 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);
     freeblock(mc, block);
     return NULL;
     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 */
     return NULL;  /* fake a memory allocation error */
   else {
   else {
     Header *newblock;
     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) {
 static int settrick (lua_State *L) {
   if (ttisnil(obj_at(L, 1)))
   if (ttisnil(obj_at(L, 1)))
     l_Trick = NULL;
     l_Trick = NULL;
@@ -1673,6 +1687,7 @@ static const struct luaL_Reg tests_funcs[] = {
   {"testC", testC},
   {"testC", testC},
   {"makeCfunc", makeCfunc},
   {"makeCfunc", makeCfunc},
   {"totalmem", mem_query},
   {"totalmem", mem_query},
+  {"alloccount", alloc_count},
   {"trick", settrick},
   {"trick", settrick},
   {"udataval", udataval},
   {"udataval", udataval},
   {"unref", unref},
   {"unref", unref},

+ 2 - 1
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
 ** Internal Header for Debugging of the Lua Implementation
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -57,6 +57,7 @@ typedef struct Memcontrol {
   unsigned long total;
   unsigned long total;
   unsigned long maxmem;
   unsigned long maxmem;
   unsigned long memlimit;
   unsigned long memlimit;
+  unsigned long countlimit;
   unsigned long objcount[LUA_NUMTAGS];
   unsigned long objcount[LUA_NUMTAGS];
 } Memcontrol;
 } Memcontrol;