Get rid of patty_list_iterator (too expensive)
This commit is contained in:
parent
c6aac035e1
commit
5a3660450f
4 changed files with 27 additions and 108 deletions
|
@ -18,13 +18,6 @@ typedef struct _patty_list {
|
||||||
*last;
|
*last;
|
||||||
} patty_list;
|
} 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);
|
typedef void (*patty_list_callback)(void *item, void *ctx);
|
||||||
|
|
||||||
patty_list *patty_list_new();
|
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);
|
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,
|
void patty_list_each(patty_list *list,
|
||||||
patty_list_callback callback,
|
patty_list_callback callback,
|
||||||
void *ctx);
|
void *ctx);
|
||||||
|
|
36
src/if.c
36
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 patty_ax25_if_addr_each(patty_ax25_if *iface,
|
||||||
int (*callback)(char *, uint8_t, void *), void *ctx) {
|
int (*callback)(char *, uint8_t, void *), void *ctx) {
|
||||||
patty_list_iterator *iter;
|
patty_list_item *item = iface->aliases->first;
|
||||||
patty_ax25_addr *addr;
|
|
||||||
|
|
||||||
char buf[7];
|
char buf[7];
|
||||||
uint8_t ssid;
|
uint8_t ssid;
|
||||||
|
@ -131,11 +130,9 @@ int patty_ax25_if_addr_each(patty_ax25_if *iface,
|
||||||
goto error_callback_addr;
|
goto error_callback_addr;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((iter = patty_list_start(iface->aliases)) == NULL) {
|
while (item) {
|
||||||
goto error_list_start;
|
patty_ax25_addr *addr = item->value;
|
||||||
}
|
|
||||||
|
|
||||||
while ((addr = patty_list_next(iter)) != NULL) {
|
|
||||||
if (patty_ax25_ntop(addr, buf, &ssid, sizeof(buf)) < 0) {
|
if (patty_ax25_ntop(addr, buf, &ssid, sizeof(buf)) < 0) {
|
||||||
goto error_ntop;
|
goto error_ntop;
|
||||||
}
|
}
|
||||||
|
@ -143,17 +140,14 @@ int patty_ax25_if_addr_each(patty_ax25_if *iface,
|
||||||
if (callback(buf, ssid, ctx) < 0) {
|
if (callback(buf, ssid, ctx) < 0) {
|
||||||
goto error_callback;
|
goto error_callback;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
patty_list_finish(iter);
|
item = item->next;
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
error_callback:
|
error_callback:
|
||||||
error_ntop:
|
error_ntop:
|
||||||
patty_list_finish(iter);
|
|
||||||
|
|
||||||
error_list_start:
|
|
||||||
error_callback_addr:
|
error_callback_addr:
|
||||||
error_ntop_addr:
|
error_ntop_addr:
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -162,8 +156,7 @@ error_ntop_addr:
|
||||||
static patty_ax25_addr *find_addr(patty_ax25_if *iface,
|
static patty_ax25_addr *find_addr(patty_ax25_if *iface,
|
||||||
const char *callsign,
|
const char *callsign,
|
||||||
uint8_t ssid) {
|
uint8_t ssid) {
|
||||||
patty_list_iterator *iter;
|
patty_list_item *item = iface->aliases->first;
|
||||||
patty_ax25_addr *addr;
|
|
||||||
|
|
||||||
char buf[7];
|
char buf[7];
|
||||||
uint8_t addr_ssid;
|
uint8_t addr_ssid;
|
||||||
|
@ -176,32 +169,27 @@ static patty_ax25_addr *find_addr(patty_ax25_if *iface,
|
||||||
return &iface->addr;
|
return &iface->addr;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((iter = patty_list_start(iface->aliases)) == NULL) {
|
while (item) {
|
||||||
goto error_list_start;
|
patty_ax25_addr *addr = item->value;
|
||||||
}
|
|
||||||
|
|
||||||
while ((addr = patty_list_next(iter)) != NULL) {
|
|
||||||
if (patty_ax25_ntop(addr, buf, &addr_ssid, sizeof(buf)) < 0) {
|
if (patty_ax25_ntop(addr, buf, &addr_ssid, sizeof(buf)) < 0) {
|
||||||
goto error_ntop;
|
goto error_ntop;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strncmp(buf, callsign, sizeof(buf)) != 0) {
|
if (strncmp(buf, callsign, sizeof(buf)) != 0) {
|
||||||
continue;
|
goto next;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (addr_ssid != ssid) {
|
if (addr_ssid != ssid) {
|
||||||
continue;
|
goto next;
|
||||||
}
|
}
|
||||||
|
|
||||||
patty_list_finish(iter);
|
|
||||||
|
|
||||||
return addr;
|
return addr;
|
||||||
|
next:
|
||||||
|
item = item->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
error_ntop:
|
error_ntop:
|
||||||
patty_list_finish(iter);
|
|
||||||
|
|
||||||
error_list_start:
|
|
||||||
error_ntop_addr:
|
error_ntop_addr:
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
36
src/list.c
36
src/list.c
|
@ -191,42 +191,6 @@ void *patty_list_index(patty_list *list, off_t index) {
|
||||||
return item->value;
|
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) {
|
void patty_list_each(patty_list *list, patty_list_callback callback, void *ctx) {
|
||||||
patty_list_item *item;
|
patty_list_item *item;
|
||||||
|
|
||||||
|
|
48
src/server.c
48
src/server.c
|
@ -377,51 +377,39 @@ error_list_splice:
|
||||||
|
|
||||||
patty_ax25_if *patty_ax25_server_get_if(patty_ax25_server *server,
|
patty_ax25_if *patty_ax25_server_get_if(patty_ax25_server *server,
|
||||||
const char *name) {
|
const char *name) {
|
||||||
patty_list_iterator *iter;
|
patty_list_item *item = server->ifaces->first;
|
||||||
patty_ax25_if *iface;
|
|
||||||
|
|
||||||
if ((iter = patty_list_start(server->ifaces)) == NULL) {
|
while (item) {
|
||||||
goto error_list_start;
|
patty_ax25_if *iface = item->value;
|
||||||
}
|
|
||||||
|
|
||||||
while ((iface = patty_list_next(iter)) != NULL) {
|
|
||||||
if (strncmp(iface->name, name, sizeof(iface->name)) == 0) {
|
if (strncmp(iface->name, name, sizeof(iface->name)) == 0) {
|
||||||
patty_list_finish(iter);
|
|
||||||
|
|
||||||
return iface;
|
return iface;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
item = item->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
patty_list_finish(iter);
|
|
||||||
|
|
||||||
error_list_start:
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
int patty_ax25_server_each_if(patty_ax25_server *server,
|
int patty_ax25_server_each_if(patty_ax25_server *server,
|
||||||
int (*callback)(patty_ax25_if *, void *),
|
int (*callback)(patty_ax25_if *, void *),
|
||||||
void *ctx) {
|
void *ctx) {
|
||||||
patty_list_iterator *iter;
|
patty_list_item *item = server->ifaces->first;
|
||||||
patty_ax25_if *iface;
|
|
||||||
|
|
||||||
if ((iter = patty_list_start(server->ifaces)) == NULL) {
|
while (item) {
|
||||||
goto error_list_start;
|
patty_ax25_if *iface = item->value;
|
||||||
}
|
|
||||||
|
|
||||||
while ((iface = patty_list_next(iter)) != NULL) {
|
|
||||||
if (callback(iface, ctx) < 0) {
|
if (callback(iface, ctx) < 0) {
|
||||||
goto error_callback;
|
goto error_callback;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
patty_list_finish(iter);
|
item = item->next;
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
error_callback:
|
error_callback:
|
||||||
patty_list_finish(iter);
|
|
||||||
|
|
||||||
error_list_start:
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1301,27 +1289,21 @@ error_io:
|
||||||
}
|
}
|
||||||
|
|
||||||
static int handle_ifaces(patty_ax25_server *server) {
|
static int handle_ifaces(patty_ax25_server *server) {
|
||||||
patty_list_iterator *iter;
|
patty_list_item *item = server->ifaces->first;
|
||||||
patty_ax25_if *iface;
|
|
||||||
|
|
||||||
if ((iter = patty_list_start(server->ifaces)) == NULL) {
|
while (item) {
|
||||||
goto error_list_start;
|
patty_ax25_if *iface = item->value;
|
||||||
}
|
|
||||||
|
|
||||||
while ((iface = patty_list_next(iter)) != NULL) {
|
|
||||||
if (handle_iface(server, iface) < 0) {
|
if (handle_iface(server, iface) < 0) {
|
||||||
goto error_io;
|
goto error_io;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
patty_list_finish(iter);
|
item = item->next;
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
error_io:
|
error_io:
|
||||||
patty_list_finish(iter);
|
|
||||||
|
|
||||||
error_list_start:
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue