From ca20d02ea8d5508fdbccf4319c0d7c83e67817bd Mon Sep 17 00:00:00 2001 From: Christopher Faulet Date: Tue, 29 Aug 2017 15:30:31 +0200 Subject: [PATCH] MINOR: stick-tables: Make static_table_key a struct variable instead of a pointer First, this variable does not need to be publicly exposed because it is only used by stick_table functions. So we declare it as a global static in stick_table.c file. Then, it is useless to use a pointer. Using a plain struct variable avoids any dynamic allocation. --- include/proto/stick_table.h | 2 -- src/haproxy.c | 2 -- src/stick_table.c | 38 ++++++++++++++++++------------------- 3 files changed, 19 insertions(+), 23 deletions(-) diff --git a/include/proto/stick_table.h b/include/proto/stick_table.h index a5fd4000c5..f48b9eb496 100644 --- a/include/proto/stick_table.h +++ b/include/proto/stick_table.h @@ -31,8 +31,6 @@ #define stktable_data_size(type) (sizeof(((union stktable_data*)0)->type)) #define stktable_data_cast(ptr, type) ((union stktable_data*)(ptr))->type -extern struct stktable_key *static_table_key; - struct stksess *stksess_new(struct stktable *t, struct stktable_key *key); void stksess_setkey(struct stktable *t, struct stksess *ts, struct stktable_key *key); void stksess_free(struct stktable *t, struct stksess *ts); diff --git a/src/haproxy.c b/src/haproxy.c index 75afd97bdb..27d4417353 100644 --- a/src/haproxy.c +++ b/src/haproxy.c @@ -1719,7 +1719,6 @@ static void init(int argc, char **argv) } get_http_auth_buff = calloc(1, global.tune.bufsize); - static_table_key = calloc(1, sizeof(*static_table_key)); fdinfo = calloc(1, sizeof(struct fdinfo) * (global.maxsock)); fdtab = calloc(1, sizeof(struct fdtab) * (global.maxsock)); @@ -2123,7 +2122,6 @@ void deinit(void) free(fdinfo); fdinfo = NULL; free(fdtab); fdtab = NULL; free(oldpids); oldpids = NULL; - free(static_table_key); static_table_key = NULL; free(get_http_auth_buff); get_http_auth_buff = NULL; free(global_listener_queue_task); global_listener_queue_task = NULL; diff --git a/src/stick_table.c b/src/stick_table.c index a00f1b6458..0be95fe5b4 100644 --- a/src/stick_table.c +++ b/src/stick_table.c @@ -41,7 +41,7 @@ #include /* structure used to return a table key built from a sample */ -struct stktable_key *static_table_key; +static struct stktable_key static_table_key; /* * Free an allocated sticky session , and decrease sticky sessions counter @@ -510,13 +510,13 @@ struct stktable_key *smp_to_stkey(struct sample *smp, struct stktable *t) switch (t->type) { case SMP_T_IPV4: - static_table_key->key = &smp->data.u.ipv4; - static_table_key->key_len = 4; + static_table_key.key = &smp->data.u.ipv4; + static_table_key.key_len = 4; break; case SMP_T_IPV6: - static_table_key->key = &smp->data.u.ipv6; - static_table_key->key_len = 16; + static_table_key.key = &smp->data.u.ipv6; + static_table_key.key_len = 16; break; case SMP_T_SINT: @@ -524,15 +524,15 @@ struct stktable_key *smp_to_stkey(struct sample *smp, struct stktable *t) * signed 64 it, so we can convert it inplace. */ *(unsigned int *)&smp->data.u.sint = (unsigned int)smp->data.u.sint; - static_table_key->key = &smp->data.u.sint; - static_table_key->key_len = 4; + static_table_key.key = &smp->data.u.sint; + static_table_key.key_len = 4; break; case SMP_T_STR: if (!smp_make_safe(smp)) return NULL; - static_table_key->key = smp->data.u.str.str; - static_table_key->key_len = smp->data.u.str.len; + static_table_key.key = smp->data.u.str.str; + static_table_key.key_len = smp->data.u.str.len; break; case SMP_T_BIN: @@ -550,15 +550,15 @@ struct stktable_key *smp_to_stkey(struct sample *smp, struct stktable *t) t->key_size - smp->data.u.str.len); smp->data.u.str.len = t->key_size; } - static_table_key->key = smp->data.u.str.str; - static_table_key->key_len = smp->data.u.str.len; + static_table_key.key = smp->data.u.str.str; + static_table_key.key_len = smp->data.u.str.len; break; default: /* impossible case. */ return NULL; } - return static_table_key; + return &static_table_key; } /* @@ -2362,11 +2362,11 @@ static int table_process_entry_per_key(struct appctx *appctx, char **args) switch (px->table.type) { case SMP_T_IPV4: uint32_key = htonl(inetaddr_host(args[4])); - static_table_key->key = &uint32_key; + static_table_key.key = &uint32_key; break; case SMP_T_IPV6: inet_pton(AF_INET6, args[4], ip6_key); - static_table_key->key = &ip6_key; + static_table_key.key = &ip6_key; break; case SMP_T_SINT: { @@ -2382,13 +2382,13 @@ static int table_process_entry_per_key(struct appctx *appctx, char **args) return 1; } uint32_key = (uint32_t) val; - static_table_key->key = &uint32_key; + static_table_key.key = &uint32_key; break; } break; case SMP_T_STR: - static_table_key->key = args[4]; - static_table_key->key_len = strlen(args[4]); + static_table_key.key = args[4]; + static_table_key.key_len = strlen(args[4]); break; default: switch (appctx->ctx.table.action) { @@ -2413,7 +2413,7 @@ static int table_process_entry_per_key(struct appctx *appctx, char **args) if (!cli_has_level(appctx, ACCESS_LVL_OPER)) return 1; - ts = stktable_lookup_key(&px->table, static_table_key); + ts = stktable_lookup_key(&px->table, &static_table_key); switch (appctx->ctx.table.action) { case STK_CLI_ACT_SHOW: @@ -2442,7 +2442,7 @@ static int table_process_entry_per_key(struct appctx *appctx, char **args) if (ts) stktable_touch(&px->table, ts, 1); else { - ts = stksess_new(&px->table, static_table_key); + ts = stksess_new(&px->table, &static_table_key); if (!ts) { /* don't delete an entry which is currently referenced */ appctx->ctx.cli.msg = "Unable to allocate a new entry\n";