aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2018-10-31 16:25:29 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2018-10-31 16:25:29 -0300
commit2fc6b55dae7a120b4272ca0e9c356d1f96053bd9 (patch)
tree073360734518f75a4f5621d87a4b47bc4642452a
parent947a372f5860a76fcafb4a2845abc322e440d6fc (diff)
downloadlua-2fc6b55dae7a120b4272ca0e9c356d1f96053bd9.tar.gz
lua-2fc6b55dae7a120b4272ca0e9c356d1f96053bd9.tar.bz2
lua-2fc6b55dae7a120b4272ca0e9c356d1f96053bd9.zip
Removed resource-related "emergency collections"
New to-be-closed variables is a better way to ensure the proper release of resources.
-rw-r--r--lauxlib.c43
-rw-r--r--lauxlib.h2
-rw-r--r--liolib.c21
-rw-r--r--loslib.c2
-rw-r--r--manual/manual.of14
5 files changed, 2 insertions, 80 deletions
diff --git a/lauxlib.c b/lauxlib.c
index 53b8c9bb..78dfb4e9 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -290,49 +290,6 @@ LUALIB_API int luaL_execresult (lua_State *L, int stat) {
290/* }====================================================== */ 290/* }====================================================== */
291 291
292 292
293/*
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
306LUALIB_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 293
337/* 294/*
338** {====================================================== 295** {======================================================
diff --git a/lauxlib.h b/lauxlib.h
index cd4d01e5..9ec0f531 100644
--- a/lauxlib.h
+++ b/lauxlib.h
@@ -77,8 +77,6 @@ LUALIB_API int (luaL_checkoption) (lua_State *L, int arg, const char *def,
77LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname); 77LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname);
78LUALIB_API int (luaL_execresult) (lua_State *L, int stat); 78LUALIB_API int (luaL_execresult) (lua_State *L, int stat);
79 79
80LUALIB_API int (luaL_resourcetryagain) (lua_State *L);
81
82 80
83/* predefined references */ 81/* predefined references */
84#define LUA_NOREF (-2) 82#define LUA_NOREF (-2)
diff --git a/liolib.c b/liolib.c
index 5881b029..b2a2fec8 100644
--- a/liolib.c
+++ b/liolib.c
@@ -246,22 +246,9 @@ static LStream *newfile (lua_State *L) {
246} 246}
247 247
248 248
249/*
250** Equivalent to 'fopen', but if it fails due to a lack of resources
251** (see 'luaL_resourcetryagain'), do an "emergency" garbage collection
252** to try to close some files and then tries to open the file again.
253*/
254static FILE *trytoopen (lua_State *L, const char *path, const char *mode) {
255 FILE *f = fopen(path, mode);
256 if (f == NULL && luaL_resourcetryagain(L)) /* resource failure? */
257 f = fopen(path, mode); /* try to open again */
258 return f;
259}
260
261
262static void opencheck (lua_State *L, const char *fname, const char *mode) { 249static void opencheck (lua_State *L, const char *fname, const char *mode) {
263 LStream *p = newfile(L); 250 LStream *p = newfile(L);
264 p->f = trytoopen(L, fname, mode); 251 p->f = fopen(fname, mode);
265 if (p->f == NULL) 252 if (p->f == NULL)
266 luaL_error(L, "cannot open file '%s' (%s)", fname, strerror(errno)); 253 luaL_error(L, "cannot open file '%s' (%s)", fname, strerror(errno));
267} 254}
@@ -273,7 +260,7 @@ static int io_open (lua_State *L) {
273 LStream *p = newfile(L); 260 LStream *p = newfile(L);
274 const char *md = mode; /* to traverse/check mode */ 261 const char *md = mode; /* to traverse/check mode */
275 luaL_argcheck(L, l_checkmode(md), 2, "invalid mode"); 262 luaL_argcheck(L, l_checkmode(md), 2, "invalid mode");
276 p->f = trytoopen(L, filename, mode); 263 p->f = fopen(filename, mode);
277 return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; 264 return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1;
278} 265}
279 266
@@ -292,8 +279,6 @@ static int io_popen (lua_State *L) {
292 const char *mode = luaL_optstring(L, 2, "r"); 279 const char *mode = luaL_optstring(L, 2, "r");
293 LStream *p = newprefile(L); 280 LStream *p = newprefile(L);
294 p->f = l_popen(L, filename, mode); 281 p->f = l_popen(L, filename, mode);
295 if (p->f == NULL && luaL_resourcetryagain(L)) /* resource failure? */
296 p->f = l_popen(L, filename, mode); /* try to open again */
297 p->closef = &io_pclose; 282 p->closef = &io_pclose;
298 return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; 283 return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1;
299} 284}
@@ -302,8 +287,6 @@ static int io_popen (lua_State *L) {
302static int io_tmpfile (lua_State *L) { 287static int io_tmpfile (lua_State *L) {
303 LStream *p = newfile(L); 288 LStream *p = newfile(L);
304 p->f = tmpfile(); 289 p->f = tmpfile();
305 if (p->f == NULL && luaL_resourcetryagain(L)) /* resource failure? */
306 p->f = tmpfile(); /* try to open again */
307 return (p->f == NULL) ? luaL_fileresult(L, 0, NULL) : 1; 290 return (p->f == NULL) ? luaL_fileresult(L, 0, NULL) : 1;
308} 291}
309 292
diff --git a/loslib.c b/loslib.c
index 1962f55f..8809e5ea 100644
--- a/loslib.c
+++ b/loslib.c
@@ -166,8 +166,6 @@ static int os_tmpname (lua_State *L) {
166 char buff[LUA_TMPNAMBUFSIZE]; 166 char buff[LUA_TMPNAMBUFSIZE];
167 int err; 167 int err;
168 lua_tmpnam(buff, err); 168 lua_tmpnam(buff, err);
169 if (err && luaL_resourcetryagain(L)) /* resource failure? */
170 lua_tmpnam(buff, err); /* try again */
171 if (err) 169 if (err)
172 return luaL_error(L, "unable to generate a unique filename"); 170 return luaL_error(L, "unable to generate a unique filename");
173 lua_pushstring(L, buff); 171 lua_pushstring(L, buff);
diff --git a/manual/manual.of b/manual/manual.of
index d8bac5da..91ba8c46 100644
--- a/manual/manual.of
+++ b/manual/manual.of
@@ -5535,20 +5535,6 @@ Leaves a copy of the module on the stack.
5535 5535
5536} 5536}
5537 5537
5538@APIEntry{int luaL_resourcetryagain (lua_State *L);|
5539@apii{0,0,m}
5540
5541Try to release resources in case of errors.
5542This function uses @id{errno} to check whether the last error was
5543related to lack of resources (e.g., not enough memory or too many
5544open files).
5545If so, the function performs a full garbage collection
5546to try to release resources, and then it returns 1 to signal to
5547the caller that it is worth trying again the failed operation.
5548Otherwise, it returns 0.
5549
5550}
5551
5552@APIEntry{void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup);| 5538@APIEntry{void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup);|
5553@apii{nup,0,m} 5539@apii{nup,0,m}
5554 5540