diff --git a/server/include/list.h b/server/include/list.h index 1defacaf4..999365833 100644 --- a/server/include/list.h +++ b/server/include/list.h @@ -12,8 +12,13 @@ #include +#include "hooks.h" + /* Forward declarations */ struct list_elm; +struct node; +struct path; +struct interface; /** Static list initialization */ #define LIST_INIT { \ @@ -24,27 +29,41 @@ struct list_elm; } #define FOREACH(list, elm) \ - for ( struct list_elm *elm = (list)->head, ; \ - elm != (list)->tail; elm = elm->next ) + for ( struct list_elm *elm = (list)->head; \ + elm; elm = elm->next ) + +#define FOREACH_R(list, elm) \ + for ( struct list_elm *elm = (list)->tail; \ + elm; elm = elm->prev ) + +#define list_first(list) ((list)->head) +#define list_last(list) ((list)->head) +#define list_length(list) ((list)->count) struct list { - struct list_elm *head, tail; + struct list_elm *head, *tail; int count; - pthread_mutex_lock_t lock; + pthread_mutex_t lock; }; struct list_elm { - void *data; + union { + void *ptr; + struct node *node; + struct path *path; + struct interface *interface; + hook_cb_t hook; + }; - struct list_elm *prev, next; + struct list_elm *prev, *next; }; void list_init(struct list *l); void list_destroy(struct list *l); -void list_push(struct list *l, void *d); +void list_push(struct list *l, void *p); struct list_elm * list_search(struct list *l, int (*cmp)(void *)); diff --git a/server/src/list.c b/server/src/list.c index 247d8af61..d34c8beef 100644 --- a/server/src/list.c +++ b/server/src/list.c @@ -7,6 +7,7 @@ * @file */ +#include "utils.h" #include "list.h" void list_init(struct list *l) @@ -33,26 +34,34 @@ void list_destroy(struct list *l) pthread_mutex_destroy(&l->lock); } -void list_push(struct list *l, void *d) +void list_push(struct list *l, void *p) { struct list_elm *e = alloc(sizeof(struct list_elm)); pthread_mutex_lock(&l->lock); - e->data = d; + e->ptr = p; e->prev = l->tail; e->next = NULL; - l->tail->next = e; + if (l->tail) + l->tail->next = e; + if (l->head) + l->head->prev = e; + else + l->head = e; + l->tail = e; + l->count++; + pthread_mutex_unlock(&l->lock); } struct list_elm * list_search(struct list *l, int (*cmp)(void *)) { - foreach(l, it) { - if (!cmp(it->data)) + FOREACH(l, it) { + if (!cmp(it->ptr)) return it; }