diff options
Diffstat (limited to 'lauxlib.c')
-rw-r--r-- | lauxlib.c | 46 |
1 files changed, 45 insertions, 1 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lauxlib.c,v 1.294 2018/02/27 18:47:32 roberto Exp roberto $ | 2 | ** $Id: lauxlib.c $ |
3 | ** Auxiliary functions for building Lua libraries | 3 | ** Auxiliary functions for building Lua libraries |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -292,6 +292,50 @@ LUALIB_API int luaL_execresult (lua_State *L, int stat) { | |||
292 | 292 | ||
293 | /* | 293 | /* |
294 | ** {====================================================== | 294 | ** {====================================================== |
295 | ** 'luaL_resourcetryagain' | ||
296 | ** This function uses 'errno' to check whether the last error was | ||
297 | ** related to lack of resources (e.g., not enough memory or too many | ||
298 | ** open files). If so, the function performs a full garbage collection | ||
299 | ** to try to release resources, and then it returns 1 to signal to | ||
300 | ** the caller that it is worth trying again the failed operation. | ||
301 | ** Otherwise, it returns 0. Because error codes are not ANSI C, the | ||
302 | ** code must handle any combination of error codes that are defined. | ||
303 | ** ======================================================= | ||
304 | */ | ||
305 | |||
306 | LUALIB_API int luaL_resourcetryagain (lua_State *L) { | ||
307 | |||
308 | /* these are the resource-related errors in Linux */ | ||
309 | #if defined(EMFILE) || defined(ENFILE) || defined(ENOMEM) | ||
310 | |||
311 | #if !defined(EMFILE) /* too many open files in the process */ | ||
312 | #define EMFILE -1 /* if not defined, use an impossible value */ | ||
313 | #endif | ||
314 | |||
315 | #if !defined(ENFILE) /* too many open files in the system */ | ||
316 | #define ENFILE -1 | ||
317 | #endif | ||
318 | |||
319 | #if !defined(ENOMEM) /* not enough memory */ | ||
320 | #define ENOMEM -1 | ||
321 | #endif | ||
322 | |||
323 | if (errno == EMFILE || errno == ENFILE || errno == ENOMEM) { | ||
324 | lua_gc(L, LUA_GCCOLLECT); /* try to release resources with a full GC */ | ||
325 | return 1; /* signal to try again the creation */ | ||
326 | } | ||
327 | |||
328 | #endif | ||
329 | |||
330 | return 0; /* else, asume errors are not due to lack of resources */ | ||
331 | |||
332 | } | ||
333 | |||
334 | /* }====================================================== */ | ||
335 | |||
336 | |||
337 | /* | ||
338 | ** {====================================================== | ||
295 | ** Userdata's metatable manipulation | 339 | ** Userdata's metatable manipulation |
296 | ** ======================================================= | 340 | ** ======================================================= |
297 | */ | 341 | */ |