From cc5576476adaea4ae6a10c61d0c3aafdabe45fa1 Mon Sep 17 00:00:00 2001 From: Donat Zenichev Date: Mon, 1 Jun 2026 17:30:40 +0200 Subject: [PATCH] MT#61856 control_ng: add func for command lookup Instead of the previous approach using the strhash, introduce if-based lookup from `NG_COMMANDS`. The usage of strhash is deprecated because it: - forses string literals in switch cases, hence gives no possibility to use variables - makes then shortcuts using macro not possible to be used - it makes the part with the command find and actual commands definition duplicate, because one has to maintain both of them when adding/removing commands - switch is redundant and feels heavy, meanwhile the same thing can be generated using marco based on `NG_COMMANDS` introduced before, which are the main source of the truth Newer approach: - avoid usage of for/while cycles (because CPU consuming) - sorts the lookup based on the cmd name (string) length, what makes the command find efficient, because exlcludes unnecessary comparisons - uses the `memcmp()` at the very last - for example, for the "offer" command it may run the `memcmp()` only two times, because there are only two commands with the length 5 our of supported 35 commands in list now Additionally: include `string.h` for `memcmp()` operations. Change-Id: Iae70de87b88cd444ae162ba2b7c6aa7690731ee3 --- daemon/control_ng.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/daemon/control_ng.c b/daemon/control_ng.c index a28975515..c62cf87d8 100644 --- a/daemon/control_ng.c +++ b/daemon/control_ng.c @@ -16,6 +16,7 @@ #include "statistics.h" #include "streambuf.h" #include "homer.h" +#include mutex_t rtpe_cngs_lock; mutex_t tcp_connections_lock; @@ -55,6 +56,26 @@ static const struct ng_command_def ng_command_defs[OP_COUNT] = { #undef X }; +static const struct ng_command_def *ng_command_find(const str *cmd) { + if (!cmd || !cmd->s) + return NULL; + +#define X(op, name, esc, short_name, handler) \ + if (cmd->len == sizeof(name) - 1 && !memcmp(cmd->s, name, sizeof(name) - 1)) \ + return &ng_command_defs[op]; + +#define XA(op, name, esc, short_name, handler) \ + if (cmd->len == sizeof(name) - 1 && !memcmp(cmd->s, name, sizeof(name) - 1)) \ + return &ng_command_defs[op]; + + NG_COMMANDS(X, XA) + +#undef XA +#undef X + + return NULL; +} + const char *ng_command_strings[OP_COUNT] = { #define X(op, name, esc, short_name, handler) [op] = name, #define XA(op, name, esc, short_name, handler) [op] = name,