[PATCH 2/3] daemon.c: simplify add_child() and kill_some_child() logic
From: Junio C Hamano <hidden>
Date: 2016-06-15 22:45:13
Subsystem:
the rest · Maintainer:
Linus Torvalds
kill_some_child() function walks the list of connections and kills the first connection it finds from the same address. There is not much point to find the existing connection from the same address in add_child() to insert the new one in front of it. At the front of the whole queue is just as good. Signed-off-by: Junio C Hamano <redacted> --- daemon.c | 19 ++++++++++--------- 1 files changed, 10 insertions(+), 9 deletions(-)
diff --git a/daemon.c b/daemon.c
index 35bd34a..8d2755a 100644
--- a/daemon.c
+++ b/daemon.c@@ -596,18 +596,18 @@ static struct child { static void add_child(pid_t pid, struct sockaddr *addr, int addrlen) { - struct child *newborn, **cradle; - newborn = xcalloc(1, sizeof(*newborn)); + struct child *newborn; + /* + * This must be xcalloc() -- we'll compare the whole sockaddr_storage + * later in kill_some_child(). + */ + newborn = xcalloc(1, sizeof(*newborn)); live_children++; newborn->pid = pid; memcpy(&newborn->address, addr, addrlen); - for (cradle = &firstborn; *cradle; cradle = &(*cradle)->next) - if (!memcmp(&(*cradle)->address, &newborn->address, - sizeof(newborn->address))) - break; - newborn->next = *cradle; - *cradle = newborn; + newborn->next = firstborn; + firstborn = newborn; } static void remove_child(pid_t pid)
@@ -627,7 +627,8 @@ static void remove_child(pid_t pid) * This gets called if the number of connections grows * past "max_connections". * - * We kill the newest connection from a duplicate IP. + * We kill the newest connection from the same address (add_child() queues + * new ones at the front). */ static void kill_some_child(void) {
--
1.6.0.129.ge10d2