diff options
author | Daan De Meyer <daan.j.demeyer@gmail.com> | 2022-08-12 16:13:05 +0200 |
---|---|---|
committer | Daan De Meyer <daan.j.demeyer@gmail.com> | 2023-01-08 16:31:15 +0100 |
commit | cc232fa09407eb027db6a3a28451c25dad3ef7aa (patch) | |
tree | 8429bfeef6a574ab32991c64b341c17c9f598e1e | |
parent | basic: Drop memory-util.h include from stdio-util.h (diff) | |
download | systemd-cc232fa09407eb027db6a3a28451c25dad3ef7aa.tar.xz systemd-cc232fa09407eb027db6a3a28451c25dad3ef7aa.zip |
basic: Use statement expressions more in list.h
Let's use statement expressions to return values instead of passing
in return arguments to the LIST macros.
-rw-r--r-- | src/basic/list.h | 65 | ||||
-rw-r--r-- | src/core/execute.c | 2 | ||||
-rw-r--r-- | src/core/load-fragment.c | 2 | ||||
-rw-r--r-- | src/resolve/resolved-dns-search-domain.c | 4 | ||||
-rw-r--r-- | src/resolve/resolved-dns-server.c | 6 | ||||
-rw-r--r-- | src/systemctl/systemctl-show.c | 2 | ||||
-rw-r--r-- | src/test/test-list.c | 51 | ||||
-rw-r--r-- | src/timesync/timesyncd-server.c | 2 |
8 files changed, 65 insertions, 69 deletions
diff --git a/src/basic/list.h b/src/basic/list.h index ca30039690..4a6e1505a5 100644 --- a/src/basic/list.h +++ b/src/basic/list.h @@ -28,26 +28,27 @@ /* Prepend an item to the list */ #define LIST_PREPEND(name,head,item) \ - do { \ + ({ \ typeof(*(head)) **_head = &(head), *_item = (item); \ assert(_item); \ if ((_item->name##_next = *_head)) \ _item->name##_next->name##_prev = _item; \ _item->name##_prev = NULL; \ *_head = _item; \ - } while (false) + _item; \ + }) /* Append an item to the list */ #define LIST_APPEND(name,head,item) \ - do { \ + ({ \ typeof(*(head)) **_hhead = &(head), *_tail; \ - LIST_FIND_TAIL(name, *_hhead, _tail); \ + _tail = LIST_FIND_TAIL(name, *_hhead); \ LIST_INSERT_AFTER(name, *_hhead, _tail, item); \ - } while (false) + }) /* Remove an item from the list */ #define LIST_REMOVE(name,head,item) \ - do { \ + ({ \ typeof(*(head)) **_head = &(head), *_item = (item); \ assert(_item); \ if (_item->name##_next) \ @@ -59,37 +60,30 @@ *_head = _item->name##_next; \ } \ _item->name##_next = _item->name##_prev = NULL; \ - } while (false) + _item; \ + }) /* Find the head of the list */ -#define LIST_FIND_HEAD(name,item,head) \ - do { \ +#define LIST_FIND_HEAD(name,item) \ + ({ \ typeof(*(item)) *_item = (item); \ - if (!_item) \ - (head) = NULL; \ - else { \ - while (_item->name##_prev) \ - _item = _item->name##_prev; \ - (head) = _item; \ - } \ - } while (false) + while (_item && _item->name##_prev) \ + _item = _item->name##_prev; \ + _item; \ + }) /* Find the tail of the list */ -#define LIST_FIND_TAIL(name,item,tail) \ - do { \ +#define LIST_FIND_TAIL(name,item) \ + ({ \ typeof(*(item)) *_item = (item); \ - if (!_item) \ - (tail) = NULL; \ - else { \ - while (_item->name##_next) \ - _item = _item->name##_next; \ - (tail) = _item; \ - } \ - } while (false) + while (_item && _item->name##_next) \ + _item = _item->name##_next; \ + _item; \ + }) /* Insert an item after another one (a = where, b = what) */ #define LIST_INSERT_AFTER(name,head,a,b) \ - do { \ + ({ \ typeof(*(head)) **_head = &(head), *_a = (a), *_b = (b); \ assert(_b); \ if (!_a) { \ @@ -103,11 +97,12 @@ _b->name##_prev = _a; \ _a->name##_next = _b; \ } \ - } while (false) + _b; \ + }) /* Insert an item before another one (a = where, b = what) */ #define LIST_INSERT_BEFORE(name,head,a,b) \ - do { \ + ({ \ typeof(*(head)) **_head = &(head), *_a = (a), *_b = (b); \ assert(_b); \ if (!_a) { \ @@ -131,7 +126,8 @@ _b->name##_next = _a; \ _a->name##_prev = _b; \ } \ - } while (false) + _b; \ + }) #define LIST_JUST_US(name,item) \ (!(item)->name##_prev && !(item)->name##_next) @@ -172,18 +168,19 @@ /* 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 { \ + ({ \ assert(b); \ if (!(a)) \ (a) = (b); \ else { \ typeof(*(a)) *_head = (b), *_tail; \ - LIST_FIND_TAIL(name, (a), _tail); \ + _tail = LIST_FIND_TAIL(name, (a)); \ _tail->name##_next = _head; \ _head->name##_prev = _tail; \ } \ (b) = NULL; \ - } while (false) + a; \ + }) #define LIST_POP(name, a) \ ({ \ diff --git a/src/core/execute.c b/src/core/execute.c index 439f491d02..94c0e6a90d 100644 --- a/src/core/execute.c +++ b/src/core/execute.c @@ -6491,7 +6491,7 @@ void exec_command_append_list(ExecCommand **l, ExecCommand *e) { if (*l) { /* It's kind of important, that we keep the order here */ - LIST_FIND_TAIL(command, *l, end); + end = LIST_FIND_TAIL(command, *l); LIST_INSERT_AFTER(command, *l, end, e); } else *l = e; diff --git a/src/core/load-fragment.c b/src/core/load-fragment.c index e115aa6270..22388d2ed6 100644 --- a/src/core/load-fragment.c +++ b/src/core/load-fragment.c @@ -679,7 +679,7 @@ int config_parse_socket_listen( p->n_auxiliary_fds = 0; p->socket = s; - LIST_FIND_TAIL(port, s->ports, tail); + tail = LIST_FIND_TAIL(port, s->ports); LIST_INSERT_AFTER(port, s->ports, tail, p); p = NULL; diff --git a/src/resolve/resolved-dns-search-domain.c b/src/resolve/resolved-dns-search-domain.c index 647c0bd1f9..a11b21350a 100644 --- a/src/resolve/resolved-dns-search-domain.c +++ b/src/resolve/resolved-dns-search-domain.c @@ -123,13 +123,13 @@ void dns_search_domain_move_back_and_unmark(DnsSearchDomain *d) { case DNS_SEARCH_DOMAIN_LINK: assert(d->link); - LIST_FIND_TAIL(domains, d, tail); + tail = LIST_FIND_TAIL(domains, d); LIST_REMOVE(domains, d->link->search_domains, d); LIST_INSERT_AFTER(domains, d->link->search_domains, tail, d); break; case DNS_SEARCH_DOMAIN_SYSTEM: - LIST_FIND_TAIL(domains, d, tail); + tail = LIST_FIND_TAIL(domains, d); LIST_REMOVE(domains, d->manager->search_domains, d); LIST_INSERT_AFTER(domains, d->manager->search_domains, tail, d); break; diff --git a/src/resolve/resolved-dns-server.c b/src/resolve/resolved-dns-server.c index 8ff513fa33..342a90abcb 100644 --- a/src/resolve/resolved-dns-server.c +++ b/src/resolve/resolved-dns-server.c @@ -195,19 +195,19 @@ void dns_server_move_back_and_unmark(DnsServer *s) { case DNS_SERVER_LINK: assert(s->link); - LIST_FIND_TAIL(servers, s, tail); + tail = LIST_FIND_TAIL(servers, s); LIST_REMOVE(servers, s->link->dns_servers, s); LIST_INSERT_AFTER(servers, s->link->dns_servers, tail, s); break; case DNS_SERVER_SYSTEM: - LIST_FIND_TAIL(servers, s, tail); + tail = LIST_FIND_TAIL(servers, s); LIST_REMOVE(servers, s->manager->dns_servers, s); LIST_INSERT_AFTER(servers, s->manager->dns_servers, tail, s); break; case DNS_SERVER_FALLBACK: - LIST_FIND_TAIL(servers, s, tail); + tail = LIST_FIND_TAIL(servers, s); LIST_REMOVE(servers, s->manager->fallback_dns_servers, s); LIST_INSERT_AFTER(servers, s->manager->fallback_dns_servers, tail, s); break; diff --git a/src/systemctl/systemctl-show.c b/src/systemctl/systemctl-show.c index f78cf307ca..c004b804a5 100644 --- a/src/systemctl/systemctl-show.c +++ b/src/systemctl/systemctl-show.c @@ -952,7 +952,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_status_info_list, i->exec_status_info_list, last); + last = LIST_FIND_TAIL(exec_status_info_list, i->exec_status_info_list); while ((r = exec_status_info_deserialize(m, info, is_ex_prop)) > 0) { diff --git a/src/test/test-list.c b/src/test/test-list.c index ea45f5b95c..307c1bf936 100644 --- a/src/test/test-list.c +++ b/src/test/test-list.c @@ -19,7 +19,7 @@ int main(int argc, const char *argv[]) { for (i = 0; i < ELEMENTSOF(items); i++) { LIST_INIT(item_list, &items[i]); assert_se(LIST_JUST_US(item_list, &items[i])); - LIST_PREPEND(item_list, head, &items[i]); + assert_se(LIST_PREPEND(item_list, head, &items[i]) == &items[i]); } i = 0; @@ -55,14 +55,13 @@ int main(int argc, const char *argv[]) { assert_se(items[2].item_list_prev == &items[3]); assert_se(items[3].item_list_prev == NULL); - list_item *cursor; - LIST_FIND_HEAD(item_list, &items[0], cursor); + list_item *cursor = LIST_FIND_HEAD(item_list, &items[0]); assert_se(cursor == &items[3]); - LIST_FIND_TAIL(item_list, &items[3], cursor); + cursor = LIST_FIND_TAIL(item_list, &items[3]); assert_se(cursor == &items[0]); - LIST_REMOVE(item_list, head, &items[1]); + assert_se(LIST_REMOVE(item_list, head, &items[1]) == &items[1]); assert_se(LIST_JUST_US(item_list, &items[1])); assert_se(items[0].item_list_next == NULL); @@ -73,7 +72,7 @@ int main(int argc, const char *argv[]) { assert_se(items[2].item_list_prev == &items[3]); assert_se(items[3].item_list_prev == NULL); - LIST_INSERT_AFTER(item_list, head, &items[3], &items[1]); + assert_se(LIST_INSERT_AFTER(item_list, head, &items[3], &items[1]) == &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]); @@ -84,7 +83,7 @@ int main(int argc, const char *argv[]) { assert_se(items[1].item_list_prev == &items[3]); assert_se(items[3].item_list_prev == NULL); - LIST_REMOVE(item_list, head, &items[1]); + assert_se(LIST_REMOVE(item_list, head, &items[1]) == &items[1]); assert_se(LIST_JUST_US(item_list, &items[1])); assert_se(items[0].item_list_next == NULL); @@ -95,7 +94,7 @@ int main(int argc, const char *argv[]) { assert_se(items[2].item_list_prev == &items[3]); assert_se(items[3].item_list_prev == NULL); - LIST_INSERT_BEFORE(item_list, head, &items[2], &items[1]); + assert_se(LIST_INSERT_BEFORE(item_list, head, &items[2], &items[1]) == &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]); @@ -106,7 +105,7 @@ int main(int argc, const char *argv[]) { assert_se(items[1].item_list_prev == &items[3]); assert_se(items[3].item_list_prev == NULL); - LIST_REMOVE(item_list, head, &items[0]); + assert_se(LIST_REMOVE(item_list, head, &items[0]) == &items[0]); assert_se(LIST_JUST_US(item_list, &items[0])); assert_se(items[2].item_list_next == NULL); @@ -117,7 +116,7 @@ int main(int argc, const char *argv[]) { assert_se(items[1].item_list_prev == &items[3]); assert_se(items[3].item_list_prev == NULL); - LIST_INSERT_BEFORE(item_list, head, &items[3], &items[0]); + assert_se(LIST_INSERT_BEFORE(item_list, head, &items[3], &items[0]) == &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]); @@ -129,7 +128,7 @@ int main(int argc, const char *argv[]) { assert_se(items[0].item_list_prev == NULL); assert_se(head == &items[0]); - LIST_REMOVE(item_list, head, &items[0]); + assert_se(LIST_REMOVE(item_list, head, &items[0]) == &items[0]); assert_se(LIST_JUST_US(item_list, &items[0])); assert_se(items[2].item_list_next == NULL); @@ -140,7 +139,7 @@ int main(int argc, const char *argv[]) { assert_se(items[1].item_list_prev == &items[3]); assert_se(items[3].item_list_prev == NULL); - LIST_INSERT_BEFORE(item_list, head, NULL, &items[0]); + assert_se(LIST_INSERT_BEFORE(item_list, head, NULL, &items[0]) == &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]); @@ -151,7 +150,7 @@ int main(int argc, const char *argv[]) { assert_se(items[1].item_list_prev == &items[3]); assert_se(items[3].item_list_prev == NULL); - LIST_REMOVE(item_list, head, &items[0]); + assert_se(LIST_REMOVE(item_list, head, &items[0]) == &items[0]); assert_se(LIST_JUST_US(item_list, &items[0])); assert_se(items[2].item_list_next == NULL); @@ -162,7 +161,7 @@ int main(int argc, const char *argv[]) { assert_se(items[1].item_list_prev == &items[3]); assert_se(items[3].item_list_prev == NULL); - LIST_REMOVE(item_list, head, &items[1]); + assert_se(LIST_REMOVE(item_list, head, &items[1]) == &items[1]); assert_se(LIST_JUST_US(item_list, &items[1])); assert_se(items[2].item_list_next == NULL); @@ -171,18 +170,18 @@ int main(int argc, const char *argv[]) { assert_se(items[2].item_list_prev == &items[3]); assert_se(items[3].item_list_prev == NULL); - LIST_REMOVE(item_list, head, &items[2]); + assert_se(LIST_REMOVE(item_list, head, &items[2]) == &items[2]); assert_se(LIST_JUST_US(item_list, &items[2])); assert_se(LIST_JUST_US(item_list, head)); - LIST_REMOVE(item_list, head, &items[3]); + assert_se(LIST_REMOVE(item_list, head, &items[3]) == &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_list, &items[i])); - LIST_APPEND(item_list, head, &items[i]); + assert_se(LIST_APPEND(item_list, head, &items[i]) == &items[i]); } assert_se(!LIST_JUST_US(item_list, head)); @@ -198,20 +197,20 @@ int main(int argc, const char *argv[]) { assert_se(items[3].item_list_prev == &items[2]); for (i = 0; i < ELEMENTSOF(items); i++) - LIST_REMOVE(item_list, head, &items[i]); + assert_se(LIST_REMOVE(item_list, head, &items[i]) == &items[i]); assert_se(head == NULL); for (i = 0; i < ELEMENTSOF(items) / 2; i++) { LIST_INIT(item_list, &items[i]); assert_se(LIST_JUST_US(item_list, &items[i])); - LIST_PREPEND(item_list, head, &items[i]); + assert_se(LIST_PREPEND(item_list, head, &items[i]) == &items[i]); } for (i = ELEMENTSOF(items) / 2; i < ELEMENTSOF(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(LIST_PREPEND(item_list, head2, &items[i]) == &items[i]); } assert_se(items[0].item_list_next == NULL); @@ -224,7 +223,7 @@ int main(int argc, const char *argv[]) { assert_se(items[2].item_list_prev == &items[3]); assert_se(items[3].item_list_prev == NULL); - LIST_JOIN(item_list, head2, head); + assert_se(LIST_JOIN(item_list, head2, head) == head2); assert_se(head == NULL); assert_se(items[0].item_list_next == NULL); @@ -237,18 +236,18 @@ int main(int argc, const char *argv[]) { assert_se(items[2].item_list_prev == &items[3]); assert_se(items[3].item_list_prev == NULL); - LIST_JOIN(item_list, head, head2); + assert_se(LIST_JOIN(item_list, head, head2) == head); assert_se(head2 == NULL); assert_se(head); for (i = 0; i < ELEMENTSOF(items); i++) - LIST_REMOVE(item_list, head, &items[i]); + assert_se(LIST_REMOVE(item_list, head, &items[i]) == &items[i]); assert_se(head == NULL); - LIST_PREPEND(item_list, head, items + 0); - LIST_PREPEND(item_list, head, items + 1); - LIST_PREPEND(item_list, head, items + 2); + assert_se(LIST_PREPEND(item_list, head, items + 0) == items + 0); + assert_se(LIST_PREPEND(item_list, head, items + 1) == items + 1); + assert_se(LIST_PREPEND(item_list, head, items + 2) == items + 2); assert_se(LIST_POP(item_list, head) == items + 2); assert_se(LIST_POP(item_list, head) == items + 1); diff --git a/src/timesync/timesyncd-server.c b/src/timesync/timesyncd-server.c index 3b7d79323f..7aa1551baf 100644 --- a/src/timesync/timesyncd-server.c +++ b/src/timesync/timesyncd-server.c @@ -37,7 +37,7 @@ int server_address_new( memcpy(&a->sockaddr, sockaddr, socklen); - LIST_FIND_TAIL(addresses, n->addresses, tail); + tail = LIST_FIND_TAIL(addresses, n->addresses); LIST_INSERT_AFTER(addresses, n->addresses, tail, a); if (ret) |