Code

utils llist: Added sdb_llist_remove().
authorSebastian Harl <sh@tokkee.org>
Mon, 30 Sep 2013 16:24:33 +0000 (18:24 +0200)
committerSebastian Harl <sh@tokkee.org>
Mon, 30 Sep 2013 16:24:33 +0000 (18:24 +0200)
src/include/utils/llist.h
src/utils/llist.c

index 298c6ceb8779e9238ade411ca1a3ffb622a41545..af8285f6a3367fdc026f81bf0387243ff143c20f 100644 (file)
@@ -146,6 +146,20 @@ sdb_llist_search(sdb_llist_t *list,
 sdb_object_t *
 sdb_llist_search_by_name(sdb_llist_t *list, const char *key);
 
+/*
+ * sdb_llist_remove:
+ * Removes and returns the first matchin element of the list. The ref-count of
+ * the item will not be changed, that is, if the element will not be used any
+ * further, it should be de-referenced by the caller.
+ *
+ * Returns:
+ *  - a pointer to the first matching object
+ *  - NULL else
+ */
+sdb_object_t *
+sdb_llist_remove(sdb_llist_t *list,
+               sdb_llist_lookup_cb lookup, void *user_data);
+
 /*
  * sdb_llist_shift:
  * Removes and returns the first element of the list. The ref-count of the
index 613e53cc8a601701318560cc34a5947f29e365a5..3f54786074941d368bc88da4ca475b8cd2559e7c 100644 (file)
@@ -115,6 +115,21 @@ sdb_llist_insert_after(sdb_llist_t *list, sdb_llist_elem_t *elem,
        return 0;
 } /* sdb_llist_insert_after */
 
+static sdb_llist_elem_t *
+llist_search(sdb_llist_t *list,
+               sdb_llist_lookup_cb lookup, void *user_data)
+{
+       sdb_llist_elem_t *elem;
+
+       if ((! list) || (! lookup))
+               return NULL;
+
+       for (elem = list->head; elem; elem = elem->next)
+               if (! lookup(elem->obj, user_data))
+                       break;
+       return elem;
+} /* llist_search */
+
 static sdb_object_t *
 sdb_llist_remove_elem(sdb_llist_t *list, sdb_llist_elem_t *elem)
 {
@@ -296,16 +311,10 @@ sdb_llist_search(sdb_llist_t *list,
 {
        sdb_llist_elem_t *elem;
 
-       if ((! list) || (! lookup))
-               return NULL;
-
        pthread_rwlock_rdlock(&list->lock);
-
-       for (elem = list->head; elem; elem = elem->next)
-               if (! lookup(elem->obj, user_data))
-                       break;
-
+       elem = llist_search(list, lookup, user_data);
        pthread_rwlock_unlock(&list->lock);
+
        if (elem)
                return elem->obj;
        return NULL;
@@ -332,6 +341,22 @@ sdb_llist_search_by_name(sdb_llist_t *list, const char *key)
        return NULL;
 } /* sdb_llist_search_by_name */
 
+sdb_object_t *
+sdb_llist_remove(sdb_llist_t *list,
+               sdb_llist_lookup_cb lookup, void *user_data)
+{
+       sdb_llist_elem_t *elem;
+       sdb_object_t *obj = NULL;
+
+       pthread_rwlock_wrlock(&list->lock);
+       elem = llist_search(list, lookup, user_data);
+       if (elem)
+               obj = sdb_llist_remove_elem(list, elem);
+       pthread_rwlock_unlock(&list->lock);
+
+       return obj;
+} /* sdb_llist_remove */
+
 sdb_object_t *
 sdb_llist_shift(sdb_llist_t *list)
 {