public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
From: "Fabian Groffen" <grobian@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] proj/portage-utils:master commit in: libq/, /
Date: Tue,  7 Feb 2023 08:10:11 +0000 (UTC)	[thread overview]
Message-ID: <1675757293.e2ebb44db31d4e0e9bfc0a9974d36eff63c8b2b1.grobian@gentoo> (raw)

commit:     e2ebb44db31d4e0e9bfc0a9974d36eff63c8b2b1
Author:     Fabian Groffen <grobian <AT> gentoo <DOT> org>
AuthorDate: Tue Feb  7 08:08:13 2023 +0000
Commit:     Fabian Groffen <grobian <AT> gentoo <DOT> org>
CommitDate: Tue Feb  7 08:08:13 2023 +0000
URL:        https://gitweb.gentoo.org/proj/portage-utils.git/commit/?id=e2ebb44d

set: ensure NULL is empty behaviour is retained throughout

Not all set functions respected NULL is empty behaviour, changed
add_set_value signature to return a set instead so it can conform.

Bug: https://bugs.gentoo.org/893424
Signed-off-by: Fabian Groffen <grobian <AT> gentoo.org>

 libq/set.c  | 47 +++++++++++++++++++++++++++++++++++++++--------
 libq/set.h  |  4 ++--
 libq/tree.c |  5 +++--
 main.c      |  6 ++++--
 qkeyword.c  |  6 +++---
 qlop.c      | 12 ++++++++----
 6 files changed, 59 insertions(+), 21 deletions(-)

diff --git a/libq/set.c b/libq/set.c
index 6c9fae0..fa485c7 100644
--- a/libq/set.c
+++ b/libq/set.c
@@ -1,5 +1,5 @@
 /*
- * Copyright 2005-2019 Gentoo Foundation
+ * Copyright 2005-2023 Gentoo Foundation
  * Distributed under the terms of the GNU General Public License v2
  *
  * Copyright 2005-2010 Ned Ludd        - <solar@gentoo.org>
@@ -112,17 +112,22 @@ add_set_unique(const char *name, set *q, bool *unique)
 
 /* add ptr to set with name as key, return existing value when key
  * already exists or NULL otherwise */
-void *
-add_set_value(const char *name, void *ptr, set *q)
+set *
+add_set_value(const char *name, void *ptr, void **prevptr, set *q)
 {
 	unsigned int hash;
 	int pos;
 	set_elem *ll;
 	set_elem *w;
 
+	if (q == NULL)
+		q = create_set();
+
 	hash = fnv1a32(name);
 	pos = hash % _SET_HASH_SIZE;
 
+	if (prevptr != NULL)
+		*prevptr = NULL;
 	if (q->buckets[pos] == NULL) {
 		q->buckets[pos] = ll = xmalloc(sizeof(*ll));
 		ll->next = NULL;
@@ -132,8 +137,11 @@ add_set_value(const char *name, void *ptr, set *q)
 	} else {
 		ll = NULL;
 		for (w = q->buckets[pos]; w != NULL; ll = w, w = w->next) {
-			if (w->hash == hash && strcmp(w->name, name) == 0)
-				return w->val;
+			if (w->hash == hash && strcmp(w->name, name) == 0) {
+				if (prevptr != NULL)
+					*prevptr = w->val;
+				return q;
+			}
 		}
 		if (w == NULL) {
 			ll = ll->next = xmalloc(sizeof(*ll));
@@ -145,7 +153,7 @@ add_set_value(const char *name, void *ptr, set *q)
 	}
 
 	q->len++;
-	return NULL;
+	return q;
 }
 
 /* returns whether name is in set, and if so, the set-internal key
@@ -158,6 +166,9 @@ contains_set(const char *name, set *q)
 	set_elem *w;
 	const char *found;
 
+	if (q == NULL)
+		return NULL;
+
 	hash = fnv1a32(name);
 	pos = hash % _SET_HASH_SIZE;
 
@@ -183,6 +194,9 @@ get_set(const char *name, set *q)
 	int pos;
 	set_elem *w;
 
+	if (q == NULL)
+		return NULL;
+
 	hash = fnv1a32(name);
 	pos = hash % _SET_HASH_SIZE;
 
@@ -211,6 +225,12 @@ del_set(const char *s, set *q, bool *removed)
 	void *ret;
 	bool rmd;
 
+	if (q == NULL) {
+		if (removed != NULL)
+			*removed = false;
+		return NULL;
+	}
+
 	hash = fnv1a32(s);
 	pos = hash % _SET_HASH_SIZE;
 
@@ -252,8 +272,8 @@ list_set(set *q, char ***l)
 	set_elem *w;
 	char **ret;
 
-	ret = *l = xmalloc(sizeof(char *) * (q->len + 1));
-	for (i = 0; i < _SET_HASH_SIZE; i++) {
+	ret = *l = xmalloc(sizeof(char *) * (cnt_set(q) + 1));
+	for (i = 0; q != NULL && i < _SET_HASH_SIZE; i++) {
 		for (w = q->buckets[i]; w != NULL; w = w->next) {
 			*ret = w->name;
 			ret++;
@@ -292,6 +312,11 @@ values_set(set *q, array_t *ret)
 	array_t blank = array_init_decl;
 
 	*ret = blank;
+
+	/* allow using empty set */
+	if (q == NULL)
+		return 0;
+
 	for (i = 0; i < _SET_HASH_SIZE; i++) {
 		for (w = q->buckets[i]; w != NULL; w = w->next)
 			xarraypush_ptr(ret, w->val);
@@ -314,6 +339,9 @@ clear_set(set *q)
 	set_elem *w;
 	set_elem *e;
 
+	if (q == NULL)
+		return;
+
 	for (i = 0; i < _SET_HASH_SIZE; i++) {
 		for (w = q->buckets[i]; w != NULL; w = e) {
 			e = w->next;
@@ -329,6 +357,9 @@ clear_set(set *q)
 void
 free_set(set *q)
 {
+	if (q == NULL)
+		return;
+
 	clear_set(q);
 	free(q);
 }

diff --git a/libq/set.h b/libq/set.h
index 5d53f95..219602e 100644
--- a/libq/set.h
+++ b/libq/set.h
@@ -1,5 +1,5 @@
 /*
- * Copyright 2005-2019 Gentoo Foundation
+ * Copyright 2005-2023 Gentoo Foundation
  * Distributed under the terms of the GNU General Public License v2
  */
 
@@ -30,7 +30,7 @@ struct set_t {
 set *create_set(void);
 set *add_set(const char *name, set *q);
 set *add_set_unique(const char *name, set *q, bool *unique);
-void *add_set_value(const char *name, void *ptr, set *q);
+set *add_set_value(const char *name, void *ptr, void **prevptr, set *q);
 const char *contains_set(const char *name, set *q);
 void *get_set(const char *name, set *q);
 void *del_set(const char *s, set *q, bool *removed);

diff --git a/libq/tree.c b/libq/tree.c
index 76190ed..a05a86e 100644
--- a/libq/tree.c
+++ b/libq/tree.c
@@ -297,7 +297,8 @@ tree_open_cat(tree_ctx *ctx, const char *name)
 	cat_ctx->pkg_cnt = 0;
 
 	if (ctx->cache.categories != NULL) {
-		add_set_value(name, cat_ctx, ctx->cache.categories);
+		ctx->cache.categories =
+			add_set_value(name, cat_ctx, NULL, ctx->cache.categories);
 		/* ensure name doesn't expire after this instantiation is closed */
 		cat_ctx->name = contains_set(name, ctx->cache.categories);
 	}
@@ -1708,7 +1709,7 @@ tree_match_atom_cache_populate_cb(tree_pkg_ctx *ctx, void *priv)
 	cat_ctx = get_set(atom->CATEGORY, cache);
 	if (cat_ctx == NULL) {
 		cat_ctx = tree_open_cat(tctx, ".");
-		add_set_value(atom->CATEGORY, cat_ctx, cache);
+		cache = add_set_value(atom->CATEGORY, cat_ctx, NULL, cache);
 		/* get a pointer from the set */
 		cat_ctx->name = contains_set(atom->CATEGORY, cache);
 	}

diff --git a/main.c b/main.c
index 347a50b..c5b27fe 100644
--- a/main.c
+++ b/main.c
@@ -1,5 +1,5 @@
 /*
- * Copyright 2005-2022 Gentoo Foundation
+ * Copyright 2005-2023 Gentoo Foundation
  * Distributed under the terms of the GNU General Public License v2
  *
  * Copyright 2005-2008 Ned Ludd        - <solar@gentoo.org>
@@ -581,10 +581,12 @@ read_portage_file(const char *file, enum portage_file_type type, void *data)
 				if ((p = del_set(buf + 1, masks, NULL)) != NULL)
 					free(p);
 			} else {
+				void *e;
 				snprintf(npath, sizeof(npath), "%s:%zu:%zu-%zu",
 						file, line, cbeg, cend);
 				p = xstrdup(npath);
-				if (add_set_value(buf, p, masks) != NULL)
+				masks = add_set_value(buf, p, &e, masks);
+				if (e != NULL)
 					free(p);
 			}
 		}

diff --git a/qkeyword.c b/qkeyword.c
index 5bc1010..4899d47 100644
--- a/qkeyword.c
+++ b/qkeyword.c
@@ -1,5 +1,5 @@
 /*
- * Copyright 2005-2022 Gentoo Foundation
+ * Copyright 2005-2023 Gentoo Foundation
  * Distributed under the terms of the GNU General Public License v2
  *
  * Copyright 2006      Thomas A. Cort - <tcort@gentoo.org>
@@ -913,8 +913,8 @@ int qkeyword_main(int argc, char **argv)
 				continue;
 			bucket = xzalloc(sizeof(array_t));
 			xarraypush_ptr(bucket, atom);
-			ebuck = add_set_value(atom_format("%[CAT]%[PN]", atom),
-					bucket, pmasks);
+			pmasks = add_set_value(atom_format("%[CAT]%[PN]", atom),
+								   bucket, (void **)&ebuck, pmasks);
 			if (ebuck != NULL) {
 				xarraypush_ptr(ebuck, atom);
 				xarrayfree_int(bucket);

diff --git a/qlop.c b/qlop.c
index 16bf69f..3e6db53 100644
--- a/qlop.c
+++ b/qlop.c
@@ -1,5 +1,5 @@
 /*
- * Copyright 2005-2022 Gentoo Foundation
+ * Copyright 2005-2023 Gentoo Foundation
  * Distributed under the terms of the GNU General Public License v2
  *
  * Copyright 2005-2010 Ned Ludd        - <solar@gentoo.org>
@@ -523,7 +523,7 @@ static int do_emerge_log(
 					last_merge = tstart_emerge;
 				}
 
-				atomw = add_set_value(afmt, atom, atomset);
+				atomset = add_set_value(afmt, atom, (void **)&atomw, atomset);
 				if (atomw != NULL)
 					atom_implode(atom);
 			}
@@ -807,7 +807,9 @@ static int do_emerge_log(
 									pkgw->atom->CATEGORY, pkgw->atom->PN);
 						}
 
-						pkg = add_set_value(afmt, pkgw, merge_averages);
+						merge_averages =
+							add_set_value(afmt, pkgw,
+										  (void **)&pkg, merge_averages);
 						if (pkg != NULL) {
 							pkg->cnt++;
 							pkg->time += elapsed;
@@ -952,7 +954,9 @@ static int do_emerge_log(
 									pkgw->atom->CATEGORY, pkgw->atom->PN);
 						}
 
-						pkg = add_set_value(afmt, pkgw, unmerge_averages);
+						unmerge_averages =
+							add_set_value(afmt, pkgw,
+										  (void **)&pkg, unmerge_averages);
 						if (pkg != NULL) {
 							pkg->cnt++;
 							pkg->time += elapsed;


             reply	other threads:[~2023-02-07  8:10 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-07  8:10 Fabian Groffen [this message]
  -- strict thread matches above, loose matches on Subject: below --
2024-06-27 19:19 [gentoo-commits] proj/portage-utils:master commit in: libq/, / Fabian Groffen
2024-03-29 10:57 Fabian Groffen
2024-01-02  7:57 Fabian Groffen
2023-02-07  8:25 Fabian Groffen
2021-08-16 13:23 Fabian Groffen
2020-02-21  8:18 Fabian Groffen
2020-01-05 13:28 Fabian Groffen
2020-01-02 11:19 Fabian Groffen
2020-01-01 19:52 Fabian Groffen
2019-12-31  9:05 Fabian Groffen
2019-12-30 17:24 Fabian Groffen
2019-12-29 13:26 Fabian Groffen
2019-12-27 16:57 Fabian Groffen
2019-07-13 10:04 Fabian Groffen
2019-06-19 10:44 Fabian Groffen
2019-06-05  9:15 Fabian Groffen
2019-05-09 20:19 Fabian Groffen
2019-05-05 18:13 Fabian Groffen
2019-04-28 15:20 Fabian Groffen
2019-03-27 20:18 Fabian Groffen
2019-03-27 10:55 Fabian Groffen
2019-03-22  9:57 Fabian Groffen
2019-03-19 20:32 Fabian Groffen
2019-03-19 20:32 Fabian Groffen
2019-03-09 18:58 Fabian Groffen
2018-03-23 11:56 Fabian Groffen
2016-12-29  2:25 Mike Frysinger
2016-11-26 23:17 Mike Frysinger
2015-11-28  2:44 Mike Frysinger
2015-02-24  1:26 Mike Frysinger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1675757293.e2ebb44db31d4e0e9bfc0a9974d36eff63c8b2b1.grobian@gentoo \
    --to=grobian@gentoo.org \
    --cc=gentoo-commits@lists.gentoo.org \
    --cc=gentoo-dev@lists.gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox