Skip to content

Commit 758dd67

Browse files
committed
basic: split hash functions into their own header files
The hash operations are not really that specific to hashmaps, hence split them into a .c module of their own.
1 parent e84750c commit 758dd67

File tree

5 files changed

+153
-102
lines changed

5 files changed

+153
-102
lines changed

Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,8 @@ libbasic_la_SOURCES = \
841841
src/basic/mempool.h \
842842
src/basic/hashmap.c \
843843
src/basic/hashmap.h \
844+
src/basic/hash-funcs.c \
845+
src/basic/hash-funcs.h \
844846
src/basic/siphash24.c \
845847
src/basic/siphash24.h \
846848
src/basic/set.h \

src/basic/hash-funcs.c

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2+
3+
/***
4+
This file is part of systemd.
5+
6+
Copyright 2010 Lennart Poettering
7+
Copyright 2014 Michal Schmidt
8+
9+
systemd is free software; you can redistribute it and/or modify it
10+
under the terms of the GNU Lesser General Public License as published by
11+
the Free Software Foundation; either version 2.1 of the License, or
12+
(at your option) any later version.
13+
14+
systemd is distributed in the hope that it will be useful, but
15+
WITHOUT ANY WARRANTY; without even the implied warranty of
16+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17+
Lesser General Public License for more details.
18+
19+
You should have received a copy of the GNU Lesser General Public License
20+
along with systemd; If not, see <http://www.gnu.org/licenses/>.
21+
***/
22+
23+
#include "hash-funcs.h"
24+
25+
void string_hash_func(const void *p, struct siphash *state) {
26+
siphash24_compress(p, strlen(p) + 1, state);
27+
}
28+
29+
int string_compare_func(const void *a, const void *b) {
30+
return strcmp(a, b);
31+
}
32+
33+
const struct hash_ops string_hash_ops = {
34+
.hash = string_hash_func,
35+
.compare = string_compare_func
36+
};
37+
38+
void trivial_hash_func(const void *p, struct siphash *state) {
39+
siphash24_compress(&p, sizeof(p), state);
40+
}
41+
42+
int trivial_compare_func(const void *a, const void *b) {
43+
return a < b ? -1 : (a > b ? 1 : 0);
44+
}
45+
46+
const struct hash_ops trivial_hash_ops = {
47+
.hash = trivial_hash_func,
48+
.compare = trivial_compare_func
49+
};
50+
51+
void uint64_hash_func(const void *p, struct siphash *state) {
52+
siphash24_compress(p, sizeof(uint64_t), state);
53+
}
54+
55+
int uint64_compare_func(const void *_a, const void *_b) {
56+
uint64_t a, b;
57+
a = *(const uint64_t*) _a;
58+
b = *(const uint64_t*) _b;
59+
return a < b ? -1 : (a > b ? 1 : 0);
60+
}
61+
62+
const struct hash_ops uint64_hash_ops = {
63+
.hash = uint64_hash_func,
64+
.compare = uint64_compare_func
65+
};
66+
67+
#if SIZEOF_DEV_T != 8
68+
void devt_hash_func(const void *p, struct siphash *state) {
69+
siphash24_compress(p, sizeof(dev_t), state);
70+
}
71+
72+
int devt_compare_func(const void *_a, const void *_b) {
73+
dev_t a, b;
74+
a = *(const dev_t*) _a;
75+
b = *(const dev_t*) _b;
76+
return a < b ? -1 : (a > b ? 1 : 0);
77+
}
78+
79+
const struct hash_ops devt_hash_ops = {
80+
.hash = devt_hash_func,
81+
.compare = devt_compare_func
82+
};
83+
#endif

src/basic/hash-funcs.h

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2+
3+
#pragma once
4+
5+
/***
6+
This file is part of systemd.
7+
8+
Copyright 2010 Lennart Poettering
9+
Copyright 2014 Michal Schmidt
10+
11+
systemd 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+
systemd 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 systemd; 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

src/basic/hashmap.c

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -280,66 +280,6 @@ static const struct hashmap_type_info hashmap_type_info[_HASHMAP_TYPE_MAX] = {
280280
},
281281
};
282282

283-
void string_hash_func(const void *p, struct siphash *state) {
284-
siphash24_compress(p, strlen(p) + 1, state);
285-
}
286-
287-
int string_compare_func(const void *a, const void *b) {
288-
return strcmp(a, b);
289-
}
290-
291-
const struct hash_ops string_hash_ops = {
292-
.hash = string_hash_func,
293-
.compare = string_compare_func
294-
};
295-
296-
void trivial_hash_func(const void *p, struct siphash *state) {
297-
siphash24_compress(&p, sizeof(p), state);
298-
}
299-
300-
int trivial_compare_func(const void *a, const void *b) {
301-
return a < b ? -1 : (a > b ? 1 : 0);
302-
}
303-
304-
const struct hash_ops trivial_hash_ops = {
305-
.hash = trivial_hash_func,
306-
.compare = trivial_compare_func
307-
};
308-
309-
void uint64_hash_func(const void *p, struct siphash *state) {
310-
siphash24_compress(p, sizeof(uint64_t), state);
311-
}
312-
313-
int uint64_compare_func(const void *_a, const void *_b) {
314-
uint64_t a, b;
315-
a = *(const uint64_t*) _a;
316-
b = *(const uint64_t*) _b;
317-
return a < b ? -1 : (a > b ? 1 : 0);
318-
}
319-
320-
const struct hash_ops uint64_hash_ops = {
321-
.hash = uint64_hash_func,
322-
.compare = uint64_compare_func
323-
};
324-
325-
#if SIZEOF_DEV_T != 8
326-
void devt_hash_func(const void *p, struct siphash *state) {
327-
siphash24_compress(p, sizeof(dev_t), state);
328-
}
329-
330-
int devt_compare_func(const void *_a, const void *_b) {
331-
dev_t a, b;
332-
a = *(const dev_t*) _a;
333-
b = *(const dev_t*) _b;
334-
return a < b ? -1 : (a > b ? 1 : 0);
335-
}
336-
337-
const struct hash_ops devt_hash_ops = {
338-
.hash = devt_hash_func,
339-
.compare = devt_compare_func
340-
};
341-
#endif
342-
343283
static unsigned n_buckets(HashmapBase *h) {
344284
return h->has_indirect ? h->indirect.n_buckets
345285
: hashmap_type_info[h->type].n_direct_buckets;

src/basic/hashmap.h

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
#include <stdbool.h>
2727
#include <stddef.h>
2828

29+
#include "hash-funcs.h"
2930
#include "macro.h"
30-
#include "siphash24.h"
3131
#include "util.h"
3232

3333
/*
@@ -70,47 +70,6 @@ typedef struct {
7070
#define _IDX_ITERATOR_FIRST (UINT_MAX - 1)
7171
#define ITERATOR_FIRST ((Iterator) { .idx = _IDX_ITERATOR_FIRST, .next_key = NULL })
7272

73-
typedef void (*hash_func_t)(const void *p, struct siphash *state);
74-
typedef int (*compare_func_t)(const void *a, const void *b);
75-
76-
struct hash_ops {
77-
hash_func_t hash;
78-
compare_func_t compare;
79-
};
80-
81-
void string_hash_func(const void *p, struct siphash *state);
82-
int string_compare_func(const void *a, const void *b) _pure_;
83-
extern const struct hash_ops string_hash_ops;
84-
85-
/* This will compare the passed pointers directly, and will not
86-
* dereference them. This is hence not useful for strings or
87-
* suchlike. */
88-
void trivial_hash_func(const void *p, struct siphash *state);
89-
int trivial_compare_func(const void *a, const void *b) _const_;
90-
extern const struct hash_ops trivial_hash_ops;
91-
92-
/* 32bit values we can always just embedd in the pointer itself, but
93-
* in order to support 32bit archs we need store 64bit values
94-
* indirectly, since they don't fit in a pointer. */
95-
void uint64_hash_func(const void *p, struct siphash *state);
96-
int uint64_compare_func(const void *a, const void *b) _pure_;
97-
extern const struct hash_ops uint64_hash_ops;
98-
99-
/* On some archs dev_t is 32bit, and on others 64bit. And sometimes
100-
* it's 64bit on 32bit archs, and sometimes 32bit on 64bit archs. Yuck! */
101-
#if SIZEOF_DEV_T != 8
102-
void devt_hash_func(const void *p, struct siphash *state) _pure_;
103-
int devt_compare_func(const void *a, const void *b) _pure_;
104-
extern const struct hash_ops devt_hash_ops = {
105-
.hash = devt_hash_func,
106-
.compare = devt_compare_func
107-
};
108-
#else
109-
#define devt_hash_func uint64_hash_func
110-
#define devt_compare_func uint64_compare_func
111-
#define devt_hash_ops uint64_hash_ops
112-
#endif
113-
11473
/* Macros for type checking */
11574
#define PTR_COMPATIBLE_WITH_HASHMAP_BASE(h) \
11675
(__builtin_types_compatible_p(typeof(h), HashmapBase*) || \

0 commit comments

Comments
 (0)