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