From 5a3660450f05601b24cb13d61103301cd5329a19 Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Thu, 2 Jul 2020 17:13:19 -0400 Subject: [PATCH] Get rid of patty_list_iterator (too expensive) --- include/patty/list.h | 15 -------------- src/if.c | 36 +++++++++++---------------------- src/list.c | 36 --------------------------------- src/server.c | 48 ++++++++++++++------------------------------ 4 files changed, 27 insertions(+), 108 deletions(-) diff --git a/include/patty/list.h b/include/patty/list.h index b2f582c..ef07b75 100644 --- a/include/patty/list.h +++ b/include/patty/list.h @@ -18,13 +18,6 @@ typedef struct _patty_list { *last; } patty_list; -typedef struct _patty_list_iterator { - patty_list *list; - - size_t index; - patty_list_item *item; -} patty_list_iterator; - typedef void (*patty_list_callback)(void *item, void *ctx); patty_list *patty_list_new(); @@ -51,14 +44,6 @@ void *patty_list_insert(patty_list *list, off_t index); void *patty_list_index(patty_list *list, off_t index); -patty_list_iterator *patty_list_start(patty_list *list); - -void *patty_list_next(patty_list_iterator *iterator); - -void patty_list_reset(patty_list_iterator *iterator); - -void patty_list_finish(patty_list_iterator *iterator); - void patty_list_each(patty_list *list, patty_list_callback callback, void *ctx); diff --git a/src/if.c b/src/if.c index ab72d7c..288534d 100644 --- a/src/if.c +++ b/src/if.c @@ -117,8 +117,7 @@ void patty_ax25_if_destroy(patty_ax25_if *iface) { int patty_ax25_if_addr_each(patty_ax25_if *iface, int (*callback)(char *, uint8_t, void *), void *ctx) { - patty_list_iterator *iter; - patty_ax25_addr *addr; + patty_list_item *item = iface->aliases->first; char buf[7]; uint8_t ssid; @@ -131,11 +130,9 @@ int patty_ax25_if_addr_each(patty_ax25_if *iface, goto error_callback_addr; } - if ((iter = patty_list_start(iface->aliases)) == NULL) { - goto error_list_start; - } + while (item) { + patty_ax25_addr *addr = item->value; - while ((addr = patty_list_next(iter)) != NULL) { if (patty_ax25_ntop(addr, buf, &ssid, sizeof(buf)) < 0) { goto error_ntop; } @@ -143,17 +140,14 @@ int patty_ax25_if_addr_each(patty_ax25_if *iface, if (callback(buf, ssid, ctx) < 0) { goto error_callback; } - } - patty_list_finish(iter); + item = item->next; + } return 0; error_callback: error_ntop: - patty_list_finish(iter); - -error_list_start: error_callback_addr: error_ntop_addr: return -1; @@ -162,8 +156,7 @@ error_ntop_addr: static patty_ax25_addr *find_addr(patty_ax25_if *iface, const char *callsign, uint8_t ssid) { - patty_list_iterator *iter; - patty_ax25_addr *addr; + patty_list_item *item = iface->aliases->first; char buf[7]; uint8_t addr_ssid; @@ -176,32 +169,27 @@ static patty_ax25_addr *find_addr(patty_ax25_if *iface, return &iface->addr; } - if ((iter = patty_list_start(iface->aliases)) == NULL) { - goto error_list_start; - } + while (item) { + patty_ax25_addr *addr = item->value; - while ((addr = patty_list_next(iter)) != NULL) { if (patty_ax25_ntop(addr, buf, &addr_ssid, sizeof(buf)) < 0) { goto error_ntop; } if (strncmp(buf, callsign, sizeof(buf)) != 0) { - continue; + goto next; } if (addr_ssid != ssid) { - continue; + goto next; } - patty_list_finish(iter); - return addr; +next: + item = item->next; } error_ntop: - patty_list_finish(iter); - -error_list_start: error_ntop_addr: return NULL; } diff --git a/src/list.c b/src/list.c index 9ca8c1c..f7335a8 100644 --- a/src/list.c +++ b/src/list.c @@ -191,42 +191,6 @@ void *patty_list_index(patty_list *list, off_t index) { return item->value; } -patty_list_iterator *patty_list_start(patty_list *list) { - patty_list_iterator *iterator; - - if ((iterator = malloc(sizeof(patty_list_iterator))) == NULL) { - goto error_malloc; - } - - iterator->list = list; - iterator->item = list->first; - - return iterator; - -error_malloc: - return NULL; -} - -void *patty_list_next(patty_list_iterator *iterator) { - void *value = NULL; - - if (iterator->item) { - value = iterator->item->value; - - iterator->item = iterator->item->next; - } - - return value; -} - -void patty_list_reset(patty_list_iterator *iterator) { - iterator->item = iterator->list->first; -} - -void patty_list_finish(patty_list_iterator *iterator) { - free(iterator); -} - void patty_list_each(patty_list *list, patty_list_callback callback, void *ctx) { patty_list_item *item; diff --git a/src/server.c b/src/server.c index 7923f1f..2e3c9c3 100644 --- a/src/server.c +++ b/src/server.c @@ -377,51 +377,39 @@ error_list_splice: patty_ax25_if *patty_ax25_server_get_if(patty_ax25_server *server, const char *name) { - patty_list_iterator *iter; - patty_ax25_if *iface; + patty_list_item *item = server->ifaces->first; - if ((iter = patty_list_start(server->ifaces)) == NULL) { - goto error_list_start; - } + while (item) { + patty_ax25_if *iface = item->value; - while ((iface = patty_list_next(iter)) != NULL) { if (strncmp(iface->name, name, sizeof(iface->name)) == 0) { - patty_list_finish(iter); - return iface; } + + item = item->next; } - patty_list_finish(iter); - -error_list_start: return NULL; } int patty_ax25_server_each_if(patty_ax25_server *server, int (*callback)(patty_ax25_if *, void *), void *ctx) { - patty_list_iterator *iter; - patty_ax25_if *iface; + patty_list_item *item = server->ifaces->first; - if ((iter = patty_list_start(server->ifaces)) == NULL) { - goto error_list_start; - } + while (item) { + patty_ax25_if *iface = item->value; - while ((iface = patty_list_next(iter)) != NULL) { if (callback(iface, ctx) < 0) { goto error_callback; } - } - patty_list_finish(iter); + item = item->next; + } return 0; error_callback: - patty_list_finish(iter); - -error_list_start: return -1; } @@ -1301,27 +1289,21 @@ error_io: } static int handle_ifaces(patty_ax25_server *server) { - patty_list_iterator *iter; - patty_ax25_if *iface; + patty_list_item *item = server->ifaces->first; - if ((iter = patty_list_start(server->ifaces)) == NULL) { - goto error_list_start; - } + while (item) { + patty_ax25_if *iface = item->value; - while ((iface = patty_list_next(iter)) != NULL) { if (handle_iface(server, iface) < 0) { goto error_io; } - } - patty_list_finish(iter); + item = item->next; + } return 0; error_io: - patty_list_finish(iter); - -error_list_start: return -1; }