]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/hash-funcs.h
7ce29fbe6cdf4bf583c615b061372765f12c20cb
[thirdparty/systemd.git] / src / basic / hash-funcs.h
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include "forward.h"
5
6 typedef void (*hash_func_t)(const void *p, struct siphash *state);
7 typedef int (*compare_func_t)(const void *a, const void *b);
8
9 struct hash_ops {
10 hash_func_t hash;
11 compare_func_t compare;
12 free_func_t free_key;
13 free_func_t free_value;
14 };
15
16 #define _DEFINE_HASH_OPS(uq, name, type, hash_func, compare_func, free_key_func, free_value_func, scope) \
17 _unused_ static void (* UNIQ_T(static_hash_wrapper, uq))(const type *, struct siphash *) = hash_func; \
18 _unused_ static int (* UNIQ_T(static_compare_wrapper, uq))(const type *, const type *) = compare_func; \
19 scope const struct hash_ops name = { \
20 .hash = (hash_func_t) hash_func, \
21 .compare = (compare_func_t) compare_func, \
22 .free_key = free_key_func, \
23 .free_value = free_value_func, \
24 }
25
26 #define _DEFINE_FREE_FUNC(uq, type, wrapper_name, func) \
27 /* Type-safe free function */ \
28 static void UNIQ_T(wrapper_name, uq)(void *a) { \
29 type *_a = a; \
30 func(_a); \
31 }
32
33 #define _DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(uq, name, type, hash_func, compare_func, free_func, scope) \
34 _DEFINE_FREE_FUNC(uq, type, static_free_wrapper, free_func); \
35 _DEFINE_HASH_OPS(uq, name, type, hash_func, compare_func, \
36 UNIQ_T(static_free_wrapper, uq), NULL, scope)
37
38 #define _DEFINE_HASH_OPS_WITH_VALUE_DESTRUCTOR(uq, name, type, hash_func, compare_func, type_value, free_func, scope) \
39 _DEFINE_FREE_FUNC(uq, type_value, static_free_wrapper, free_func); \
40 _DEFINE_HASH_OPS(uq, name, type, hash_func, compare_func, \
41 NULL, UNIQ_T(static_free_wrapper, uq), scope)
42
43 #define _DEFINE_HASH_OPS_FULL(uq, name, type, hash_func, compare_func, free_key_func, type_value, free_value_func, scope) \
44 _DEFINE_FREE_FUNC(uq, type, static_free_key_wrapper, free_key_func); \
45 _DEFINE_FREE_FUNC(uq, type_value, static_free_value_wrapper, free_value_func); \
46 _DEFINE_HASH_OPS(uq, name, type, hash_func, compare_func, \
47 UNIQ_T(static_free_key_wrapper, uq), \
48 UNIQ_T(static_free_value_wrapper, uq), scope)
49
50 #define DEFINE_HASH_OPS(name, type, hash_func, compare_func) \
51 _DEFINE_HASH_OPS(UNIQ, name, type, hash_func, compare_func, NULL, NULL,)
52
53 #define DEFINE_PRIVATE_HASH_OPS(name, type, hash_func, compare_func) \
54 _DEFINE_HASH_OPS(UNIQ, name, type, hash_func, compare_func, NULL, NULL, static)
55
56 #define DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(name, type, hash_func, compare_func, free_func) \
57 _DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(UNIQ, name, type, hash_func, compare_func, free_func,)
58
59 #define DEFINE_PRIVATE_HASH_OPS_WITH_KEY_DESTRUCTOR(name, type, hash_func, compare_func, free_func) \
60 _DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(UNIQ, name, type, hash_func, compare_func, free_func, static)
61
62 #define DEFINE_HASH_OPS_WITH_VALUE_DESTRUCTOR(name, type, hash_func, compare_func, value_type, free_func) \
63 _DEFINE_HASH_OPS_WITH_VALUE_DESTRUCTOR(UNIQ, name, type, hash_func, compare_func, value_type, free_func,)
64
65 #define DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(name, type, hash_func, compare_func, value_type, free_func) \
66 _DEFINE_HASH_OPS_WITH_VALUE_DESTRUCTOR(UNIQ, name, type, hash_func, compare_func, value_type, free_func, static)
67
68 #define DEFINE_HASH_OPS_FULL(name, type, hash_func, compare_func, free_key_func, value_type, free_value_func) \
69 _DEFINE_HASH_OPS_FULL(UNIQ, name, type, hash_func, compare_func, free_key_func, value_type, free_value_func,)
70
71 #define DEFINE_PRIVATE_HASH_OPS_FULL(name, type, hash_func, compare_func, free_key_func, value_type, free_value_func) \
72 _DEFINE_HASH_OPS_FULL(UNIQ, name, type, hash_func, compare_func, free_key_func, value_type, free_value_func, static)
73
74 void string_hash_func(const char *p, struct siphash *state);
75 #define string_compare_func strcmp
76 extern const struct hash_ops string_hash_ops;
77 extern const struct hash_ops string_hash_ops_free;
78 extern const struct hash_ops string_hash_ops_value_free;
79 extern const struct hash_ops string_hash_ops_free_free;
80 extern const struct hash_ops string_hash_ops_free_strv_free;
81
82 void path_hash_func(const char *p, struct siphash *state);
83 extern const struct hash_ops path_hash_ops;
84 extern const struct hash_ops path_hash_ops_free;
85 extern const struct hash_ops path_hash_ops_free_free;
86
87 /* This will compare the passed pointers directly, and will not dereference them. This is hence not useful for strings
88 * or suchlike. */
89 void trivial_hash_func(const void *p, struct siphash *state);
90 int trivial_compare_func(const void *a, const void *b) _const_;
91 extern const struct hash_ops trivial_hash_ops;
92 extern const struct hash_ops trivial_hash_ops_free;
93 extern const struct hash_ops trivial_hash_ops_value_free;
94 extern const struct hash_ops trivial_hash_ops_free_free;
95
96 /* 32-bit values we can always just embed in the pointer itself, but in order to support 32-bit archs we need store 64-bit
97 * values indirectly, since they don't fit in a pointer. */
98 void uint64_hash_func(const uint64_t *p, struct siphash *state);
99 int uint64_compare_func(const uint64_t *a, const uint64_t *b) _pure_;
100 extern const struct hash_ops uint64_hash_ops;
101 extern const struct hash_ops uint64_hash_ops_value_free;
102
103 /* On some archs dev_t is 32-bit, and on others 64-bit. And sometimes it's 64-bit on 32-bit archs, and sometimes 32-bit on
104 * 64-bit archs. Yuck! */
105 #if SIZEOF_DEV_T != 8
106 void devt_hash_func(const dev_t *p, struct siphash *state);
107 #else
108 #define devt_hash_func uint64_hash_func
109 #endif
110
111 int devt_compare_func(const dev_t *a, const dev_t *b) _pure_;
112 extern const struct hash_ops devt_hash_ops;