commit:     deefb1d324ea8e7fc125c0b8b8271da3e3f3f0b3
Author:     Fabian Groffen <grobian <AT> gentoo <DOT> org>
AuthorDate: Sun Dec 15 14:16:08 2019 +0000
Commit:     Fabian Groffen <grobian <AT> gentoo <DOT> org>
CommitDate: Sun Dec 15 14:16:08 2019 +0000
URL:        https://gitweb.gentoo.org/proj/portage-utils.git/commit/?id=deefb1d3

libq/set: have del_set return the old value when set

Allow to easily free a set entry that has a value.

Signed-off-by: Fabian Groffen <grobian <AT> gentoo.org>

 libq/set.c | 22 ++++++++++++++++------
 libq/set.h |  2 +-
 qmerge.c   |  2 +-
 3 files changed, 18 insertions(+), 8 deletions(-)

diff --git a/libq/set.c b/libq/set.c
index e0ea396..3b56f81 100644
--- a/libq/set.c
+++ b/libq/set.c
@@ -195,19 +195,26 @@ get_set(const char *name, set *q)
        return NULL;
 }
 
-/* remove elem from a set. matches ->name and frees name,item */
-set *
+/* remove elem from a set. matches ->name and frees name,item, returns
+ * val if removed, NULL otherwise
+ * note that when val isn't set, NULL is returned, so the caller should
+ * use the removed argument to determine if something was removed from
+ * the set. */
+void *
 del_set(const char *s, set *q, bool *removed)
 {
        unsigned int hash;
        int pos;
        elem *ll;
        elem *w;
+       void *ret;
+       bool rmd;
 
        hash = fnv1a32(s);
        pos = hash % _SET_HASH_SIZE;
 
-       *removed = false;
+       ret = NULL;
+       rmd = false;
        if (q->buckets[pos] != NULL) {
                ll = NULL;
                for (w = q->buckets[pos]; w != NULL; ll = w, w = w->next) {
@@ -217,17 +224,20 @@ del_set(const char *s, set *q, bool *removed)
                                } else {
                                        ll->next = w->next;
                                }
+                               ret = w->val;
                                free(w->name);
                                free(w);
-                               *removed = true;
+                               rmd = true;
                                break;
                        }
                }
        }
 
-       if (*removed)
+       if (rmd)
                q->len--;
-       return q;
+       if (removed != NULL)
+               *removed = rmd;
+       return ret;
 }
 
 /* return the contents of a set as an array of strings

diff --git a/libq/set.h b/libq/set.h
index 638cc15..8546a90 100644
--- a/libq/set.h
+++ b/libq/set.h
@@ -34,7 +34,7 @@ set *add_set_unique(const char *name, set *q, bool *unique);
 void *add_set_value(const char *name, void *ptr, set *q);
 bool contains_set(const char *name, set *q);
 void *get_set(const char *name, set *q);
-set *del_set(const char *s, set *q, bool *removed);
+void *del_set(const char *s, set *q, bool *removed);
 size_t list_set(set *q, char ***l);
 size_t values_set(set *q, array_t *ret);
 size_t cnt_set(set *q);

diff --git a/qmerge.c b/qmerge.c
index 5b7a298..a2d06ab 100644
--- a/qmerge.c
+++ b/qmerge.c
@@ -2341,7 +2341,7 @@ qmerge_add_set_system(void *data, char *buf)
                q = add_set(s + 1, q);
        else if (s[0] == '-' && s[1] == '*') {
                bool ok;
-               q = del_set(s + 2, q, &ok);
+               (void)del_set(s + 2, q, &ok);
        }
 
        return q;

Reply via email to