]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
networkd: replace a table with log2 fields by a list
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 26 Nov 2021 10:51:12 +0000 (11:51 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 29 Nov 2021 10:15:31 +0000 (11:15 +0100)
The code looks a bit more complicated, but the compiler generates a simpler and
more compact text.

An additional advantage is that if any of the fields were repeating or not
power-of-two, the compiler would warn about an overridden entry in the table.

src/network/meson.build
src/network/networkd-util.c
src/network/test-networkd-util.c [new file with mode: 0644]

index 9247734c75c7c7a99dbb510750f9410e7b684d44..cfa16a8ecf08a4481d03c7f53a07b36eaff49739 100644 (file)
@@ -284,6 +284,12 @@ tests += [
          [],
          network_includes],
 
+        [['src/network/test-networkd-util.c'],
+         [libnetworkd_core,
+          libsystemd_network],
+         [],
+         network_includes],
+
         [['src/network/test-network.c'],
          [libnetworkd_core,
           libsystemd_network],
index b0020efe8993a586716a9f2eb21b5cb024ce9975..6011e9b998a612058faff009e2f5b9fb4b8d71e6 100644 (file)
@@ -24,25 +24,25 @@ static const char * const network_config_source_table[_NETWORK_CONFIG_SOURCE_MAX
 DEFINE_STRING_TABLE_LOOKUP_TO_STRING(network_config_source, NetworkConfigSource);
 
 int network_config_state_to_string_alloc(NetworkConfigState s, char **ret) {
-        static const struct {
-                NetworkConfigState state;
-                const char *str;
-        } map[] = {
-                { .state = NETWORK_CONFIG_STATE_PROBING,     .str = "probing",     },
-                { .state = NETWORK_CONFIG_STATE_REQUESTING,  .str = "requesting",  },
-                { .state = NETWORK_CONFIG_STATE_CONFIGURING, .str = "configuring", },
-                { .state = NETWORK_CONFIG_STATE_CONFIGURED,  .str = "configured",  },
-                { .state = NETWORK_CONFIG_STATE_MARKED,      .str = "marked",      },
-                { .state = NETWORK_CONFIG_STATE_REMOVING,    .str = "removing",    },
+        static const char* states[] = {
+                [LOG2U(NETWORK_CONFIG_STATE_PROBING)]     = "probing",
+                [LOG2U(NETWORK_CONFIG_STATE_REQUESTING)]  = "requesting",
+                [LOG2U(NETWORK_CONFIG_STATE_CONFIGURING)] = "configuring",
+                [LOG2U(NETWORK_CONFIG_STATE_CONFIGURED)]  = "configured",
+                [LOG2U(NETWORK_CONFIG_STATE_MARKED)]      = "marked",
+                [LOG2U(NETWORK_CONFIG_STATE_REMOVING)]    = "removing",
         };
         _cleanup_free_ char *buf = NULL;
 
         assert(ret);
 
-        for (size_t i = 0; i < ELEMENTSOF(map); i++)
-                if (FLAGS_SET(s, map[i].state) &&
-                    !strextend_with_separator(&buf, ",", map[i].str))
-                        return -ENOMEM;
+        for (size_t i = 0; i < ELEMENTSOF(states); i++)
+                if (FLAGS_SET(s, 1 << i)) {
+                        assert(states[i]);
+
+                        if (!strextend_with_separator(&buf, ",", states[i]))
+                                return -ENOMEM;
+                }
 
         *ret = TAKE_PTR(buf);
         return 0;
diff --git a/src/network/test-networkd-util.c b/src/network/test-networkd-util.c
new file mode 100644 (file)
index 0000000..f29ca2c
--- /dev/null
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include "networkd-util.h"
+#include "tests.h"
+
+TEST(network_config_state_to_string_alloc) {
+        for (unsigned i = 1; i <= NETWORK_CONFIG_STATE_REMOVING; i <<= 1) {
+                _cleanup_free_ char *x;
+
+                assert_se(network_config_state_to_string_alloc(i, &x) == 0);
+                log_debug("%u → %s", i, x);
+        }
+
+        _cleanup_free_ char *x;
+        assert_se(network_config_state_to_string_alloc(~0u, &x) == 0);
+        log_debug("%u → %s", ~0u, x);
+};
+
+DEFINE_TEST_MAIN(LOG_DEBUG);