chiark / gitweb /
3715c56e07f91a6c221c9badae8d8bcf33a33884
[elogind.git] / src / basic / hash-funcs.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 /***
5   Copyright © 2014 Michal Schmidt
6 ***/
7
8 #include "macro.h"
9 #include "siphash24.h"
10
11 typedef void (*hash_func_t)(const void *p, struct siphash *state);
12 typedef int (*compare_func_t)(const void *a, const void *b);
13
14 struct hash_ops {
15         hash_func_t hash;
16         compare_func_t compare;
17 };
18
19 void string_hash_func(const void *p, struct siphash *state);
20 int string_compare_func(const void *a, const void *b) _pure_;
21 extern const struct hash_ops string_hash_ops;
22
23 void path_hash_func(const void *p, struct siphash *state);
24 int path_compare_func(const void *a, const void *b) _pure_;
25 extern const struct hash_ops path_hash_ops;
26
27 /* This will compare the passed pointers directly, and will not dereference them. This is hence not useful for strings
28  * or suchlike. */
29 void trivial_hash_func(const void *p, struct siphash *state);
30 int trivial_compare_func(const void *a, const void *b) _const_;
31 extern const struct hash_ops trivial_hash_ops;
32
33 /* 32bit values we can always just embed in the pointer itself, but in order to support 32bit archs we need store 64bit
34  * values indirectly, since they don't fit in a pointer. */
35 void uint64_hash_func(const void *p, struct siphash *state);
36 int uint64_compare_func(const void *a, const void *b) _pure_;
37 extern const struct hash_ops uint64_hash_ops;
38
39 /* On some archs dev_t is 32bit, and on others 64bit. And sometimes it's 64bit on 32bit archs, and sometimes 32bit on
40  * 64bit archs. Yuck! */
41 #if SIZEOF_DEV_T != 8
42 void devt_hash_func(const void *p, struct siphash *state) _pure_;
43 int devt_compare_func(const void *a, const void *b) _pure_;
44 extern const struct hash_ops devt_hash_ops;
45 #else
46 #define devt_hash_func uint64_hash_func
47 #define devt_compare_func uint64_compare_func
48 #define devt_hash_ops uint64_hash_ops
49 #endif