/
khashmap.c
173 lines (145 loc) · 4.13 KB
/
khashmap.c
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
* khashmap.c - Generic key/value hash map implementation
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 021110-1307, USA.
*
* Copyright (C) 2013 Andrea Righi <andrea@betterlinux.com>
*/
#include <linux/init.h>
#include <linux/list.h>
#include <linux/hash.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/vmalloc.h>
#include "khashmap.h"
#define khashmap_hashfn(__hash, __id) \
hash_long((unsigned long)__id, __hash->bits)
static struct kmem_cache *hlist_cachep __read_mostly;
bool khashmap_empty(const struct khashmap *hlist)
{
struct khashmap_item *item;
struct hlist_node *node;
int i;
if (likely(hlist))
khashmap_for_each_entry(hlist, i, node, item)
return false;
return true;
}
EXPORT_SYMBOL(khashmap_empty);
static struct khashmap_item *
khashmap_find_item(const struct khashmap *hlist, u64 key)
{
/* Silent preprocessor macro warning */
struct hlist_node *node = node;
struct khashmap_item *item;
khashmap_for_each_key(hlist, key, node, item)
if (item->key == key)
return item;
return NULL;
}
void *khashmap_find(const struct khashmap *hlist, u64 key)
{
struct khashmap_item *item = khashmap_find_item(hlist, key);
return item ? item->val : NULL;
}
EXPORT_SYMBOL(khashmap_find);
int khashmap_add(struct khashmap *hlist, u64 key, void *val, gfp_t flags)
{
struct khashmap_item *item = khashmap_find_item(hlist, key);
if (item) {
item->val = val;
return 0;
}
item = kmem_cache_zalloc(hlist_cachep, flags);
if (unlikely(!item))
return -ENOMEM;
item->key = key;
item->val = val;
hlist_add_head(&item->hlist, &hlist->hash[khashmap_hashfn(hlist, key)]);
return 0;
}
EXPORT_SYMBOL(khashmap_add);
void khashmap_del(struct khashmap *hlist, u64 key)
{
struct khashmap_item *item;
item = khashmap_find_item(hlist, key);
if (!item)
return;
hlist_del(&item->hlist);
kmem_cache_free(hlist_cachep, item);
}
EXPORT_SYMBOL(khashmap_del);
static int khashmap_alloc(struct khashmap *hlist)
{
size_t size = khashmap_size_in_bytes(hlist);
int i;
if (size < PAGE_SIZE)
hlist->hash = kmalloc(size, GFP_KERNEL);
else
hlist->hash = vmalloc(size);
if (unlikely(!hlist->hash))
return -ENOMEM;
for (i = 0; i < khashmap_size(hlist); i++)
INIT_HLIST_HEAD(&hlist->hash[i]);
return 0;
}
static void khashmap_free(struct khashmap *hlist)
{
size_t size = khashmap_size_in_bytes(hlist);
if (size < PAGE_SIZE)
kfree(hlist->hash);
else
vfree(hlist->hash);
}
void khashmap_destroy(struct khashmap *hlist)
{
struct khashmap_item *item;
struct hlist_node *node, *p;
int i;
if (unlikely(!hlist))
return;
khashmap_for_each_entry_safe(hlist, i, node, p, item) {
hlist_del(&item->hlist);
kmem_cache_free(hlist_cachep, item);
}
khashmap_free(hlist);
}
EXPORT_SYMBOL(khashmap_destroy);
int khashmap_init(struct khashmap *hlist)
{
return khashmap_alloc(hlist);
}
EXPORT_SYMBOL(khashmap_init);
static int __init khashmap_module_init(void)
{
hlist_cachep = kmem_cache_create("khashmap_cache",
sizeof(struct khashmap_item),
0, 0, NULL);
if (unlikely(!hlist_cachep)) {
printk(KERN_ERR "khashmap: failed to create slab cache\n");
return -ENOMEM;
}
return 0;
}
static void __exit khashmap_module_exit(void)
{
kmem_cache_destroy(hlist_cachep);
}
module_init(khashmap_module_init);
module_exit(khashmap_module_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Andrea Righi <andrea@betterlinux.com>");
MODULE_DESCRIPTION("Generic key/value hash map");