richardphibel / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
be0c12
From 3bee193141bdf3106732a2c925ffaf5ce48f0ecb Mon Sep 17 00:00:00 2001
be0c12
From: Yu Watanabe <watanabe.yu+github@gmail.com>
be0c12
Date: Tue, 27 Nov 2018 22:25:40 +0900
be0c12
Subject: [PATCH] hash-func: add destructors for key and value
be0c12
be0c12
If they are set, then they are called in hashmap_clear() or
be0c12
hashmap_free().
be0c12
be0c12
(cherry picked from commit 59a5cda7b904cd7ef9853bda15b498bbc0577524)
be0c12
be0c12
Resolves: #2037807
be0c12
---
be0c12
 src/basic/hash-funcs.h  | 54 ++++++++++++++++++++++++++---
be0c12
 src/basic/hashmap.c     | 76 +++++++++++------------------------------
be0c12
 src/basic/hashmap.h     | 50 ++++++++++++++++++---------
be0c12
 src/basic/ordered-set.h |  6 ++--
be0c12
 src/basic/set.h         | 10 +++---
be0c12
 5 files changed, 109 insertions(+), 87 deletions(-)
be0c12
be0c12
diff --git a/src/basic/hash-funcs.h b/src/basic/hash-funcs.h
be0c12
index 2ff687e5f9..2d3125d0f9 100644
be0c12
--- a/src/basic/hash-funcs.h
be0c12
+++ b/src/basic/hash-funcs.h
be0c12
@@ -1,7 +1,7 @@
be0c12
 /* SPDX-License-Identifier: LGPL-2.1+ */
be0c12
 #pragma once
be0c12
 
be0c12
-
be0c12
+#include "alloc-util.h"
be0c12
 #include "macro.h"
be0c12
 #include "siphash24.h"
be0c12
 
be0c12
@@ -11,21 +11,67 @@ typedef int (*compare_func_t)(const void *a, const void *b);
be0c12
 struct hash_ops {
be0c12
         hash_func_t hash;
be0c12
         compare_func_t compare;
be0c12
+        free_func_t free_key;
be0c12
+        free_func_t free_value;
be0c12
 };
be0c12
 
be0c12
-#define _DEFINE_HASH_OPS(uq, name, type, hash_func, compare_func, scope) \
be0c12
+#define _DEFINE_HASH_OPS(uq, name, type, hash_func, compare_func, free_key_func, free_value_func, scope) \
be0c12
         _unused_ static void (* UNIQ_T(static_hash_wrapper, uq))(const type *, struct siphash *) = hash_func; \
be0c12
         _unused_ static int (* UNIQ_T(static_compare_wrapper, uq))(const type *, const type *) = compare_func; \
be0c12
         scope const struct hash_ops name = {                            \
be0c12
                 .hash = (hash_func_t) hash_func,                        \
be0c12
                 .compare = (compare_func_t) compare_func,               \
be0c12
+                .free_key = free_key_func,                              \
be0c12
+                .free_value = free_value_func,                          \
be0c12
+        }
be0c12
+
be0c12
+#define _DEFINE_FREE_FUNC(uq, type, wrapper_name, func)                 \
be0c12
+        /* Type-safe free function */                                   \
be0c12
+        static void UNIQ_T(wrapper_name, uq)(void *a) {                 \
be0c12
+                type *_a = a;                                           \
be0c12
+                func(_a);                                               \
be0c12
         }
be0c12
 
be0c12
+#define _DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(uq, name, type, hash_func, compare_func, free_func, scope) \
be0c12
+        _DEFINE_FREE_FUNC(uq, type, static_free_wrapper, free_func);    \
be0c12
+        _DEFINE_HASH_OPS(uq, name, type, hash_func, compare_func,       \
be0c12
+                         UNIQ_T(static_free_wrapper, uq), NULL, scope)
be0c12
+
be0c12
+#define _DEFINE_HASH_OPS_WITH_VALUE_DESTRUCTOR(uq, name, type, hash_func, compare_func, type_value, free_func, scope) \
be0c12
+        _DEFINE_FREE_FUNC(uq, type_value, static_free_wrapper, free_func); \
be0c12
+        _DEFINE_HASH_OPS(uq, name, type, hash_func, compare_func,       \
be0c12
+                         NULL, UNIQ_T(static_free_wrapper, uq), scope)
be0c12
+
be0c12
+#define _DEFINE_HASH_OPS_FULL(uq, name, type, hash_func, compare_func, free_key_func, type_value, free_value_func, scope) \
be0c12
+        _DEFINE_FREE_FUNC(uq, type, static_free_key_wrapper, free_key_func); \
be0c12
+        _DEFINE_FREE_FUNC(uq, type_value, static_free_value_wrapper, free_value_func); \
be0c12
+        _DEFINE_HASH_OPS(uq, name, type, hash_func, compare_func,       \
be0c12
+                         UNIQ_T(static_free_key_wrapper, uq),           \
be0c12
+                         UNIQ_T(static_free_value_wrapper, uq), scope)
be0c12
+
be0c12
 #define DEFINE_HASH_OPS(name, type, hash_func, compare_func)            \
be0c12
-        _DEFINE_HASH_OPS(UNIQ, name, type, hash_func, compare_func,)
be0c12
+        _DEFINE_HASH_OPS(UNIQ, name, type, hash_func, compare_func, NULL, NULL,)
be0c12
 
be0c12
 #define DEFINE_PRIVATE_HASH_OPS(name, type, hash_func, compare_func)    \
be0c12
-        _DEFINE_HASH_OPS(UNIQ, name, type, hash_func, compare_func, static)
be0c12
+        _DEFINE_HASH_OPS(UNIQ, name, type, hash_func, compare_func, NULL, NULL, static)
be0c12
+
be0c12
+#define DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(name, type, hash_func, compare_func, free_func) \
be0c12
+        _DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(UNIQ, name, type, hash_func, compare_func, free_func,)
be0c12
+
be0c12
+#define DEFINE_PRIVATE_HASH_OPS_WITH_KEY_DESTRUCTOR(name, type, hash_func, compare_func, free_func) \
be0c12
+        _DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(UNIQ, name, type, hash_func, compare_func, free_func, static)
be0c12
+
be0c12
+#define DEFINE_HASH_OPS_WITH_VALUE_DESTRUCTOR(name, type, hash_func, compare_func, value_type, free_func) \
be0c12
+        _DEFINE_HASH_OPS_WITH_VALUE_DESTRUCTOR(UNIQ, name, type, hash_func, compare_func, value_type, free_func,)
be0c12
+
be0c12
+#define DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(name, type, hash_func, compare_func, value_type, free_func) \
be0c12
+        _DEFINE_HASH_OPS_WITH_VALUE_DESTRUCTOR(UNIQ, name, type, hash_func, compare_func, value_type, free_func, static)
be0c12
+
be0c12
+#define DEFINE_HASH_OPS_FULL(name, type, hash_func, compare_func, free_key_func, value_type, free_value_func) \
be0c12
+        _DEFINE_HASH_OPS_FULL(UNIQ, name, type, hash_func, compare_func, free_key_func, value_type, free_value_func,)
be0c12
+
be0c12
+#define DEFINE_PRIVATE_HASH_OPS_FULL(name, type, hash_func, compare_func, free_key_func, value_type, free_value_func) \
be0c12
+        _DEFINE_HASH_OPS_FULL(UNIQ, name, type, hash_func, compare_func, free_key_func, value_type, free_value_func, static)
be0c12
 
be0c12
 void string_hash_func(const void *p, struct siphash *state);
be0c12
 int string_compare_func(const void *a, const void *b) _pure_;
be0c12
diff --git a/src/basic/hashmap.c b/src/basic/hashmap.c
be0c12
index 69a7d70b04..7c508086f0 100644
be0c12
--- a/src/basic/hashmap.c
be0c12
+++ b/src/basic/hashmap.c
be0c12
@@ -863,47 +863,38 @@ static void hashmap_free_no_clear(HashmapBase *h) {
be0c12
                 free(h);
be0c12
 }
be0c12
 
be0c12
-HashmapBase *internal_hashmap_free(HashmapBase *h) {
be0c12
-
be0c12
-        /* Free the hashmap, but nothing in it */
be0c12
-
be0c12
+HashmapBase *internal_hashmap_free(HashmapBase *h, free_func_t default_free_key, free_func_t default_free_value) {
be0c12
         if (h) {
be0c12
-                internal_hashmap_clear(h);
be0c12
+                internal_hashmap_clear(h, default_free_key, default_free_value);
be0c12
                 hashmap_free_no_clear(h);
be0c12
         }
be0c12
 
be0c12
         return NULL;
be0c12
 }
be0c12
 
be0c12
-HashmapBase *internal_hashmap_free_free(HashmapBase *h) {
be0c12
+void internal_hashmap_clear(HashmapBase *h, free_func_t default_free_key, free_func_t default_free_value) {
be0c12
+        free_func_t free_key, free_value;
be0c12
+        if (!h)
be0c12
+                return;
be0c12
 
be0c12
-        /* Free the hashmap and all data objects in it, but not the
be0c12
-         * keys */
be0c12
+        free_key = h->hash_ops->free_key ?: default_free_key;
be0c12
+        free_value = h->hash_ops->free_value ?: default_free_value;
be0c12
 
be0c12
-        if (h) {
be0c12
-                internal_hashmap_clear_free(h);
be0c12
-                hashmap_free_no_clear(h);
be0c12
-        }
be0c12
-
be0c12
-        return NULL;
be0c12
-}
be0c12
+        if (free_key || free_value) {
be0c12
+                unsigned idx;
be0c12
 
be0c12
-Hashmap *hashmap_free_free_free(Hashmap *h) {
be0c12
+                for (idx = skip_free_buckets(h, 0); idx != IDX_NIL;
be0c12
+                     idx = skip_free_buckets(h, idx + 1)) {
be0c12
+                        struct hashmap_base_entry *e = bucket_at(h, idx);
be0c12
 
be0c12
-        /* Free the hashmap and all data and key objects in it */
be0c12
+                        if (free_key)
be0c12
+                                free_key((void *) e->key);
be0c12
 
be0c12
-        if (h) {
be0c12
-                hashmap_clear_free_free(h);
be0c12
-                hashmap_free_no_clear(HASHMAP_BASE(h));
be0c12
+                        if (free_value)
be0c12
+                                free_value(entry_value(h, e));
be0c12
+                }
be0c12
         }
be0c12
 
be0c12
-        return NULL;
be0c12
-}
be0c12
-
be0c12
-void internal_hashmap_clear(HashmapBase *h) {
be0c12
-        if (!h)
be0c12
-                return;
be0c12
-
be0c12
         if (h->has_indirect) {
be0c12
                 free(h->indirect.storage);
be0c12
                 h->has_indirect = false;
be0c12
@@ -920,35 +911,6 @@ void internal_hashmap_clear(HashmapBase *h) {
be0c12
         base_set_dirty(h);
be0c12
 }
be0c12
 
be0c12
-void internal_hashmap_clear_free(HashmapBase *h) {
be0c12
-        unsigned idx;
be0c12
-
be0c12
-        if (!h)
be0c12
-                return;
be0c12
-
be0c12
-        for (idx = skip_free_buckets(h, 0); idx != IDX_NIL;
be0c12
-             idx = skip_free_buckets(h, idx + 1))
be0c12
-                free(entry_value(h, bucket_at(h, idx)));
be0c12
-
be0c12
-        internal_hashmap_clear(h);
be0c12
-}
be0c12
-
be0c12
-void hashmap_clear_free_free(Hashmap *h) {
be0c12
-        unsigned idx;
be0c12
-
be0c12
-        if (!h)
be0c12
-                return;
be0c12
-
be0c12
-        for (idx = skip_free_buckets(HASHMAP_BASE(h), 0); idx != IDX_NIL;
be0c12
-             idx = skip_free_buckets(HASHMAP_BASE(h), idx + 1)) {
be0c12
-                struct plain_hashmap_entry *e = plain_bucket_at(h, idx);
be0c12
-                free((void*)e->b.key);
be0c12
-                free(e->value);
be0c12
-        }
be0c12
-
be0c12
-        internal_hashmap_clear(HASHMAP_BASE(h));
be0c12
-}
be0c12
-
be0c12
 static int resize_buckets(HashmapBase *h, unsigned entries_add);
be0c12
 
be0c12
 /*
be0c12
@@ -1771,7 +1733,7 @@ HashmapBase *internal_hashmap_copy(HashmapBase *h) {
be0c12
         }
be0c12
 
be0c12
         if (r < 0) {
be0c12
-                internal_hashmap_free(copy);
be0c12
+                internal_hashmap_free(copy, false, false);
be0c12
                 return NULL;
be0c12
         }
be0c12
 
be0c12
diff --git a/src/basic/hashmap.h b/src/basic/hashmap.h
be0c12
index 5c70c102d7..9e4772b497 100644
be0c12
--- a/src/basic/hashmap.h
be0c12
+++ b/src/basic/hashmap.h
be0c12
@@ -23,6 +23,8 @@
be0c12
 
be0c12
 #define HASH_KEY_SIZE 16
be0c12
 
be0c12
+typedef void* (*hashmap_destroy_t)(void *p);
be0c12
+
be0c12
 /* The base type for all hashmap and set types. Many functions in the
be0c12
  * implementation take (HashmapBase*) parameters and are run-time polymorphic,
be0c12
  * though the API is not meant to be polymorphic (do not call functions
be0c12
@@ -88,25 +90,33 @@ OrderedHashmap *internal_ordered_hashmap_new(const struct hash_ops *hash_ops  HA
be0c12
 #define hashmap_new(ops) internal_hashmap_new(ops  HASHMAP_DEBUG_SRC_ARGS)
be0c12
 #define ordered_hashmap_new(ops) internal_ordered_hashmap_new(ops  HASHMAP_DEBUG_SRC_ARGS)
be0c12
 
be0c12
-HashmapBase *internal_hashmap_free(HashmapBase *h);
be0c12
+HashmapBase *internal_hashmap_free(HashmapBase *h, free_func_t default_free_key, free_func_t default_free_value);
be0c12
 static inline Hashmap *hashmap_free(Hashmap *h) {
be0c12
-        return (void*)internal_hashmap_free(HASHMAP_BASE(h));
be0c12
+        return (void*) internal_hashmap_free(HASHMAP_BASE(h), NULL, NULL);
be0c12
 }
be0c12
 static inline OrderedHashmap *ordered_hashmap_free(OrderedHashmap *h) {
be0c12
-        return (void*)internal_hashmap_free(HASHMAP_BASE(h));
be0c12
+        return (void*) internal_hashmap_free(HASHMAP_BASE(h), NULL, NULL);
be0c12
 }
be0c12
 
be0c12
-HashmapBase *internal_hashmap_free_free(HashmapBase *h);
be0c12
 static inline Hashmap *hashmap_free_free(Hashmap *h) {
be0c12
-        return (void*)internal_hashmap_free_free(HASHMAP_BASE(h));
be0c12
+        return (void*) internal_hashmap_free(HASHMAP_BASE(h), NULL, free);
be0c12
 }
be0c12
 static inline OrderedHashmap *ordered_hashmap_free_free(OrderedHashmap *h) {
be0c12
-        return (void*)internal_hashmap_free_free(HASHMAP_BASE(h));
be0c12
+        return (void*) internal_hashmap_free(HASHMAP_BASE(h), NULL, free);
be0c12
 }
be0c12
 
be0c12
-Hashmap *hashmap_free_free_free(Hashmap *h);
be0c12
+static inline Hashmap *hashmap_free_free_key(Hashmap *h) {
be0c12
+        return (void*) internal_hashmap_free(HASHMAP_BASE(h), free, NULL);
be0c12
+}
be0c12
+static inline OrderedHashmap *ordered_hashmap_free_free_key(OrderedHashmap *h) {
be0c12
+        return (void*) internal_hashmap_free(HASHMAP_BASE(h), free, NULL);
be0c12
+}
be0c12
+
be0c12
+static inline Hashmap *hashmap_free_free_free(Hashmap *h) {
be0c12
+        return (void*) internal_hashmap_free(HASHMAP_BASE(h), free, free);
be0c12
+}
be0c12
 static inline OrderedHashmap *ordered_hashmap_free_free_free(OrderedHashmap *h) {
be0c12
-        return (void*)hashmap_free_free_free(PLAIN_HASHMAP(h));
be0c12
+        return (void*) internal_hashmap_free(HASHMAP_BASE(h), free, free);
be0c12
 }
be0c12
 
be0c12
 IteratedCache *iterated_cache_free(IteratedCache *cache);
be0c12
@@ -259,25 +269,33 @@ static inline bool ordered_hashmap_iterate(OrderedHashmap *h, Iterator *i, void
be0c12
         return internal_hashmap_iterate(HASHMAP_BASE(h), i, value, key);
be0c12
 }
be0c12
 
be0c12
-void internal_hashmap_clear(HashmapBase *h);
be0c12
+void internal_hashmap_clear(HashmapBase *h, free_func_t default_free_key, free_func_t default_free_value);
be0c12
 static inline void hashmap_clear(Hashmap *h) {
be0c12
-        internal_hashmap_clear(HASHMAP_BASE(h));
be0c12
+        internal_hashmap_clear(HASHMAP_BASE(h), NULL, NULL);
be0c12
 }
be0c12
 static inline void ordered_hashmap_clear(OrderedHashmap *h) {
be0c12
-        internal_hashmap_clear(HASHMAP_BASE(h));
be0c12
+        internal_hashmap_clear(HASHMAP_BASE(h), NULL, NULL);
be0c12
 }
be0c12
 
be0c12
-void internal_hashmap_clear_free(HashmapBase *h);
be0c12
 static inline void hashmap_clear_free(Hashmap *h) {
be0c12
-        internal_hashmap_clear_free(HASHMAP_BASE(h));
be0c12
+        internal_hashmap_clear(HASHMAP_BASE(h), NULL, free);
be0c12
 }
be0c12
 static inline void ordered_hashmap_clear_free(OrderedHashmap *h) {
be0c12
-        internal_hashmap_clear_free(HASHMAP_BASE(h));
be0c12
+        internal_hashmap_clear(HASHMAP_BASE(h), NULL, free);
be0c12
 }
be0c12
 
be0c12
-void hashmap_clear_free_free(Hashmap *h);
be0c12
+static inline void hashmap_clear_free_key(Hashmap *h) {
be0c12
+        internal_hashmap_clear(HASHMAP_BASE(h), free, NULL);
be0c12
+}
be0c12
+static inline void ordered_hashmap_clear_free_key(OrderedHashmap *h) {
be0c12
+        internal_hashmap_clear(HASHMAP_BASE(h), free, NULL);
be0c12
+}
be0c12
+
be0c12
+static inline void hashmap_clear_free_free(Hashmap *h) {
be0c12
+        internal_hashmap_clear(HASHMAP_BASE(h), free, free);
be0c12
+}
be0c12
 static inline void ordered_hashmap_clear_free_free(OrderedHashmap *h) {
be0c12
-        hashmap_clear_free_free(PLAIN_HASHMAP(h));
be0c12
+        internal_hashmap_clear(HASHMAP_BASE(h), free, free);
be0c12
 }
be0c12
 
be0c12
 /*
be0c12
diff --git a/src/basic/ordered-set.h b/src/basic/ordered-set.h
be0c12
index e7c054d8e4..7cbb71819b 100644
be0c12
--- a/src/basic/ordered-set.h
be0c12
+++ b/src/basic/ordered-set.h
be0c12
@@ -21,13 +21,11 @@ static inline int ordered_set_ensure_allocated(OrderedSet **s, const struct hash
be0c12
 }
be0c12
 
be0c12
 static inline OrderedSet* ordered_set_free(OrderedSet *s) {
be0c12
-        ordered_hashmap_free((OrderedHashmap*) s);
be0c12
-        return NULL;
be0c12
+        return (OrderedSet*) ordered_hashmap_free((OrderedHashmap*) s);
be0c12
 }
be0c12
 
be0c12
 static inline OrderedSet* ordered_set_free_free(OrderedSet *s) {
be0c12
-        ordered_hashmap_free_free((OrderedHashmap*) s);
be0c12
-        return NULL;
be0c12
+        return (OrderedSet*) ordered_hashmap_free_free((OrderedHashmap*) s);
be0c12
 }
be0c12
 
be0c12
 static inline int ordered_set_put(OrderedSet *s, void *p) {
be0c12
diff --git a/src/basic/set.h b/src/basic/set.h
be0c12
index 664713810d..8e12670a6e 100644
be0c12
--- a/src/basic/set.h
be0c12
+++ b/src/basic/set.h
be0c12
@@ -9,13 +9,11 @@ Set *internal_set_new(const struct hash_ops *hash_ops HASHMAP_DEBUG_PARAMS);
be0c12
 #define set_new(ops) internal_set_new(ops HASHMAP_DEBUG_SRC_ARGS)
be0c12
 
be0c12
 static inline Set *set_free(Set *s) {
be0c12
-        internal_hashmap_free(HASHMAP_BASE(s));
be0c12
-        return NULL;
be0c12
+        return (Set*) internal_hashmap_free(HASHMAP_BASE(s), NULL, NULL);
be0c12
 }
be0c12
 
be0c12
 static inline Set *set_free_free(Set *s) {
be0c12
-        internal_hashmap_free_free(HASHMAP_BASE(s));
be0c12
-        return NULL;
be0c12
+        return (Set*) internal_hashmap_free(HASHMAP_BASE(s), free, NULL);
be0c12
 }
be0c12
 
be0c12
 /* no set_free_free_free */
be0c12
@@ -76,11 +74,11 @@ static inline unsigned set_buckets(Set *s) {
be0c12
 bool set_iterate(Set *s, Iterator *i, void **value);
be0c12
 
be0c12
 static inline void set_clear(Set *s) {
be0c12
-        internal_hashmap_clear(HASHMAP_BASE(s));
be0c12
+        internal_hashmap_clear(HASHMAP_BASE(s), NULL, NULL);
be0c12
 }
be0c12
 
be0c12
 static inline void set_clear_free(Set *s) {
be0c12
-        internal_hashmap_clear_free(HASHMAP_BASE(s));
be0c12
+        internal_hashmap_clear(HASHMAP_BASE(s), free, NULL);
be0c12
 }
be0c12
 
be0c12
 /* no set_clear_free_free */