]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/hash-funcs.h
tree-wide: make hash_ops typesafe
[thirdparty/systemd.git] / src / basic / hash-funcs.h
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
758dd67e
LP
2#pragma once
3
758dd67e
LP
4#include "macro.h"
5#include "siphash24.h"
6
7typedef void (*hash_func_t)(const void *p, struct siphash *state);
8typedef int (*compare_func_t)(const void *a, const void *b);
9
10struct hash_ops {
11 hash_func_t hash;
12 compare_func_t compare;
13};
14
d1005d1c
YW
15#define _DEFINE_HASH_OPS(uq, name, type, hash_func, compare_func, scope) \
16 _unused_ static void (* UNIQ_T(static_hash_wrapper, uq))(const type *, struct siphash *) = hash_func; \
17 _unused_ static int (* UNIQ_T(static_compare_wrapper, uq))(const type *, const type *) = compare_func; \
18 scope const struct hash_ops name = { \
19 .hash = (hash_func_t) hash_func, \
20 .compare = (compare_func_t) compare_func, \
21 }
22
23#define DEFINE_HASH_OPS(name, type, hash_func, compare_func) \
24 _DEFINE_HASH_OPS(UNIQ, name, type, hash_func, compare_func,)
25
26#define DEFINE_PRIVATE_HASH_OPS(name, type, hash_func, compare_func) \
27 _DEFINE_HASH_OPS(UNIQ, name, type, hash_func, compare_func, static)
28
25073e50
YW
29void string_hash_func(const char *p, struct siphash *state);
30#define string_compare_func strcmp
758dd67e
LP
31extern const struct hash_ops string_hash_ops;
32
25073e50
YW
33void path_hash_func(const char *p, struct siphash *state);
34int path_compare_func(const char *a, const char *b) _pure_;
46e16b34
LP
35extern const struct hash_ops path_hash_ops;
36
37/* This will compare the passed pointers directly, and will not dereference them. This is hence not useful for strings
38 * or suchlike. */
758dd67e
LP
39void trivial_hash_func(const void *p, struct siphash *state);
40int trivial_compare_func(const void *a, const void *b) _const_;
41extern const struct hash_ops trivial_hash_ops;
42
9bac7d42
LP
43/* 32bit values we can always just embed in the pointer itself, but in order to support 32bit archs we need store 64bit
44 * values indirectly, since they don't fit in a pointer. */
25073e50
YW
45void uint64_hash_func(const uint64_t *p, struct siphash *state);
46int uint64_compare_func(const uint64_t *a, const uint64_t *b) _pure_;
758dd67e
LP
47extern const struct hash_ops uint64_hash_ops;
48
9bac7d42
LP
49/* On some archs dev_t is 32bit, and on others 64bit. And sometimes it's 64bit on 32bit archs, and sometimes 32bit on
50 * 64bit archs. Yuck! */
758dd67e 51#if SIZEOF_DEV_T != 8
25073e50
YW
52void devt_hash_func(const dev_t *p, struct siphash *state) _pure_;
53int devt_compare_func(const dev_t *a, const dev_t *b) _pure_;
9bac7d42 54extern const struct hash_ops devt_hash_ops;
758dd67e
LP
55#else
56#define devt_hash_func uint64_hash_func
57#define devt_compare_func uint64_compare_func
58#define devt_hash_ops uint64_hash_ops
59#endif