summaryrefslogtreecommitdiffstats
path: root/src/basic/hashmap.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-12 03:50:42 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-12 03:50:42 +0000
commit78e9bb837c258ac0ec7712b3d612cc2f407e731e (patch)
treef515d16b6efd858a9aeb5b0ef5d6f90bf288283d /src/basic/hashmap.c
parentAdding debian version 255.5-1. (diff)
downloadsystemd-78e9bb837c258ac0ec7712b3d612cc2f407e731e.tar.xz
systemd-78e9bb837c258ac0ec7712b3d612cc2f407e731e.zip
Merging upstream version 256.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/basic/hashmap.c')
-rw-r--r--src/basic/hashmap.c53
1 files changed, 45 insertions, 8 deletions
diff --git a/src/basic/hashmap.c b/src/basic/hashmap.c
index 894760c..a9fd762 100644
--- a/src/basic/hashmap.c
+++ b/src/basic/hashmap.c
@@ -2120,24 +2120,27 @@ static int hashmap_entry_compare(
return compare((*a)->key, (*b)->key);
}
-int _hashmap_dump_sorted(HashmapBase *h, void ***ret, size_t *ret_n) {
- _cleanup_free_ struct hashmap_base_entry **entries = NULL;
+static int _hashmap_dump_entries_sorted(
+ HashmapBase *h,
+ void ***ret,
+ size_t *ret_n) {
+ _cleanup_free_ void **entries = NULL;
Iterator iter;
unsigned idx;
size_t n = 0;
assert(ret);
+ assert(ret_n);
if (_hashmap_size(h) == 0) {
*ret = NULL;
- if (ret_n)
- *ret_n = 0;
+ *ret_n = 0;
return 0;
}
/* We append one more element than needed so that the resulting array can be used as a strv. We
* don't count this entry in the returned size. */
- entries = new(struct hashmap_base_entry*, _hashmap_size(h) + 1);
+ entries = new(void*, _hashmap_size(h) + 1);
if (!entries)
return -ENOMEM;
@@ -2147,13 +2150,47 @@ int _hashmap_dump_sorted(HashmapBase *h, void ***ret, size_t *ret_n) {
assert(n == _hashmap_size(h));
entries[n] = NULL;
- typesafe_qsort_r(entries, n, hashmap_entry_compare, h->hash_ops->compare);
+ typesafe_qsort_r((struct hashmap_base_entry**) entries, n,
+ hashmap_entry_compare, h->hash_ops->compare);
+
+ *ret = TAKE_PTR(entries);
+ *ret_n = n;
+ return 0;
+}
+
+int _hashmap_dump_keys_sorted(HashmapBase *h, void ***ret, size_t *ret_n) {
+ _cleanup_free_ void **entries = NULL;
+ size_t n;
+ int r;
+
+ r = _hashmap_dump_entries_sorted(h, &entries, &n);
+ if (r < 0)
+ return r;
+
+ /* Reuse the array. */
+ FOREACH_ARRAY(e, entries, n)
+ *e = (void*) (*(struct hashmap_base_entry**) e)->key;
+
+ *ret = TAKE_PTR(entries);
+ if (ret_n)
+ *ret_n = n;
+ return 0;
+}
+
+int _hashmap_dump_sorted(HashmapBase *h, void ***ret, size_t *ret_n) {
+ _cleanup_free_ void **entries = NULL;
+ size_t n;
+ int r;
+
+ r = _hashmap_dump_entries_sorted(h, &entries, &n);
+ if (r < 0)
+ return r;
/* Reuse the array. */
FOREACH_ARRAY(e, entries, n)
- *e = entry_value(h, *e);
+ *e = entry_value(h, *(struct hashmap_base_entry**) e);
- *ret = (void**) TAKE_PTR(entries);
+ *ret = TAKE_PTR(entries);
if (ret_n)
*ret_n = n;
return 0;