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.
This commit is contained in:
Daan De Meyer 2022-08-12 16:13:05 +02:00
parent d80463ea2f
commit cc232fa094
8 changed files with 65 additions and 69 deletions

View file

@ -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) \
({ \

View 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;

View 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;

View 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;

View 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;

View 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) {

View 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);

View 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)