forked from WuBingzheng/memleax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.h
56 lines (46 loc) · 984 Bytes
/
hash.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
* simple hash utils
*
* Author: Wu Bingzheng
* Date: 2016-5
*/
#ifndef MLD_HASH_H
#define MLD_HASH_H
#include "list.h"
#include <stdint.h>
#define HASH_SIZE 29989
static inline uint32_t hash_slot(void *key, int len)
{
uint32_t *p = key;
uint32_t n = 0;
int i;
for (i = 0; i < len / sizeof(uint32_t); i++) {
n ^= p[i];
}
return n % HASH_SIZE;
}
static inline void hash_add(struct hlist_head *hash, struct hlist_node *node, int len)
{
void *key = node + 1;
hlist_add_head(node, &hash[hash_slot(key, len)]);
}
static inline void hash_delete(struct hlist_node *node)
{
hlist_del(node);
}
static inline struct hlist_node *hash_search(
struct hlist_head *hash, void *vkey, int len)
{
uint32_t n = hash_slot(vkey, len);
struct hlist_node *p;
hlist_for_each(p, &hash[n]) {
if (memcmp(p + 1, vkey, len) == 0) {
return p;
}
}
return NULL;
}
#define hash_for_each(p, i, hash) \
for (i = 0; i < HASH_SIZE; i++) \
hlist_for_each(p, &hash[i])
#endif