]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
pidref: add pidref_hash_ops
authorLennart Poettering <lennart@poettering.net>
Tue, 19 Sep 2023 14:11:34 +0000 (16:11 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 28 Sep 2023 21:22:58 +0000 (23:22 +0200)
This adds a "hash_ops" structure, which allows using PidRef structures
as keys in Hashmap and Set objects.

src/basic/pidref.c
src/basic/pidref.h

index 4932266c2470a1cde653a8486c129440623828ef..02d6832bd085b806179e79bf3b6d435b4983edb9 100644 (file)
@@ -239,3 +239,18 @@ int pidref_sigqueue(PidRef *pidref, int sig, int value) {
 
         return -ESRCH;
 }
+
+static void pidref_hash_func(const PidRef *pidref, struct siphash *state) {
+        siphash24_compress(&pidref->pid, sizeof(pidref->pid), state);
+}
+
+static int pidref_compare_func(const PidRef *a, const PidRef *b) {
+        return CMP(a->pid, b->pid);
+}
+
+DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(
+                pidref_hash_ops,
+                PidRef,
+                pidref_hash_func,
+                pidref_compare_func,
+                pidref_free);
index 60f0b064abb4763e1b98308573af7679ac9416ff..835bafc96e44c70b959e5167010b370dd461cf3f 100644 (file)
@@ -52,3 +52,5 @@ int pidref_kill_and_sigcont(PidRef *pidref, int sig);
 int pidref_sigqueue(PidRef *pidfref, int sig, int value);
 
 #define TAKE_PIDREF(p) TAKE_GENERIC((p), PidRef, PIDREF_NULL)
+
+extern const struct hash_ops pidref_hash_ops; /* Has destructor call for pidref_free(), i.e. expects heap allocated PidRef as keys */