]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: lua-thread: Replace "struct hlua_function" allocation by dedicated function
authorThierry Fournier <thierry.fournier@ozon.io>
Sat, 28 Nov 2020 20:06:35 +0000 (21:06 +0100)
committerWilly Tarreau <w@1wt.eu>
Wed, 2 Dec 2020 20:53:16 +0000 (21:53 +0100)
The goal is to allow execution of one main lua state per thread.

This function will initialize the struct with other things than 0.
With this function helper, the initialization is centralized and
it prevents mistakes. This patch also keeps a reference to each
declared function in a list. It will be useful in next patches to
control consistency of declared references.

include/haproxy/hlua-t.h
src/hlua.c

index 12c3cf2fc6c8cbf8cb4eefc26cf9c5a76cdcae72..86a3276721bcfce1cf887039604b1b8f052eec9d 100644 (file)
@@ -111,6 +111,7 @@ struct hlua_init_function {
  * or actions.
  */
 struct hlua_function {
+       struct list l;
        char *name;
        int function_ref;
        int nargs;
index 6cb5e2c3b9e78b07d31a95cb472002a0a9aef000..3e903110f20b9c8566674c72ff1b19e4d745f1dd 100644 (file)
@@ -124,6 +124,12 @@ THREAD_LOCAL jmp_buf safe_ljmp_env;
 static int hlua_panic_safe(lua_State *L) { return 0; }
 static int hlua_panic_ljmp(lua_State *L) { WILL_LJMP(longjmp(safe_ljmp_env, 1)); }
 
+/* This is the chained list of struct hlua_function referenced
+ * for haproxy action, sample-fetches, converters, cli and
+ * applet bindings. It is used for a post-initialisation control.
+ */
+static struct list referenced_functions = LIST_HEAD_INIT(referenced_functions);
+
 #define SET_SAFE_LJMP_L(__L, __HLUA) \
        ({ \
                int ret; \
@@ -277,6 +283,17 @@ __LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg);
                        ha_alert(__fmt, ## __args); \
        } while (0)
 
+static inline struct hlua_function *new_hlua_function()
+{
+       struct hlua_function *fcn;
+
+       fcn = calloc(1, sizeof(*fcn));
+       if (!fcn)
+               return NULL;
+       LIST_ADDQ(&referenced_functions, &fcn->l);
+       return fcn;
+}
+
 /* Used to check an Lua function type in the stack. It creates and
  * returns a reference of the function. This function throws an
  * error if the rgument is not a "function".
@@ -6683,7 +6700,7 @@ __LJMP static int hlua_register_converters(lua_State *L)
        sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
        if (!sck)
                WILL_LJMP(luaL_error(L, "Lua out of memory error."));
-       fcn = calloc(1, sizeof(*fcn));
+       fcn = new_hlua_function();
        if (!fcn)
                WILL_LJMP(luaL_error(L, "Lua out of memory error."));
 
@@ -6751,7 +6768,7 @@ __LJMP static int hlua_register_fetches(lua_State *L)
        sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
        if (!sfk)
                WILL_LJMP(luaL_error(L, "Lua out of memory error."));
-       fcn = calloc(1, sizeof(*fcn));
+       fcn = new_hlua_function();
        if (!fcn)
                WILL_LJMP(luaL_error(L, "Lua out of memory error."));
 
@@ -7616,7 +7633,7 @@ __LJMP static int hlua_register_action(lua_State *L)
                akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
                if (!akl)
                        WILL_LJMP(luaL_error(L, "Lua out of memory error."));
-               fcn = calloc(1, sizeof(*fcn));
+               fcn = new_hlua_function();
                if (!fcn)
                        WILL_LJMP(luaL_error(L, "Lua out of memory error."));
 
@@ -7737,7 +7754,7 @@ __LJMP static int hlua_register_service(lua_State *L)
        akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
        if (!akl)
                WILL_LJMP(luaL_error(L, "Lua out of memory error."));
-       fcn = calloc(1, sizeof(*fcn));
+       fcn = new_hlua_function();
        if (!fcn)
                WILL_LJMP(luaL_error(L, "Lua out of memory error."));
 
@@ -8009,7 +8026,7 @@ __LJMP static int hlua_register_cli(lua_State *L)
        cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
        if (!cli_kws)
                WILL_LJMP(luaL_error(L, "Lua out of memory error."));
-       fcn = calloc(1, sizeof(*fcn));
+       fcn = new_hlua_function();
        if (!fcn)
                WILL_LJMP(luaL_error(L, "Lua out of memory error."));