From: Lennart Poettering Date: Tue, 19 Sep 2023 14:11:34 +0000 (+0200) Subject: pidref: add pidref_hash_ops X-Git-Tag: v255-rc1~387^2~4 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=9cb7e49f114d572027a80402f9266c47b06728f0;p=thirdparty%2Fsystemd.git pidref: add pidref_hash_ops This adds a "hash_ops" structure, which allows using PidRef structures as keys in Hashmap and Set objects. --- diff --git a/src/basic/pidref.c b/src/basic/pidref.c index 4932266c247..02d6832bd08 100644 --- a/src/basic/pidref.c +++ b/src/basic/pidref.c @@ -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); diff --git a/src/basic/pidref.h b/src/basic/pidref.h index 60f0b064abb..835bafc96e4 100644 --- a/src/basic/pidref.h +++ b/src/basic/pidref.h @@ -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 */