chiark / gitweb /
b085054ee21ecb18af9dc0f23a64128511debc41
[elogind.git] / src / basic / hash-funcs.h
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 #pragma once
4
5 /***
6   This file is part of elogind.
7
8   Copyright 2010 Lennart Poettering
9   Copyright 2014 Michal Schmidt
10
11   elogind is free software; you can redistribute it and/or modify it
12   under the terms of the GNU Lesser General Public License as published by
13   the Free Software Foundation; either version 2.1 of the License, or
14   (at your option) any later version.
15
16   elogind is distributed in the hope that it will be useful, but
17   WITHOUT ANY WARRANTY; without even the implied warranty of
18   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19   Lesser General Public License for more details.
20
21   You should have received a copy of the GNU Lesser General Public License
22   along with elogind; If not, see <http://www.gnu.org/licenses/>.
23 ***/
24
25 #include "macro.h"
26 #include "siphash24.h"
27
28 typedef void (*hash_func_t)(const void *p, struct siphash *state);
29 typedef int (*compare_func_t)(const void *a, const void *b);
30
31 struct hash_ops {
32         hash_func_t hash;
33         compare_func_t compare;
34 };
35
36 void string_hash_func(const void *p, struct siphash *state);
37 int string_compare_func(const void *a, const void *b) _pure_;
38 extern const struct hash_ops string_hash_ops;
39
40 /* This will compare the passed pointers directly, and will not
41  * dereference them. This is hence not useful for strings or
42  * suchlike. */
43 void trivial_hash_func(const void *p, struct siphash *state);
44 int trivial_compare_func(const void *a, const void *b) _const_;
45 extern const struct hash_ops trivial_hash_ops;
46
47 /* 32bit values we can always just embed in the pointer itself, but
48  * in order to support 32bit archs we need store 64bit values
49  * indirectly, since they don't fit in a pointer. */
50 void uint64_hash_func(const void *p, struct siphash *state);
51 int uint64_compare_func(const void *a, const void *b) _pure_;
52 extern const struct hash_ops uint64_hash_ops;
53
54 /* On some archs dev_t is 32bit, and on others 64bit. And sometimes
55  * it's 64bit on 32bit archs, and sometimes 32bit on 64bit archs. Yuck! */
56 #if SIZEOF_DEV_T != 8
57 void devt_hash_func(const void *p, struct siphash *state) _pure_;
58 int devt_compare_func(const void *a, const void *b) _pure_;
59 extern const struct hash_ops devt_hash_ops = {
60         .hash = devt_hash_func,
61         .compare = devt_compare_func
62 };
63 #else
64 #define devt_hash_func uint64_hash_func
65 #define devt_compare_func uint64_compare_func
66 #define devt_hash_ops uint64_hash_ops
67 #endif