From: Pietro Cerutti Date: Tue, 3 Oct 2017 08:18:26 +0000 (+0000) Subject: TAILQify struct Connection X-Git-Tag: neomutt-20171006~1 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b97206f1a76480e03f27ca32df44b2caef950e73;p=neomutt TAILQify struct Connection --- diff --git a/imap/imap.c b/imap/imap.c index ae1116c75..302a4681e 100644 --- a/imap/imap.c +++ b/imap/imap.c @@ -200,24 +200,18 @@ int imap_delete_mailbox(struct Context *ctx, struct ImapMbox *mx) */ void imap_logout_all(void) { - struct Connection *conn = NULL; - struct Connection *tmp = NULL; - - conn = mutt_socket_head(); - - while (conn) + struct ConnectionList *head = mutt_socket_head(); + struct Connection *np, *tmp; + TAILQ_FOREACH_SAFE(np, head, entries, tmp) { - tmp = conn->next; - - if (conn->account.type == MUTT_ACCT_TYPE_IMAP && conn->fd >= 0) + if (np->account.type == MUTT_ACCT_TYPE_IMAP && np->fd >= 0) { - mutt_message(_("Closing connection to %s..."), conn->account.host); - imap_logout((struct ImapData **) (void *) &conn->data); + TAILQ_REMOVE(head, np, entries); + mutt_message(_("Closing connection to %s..."), np->account.host); + imap_logout((struct ImapData **) (void *) &np->data); mutt_clear_error(); - mutt_socket_free(conn); + mutt_socket_free(np); } - - conn = tmp; } } @@ -2303,7 +2297,7 @@ static int imap_complete_hosts(char *dest, size_t len) } } - for (conn = mutt_socket_head(); conn; conn = conn->next) + TAILQ_FOREACH(conn, mutt_socket_head(), entries) { struct Url url; char urlstr[LONG_STRING]; diff --git a/imap/util.c b/imap/util.c index 6d6acce3c..baa29d2c4 100644 --- a/imap/util.c +++ b/imap/util.c @@ -902,7 +902,7 @@ void imap_keepalive(void) struct ImapData *idata = NULL; time_t now = time(NULL); - for (conn = mutt_socket_head(); conn; conn = conn->next) + TAILQ_FOREACH(conn, mutt_socket_head(), entries) { if (conn->account.type == MUTT_ACCT_TYPE_IMAP) { diff --git a/mutt_socket.c b/mutt_socket.c index d4bb01674..0288e201c 100644 --- a/mutt_socket.c +++ b/mutt_socket.c @@ -46,7 +46,7 @@ #endif /* support for multiple socket connections */ -static struct Connection *Connections = NULL; +static struct ConnectionList Connections = TAILQ_HEAD_INITIALIZER(Connections); static int socket_preconnect(void) { @@ -218,9 +218,9 @@ int mutt_socket_readln_d(char *buf, size_t buflen, struct Connection *conn, int return i + 1; } -struct Connection *mutt_socket_head(void) +struct ConnectionList *mutt_socket_head(void) { - return Connections; + return &Connections; } /** @@ -228,29 +228,15 @@ struct Connection *mutt_socket_head(void) */ void mutt_socket_free(struct Connection *conn) { - struct Connection *iter = NULL; - struct Connection *tmp = NULL; - - iter = Connections; - - /* head is special case, doesn't need prev updated */ - if (iter == conn) - { - Connections = iter->next; - FREE(&iter); - return; - } - - while (iter->next) + struct Connection *np = NULL; + TAILQ_FOREACH(np, &Connections, entries) { - if (iter->next == conn) + if (np == conn) { - tmp = iter->next; - iter->next = tmp->next; - FREE(&tmp); + TAILQ_REMOVE(&Connections, np, entries); + FREE(&np); return; } - iter = iter->next; } } @@ -288,19 +274,18 @@ struct Connection *mutt_conn_find(const struct Connection *start, const struct A url_tostring(&url, hook, sizeof(hook), 0); mutt_account_hook(hook); - conn = start ? start->next : Connections; + conn = start ? TAILQ_NEXT(start, entries) : TAILQ_FIRST(&Connections); while (conn) { if (mutt_account_match(account, &(conn->account))) return conn; - conn = conn->next; + conn = TAILQ_NEXT(conn, entries); } conn = socket_new_conn(); memcpy(&conn->account, account, sizeof(struct Account)); - conn->next = Connections; - Connections = conn; + TAILQ_INSERT_HEAD(&Connections, conn, entries); if (Tunnel && *Tunnel) mutt_tunnel_socket_setup(conn); diff --git a/mutt_socket.h b/mutt_socket.h index 0e580e32b..c770a9bfb 100644 --- a/mutt_socket.h +++ b/mutt_socket.h @@ -26,6 +26,7 @@ #include #include "account.h" +#include "list.h" #include "lib/lib.h" /* logging levels */ @@ -33,6 +34,11 @@ #define MUTT_SOCK_LOG_HDR 3 #define MUTT_SOCK_LOG_FULL 4 +/** + * struct ConnectionList - A list of connections + */ +TAILQ_HEAD(ConnectionList, Connection); + /** * struct Connection - An open network connection (socket) */ @@ -48,7 +54,7 @@ struct Connection int fd; int available; - struct Connection *next; + TAILQ_ENTRY(Connection) entries; void *sockdata; int (*conn_read)(struct Connection *conn, char *buf, size_t len); @@ -69,7 +75,7 @@ int mutt_socket_readln_d(char *buf, size_t buflen, struct Connection *conn, int int mutt_socket_write_d(struct Connection *conn, const char *buf, int len, int dbg); /* stupid hack for imap_logout_all */ -struct Connection *mutt_socket_head(void); +struct ConnectionList *mutt_socket_head(void); void mutt_socket_free(struct Connection *conn); struct Connection *mutt_conn_find(const struct Connection *start, const struct Account *account);