diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2021-02-24 11:14:44 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2021-02-24 11:14:44 -0300 |
commit | 59c88f846d1dcd901a4420651aedf27816618923 (patch) | |
tree | 0e76a066c383cbc99cc2f60b8b4f97c5df45e479 /liolib.c | |
parent | c03c527fd207b4ad8f5a8e0f4f2c176bd227c979 (diff) | |
download | lua-59c88f846d1dcd901a4420651aedf27816618923.tar.gz lua-59c88f846d1dcd901a4420651aedf27816618923.tar.bz2 lua-59c88f846d1dcd901a4420651aedf27816618923.zip |
Broadening the use of branch hints
More uses of macros 'likely'/'unlikely' (renamed to
'l_likely'/'l_unlikely'), both in range (extended to the
libraries) and in scope (extended to hooks, stack growth).
Diffstat (limited to 'liolib.c')
-rw-r--r-- | liolib.c | 17 |
1 files changed, 9 insertions, 8 deletions
@@ -186,7 +186,7 @@ static int f_tostring (lua_State *L) { | |||
186 | 186 | ||
187 | static FILE *tofile (lua_State *L) { | 187 | static FILE *tofile (lua_State *L) { |
188 | LStream *p = tolstream(L); | 188 | LStream *p = tolstream(L); |
189 | if (isclosed(p)) | 189 | if (l_unlikely(isclosed(p))) |
190 | luaL_error(L, "attempt to use a closed file"); | 190 | luaL_error(L, "attempt to use a closed file"); |
191 | lua_assert(p->f); | 191 | lua_assert(p->f); |
192 | return p->f; | 192 | return p->f; |
@@ -261,7 +261,7 @@ static LStream *newfile (lua_State *L) { | |||
261 | static void opencheck (lua_State *L, const char *fname, const char *mode) { | 261 | static void opencheck (lua_State *L, const char *fname, const char *mode) { |
262 | LStream *p = newfile(L); | 262 | LStream *p = newfile(L); |
263 | p->f = fopen(fname, mode); | 263 | p->f = fopen(fname, mode); |
264 | if (p->f == NULL) | 264 | if (l_unlikely(p->f == NULL)) |
265 | luaL_error(L, "cannot open file '%s' (%s)", fname, strerror(errno)); | 265 | luaL_error(L, "cannot open file '%s' (%s)", fname, strerror(errno)); |
266 | } | 266 | } |
267 | 267 | ||
@@ -309,7 +309,7 @@ static FILE *getiofile (lua_State *L, const char *findex) { | |||
309 | LStream *p; | 309 | LStream *p; |
310 | lua_getfield(L, LUA_REGISTRYINDEX, findex); | 310 | lua_getfield(L, LUA_REGISTRYINDEX, findex); |
311 | p = (LStream *)lua_touserdata(L, -1); | 311 | p = (LStream *)lua_touserdata(L, -1); |
312 | if (isclosed(p)) | 312 | if (l_unlikely(isclosed(p))) |
313 | luaL_error(L, "default %s file is closed", findex + IOPREF_LEN); | 313 | luaL_error(L, "default %s file is closed", findex + IOPREF_LEN); |
314 | return p->f; | 314 | return p->f; |
315 | } | 315 | } |
@@ -436,7 +436,7 @@ typedef struct { | |||
436 | ** Add current char to buffer (if not out of space) and read next one | 436 | ** Add current char to buffer (if not out of space) and read next one |
437 | */ | 437 | */ |
438 | static int nextc (RN *rn) { | 438 | static int nextc (RN *rn) { |
439 | if (rn->n >= L_MAXLENNUM) { /* buffer overflow? */ | 439 | if (l_unlikely(rn->n >= L_MAXLENNUM)) { /* buffer overflow? */ |
440 | rn->buff[0] = '\0'; /* invalidate result */ | 440 | rn->buff[0] = '\0'; /* invalidate result */ |
441 | return 0; /* fail */ | 441 | return 0; /* fail */ |
442 | } | 442 | } |
@@ -499,8 +499,8 @@ static int read_number (lua_State *L, FILE *f) { | |||
499 | ungetc(rn.c, rn.f); /* unread look-ahead char */ | 499 | ungetc(rn.c, rn.f); /* unread look-ahead char */ |
500 | l_unlockfile(rn.f); | 500 | l_unlockfile(rn.f); |
501 | rn.buff[rn.n] = '\0'; /* finish string */ | 501 | rn.buff[rn.n] = '\0'; /* finish string */ |
502 | if (lua_stringtonumber(L, rn.buff)) /* is this a valid number? */ | 502 | if (l_likely(lua_stringtonumber(L, rn.buff))) |
503 | return 1; /* ok */ | 503 | return 1; /* ok, it is a valid number */ |
504 | else { /* invalid format */ | 504 | else { /* invalid format */ |
505 | lua_pushnil(L); /* "result" to be removed */ | 505 | lua_pushnil(L); /* "result" to be removed */ |
506 | return 0; /* read fails */ | 506 | return 0; /* read fails */ |
@@ -676,7 +676,8 @@ static int g_write (lua_State *L, FILE *f, int arg) { | |||
676 | status = status && (fwrite(s, sizeof(char), l, f) == l); | 676 | status = status && (fwrite(s, sizeof(char), l, f) == l); |
677 | } | 677 | } |
678 | } | 678 | } |
679 | if (status) return 1; /* file handle already on stack top */ | 679 | if (l_likely(status)) |
680 | return 1; /* file handle already on stack top */ | ||
680 | else return luaL_fileresult(L, status, NULL); | 681 | else return luaL_fileresult(L, status, NULL); |
681 | } | 682 | } |
682 | 683 | ||
@@ -703,7 +704,7 @@ static int f_seek (lua_State *L) { | |||
703 | luaL_argcheck(L, (lua_Integer)offset == p3, 3, | 704 | luaL_argcheck(L, (lua_Integer)offset == p3, 3, |
704 | "not an integer in proper range"); | 705 | "not an integer in proper range"); |
705 | op = l_fseek(f, offset, mode[op]); | 706 | op = l_fseek(f, offset, mode[op]); |
706 | if (op) | 707 | if (l_unlikely(op)) |
707 | return luaL_fileresult(L, 0, NULL); /* error */ | 708 | return luaL_fileresult(L, 0, NULL); /* error */ |
708 | else { | 709 | else { |
709 | lua_pushinteger(L, (lua_Integer)l_ftell(f)); | 710 | lua_pushinteger(L, (lua_Integer)l_ftell(f)); |