diff --git a/include/hooks.h b/include/hooks.h index 9914ffd65..c10198340 100644 --- a/include/hooks.h +++ b/include/hooks.h @@ -88,6 +88,9 @@ struct hook { hook_cb_t cb; /**< The hook callback function as a function pointer. */ }; +/** Sort hook list according to the their priority. See hook::priority. */ +int hooks_sort_priority(const void *a, const void *b); + /** The following prototypes are example hooks * * @addtogroup hooks_examples Examples for hook functions diff --git a/include/path.h b/include/path.h index 8592fa7ca..7ec31abe6 100644 --- a/include/path.h +++ b/include/path.h @@ -140,4 +140,7 @@ const char * path_name(struct path *p); */ int path_run_hook(struct path *p, enum hook_type t); +/** Check if node is used as source or destination of a path. */ +int path_uses_node(struct path *p, struct node *n); + #endif /** _PATH_H_ @} */ diff --git a/lib/list.c b/lib/list.c index 47c59578b..d355cd507 100644 --- a/lib/list.c +++ b/lib/list.c @@ -13,6 +13,25 @@ #include "list.h" +/* Compare functions */ +static int cmp_lookup(const void *a, const void *b) { + const struct { + char *name; + } *obj = a; + + return strcmp(obj->name, b); +} + +int cmp_contains(const void *a, const void *b) { + return a == b ? 0 : 1; +} + +int cmp_sort(const void *a, const void *b, void *thunk) { + cmp_cb_t cmp = (cmp_cb_t) thunk; + + return cmp(*(void **) a, *(void **) b); +} + void list_init(struct list *l, dtor_cb_t dtor) { pthread_mutex_init(&l->lock, NULL); @@ -80,24 +99,12 @@ void list_remove(struct list *l, void *p) void * list_lookup(struct list *l, const char *name) { - int cmp_helper(const void *a, const void *b) { - const struct { - char *name; - } *obj = a; - - return strcmp(obj->name, b); - } - - return list_search(l, cmp_helper, (void *) name); + return list_search(l, cmp_lookup, (void *) name); } int list_contains(struct list *l, void *p) -{ - int cmp_helper(const void *a, const void *b) { - return a == b ? 0 : 1; - } - - return list_count(l, cmp_helper, p); +{ + return list_count(l, cmp_contains, p); } int list_count(struct list *l, cmp_cb_t cmp, void *ctx) @@ -118,9 +125,10 @@ int list_count(struct list *l, cmp_cb_t cmp, void *ctx) void * list_search(struct list *l, cmp_cb_t cmp, void *ctx) { + void *e; + pthread_mutex_lock(&l->lock); - void *e; list_foreach(e, l) { if (!cmp(e, ctx)) goto out; @@ -135,13 +143,9 @@ out: pthread_mutex_unlock(&l->lock); void list_sort(struct list *l, cmp_cb_t cmp) { - int cmp_helper(const void *a, const void *b) { - return cmp(*(void **) a, *(void **) b); - } - pthread_mutex_lock(&l->lock); - qsort(l->array, l->length, sizeof(void *), cmp_helper); + qsort_r(l->array, l->length, sizeof(void *), cmp_sort, (void *) cmp); pthread_mutex_unlock(&l->lock); } \ No newline at end of file diff --git a/lib/path.c b/lib/path.c index f2166eae7..d2f2b5928 100644 --- a/lib/path.c +++ b/lib/path.c @@ -139,9 +139,7 @@ int path_start(struct path *p) path_name(p), p->poolsize, p->msgsize, list_length(&p->hooks), p->rate); /* We sort the hooks according to their priority before starting the path */ - list_sort(&p->hooks, ({int cmp(const void *a, const void *b) { - return ((struct hook *) a)->priority - ((struct hook *) b)->priority; - }; &cmp; })); + list_sort(&p->hooks, hooks_sort_priority); if (path_run_hook(p, HOOK_PATH_START)) return -1; @@ -231,3 +229,7 @@ void path_destroy(struct path *p) free(p->pool); free(p); } + +int path_uses_node(struct path *p, struct node *n) { + return (p->in == n) || list_contains(&p->destinations, n) ? 0 : 1; +} diff --git a/src/server.c b/src/server.c index 15c0a6f89..e1b0ee5a1 100644 --- a/src/server.c +++ b/src/server.c @@ -167,11 +167,7 @@ int main(int argc, char *argv[]) info("Starting nodes"); list_foreach(struct node *n, &nodes) { INDENT - int used_by_path(struct path *p, struct node *n) { - return (p->in == n) || list_contains(&p->destinations, n) ? 0 : 1; - } - - int refs = list_count(&paths, (cmp_cb_t) used_by_path, n); + int refs = list_count(&paths, (cmp_cb_t) path_uses_node, n); if (refs > 0) node_start(n); else