diff options
-rw-r--r-- | src/libmdsserver/linked-list.h | 9 | ||||
-rw-r--r-- | src/mds-server.c | 29 |
2 files changed, 16 insertions, 22 deletions
diff --git a/src/libmdsserver/linked-list.h b/src/libmdsserver/linked-list.h index 69298ca..30808fc 100644 --- a/src/libmdsserver/linked-list.h +++ b/src/libmdsserver/linked-list.h @@ -266,6 +266,15 @@ void linked_list_marshal(const linked_list_t* restrict this, char* restrict data */ int linked_list_unmarshal(linked_list_t* restrict this, char* restrict data); +/** + * Wrapper for `for` keyword that iterates over each element in a linked list + * + * @param list:linked_list_t The linked list + * @param node:ssize_t The variable to store the node in at each iteration + */ +#define foreach_linked_list_node(list, node) \ + for (node = list.edge; (node = list.next[node]) != list.edge;) + #endif diff --git a/src/mds-server.c b/src/mds-server.c index 8c9a863..d3ef424 100644 --- a/src/mds-server.c +++ b/src/mds-server.c @@ -683,13 +683,9 @@ void sigusr1_trap(int signo) pthread_kill(master_thread, signo); with_mutex(slave_mutex, - for (node = client_list.edge;;) + foreach_linked_list_node (client_list, node) { - client_t* value; - if ((node = client_list.next[node]) == client_list.edge) - break; - - value = (client_t*)(void*)(client_list.values[node]); + client_t* value = (client_t*)(void*)(client_list.values[node]); if (pthread_equal(current_thread, value->thread) == 0) pthread_kill(value->thread, signo); }); @@ -751,19 +747,12 @@ int marshal_server(int fd) buf_set_next(state_buf_, size_t, list_elements); /* Marshal the clients. */ - for (node = client_list.edge;;) + foreach_linked_list_node (client_list, node) { - size_t value_address; - client_t* value; - - /* Get the next client's node in the linked list. */ - if ((node = client_list.next[node]) == client_list.edge) - break; - /* Get the memory address of the client. */ - value_address = client_list.values[node]; + size_t value_address = client_list.values[node]; /* Get the client's information. */ - value = (client_t*)(void*)value_address; + client_t* value = (client_t*)(void*)value_address; /* Get the marshalled size of the message. */ msg_size = mds_message_marshal_size(&(value->message), 1); @@ -1004,14 +993,10 @@ int unmarshal_server(int fd) client_map.used[i / 64] &= ~((uint64_t)1 << (i % 64)); /* Remap the linked list and remove non-found elements, and start the clients. */ - for (node = client_list.edge;;) + foreach_linked_list_node (client_list, node) { - size_t new_address; - if ((node = client_list.next[node]) == client_list.edge) - break; - /* Remap the linked list and remove non-found elements. */ - new_address = unmarshal_remapper(client_list.values[node]); + size_t new_address = unmarshal_remapper(client_list.values[node]); client_list.values[node] = new_address; if (new_address == 0) /* Returned if missing (or if the address is the invalid NULL.) */ linked_list_remove(&client_list, node); |