From 64903d18dfd0344d5af3c0f8006b2e220de90091 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Tue, 28 Jun 2022 18:37:34 +0200 Subject: [PATCH 1/8] basic/list: drop LIST_IS_EMPTY This was a trivial wrapper that didn't provide any added value. With more complicated structures like strvs, hashmaps, sets, and arrays, it is possible to have an empty container. But in case of a list, the list is empty only when the head is missing. Also, we generally want the positive condition, so we replace many if (!LIST_IS_EMPTY(x)) with just if (x). --- coccinelle/macros.h | 2 -- src/basic/list.h | 3 --- src/core/cgroup.c | 2 +- src/core/dbus-cgroup.c | 2 +- src/core/dbus-execute.c | 8 ++++---- src/core/dbus-util.c | 2 +- src/core/load-fragment.c | 8 ++++---- src/core/unit.c | 2 +- src/libsystemd/sd-event/sd-event.c | 8 ++++---- src/resolve/resolved-conf.c | 2 +- src/resolve/resolved-dnssd.c | 2 +- src/test/test-list.c | 2 +- src/udev/udev-rules.c | 2 +- src/udev/udevd.c | 7 +++---- 14 files changed, 23 insertions(+), 29 deletions(-) diff --git a/coccinelle/macros.h b/coccinelle/macros.h index 6a0a64bb05c..f44b3f2d26f 100644 --- a/coccinelle/macros.h +++ b/coccinelle/macros.h @@ -189,8 +189,6 @@ (i) != (p); \ (i) = (i)->name##_next ? (i)->name##_next : (head)) -#define LIST_IS_EMPTY(head) \ - (!(head)) #define LIST_JOIN(name,a,b) \ do { \ assert(b); \ diff --git a/src/basic/list.h b/src/basic/list.h index 58e83a6cb2b..ca300396908 100644 --- a/src/basic/list.h +++ b/src/basic/list.h @@ -170,9 +170,6 @@ i != (p); \ i = i->name##_next ? i->name##_next : (head)) -#define LIST_IS_EMPTY(head) \ - (!(head)) - /* Join two lists tail to head: a->b, c->d to a->b->c->d and de-initialise second list */ #define LIST_JOIN(name,a,b) \ do { \ diff --git a/src/core/cgroup.c b/src/core/cgroup.c index 25707fce642..0fce812b400 100644 --- a/src/core/cgroup.c +++ b/src/core/cgroup.c @@ -1689,7 +1689,7 @@ static bool unit_get_needs_bpf_foreign_program(Unit *u) { if (!c) return false; - return !LIST_IS_EMPTY(c->bpf_foreign_programs); + return !!c->bpf_foreign_programs; } static bool unit_get_needs_socket_bind(Unit *u) { diff --git a/src/core/dbus-cgroup.c b/src/core/dbus-cgroup.c index 607370d7bfe..7e95f0e3907 100644 --- a/src/core/dbus-cgroup.c +++ b/src/core/dbus-cgroup.c @@ -737,7 +737,7 @@ static int bus_cgroup_set_transient_property( unit_write_setting(u, flags, name, buf); - if (!LIST_IS_EMPTY(c->bpf_foreign_programs)) { + if (c->bpf_foreign_programs) { r = bpf_foreign_supported(); if (r < 0) return r; diff --git a/src/core/dbus-execute.c b/src/core/dbus-execute.c index 1a9e5da6350..8de092ee4e2 100644 --- a/src/core/dbus-execute.c +++ b/src/core/dbus-execute.c @@ -1698,16 +1698,16 @@ int bus_exec_context_set_transient_property( return r; if (!UNIT_WRITE_FLAGS_NOOP(flags)) { - if (LIST_IS_EMPTY(options)) { - c->root_image_options = mount_options_free_all(c->root_image_options); - unit_write_settingf(u, flags, name, "%s=", name); - } else { + if (options) { LIST_JOIN(mount_options, c->root_image_options, options); unit_write_settingf( u, flags|UNIT_ESCAPE_SPECIFIERS, name, "%s=%s", name, format_str); + } else { + c->root_image_options = mount_options_free_all(c->root_image_options); + unit_write_settingf(u, flags, name, "%s=", name); } } diff --git a/src/core/dbus-util.c b/src/core/dbus-util.c index 32a2ec0ff90..264a4f55b67 100644 --- a/src/core/dbus-util.c +++ b/src/core/dbus-util.c @@ -216,7 +216,7 @@ int bus_read_mount_options( if (r < 0) return r; - if (!LIST_IS_EMPTY(options)) { + if (options) { if (ret_format_str) { char *final = strjoin(*ret_format_str, !isempty(*ret_format_str) ? separator : "", format_str); if (!final) diff --git a/src/core/load-fragment.c b/src/core/load-fragment.c index 3ff6eae8fce..313f667cd79 100644 --- a/src/core/load-fragment.c +++ b/src/core/load-fragment.c @@ -1691,11 +1691,11 @@ int config_parse_root_image_options( LIST_APPEND(mount_options, options, TAKE_PTR(o)); } - /* empty spaces/separators only */ - if (LIST_IS_EMPTY(options)) - c->root_image_options = mount_options_free_all(c->root_image_options); - else + if (options) LIST_JOIN(mount_options, c->root_image_options, options); + else + /* empty spaces/separators only */ + c->root_image_options = mount_options_free_all(c->root_image_options); return 0; } diff --git a/src/core/unit.c b/src/core/unit.c index 180dccc2b29..40fc79995ad 100644 --- a/src/core/unit.c +++ b/src/core/unit.c @@ -4138,7 +4138,7 @@ int unit_patch_contexts(Unit *u) { cc->device_policy == CGROUP_DEVICE_POLICY_AUTO) cc->device_policy = CGROUP_DEVICE_POLICY_CLOSED; - if ((ec->root_image || !LIST_IS_EMPTY(ec->mount_images)) && + if ((ec->root_image || ec->mount_images) && (cc->device_policy != CGROUP_DEVICE_POLICY_AUTO || cc->device_allow)) { /* When RootImage= or MountImages= is specified, the following devices are touched. */ diff --git a/src/libsystemd/sd-event/sd-event.c b/src/libsystemd/sd-event/sd-event.c index a37147d1d55..5833a833463 100644 --- a/src/libsystemd/sd-event/sd-event.c +++ b/src/libsystemd/sd-event/sd-event.c @@ -1799,7 +1799,7 @@ static void event_free_inode_data( if (!d) return; - assert(LIST_IS_EMPTY(d->event_sources)); + assert(!d->event_sources); if (d->fd >= 0) { LIST_REMOVE(to_close, e->inode_data_to_close, d); @@ -1862,7 +1862,7 @@ static void event_gc_inode_data( if (!d) return; - if (!LIST_IS_EMPTY(d->event_sources)) + if (d->event_sources) return; inotify_data = d->inotify_data; @@ -3874,7 +3874,7 @@ _public_ int sd_event_prepare(sd_event *e) { event_close_inode_data_fds(e); - if (event_next_pending(e) || e->need_process_child || !LIST_IS_EMPTY(e->inotify_data_buffered)) + if (event_next_pending(e) || e->need_process_child || e->inotify_data_buffered) goto pending; e->state = SD_EVENT_ARMED; @@ -3954,7 +3954,7 @@ static int process_epoll(sd_event *e, usec_t timeout, int64_t threshold, int64_t n_event_max = MALLOC_ELEMENTSOF(e->event_queue); /* If we still have inotify data buffered, then query the other fds, but don't wait on it */ - if (!LIST_IS_EMPTY(e->inotify_data_buffered)) + if (e->inotify_data_buffered) timeout = 0; for (;;) { diff --git a/src/resolve/resolved-conf.c b/src/resolve/resolved-conf.c index a25dffae630..babd19b925f 100644 --- a/src/resolve/resolved-conf.c +++ b/src/resolve/resolved-conf.c @@ -394,7 +394,7 @@ int config_parse_dnssd_txt( last = i; } - if (!LIST_IS_EMPTY(txt_data->txt)) { + if (txt_data->txt) { LIST_PREPEND(items, s->txt_data_items, txt_data); TAKE_PTR(txt_data); } diff --git a/src/resolve/resolved-dnssd.c b/src/resolve/resolved-dnssd.c index 9c2594c1ba0..169c942308a 100644 --- a/src/resolve/resolved-dnssd.c +++ b/src/resolve/resolved-dnssd.c @@ -107,7 +107,7 @@ static int dnssd_service_load(Manager *manager, const char *filename) { "%s doesn't define service type", service->name); - if (LIST_IS_EMPTY(service->txt_data_items)) { + if (!service->txt_data_items) { txt_data = new0(DnssdTxtData, 1); if (!txt_data) return log_oom(); diff --git a/src/test/test-list.c b/src/test/test-list.c index 78bbad8e4e0..dc7ecb7572e 100644 --- a/src/test/test-list.c +++ b/src/test/test-list.c @@ -240,7 +240,7 @@ int main(int argc, const char *argv[]) { LIST_JOIN(item, head, head2); assert_se(head2 == NULL); - assert_se(!LIST_IS_EMPTY(head)); + assert_se(head); for (i = 0; i < ELEMENTSOF(items); i++) LIST_REMOVE(item, head, &items[i]); diff --git a/src/udev/udev-rules.c b/src/udev/udev-rules.c index f95b751b75a..9bbf797acd2 100644 --- a/src/udev/udev-rules.c +++ b/src/udev/udev-rules.c @@ -1063,7 +1063,7 @@ static void sort_tokens(UdevRuleLine *rule_line) { head_old = TAKE_PTR(rule_line->tokens); rule_line->current_token = NULL; - while (!LIST_IS_EMPTY(head_old)) { + while (head_old) { UdevRuleToken *min_token = NULL; LIST_FOREACH(tokens, t, head_old) diff --git a/src/udev/udevd.c b/src/udev/udevd.c index 1994b6b2d52..d3e949dae4b 100644 --- a/src/udev/udevd.c +++ b/src/udev/udevd.c @@ -1008,8 +1008,7 @@ static int event_queue_start(Manager *manager) { assert(manager); - if (LIST_IS_EMPTY(manager->events) || - manager->exit || manager->stop_exec_queue) + if (!manager->events || manager->exit || manager->stop_exec_queue) return 0; assert_se(sd_event_now(manager->event, CLOCK_MONOTONIC, &usec) >= 0); @@ -1166,7 +1165,7 @@ static int event_queue_insert(Manager *manager, sd_device *dev) { .state = EVENT_QUEUED, }; - if (LIST_IS_EMPTY(manager->events)) { + if (!manager->events) { r = touch("/run/udev/queue"); if (r < 0) log_warning_errno(r, "Failed to touch /run/udev/queue, ignoring: %m"); @@ -1611,7 +1610,7 @@ static int on_post(sd_event_source *s, void *userdata) { assert(manager); - if (!LIST_IS_EMPTY(manager->events)) { + if (manager->events) { /* Try to process pending events if idle workers exist. Why is this necessary? * When a worker finished an event and became idle, even if there was a pending event, * the corresponding device might have been locked and the processing of the event From d70f15f5aa92ade72355c0f54e6a306edbfddafb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Wed, 29 Jun 2022 14:17:44 +0200 Subject: [PATCH 2/8] resolved: rename field to indicate that it's a list --- src/resolve/resolved-bus.c | 6 +++--- src/resolve/resolved-conf.c | 4 ++-- src/resolve/resolved-dnssd.c | 6 +++--- src/resolve/resolved-dnssd.h | 4 ++-- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/resolve/resolved-bus.c b/src/resolve/resolved-bus.c index e19d3de8ea7..9e7d1cc380c 100644 --- a/src/resolve/resolved-bus.c +++ b/src/resolve/resolved-bus.c @@ -1988,7 +1988,7 @@ static int bus_method_register_service(sd_bus_message *message, void *userdata, if (r < 0) return r; - LIST_INSERT_AFTER(items, txt_data->txt, last, i); + LIST_INSERT_AFTER(items, txt_data->txts, last, i); last = i; r = sd_bus_message_exit_container(message); @@ -2003,7 +2003,7 @@ static int bus_method_register_service(sd_bus_message *message, void *userdata, if (r < 0) return r; - if (txt_data->txt) { + if (txt_data->txts) { LIST_PREPEND(items, service->txt_data_items, txt_data); txt_data = NULL; } @@ -2022,7 +2022,7 @@ static int bus_method_register_service(sd_bus_message *message, void *userdata, if (!txt_data) return log_oom(); - r = dns_txt_item_new_empty(&txt_data->txt); + r = dns_txt_item_new_empty(&txt_data->txts); if (r < 0) return r; diff --git a/src/resolve/resolved-conf.c b/src/resolve/resolved-conf.c index babd19b925f..3176692c5f8 100644 --- a/src/resolve/resolved-conf.c +++ b/src/resolve/resolved-conf.c @@ -390,11 +390,11 @@ int config_parse_dnssd_txt( assert_not_reached(); } - LIST_INSERT_AFTER(items, txt_data->txt, last, i); + LIST_INSERT_AFTER(items, txt_data->txts, last, i); last = i; } - if (txt_data->txt) { + if (txt_data->txts) { LIST_PREPEND(items, s->txt_data_items, txt_data); TAKE_PTR(txt_data); } diff --git a/src/resolve/resolved-dnssd.c b/src/resolve/resolved-dnssd.c index 169c942308a..c5b38048895 100644 --- a/src/resolve/resolved-dnssd.c +++ b/src/resolve/resolved-dnssd.c @@ -17,7 +17,7 @@ DnssdTxtData *dnssd_txtdata_free(DnssdTxtData *txt_data) { return NULL; dns_resource_record_unref(txt_data->rr); - dns_txt_item_free_all(txt_data->txt); + dns_txt_item_free_all(txt_data->txts); return mfree(txt_data); } @@ -112,7 +112,7 @@ static int dnssd_service_load(Manager *manager, const char *filename) { if (!txt_data) return log_oom(); - r = dns_txt_item_new_empty(&txt_data->txt); + r = dns_txt_item_new_empty(&txt_data->txts); if (r < 0) return r; @@ -237,7 +237,7 @@ int dnssd_update_rrs(DnssdService *s) { goto oom; txt_data->rr->ttl = MDNS_DEFAULT_TTL; - txt_data->rr->txt.items = dns_txt_item_copy(txt_data->txt); + txt_data->rr->txt.items = dns_txt_item_copy(txt_data->txts); if (!txt_data->rr->txt.items) goto oom; } diff --git a/src/resolve/resolved-dnssd.h b/src/resolve/resolved-dnssd.h index 219c0ddfea2..e978a0d5fc6 100644 --- a/src/resolve/resolved-dnssd.h +++ b/src/resolve/resolved-dnssd.h @@ -13,13 +13,13 @@ typedef struct DnsTxtItem DnsTxtItem; enum { DNS_TXT_ITEM_TEXT, - DNS_TXT_ITEM_DATA + DNS_TXT_ITEM_DATA, }; struct DnssdTxtData { DnsResourceRecord *rr; - LIST_HEAD(DnsTxtItem, txt); + LIST_HEAD(DnsTxtItem, txts); LIST_FIELDS(DnssdTxtData, items); }; From 3f3548f8d2b3e5179677a2486d9060da446ec2e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Wed, 29 Jun 2022 14:19:41 +0200 Subject: [PATCH 3/8] sd-event: align table --- src/libsystemd/sd-event/sd-event.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/libsystemd/sd-event/sd-event.c b/src/libsystemd/sd-event/sd-event.c index 5833a833463..c842c840515 100644 --- a/src/libsystemd/sd-event/sd-event.c +++ b/src/libsystemd/sd-event/sd-event.c @@ -48,19 +48,19 @@ static bool event_source_is_offline(sd_event_source *s) { } static const char* const event_source_type_table[_SOURCE_EVENT_SOURCE_TYPE_MAX] = { - [SOURCE_IO] = "io", - [SOURCE_TIME_REALTIME] = "realtime", - [SOURCE_TIME_BOOTTIME] = "bootime", - [SOURCE_TIME_MONOTONIC] = "monotonic", + [SOURCE_IO] = "io", + [SOURCE_TIME_REALTIME] = "realtime", + [SOURCE_TIME_BOOTTIME] = "bootime", + [SOURCE_TIME_MONOTONIC] = "monotonic", [SOURCE_TIME_REALTIME_ALARM] = "realtime-alarm", [SOURCE_TIME_BOOTTIME_ALARM] = "boottime-alarm", - [SOURCE_SIGNAL] = "signal", - [SOURCE_CHILD] = "child", - [SOURCE_DEFER] = "defer", - [SOURCE_POST] = "post", - [SOURCE_EXIT] = "exit", - [SOURCE_WATCHDOG] = "watchdog", - [SOURCE_INOTIFY] = "inotify", + [SOURCE_SIGNAL] = "signal", + [SOURCE_CHILD] = "child", + [SOURCE_DEFER] = "defer", + [SOURCE_POST] = "post", + [SOURCE_EXIT] = "exit", + [SOURCE_WATCHDOG] = "watchdog", + [SOURCE_INOTIFY] = "inotify", }; DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(event_source_type, int); From 0601b95877b24aae9e025155dd34c341cad2bab5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Wed, 29 Jun 2022 14:21:12 +0200 Subject: [PATCH 4/8] sd-event: rename field to indicate that it's a list --- src/libsystemd/sd-event/sd-event.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/libsystemd/sd-event/sd-event.c b/src/libsystemd/sd-event/sd-event.c index c842c840515..3a6e0e81884 100644 --- a/src/libsystemd/sd-event/sd-event.c +++ b/src/libsystemd/sd-event/sd-event.c @@ -126,7 +126,7 @@ struct sd_event { LIST_HEAD(struct inode_data, inode_data_to_close); /* A list of inotify objects that already have events buffered which aren't processed yet */ - LIST_HEAD(struct inotify_data, inotify_data_buffered); + LIST_HEAD(struct inotify_data, buffered_inotify_data_list); pid_t original_pid; @@ -1690,7 +1690,7 @@ static void event_free_inotify_data(sd_event *e, struct inotify_data *d) { assert(hashmap_isempty(d->wd)); if (d->buffer_filled > 0) - LIST_REMOVE(buffered, e->inotify_data_buffered, d); + LIST_REMOVE(buffered, e->buffered_inotify_data_list, d); hashmap_free(d->inodes); hashmap_free(d->wd); @@ -3408,7 +3408,7 @@ static int event_inotify_data_read(sd_event *e, struct inotify_data *d, uint32_t assert(n > 0); d->buffer_filled = (size_t) n; - LIST_PREPEND(buffered, e->inotify_data_buffered, d); + LIST_PREPEND(buffered, e->buffered_inotify_data_list, d); return 1; } @@ -3426,7 +3426,7 @@ static void event_inotify_data_drop(sd_event *e, struct inotify_data *d, size_t d->buffer_filled -= sz; if (d->buffer_filled == 0) - LIST_REMOVE(buffered, e->inotify_data_buffered, d); + LIST_REMOVE(buffered, e->buffered_inotify_data_list, d); } static int event_inotify_data_process(sd_event *e, struct inotify_data *d) { @@ -3519,7 +3519,7 @@ static int process_inotify(sd_event *e) { assert(e); - LIST_FOREACH(buffered, d, e->inotify_data_buffered) { + LIST_FOREACH(buffered, d, e->buffered_inotify_data_list) { r = event_inotify_data_process(e, d); if (r < 0) return r; @@ -3874,7 +3874,7 @@ _public_ int sd_event_prepare(sd_event *e) { event_close_inode_data_fds(e); - if (event_next_pending(e) || e->need_process_child || e->inotify_data_buffered) + if (event_next_pending(e) || e->need_process_child || e->buffered_inotify_data_list) goto pending; e->state = SD_EVENT_ARMED; @@ -3954,7 +3954,7 @@ static int process_epoll(sd_event *e, usec_t timeout, int64_t threshold, int64_t n_event_max = MALLOC_ELEMENTSOF(e->event_queue); /* If we still have inotify data buffered, then query the other fds, but don't wait on it */ - if (e->inotify_data_buffered) + if (e->buffered_inotify_data_list) timeout = 0; for (;;) { From ed828563a5058f048945b7e49d0b62029d757164 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Wed, 29 Jun 2022 14:22:33 +0200 Subject: [PATCH 5/8] sd-event: rename field to indicate that it's a list --- src/libsystemd/sd-event/sd-event.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/libsystemd/sd-event/sd-event.c b/src/libsystemd/sd-event/sd-event.c index 3a6e0e81884..d689615016b 100644 --- a/src/libsystemd/sd-event/sd-event.c +++ b/src/libsystemd/sd-event/sd-event.c @@ -123,7 +123,7 @@ struct sd_event { Hashmap *inotify_data; /* indexed by priority */ /* A list of inode structures that still have an fd open, that we need to close before the next loop iteration */ - LIST_HEAD(struct inode_data, inode_data_to_close); + LIST_HEAD(struct inode_data, inode_data_to_close_list); /* A list of inotify objects that already have events buffered which aren't processed yet */ LIST_HEAD(struct inotify_data, buffered_inotify_data_list); @@ -1802,7 +1802,7 @@ static void event_free_inode_data( assert(!d->event_sources); if (d->fd >= 0) { - LIST_REMOVE(to_close, e->inode_data_to_close, d); + LIST_REMOVE(to_close, e->inode_data_to_close_list, d); safe_close(d->fd); } @@ -2064,7 +2064,7 @@ static int event_add_inotify_fd_internal( } } - LIST_PREPEND(to_close, e->inode_data_to_close, inode_data); + LIST_PREPEND(to_close, e->inode_data_to_close_list, inode_data); } /* Link our event source to the inode data object */ @@ -2351,7 +2351,7 @@ _public_ int sd_event_source_set_priority(sd_event_source *s, int64_t priority) goto fail; } - LIST_PREPEND(to_close, s->event->inode_data_to_close, new_inode_data); + LIST_PREPEND(to_close, s->event->inode_data_to_close_list, new_inode_data); } /* Move the event source to the new inode data structure */ @@ -3816,11 +3816,11 @@ static void event_close_inode_data_fds(sd_event *e) { * for the inode). Hence, let's close them when entering the first iteration after they were added, as a * compromise. */ - while ((d = e->inode_data_to_close)) { + while ((d = e->inode_data_to_close_list)) { assert(d->fd >= 0); d->fd = safe_close(d->fd); - LIST_REMOVE(to_close, e->inode_data_to_close, d); + LIST_REMOVE(to_close, e->inode_data_to_close_list, d); } } From d5fe7f7fb160026c2eb61a6f5f60623cb355af67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Wed, 29 Jun 2022 14:25:11 +0200 Subject: [PATCH 6/8] udev: rename field When we start, the contents of the variable match the name. But then in the loop, the variable doesn't point at the old head any more. So let's rename it to something with a plural. --- src/udev/udev-rules.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/udev/udev-rules.c b/src/udev/udev-rules.c index 9bbf797acd2..753ebb015db 100644 --- a/src/udev/udev-rules.c +++ b/src/udev/udev-rules.c @@ -1056,21 +1056,19 @@ static int parse_line(char **line, char **ret_key, char **ret_attr, UdevRuleOper } static void sort_tokens(UdevRuleLine *rule_line) { - UdevRuleToken *head_old; - assert(rule_line); - head_old = TAKE_PTR(rule_line->tokens); + UdevRuleToken *old_tokens = TAKE_PTR(rule_line->tokens); rule_line->current_token = NULL; - while (head_old) { + while (old_tokens) { UdevRuleToken *min_token = NULL; - LIST_FOREACH(tokens, t, head_old) + LIST_FOREACH(tokens, t, old_tokens) if (!min_token || min_token->type > t->type) min_token = t; - LIST_REMOVE(tokens, head_old, min_token); + LIST_REMOVE(tokens, old_tokens, min_token); rule_line_append_token(rule_line, min_token); } } From eb506199f097f6a30f059d46a6dbc7a9c0765a71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Wed, 29 Jun 2022 14:35:42 +0200 Subject: [PATCH 7/8] systemctl: rename field for clarity --- src/systemctl/systemctl-show.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/systemctl/systemctl-show.c b/src/systemctl/systemctl-show.c index b24e5fd6bad..a7573786141 100644 --- a/src/systemctl/systemctl-show.c +++ b/src/systemctl/systemctl-show.c @@ -64,7 +64,7 @@ typedef struct ExecStatusInfo { ExecCommandFlags flags; - LIST_FIELDS(struct ExecStatusInfo, exec); + LIST_FIELDS(struct ExecStatusInfo, exec_status_info_list); } ExecStatusInfo; static void exec_status_info_free(ExecStatusInfo *i) { @@ -263,7 +263,7 @@ typedef struct UnitStatusInfo { uint64_t default_memory_min; uint64_t default_memory_low; - LIST_HEAD(ExecStatusInfo, exec); + LIST_HEAD(ExecStatusInfo, exec_status_info_list); } UnitStatusInfo; static void unit_status_info_free(UnitStatusInfo *info) { @@ -281,8 +281,8 @@ static void unit_status_info_free(UnitStatusInfo *info) { unit_condition_free(c); } - while ((p = info->exec)) { - LIST_REMOVE(exec, info->exec, p); + while ((p = info->exec_status_info_list)) { + LIST_REMOVE(exec_status_info_list, info->exec_status_info_list, p); exec_status_info_free(p); } } @@ -566,7 +566,7 @@ static void print_status_info( printf("\n"); } - LIST_FOREACH(exec, p, i->exec) { + LIST_FOREACH(exec_status_info_list, p, i->exec_status_info_list) { _cleanup_free_ char *argv = NULL; bool good; @@ -944,7 +944,7 @@ static int map_exec(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_e if (!info) return -ENOMEM; - LIST_FIND_TAIL(exec, i->exec, last); + LIST_FIND_TAIL(exec_status_info_list, i->exec_status_info_list, last); while ((r = exec_status_info_deserialize(m, info, is_ex_prop)) > 0) { @@ -952,7 +952,7 @@ static int map_exec(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_e if (!info->name) return -ENOMEM; - LIST_INSERT_AFTER(exec, i->exec, last, info); + LIST_INSERT_AFTER(exec_status_info_list, i->exec_status_info_list, last, info); last = info; info = new0(ExecStatusInfo, 1); From c4f2bff1213c99e355ae5dd26583ec02a8712e89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Wed, 29 Jun 2022 14:46:47 +0200 Subject: [PATCH 8/8] test-list: rename field to indicate that it's a list With this commit, lists are generally either names with plural (items), or in a way that indicates a multi-item container (queue, *_list, or similar). --- src/test/test-list.c | 298 +++++++++++++++++++++---------------------- 1 file changed, 149 insertions(+), 149 deletions(-) diff --git a/src/test/test-list.c b/src/test/test-list.c index dc7ecb7572e..2c764d7b711 100644 --- a/src/test/test-list.c +++ b/src/test/test-list.c @@ -6,7 +6,7 @@ int main(int argc, const char *argv[]) { size_t i; typedef struct list_item { - LIST_FIELDS(struct list_item, item); + LIST_FIELDS(struct list_item, item_list); } list_item; LIST_HEAD(list_item, head); LIST_HEAD(list_item, head2); @@ -18,243 +18,243 @@ int main(int argc, const char *argv[]) { assert_se(head2 == NULL); for (i = 0; i < ELEMENTSOF(items); i++) { - LIST_INIT(item, &items[i]); - assert_se(LIST_JUST_US(item, &items[i])); - LIST_PREPEND(item, head, &items[i]); + LIST_INIT(item_list, &items[i]); + assert_se(LIST_JUST_US(item_list, &items[i])); + LIST_PREPEND(item_list, head, &items[i]); } i = 0; - LIST_FOREACH_OTHERS(item, cursor, &items[2]) { + LIST_FOREACH_OTHERS(item_list, cursor, &items[2]) { i++; assert_se(cursor != &items[2]); } assert_se(i == ELEMENTSOF(items)-1); i = 0; - LIST_FOREACH_OTHERS(item, cursor, &items[0]) { + LIST_FOREACH_OTHERS(item_list, cursor, &items[0]) { i++; assert_se(cursor != &items[0]); } assert_se(i == ELEMENTSOF(items)-1); i = 0; - LIST_FOREACH_OTHERS(item, cursor, &items[3]) { + LIST_FOREACH_OTHERS(item_list, cursor, &items[3]) { i++; assert_se(cursor != &items[3]); } assert_se(i == ELEMENTSOF(items)-1); - assert_se(!LIST_JUST_US(item, head)); + assert_se(!LIST_JUST_US(item_list, head)); - assert_se(items[0].item_next == NULL); - assert_se(items[1].item_next == &items[0]); - assert_se(items[2].item_next == &items[1]); - assert_se(items[3].item_next == &items[2]); + assert_se(items[0].item_list_next == NULL); + assert_se(items[1].item_list_next == &items[0]); + assert_se(items[2].item_list_next == &items[1]); + assert_se(items[3].item_list_next == &items[2]); - assert_se(items[0].item_prev == &items[1]); - assert_se(items[1].item_prev == &items[2]); - assert_se(items[2].item_prev == &items[3]); - assert_se(items[3].item_prev == NULL); + assert_se(items[0].item_list_prev == &items[1]); + assert_se(items[1].item_list_prev == &items[2]); + assert_se(items[2].item_list_prev == &items[3]); + assert_se(items[3].item_list_prev == NULL); list_item *cursor; - LIST_FIND_HEAD(item, &items[0], cursor); + LIST_FIND_HEAD(item_list, &items[0], cursor); assert_se(cursor == &items[3]); - LIST_FIND_TAIL(item, &items[3], cursor); + LIST_FIND_TAIL(item_list, &items[3], cursor); assert_se(cursor == &items[0]); - LIST_REMOVE(item, head, &items[1]); - assert_se(LIST_JUST_US(item, &items[1])); + LIST_REMOVE(item_list, head, &items[1]); + assert_se(LIST_JUST_US(item_list, &items[1])); - assert_se(items[0].item_next == NULL); - assert_se(items[2].item_next == &items[0]); - assert_se(items[3].item_next == &items[2]); + assert_se(items[0].item_list_next == NULL); + assert_se(items[2].item_list_next == &items[0]); + assert_se(items[3].item_list_next == &items[2]); - assert_se(items[0].item_prev == &items[2]); - assert_se(items[2].item_prev == &items[3]); - assert_se(items[3].item_prev == NULL); + assert_se(items[0].item_list_prev == &items[2]); + assert_se(items[2].item_list_prev == &items[3]); + assert_se(items[3].item_list_prev == NULL); - LIST_INSERT_AFTER(item, head, &items[3], &items[1]); - assert_se(items[0].item_next == NULL); - assert_se(items[2].item_next == &items[0]); - assert_se(items[1].item_next == &items[2]); - assert_se(items[3].item_next == &items[1]); + LIST_INSERT_AFTER(item_list, head, &items[3], &items[1]); + assert_se(items[0].item_list_next == NULL); + assert_se(items[2].item_list_next == &items[0]); + assert_se(items[1].item_list_next == &items[2]); + assert_se(items[3].item_list_next == &items[1]); - assert_se(items[0].item_prev == &items[2]); - assert_se(items[2].item_prev == &items[1]); - assert_se(items[1].item_prev == &items[3]); - assert_se(items[3].item_prev == NULL); + assert_se(items[0].item_list_prev == &items[2]); + assert_se(items[2].item_list_prev == &items[1]); + assert_se(items[1].item_list_prev == &items[3]); + assert_se(items[3].item_list_prev == NULL); - LIST_REMOVE(item, head, &items[1]); - assert_se(LIST_JUST_US(item, &items[1])); + LIST_REMOVE(item_list, head, &items[1]); + assert_se(LIST_JUST_US(item_list, &items[1])); - assert_se(items[0].item_next == NULL); - assert_se(items[2].item_next == &items[0]); - assert_se(items[3].item_next == &items[2]); + assert_se(items[0].item_list_next == NULL); + assert_se(items[2].item_list_next == &items[0]); + assert_se(items[3].item_list_next == &items[2]); - assert_se(items[0].item_prev == &items[2]); - assert_se(items[2].item_prev == &items[3]); - assert_se(items[3].item_prev == NULL); + assert_se(items[0].item_list_prev == &items[2]); + assert_se(items[2].item_list_prev == &items[3]); + assert_se(items[3].item_list_prev == NULL); - LIST_INSERT_BEFORE(item, head, &items[2], &items[1]); - assert_se(items[0].item_next == NULL); - assert_se(items[2].item_next == &items[0]); - assert_se(items[1].item_next == &items[2]); - assert_se(items[3].item_next == &items[1]); + LIST_INSERT_BEFORE(item_list, head, &items[2], &items[1]); + assert_se(items[0].item_list_next == NULL); + assert_se(items[2].item_list_next == &items[0]); + assert_se(items[1].item_list_next == &items[2]); + assert_se(items[3].item_list_next == &items[1]); - assert_se(items[0].item_prev == &items[2]); - assert_se(items[2].item_prev == &items[1]); - assert_se(items[1].item_prev == &items[3]); - assert_se(items[3].item_prev == NULL); + assert_se(items[0].item_list_prev == &items[2]); + assert_se(items[2].item_list_prev == &items[1]); + assert_se(items[1].item_list_prev == &items[3]); + assert_se(items[3].item_list_prev == NULL); - LIST_REMOVE(item, head, &items[0]); - assert_se(LIST_JUST_US(item, &items[0])); + LIST_REMOVE(item_list, head, &items[0]); + assert_se(LIST_JUST_US(item_list, &items[0])); - assert_se(items[2].item_next == NULL); - assert_se(items[1].item_next == &items[2]); - assert_se(items[3].item_next == &items[1]); + assert_se(items[2].item_list_next == NULL); + assert_se(items[1].item_list_next == &items[2]); + assert_se(items[3].item_list_next == &items[1]); - assert_se(items[2].item_prev == &items[1]); - assert_se(items[1].item_prev == &items[3]); - assert_se(items[3].item_prev == NULL); + assert_se(items[2].item_list_prev == &items[1]); + assert_se(items[1].item_list_prev == &items[3]); + assert_se(items[3].item_list_prev == NULL); - LIST_INSERT_BEFORE(item, head, &items[3], &items[0]); - assert_se(items[2].item_next == NULL); - assert_se(items[1].item_next == &items[2]); - assert_se(items[3].item_next == &items[1]); - assert_se(items[0].item_next == &items[3]); + LIST_INSERT_BEFORE(item_list, head, &items[3], &items[0]); + assert_se(items[2].item_list_next == NULL); + assert_se(items[1].item_list_next == &items[2]); + assert_se(items[3].item_list_next == &items[1]); + assert_se(items[0].item_list_next == &items[3]); - assert_se(items[2].item_prev == &items[1]); - assert_se(items[1].item_prev == &items[3]); - assert_se(items[3].item_prev == &items[0]); - assert_se(items[0].item_prev == NULL); + assert_se(items[2].item_list_prev == &items[1]); + assert_se(items[1].item_list_prev == &items[3]); + assert_se(items[3].item_list_prev == &items[0]); + assert_se(items[0].item_list_prev == NULL); assert_se(head == &items[0]); - LIST_REMOVE(item, head, &items[0]); - assert_se(LIST_JUST_US(item, &items[0])); + LIST_REMOVE(item_list, head, &items[0]); + assert_se(LIST_JUST_US(item_list, &items[0])); - assert_se(items[2].item_next == NULL); - assert_se(items[1].item_next == &items[2]); - assert_se(items[3].item_next == &items[1]); + assert_se(items[2].item_list_next == NULL); + assert_se(items[1].item_list_next == &items[2]); + assert_se(items[3].item_list_next == &items[1]); - assert_se(items[2].item_prev == &items[1]); - assert_se(items[1].item_prev == &items[3]); - assert_se(items[3].item_prev == NULL); + assert_se(items[2].item_list_prev == &items[1]); + assert_se(items[1].item_list_prev == &items[3]); + assert_se(items[3].item_list_prev == NULL); - LIST_INSERT_BEFORE(item, head, NULL, &items[0]); - assert_se(items[0].item_next == NULL); - assert_se(items[2].item_next == &items[0]); - assert_se(items[1].item_next == &items[2]); - assert_se(items[3].item_next == &items[1]); + LIST_INSERT_BEFORE(item_list, head, NULL, &items[0]); + assert_se(items[0].item_list_next == NULL); + assert_se(items[2].item_list_next == &items[0]); + assert_se(items[1].item_list_next == &items[2]); + assert_se(items[3].item_list_next == &items[1]); - assert_se(items[0].item_prev == &items[2]); - assert_se(items[2].item_prev == &items[1]); - assert_se(items[1].item_prev == &items[3]); - assert_se(items[3].item_prev == NULL); + assert_se(items[0].item_list_prev == &items[2]); + assert_se(items[2].item_list_prev == &items[1]); + assert_se(items[1].item_list_prev == &items[3]); + assert_se(items[3].item_list_prev == NULL); - LIST_REMOVE(item, head, &items[0]); - assert_se(LIST_JUST_US(item, &items[0])); + LIST_REMOVE(item_list, head, &items[0]); + assert_se(LIST_JUST_US(item_list, &items[0])); - assert_se(items[2].item_next == NULL); - assert_se(items[1].item_next == &items[2]); - assert_se(items[3].item_next == &items[1]); + assert_se(items[2].item_list_next == NULL); + assert_se(items[1].item_list_next == &items[2]); + assert_se(items[3].item_list_next == &items[1]); - assert_se(items[2].item_prev == &items[1]); - assert_se(items[1].item_prev == &items[3]); - assert_se(items[3].item_prev == NULL); + assert_se(items[2].item_list_prev == &items[1]); + assert_se(items[1].item_list_prev == &items[3]); + assert_se(items[3].item_list_prev == NULL); - LIST_REMOVE(item, head, &items[1]); - assert_se(LIST_JUST_US(item, &items[1])); + LIST_REMOVE(item_list, head, &items[1]); + assert_se(LIST_JUST_US(item_list, &items[1])); - assert_se(items[2].item_next == NULL); - assert_se(items[3].item_next == &items[2]); + assert_se(items[2].item_list_next == NULL); + assert_se(items[3].item_list_next == &items[2]); - assert_se(items[2].item_prev == &items[3]); - assert_se(items[3].item_prev == NULL); + assert_se(items[2].item_list_prev == &items[3]); + assert_se(items[3].item_list_prev == NULL); - LIST_REMOVE(item, head, &items[2]); - assert_se(LIST_JUST_US(item, &items[2])); - assert_se(LIST_JUST_US(item, head)); + LIST_REMOVE(item_list, head, &items[2]); + assert_se(LIST_JUST_US(item_list, &items[2])); + assert_se(LIST_JUST_US(item_list, head)); - LIST_REMOVE(item, head, &items[3]); - assert_se(LIST_JUST_US(item, &items[3])); + LIST_REMOVE(item_list, head, &items[3]); + assert_se(LIST_JUST_US(item_list, &items[3])); assert_se(head == NULL); for (i = 0; i < ELEMENTSOF(items); i++) { - assert_se(LIST_JUST_US(item, &items[i])); - LIST_APPEND(item, head, &items[i]); + assert_se(LIST_JUST_US(item_list, &items[i])); + LIST_APPEND(item_list, head, &items[i]); } - assert_se(!LIST_JUST_US(item, head)); + assert_se(!LIST_JUST_US(item_list, head)); - assert_se(items[0].item_next == &items[1]); - assert_se(items[1].item_next == &items[2]); - assert_se(items[2].item_next == &items[3]); - assert_se(items[3].item_next == NULL); + assert_se(items[0].item_list_next == &items[1]); + assert_se(items[1].item_list_next == &items[2]); + assert_se(items[2].item_list_next == &items[3]); + assert_se(items[3].item_list_next == NULL); - assert_se(items[0].item_prev == NULL); - assert_se(items[1].item_prev == &items[0]); - assert_se(items[2].item_prev == &items[1]); - assert_se(items[3].item_prev == &items[2]); + assert_se(items[0].item_list_prev == NULL); + assert_se(items[1].item_list_prev == &items[0]); + assert_se(items[2].item_list_prev == &items[1]); + assert_se(items[3].item_list_prev == &items[2]); for (i = 0; i < ELEMENTSOF(items); i++) - LIST_REMOVE(item, head, &items[i]); + LIST_REMOVE(item_list, head, &items[i]); assert_se(head == NULL); for (i = 0; i < ELEMENTSOF(items) / 2; i++) { - LIST_INIT(item, &items[i]); - assert_se(LIST_JUST_US(item, &items[i])); - LIST_PREPEND(item, head, &items[i]); + LIST_INIT(item_list, &items[i]); + assert_se(LIST_JUST_US(item_list, &items[i])); + LIST_PREPEND(item_list, head, &items[i]); } for (i = ELEMENTSOF(items) / 2; i < ELEMENTSOF(items); i++) { - LIST_INIT(item, &items[i]); - assert_se(LIST_JUST_US(item, &items[i])); - LIST_PREPEND(item, head2, &items[i]); + LIST_INIT(item_list, &items[i]); + assert_se(LIST_JUST_US(item_list, &items[i])); + LIST_PREPEND(item_list, head2, &items[i]); } - assert_se(items[0].item_next == NULL); - assert_se(items[1].item_next == &items[0]); - assert_se(items[2].item_next == NULL); - assert_se(items[3].item_next == &items[2]); + assert_se(items[0].item_list_next == NULL); + assert_se(items[1].item_list_next == &items[0]); + assert_se(items[2].item_list_next == NULL); + assert_se(items[3].item_list_next == &items[2]); - assert_se(items[0].item_prev == &items[1]); - assert_se(items[1].item_prev == NULL); - assert_se(items[2].item_prev == &items[3]); - assert_se(items[3].item_prev == NULL); + assert_se(items[0].item_list_prev == &items[1]); + assert_se(items[1].item_list_prev == NULL); + assert_se(items[2].item_list_prev == &items[3]); + assert_se(items[3].item_list_prev == NULL); - LIST_JOIN(item, head2, head); + LIST_JOIN(item_list, head2, head); assert_se(head == NULL); - assert_se(items[0].item_next == NULL); - assert_se(items[1].item_next == &items[0]); - assert_se(items[2].item_next == &items[1]); - assert_se(items[3].item_next == &items[2]); + assert_se(items[0].item_list_next == NULL); + assert_se(items[1].item_list_next == &items[0]); + assert_se(items[2].item_list_next == &items[1]); + assert_se(items[3].item_list_next == &items[2]); - assert_se(items[0].item_prev == &items[1]); - assert_se(items[1].item_prev == &items[2]); - assert_se(items[2].item_prev == &items[3]); - assert_se(items[3].item_prev == NULL); + assert_se(items[0].item_list_prev == &items[1]); + assert_se(items[1].item_list_prev == &items[2]); + assert_se(items[2].item_list_prev == &items[3]); + assert_se(items[3].item_list_prev == NULL); - LIST_JOIN(item, head, head2); + LIST_JOIN(item_list, head, head2); assert_se(head2 == NULL); assert_se(head); for (i = 0; i < ELEMENTSOF(items); i++) - LIST_REMOVE(item, head, &items[i]); + LIST_REMOVE(item_list, head, &items[i]); assert_se(head == NULL); - LIST_PREPEND(item, head, items + 0); - LIST_PREPEND(item, head, items + 1); - LIST_PREPEND(item, head, items + 2); + LIST_PREPEND(item_list, head, items + 0); + LIST_PREPEND(item_list, head, items + 1); + LIST_PREPEND(item_list, head, items + 2); - assert_se(LIST_POP(item, head) == items + 2); - assert_se(LIST_POP(item, head) == items + 1); - assert_se(LIST_POP(item, head) == items + 0); - assert_se(LIST_POP(item, head) == NULL); + assert_se(LIST_POP(item_list, head) == items + 2); + assert_se(LIST_POP(item_list, head) == items + 1); + assert_se(LIST_POP(item_list, head) == items + 0); + assert_se(LIST_POP(item_list, head) == NULL); return 0; }