diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2020-07-04 16:40:18 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2020-07-04 16:40:18 -0300 |
commit | bfcf06d91a87b7ffb8c83e290db0cb6176a167f8 (patch) | |
tree | 0bcba905a2772e536c845e39e9eeed0c7330312c /ltests.c | |
parent | 0280407fc54f9b6225139c5ac27326f98f0cf043 (diff) | |
download | lua-bfcf06d91a87b7ffb8c83e290db0cb6176a167f8.tar.gz lua-bfcf06d91a87b7ffb8c83e290db0cb6176a167f8.tar.bz2 lua-bfcf06d91a87b7ffb8c83e290db0cb6176a167f8.zip |
Avoid memory allocation in some functions from 'ltests.c'
To allow their use in memory tests, some functions in 'ltests.c'
should never allocate memory. To avoid this allocation, the
library registers the strings used for status codes, and keeps
the variable '_WARN' always defined (with false instead of nil).
Diffstat (limited to 'ltests.c')
-rw-r--r-- | ltests.c | 27 |
1 files changed, 20 insertions, 7 deletions
@@ -121,7 +121,8 @@ static void warnf (void *ud, const char *msg, int tocont) { | |||
121 | strcat(buff, msg); /* add new message to current warning */ | 121 | strcat(buff, msg); /* add new message to current warning */ |
122 | if (!tocont) { /* message finished? */ | 122 | if (!tocont) { /* message finished? */ |
123 | lua_unlock(L); | 123 | lua_unlock(L); |
124 | if (lua_getglobal(L, "_WARN") == LUA_TNIL) | 124 | lua_getglobal(L, "_WARN"); |
125 | if (!lua_toboolean(L, -1)) | ||
125 | lua_pop(L, 1); /* ok, no previous unexpected warning */ | 126 | lua_pop(L, 1); /* ok, no previous unexpected warning */ |
126 | else { | 127 | else { |
127 | badexit("Unhandled warning in store mode: %s\naborting...\n", | 128 | badexit("Unhandled warning in store mode: %s\naborting...\n", |
@@ -1282,10 +1283,19 @@ static int getindex_aux (lua_State *L, lua_State *L1, const char **pc) { | |||
1282 | } | 1283 | } |
1283 | 1284 | ||
1284 | 1285 | ||
1285 | static void pushcode (lua_State *L, int code) { | 1286 | static const char *const statcodes[] = {"OK", "YIELD", "ERRRUN", |
1286 | static const char *const codes[] = {"OK", "YIELD", "ERRRUN", | 1287 | "ERRSYNTAX", MEMERRMSG, "ERRGCMM", "ERRERR"}; |
1287 | "ERRSYNTAX", MEMERRMSG, "ERRGCMM", "ERRERR"}; | 1288 | |
1288 | lua_pushstring(L, codes[code]); | 1289 | /* |
1290 | ** Avoid these stat codes from being collected, to avoid possible | ||
1291 | ** memory error when pushing them. | ||
1292 | */ | ||
1293 | static void regcodes (lua_State *L) { | ||
1294 | unsigned int i; | ||
1295 | for (i = 0; i < sizeof(statcodes) / sizeof(statcodes[0]); i++) { | ||
1296 | lua_pushboolean(L, 1); | ||
1297 | lua_setfield(L, LUA_REGISTRYINDEX, statcodes[i]); | ||
1298 | } | ||
1289 | } | 1299 | } |
1290 | 1300 | ||
1291 | 1301 | ||
@@ -1508,7 +1518,7 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { | |||
1508 | lua_pushnumber(L1, (lua_Number)getnum); | 1518 | lua_pushnumber(L1, (lua_Number)getnum); |
1509 | } | 1519 | } |
1510 | else if EQ("pushstatus") { | 1520 | else if EQ("pushstatus") { |
1511 | pushcode(L1, status); | 1521 | lua_pushstring(L1, statcodes[status]); |
1512 | } | 1522 | } |
1513 | else if EQ("pushstring") { | 1523 | else if EQ("pushstring") { |
1514 | lua_pushstring(L1, getstring); | 1524 | lua_pushstring(L1, getstring); |
@@ -1710,7 +1720,7 @@ static int Cfunc (lua_State *L) { | |||
1710 | 1720 | ||
1711 | 1721 | ||
1712 | static int Cfunck (lua_State *L, int status, lua_KContext ctx) { | 1722 | static int Cfunck (lua_State *L, int status, lua_KContext ctx) { |
1713 | pushcode(L, status); | 1723 | lua_pushstring(L, statcodes[status]); |
1714 | lua_setglobal(L, "status"); | 1724 | lua_setglobal(L, "status"); |
1715 | lua_pushinteger(L, ctx); | 1725 | lua_pushinteger(L, ctx); |
1716 | lua_setglobal(L, "ctx"); | 1726 | lua_setglobal(L, "ctx"); |
@@ -1865,6 +1875,9 @@ int luaB_opentests (lua_State *L) { | |||
1865 | void *ud; | 1875 | void *ud; |
1866 | lua_atpanic(L, &tpanic); | 1876 | lua_atpanic(L, &tpanic); |
1867 | lua_setwarnf(L, &warnf, L); | 1877 | lua_setwarnf(L, &warnf, L); |
1878 | lua_pushboolean(L, 0); | ||
1879 | lua_setglobal(L, "_WARN"); /* _WARN = false */ | ||
1880 | regcodes(L); | ||
1868 | atexit(checkfinalmem); | 1881 | atexit(checkfinalmem); |
1869 | lua_assert(lua_getallocf(L, &ud) == debug_realloc); | 1882 | lua_assert(lua_getallocf(L, &ud) == debug_realloc); |
1870 | lua_assert(ud == cast_voidp(&l_memcontrol)); | 1883 | lua_assert(ud == cast_voidp(&l_memcontrol)); |