Code

utils llist: Added lookup callback type to be used for llist_search.
authorSebastian Harl <sh@tokkee.org>
Mon, 30 Sep 2013 16:16:26 +0000 (18:16 +0200)
committerSebastian Harl <sh@tokkee.org>
Mon, 30 Sep 2013 16:16:26 +0000 (18:16 +0200)
Lookups should be more flexible than comparing two objects.

src/include/utils/llist.h
src/utils/llist.c

index 4bf9d605ad74e8d46e28c262a8c92470acf6dc47..bd44d44c5053870b726b575ee7452a909e03d149 100644 (file)
@@ -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:
index dfc8fb99d18ecfeb6e38cc0a7339426854691704..613e53cc8a601701318560cc34a5947f29e365a5 100644 (file)
@@ -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;