From: Sebastian Harl Date: Mon, 30 Sep 2013 16:16:26 +0000 (+0200) Subject: utils llist: Added lookup callback type to be used for llist_search. X-Git-Tag: sysdb-0.1.0~336^2~68 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=94500efc2f62a72f54bc1b21e458f311bdbffb42;p=sysdb.git utils llist: Added lookup callback type to be used for llist_search. Lookups should be more flexible than comparing two objects. --- diff --git a/src/include/utils/llist.h b/src/include/utils/llist.h index 4bf9d60..bd44d44 100644 --- a/src/include/utils/llist.h +++ b/src/include/utils/llist.h @@ -41,6 +41,7 @@ struct sdb_llist_iter; typedef struct sdb_llist_iter sdb_llist_iter_t; typedef int (*sdb_llist_cmp_cb)(const sdb_object_t *, const sdb_object_t *); +typedef int (*sdb_llist_lookup_cb)(const sdb_object_t *, void *user_data); /* * sdb_llist_create, sdb_llist_destroy: @@ -130,7 +131,7 @@ sdb_llist_insert_sorted(sdb_llist_t *list, */ sdb_object_t * sdb_llist_search(sdb_llist_t *list, - const sdb_object_t *key, sdb_llist_cmp_cb); + sdb_llist_lookup_cb lookup, void *user_data); /* * sdb_llist_search_by_name: diff --git a/src/utils/llist.c b/src/utils/llist.c index dfc8fb9..613e53c 100644 --- a/src/utils/llist.c +++ b/src/utils/llist.c @@ -292,21 +292,20 @@ sdb_llist_insert_sorted(sdb_llist_t *list, sdb_object_t * sdb_llist_search(sdb_llist_t *list, - const sdb_object_t *key, sdb_llist_cmp_cb compare) + sdb_llist_lookup_cb lookup, void *user_data) { sdb_llist_elem_t *elem; - if ((! list) || (! compare)) + if ((! list) || (! lookup)) return NULL; pthread_rwlock_rdlock(&list->lock); for (elem = list->head; elem; elem = elem->next) - if (! compare(elem->obj, key)) + if (! lookup(elem->obj, user_data)) break; pthread_rwlock_unlock(&list->lock); - if (elem) return elem->obj; return NULL;