|
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 | 3 | ** Internal Module for Debugging of the Lua Implementation |
4 | 4 | ** See Copyright Notice in lua.h |
5 | 5 | */ |
@@ -102,7 +102,7 @@ typedef union Header { |
102 | 102 |
|
103 | 103 |
|
104 | 104 | 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}}; |
106 | 106 |
|
107 | 107 |
|
108 | 108 | static void freeblock (Memcontrol *mc, Header *block) { |
@@ -141,7 +141,12 @@ void *debug_realloc (void *ud, void *b, size_t oldsize, size_t size) { |
141 | 141 | freeblock(mc, block); |
142 | 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 | 150 | return NULL; /* fake a memory allocation error */ |
146 | 151 | else { |
147 | 152 | Header *newblock; |
@@ -695,6 +700,15 @@ static int mem_query (lua_State *L) { |
695 | 700 | } |
696 | 701 |
|
697 | 702 |
|
| 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 | + |
698 | 712 | static int settrick (lua_State *L) { |
699 | 713 | if (ttisnil(obj_at(L, 1))) |
700 | 714 | l_Trick = NULL; |
@@ -1673,6 +1687,7 @@ static const struct luaL_Reg tests_funcs[] = { |
1673 | 1687 | {"testC", testC}, |
1674 | 1688 | {"makeCfunc", makeCfunc}, |
1675 | 1689 | {"totalmem", mem_query}, |
| 1690 | + {"alloccount", alloc_count}, |
1676 | 1691 | {"trick", settrick}, |
1677 | 1692 | {"udataval", udataval}, |
1678 | 1693 | {"unref", unref}, |
|
0 commit comments