]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/hash-funcs.h
logind: Don't match non-leader processes for utmp TTY determination (#38027)
[thirdparty/systemd.git] / src / basic / hash-funcs.h
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
758dd67e
LP
2#pragma once
3
0c15577a 4#include "forward.h"
758dd67e
LP
5
6typedef void (*hash_func_t)(const void *p, struct siphash *state);
7typedef int (*compare_func_t)(const void *a, const void *b);
8
9struct hash_ops {
10 hash_func_t hash;
11 compare_func_t compare;
59a5cda7
YW
12 free_func_t free_key;
13 free_func_t free_value;
758dd67e
LP
14};
15
59a5cda7 16#define _DEFINE_HASH_OPS(uq, name, type, hash_func, compare_func, free_key_func, free_value_func, scope) \
d1005d1c
YW
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, \
59a5cda7
YW
22 .free_key = free_key_func, \
23 .free_value = free_value_func, \
d1005d1c
YW
24 }
25
59a5cda7
YW
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
d1005d1c 50#define DEFINE_HASH_OPS(name, type, hash_func, compare_func) \
59a5cda7 51 _DEFINE_HASH_OPS(UNIQ, name, type, hash_func, compare_func, NULL, NULL,)
d1005d1c
YW
52
53#define DEFINE_PRIVATE_HASH_OPS(name, type, hash_func, compare_func) \
59a5cda7
YW
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)
d1005d1c 73
25073e50
YW
74void string_hash_func(const char *p, struct siphash *state);
75#define string_compare_func strcmp
758dd67e 76extern const struct hash_ops string_hash_ops;
be327321 77extern const struct hash_ops string_hash_ops_free;
0284fcc0 78extern const struct hash_ops string_hash_ops_value_free;
87da8784 79extern const struct hash_ops string_hash_ops_free_free;
52be2b43 80extern const struct hash_ops string_hash_ops_free_strv_free;
758dd67e 81
25073e50 82void path_hash_func(const char *p, struct siphash *state);
46e16b34 83extern const struct hash_ops path_hash_ops;
3fb2326f 84extern const struct hash_ops path_hash_ops_free;
730b9c1e 85extern const struct hash_ops path_hash_ops_free_free;
46e16b34
LP
86
87/* This will compare the passed pointers directly, and will not dereference them. This is hence not useful for strings
88 * or suchlike. */
758dd67e
LP
89void trivial_hash_func(const void *p, struct siphash *state);
90int trivial_compare_func(const void *a, const void *b) _const_;
91extern const struct hash_ops trivial_hash_ops;
5e71868c 92extern const struct hash_ops trivial_hash_ops_free;
0284fcc0 93extern const struct hash_ops trivial_hash_ops_value_free;
5e71868c 94extern const struct hash_ops trivial_hash_ops_free_free;
758dd67e 95
da890466 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
9bac7d42 97 * values indirectly, since they don't fit in a pointer. */
25073e50
YW
98void uint64_hash_func(const uint64_t *p, struct siphash *state);
99int uint64_compare_func(const uint64_t *a, const uint64_t *b) _pure_;
758dd67e 100extern const struct hash_ops uint64_hash_ops;
0284fcc0 101extern const struct hash_ops uint64_hash_ops_value_free;
758dd67e 102
da890466
ZJS
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! */
758dd67e 105#if SIZEOF_DEV_T != 8
bc528010 106void devt_hash_func(const dev_t *p, struct siphash *state);
758dd67e
LP
107#else
108#define devt_hash_func uint64_hash_func
758dd67e 109#endif
17488537
LP
110
111int devt_compare_func(const dev_t *a, const dev_t *b) _pure_;
112extern const struct hash_ops devt_hash_ops;