]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
basic: Use statement expressions more in list.h
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Fri, 12 Aug 2022 14:13:05 +0000 (16:13 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Sun, 8 Jan 2023 15:31:15 +0000 (16:31 +0100)
Let's use statement expressions to return values instead of passing
in return arguments to the LIST macros.

src/basic/list.h
src/core/execute.c
src/core/load-fragment.c
src/resolve/resolved-dns-search-domain.c
src/resolve/resolved-dns-server.c
src/systemctl/systemctl-show.c
src/test/test-list.c
src/timesync/timesyncd-server.c

index ca300396908c8d9dba5f9133e7f55e68bb5bcbe0..4a6e1505a51d29f5392da824004c2897138b4190 100644 (file)
 
 /* 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)                                 \
                         *_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) {                                              \
                         _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) {                                              \
                         _b->name##_next = _a;                           \
                         _a->name##_prev = _b;                           \
                 }                                                       \
-        } while (false)
+                _b;                                                     \
+        })
 
 #define LIST_JUST_US(name,item)                                         \
         (!(item)->name##_prev && !(item)->name##_next)
 
 /* 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)                                               \
         ({                                                              \
index 439f491d024682e6cfd0619c8a47c64b4afd4a56..94c0e6a90d83f1c0917f96fbfdcab6fb72b5cbce 100644 (file)
@@ -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;
index e115aa62706b94bbc4b4fd2f7724d988965f528d..22388d2ed6b4bf1ec027c0bc99e59491977a51b7 100644 (file)
@@ -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;
index 647c0bd1f96c69169b424b05e4e265ee81dfc74e..a11b21350a57ab43286df6ad8246a25c7f087b57 100644 (file)
@@ -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;
index 8ff513fa33f08c62d9790138961d2d3a9d976185..342a90abcb697b7ecdaeb7bf1d785c5b12b8182b 100644 (file)
@@ -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;
index f78cf307cac515fdac0d81ae44b5170f88bf8474..c004b804a58c2fd1d892c3dfc539312533e5124e 100644 (file)
@@ -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) {
 
index ea45f5b95cf0cd8e4004fa569859037639cbc0a6..307c1bf9361197529f768b8fc7ebca1925030303 100644 (file)
@@ -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);
index 3b7d79323fee443e8892f43aea087d815009c2bc..7aa1551baf149e7d6993d14958aaf557057c4a31 100644 (file)
@@ -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)