mirror of
https://github.com/systemd/systemd-stable.git
synced 2025-01-08 21:17:47 +03:00
networkd: replace a table with log2 fields by a list
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.
This commit is contained in:
parent
f4ada1b42f
commit
67b65e1104
@ -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],
|
||||
|
@ -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;
|
||||
|
19
src/network/test-networkd-util.c
Normal file
19
src/network/test-networkd-util.c
Normal file
@ -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);
|
Loading…
Reference in New Issue
Block a user