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 /loadlib.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 'loadlib.c')
-rw-r--r-- | loadlib.c | 17 |
1 files changed, 10 insertions, 7 deletions
@@ -132,14 +132,16 @@ static void lsys_unloadlib (void *lib) { | |||
132 | 132 | ||
133 | static void *lsys_load (lua_State *L, const char *path, int seeglb) { | 133 | static void *lsys_load (lua_State *L, const char *path, int seeglb) { |
134 | void *lib = dlopen(path, RTLD_NOW | (seeglb ? RTLD_GLOBAL : RTLD_LOCAL)); | 134 | void *lib = dlopen(path, RTLD_NOW | (seeglb ? RTLD_GLOBAL : RTLD_LOCAL)); |
135 | if (lib == NULL) lua_pushstring(L, dlerror()); | 135 | if (l_unlikely(lib == NULL)) |
136 | lua_pushstring(L, dlerror()); | ||
136 | return lib; | 137 | return lib; |
137 | } | 138 | } |
138 | 139 | ||
139 | 140 | ||
140 | static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { | 141 | static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { |
141 | lua_CFunction f = cast_func(dlsym(lib, sym)); | 142 | lua_CFunction f = cast_func(dlsym(lib, sym)); |
142 | if (f == NULL) lua_pushstring(L, dlerror()); | 143 | if (l_unlikely(f == NULL)) |
144 | lua_pushstring(L, dlerror()); | ||
143 | return f; | 145 | return f; |
144 | } | 146 | } |
145 | 147 | ||
@@ -410,7 +412,7 @@ static int ll_loadlib (lua_State *L) { | |||
410 | const char *path = luaL_checkstring(L, 1); | 412 | const char *path = luaL_checkstring(L, 1); |
411 | const char *init = luaL_checkstring(L, 2); | 413 | const char *init = luaL_checkstring(L, 2); |
412 | int stat = lookforfunc(L, path, init); | 414 | int stat = lookforfunc(L, path, init); |
413 | if (stat == 0) /* no errors? */ | 415 | if (l_likely(stat == 0)) /* no errors? */ |
414 | return 1; /* return the loaded function */ | 416 | return 1; /* return the loaded function */ |
415 | else { /* error; error message is on stack top */ | 417 | else { /* error; error message is on stack top */ |
416 | luaL_pushfail(L); | 418 | luaL_pushfail(L); |
@@ -523,14 +525,14 @@ static const char *findfile (lua_State *L, const char *name, | |||
523 | const char *path; | 525 | const char *path; |
524 | lua_getfield(L, lua_upvalueindex(1), pname); | 526 | lua_getfield(L, lua_upvalueindex(1), pname); |
525 | path = lua_tostring(L, -1); | 527 | path = lua_tostring(L, -1); |
526 | if (path == NULL) | 528 | if (l_unlikely(path == NULL)) |
527 | luaL_error(L, "'package.%s' must be a string", pname); | 529 | luaL_error(L, "'package.%s' must be a string", pname); |
528 | return searchpath(L, name, path, ".", dirsep); | 530 | return searchpath(L, name, path, ".", dirsep); |
529 | } | 531 | } |
530 | 532 | ||
531 | 533 | ||
532 | static int checkload (lua_State *L, int stat, const char *filename) { | 534 | static int checkload (lua_State *L, int stat, const char *filename) { |
533 | if (stat) { /* module loaded successfully? */ | 535 | if (l_likely(stat)) { /* module loaded successfully? */ |
534 | lua_pushstring(L, filename); /* will be 2nd argument to module */ | 536 | lua_pushstring(L, filename); /* will be 2nd argument to module */ |
535 | return 2; /* return open function and file name */ | 537 | return 2; /* return open function and file name */ |
536 | } | 538 | } |
@@ -623,13 +625,14 @@ static void findloader (lua_State *L, const char *name) { | |||
623 | int i; | 625 | int i; |
624 | luaL_Buffer msg; /* to build error message */ | 626 | luaL_Buffer msg; /* to build error message */ |
625 | /* push 'package.searchers' to index 3 in the stack */ | 627 | /* push 'package.searchers' to index 3 in the stack */ |
626 | if (lua_getfield(L, lua_upvalueindex(1), "searchers") != LUA_TTABLE) | 628 | if (l_unlikely(lua_getfield(L, lua_upvalueindex(1), "searchers") |
629 | != LUA_TTABLE)) | ||
627 | luaL_error(L, "'package.searchers' must be a table"); | 630 | luaL_error(L, "'package.searchers' must be a table"); |
628 | luaL_buffinit(L, &msg); | 631 | luaL_buffinit(L, &msg); |
629 | /* iterate over available searchers to find a loader */ | 632 | /* iterate over available searchers to find a loader */ |
630 | for (i = 1; ; i++) { | 633 | for (i = 1; ; i++) { |
631 | luaL_addstring(&msg, "\n\t"); /* error-message prefix */ | 634 | luaL_addstring(&msg, "\n\t"); /* error-message prefix */ |
632 | if (lua_rawgeti(L, 3, i) == LUA_TNIL) { /* no more searchers? */ | 635 | if (l_unlikely(lua_rawgeti(L, 3, i) == LUA_TNIL)) { /* no more searchers? */ |
633 | lua_pop(L, 1); /* remove nil */ | 636 | lua_pop(L, 1); /* remove nil */ |
634 | luaL_buffsub(&msg, 2); /* remove prefix */ | 637 | luaL_buffsub(&msg, 2); /* remove prefix */ |
635 | luaL_pushresult(&msg); /* create error message */ | 638 | luaL_pushresult(&msg); /* create error message */ |