blob: bef7577d1270c5b971f537f9f09e3abd4b6750ba [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Implementation of the hash table type.
3 *
Stephen Smalley7efbb602017-08-17 13:32:36 -04004 * Author : Stephen Smalley, <sds@tycho.nsa.gov>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
6#include <linux/kernel.h>
7#include <linux/slab.h>
8#include <linux/errno.h>
Dave Jonesed1c9642014-05-15 15:03:53 -04009#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include "hashtab.h"
11
Kyeongdon Kim7c620ec2017-09-06 18:50:19 +090012static struct kmem_cache *hashtab_node_cachep;
13
Chad Sellersbb242492006-11-06 12:38:17 -050014struct hashtab *hashtab_create(u32 (*hash_value)(struct hashtab *h, const void *key),
Eric Paris719a2f82008-04-18 17:38:31 -040015 int (*keycmp)(struct hashtab *h, const void *key1, const void *key2),
16 u32 size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017{
18 struct hashtab *p;
19 u32 i;
20
James Morris89d155e2005-10-30 14:59:21 -080021 p = kzalloc(sizeof(*p), GFP_KERNEL);
Markus Elfringcb8d21e2017-01-14 12:36:59 +010022 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 return p;
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 p->size = size;
26 p->nel = 0;
27 p->hash_value = hash_value;
28 p->keycmp = keycmp;
Markus Elfring2f00e682017-01-14 12:06:13 +010029 p->htable = kmalloc_array(size, sizeof(*p->htable), GFP_KERNEL);
Markus Elfringcb8d21e2017-01-14 12:36:59 +010030 if (!p->htable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 kfree(p);
32 return NULL;
33 }
34
35 for (i = 0; i < size; i++)
36 p->htable[i] = NULL;
37
38 return p;
39}
40
41int hashtab_insert(struct hashtab *h, void *key, void *datum)
42{
43 u32 hvalue;
44 struct hashtab_node *prev, *cur, *newnode;
45
Dave Jonesed1c9642014-05-15 15:03:53 -040046 cond_resched();
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 if (!h || h->nel == HASHTAB_MAX_NODES)
49 return -EINVAL;
50
51 hvalue = h->hash_value(h, key);
52 prev = NULL;
53 cur = h->htable[hvalue];
54 while (cur && h->keycmp(h, key, cur->key) > 0) {
55 prev = cur;
56 cur = cur->next;
57 }
58
59 if (cur && (h->keycmp(h, key, cur->key) == 0))
60 return -EEXIST;
61
Kyeongdon Kim7c620ec2017-09-06 18:50:19 +090062 newnode = kmem_cache_zalloc(hashtab_node_cachep, GFP_KERNEL);
Markus Elfringcb8d21e2017-01-14 12:36:59 +010063 if (!newnode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 newnode->key = key;
66 newnode->datum = datum;
67 if (prev) {
68 newnode->next = prev->next;
69 prev->next = newnode;
70 } else {
71 newnode->next = h->htable[hvalue];
72 h->htable[hvalue] = newnode;
73 }
74
75 h->nel++;
76 return 0;
77}
78
Chad Sellersbb242492006-11-06 12:38:17 -050079void *hashtab_search(struct hashtab *h, const void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
81 u32 hvalue;
82 struct hashtab_node *cur;
83
84 if (!h)
85 return NULL;
86
87 hvalue = h->hash_value(h, key);
88 cur = h->htable[hvalue];
Vesa-Matti Karidbc74c62008-08-07 03:18:20 +030089 while (cur && h->keycmp(h, key, cur->key) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 cur = cur->next;
91
Markus Elfringcb8d21e2017-01-14 12:36:59 +010092 if (!cur || (h->keycmp(h, key, cur->key) != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 return NULL;
94
95 return cur->datum;
96}
97
98void hashtab_destroy(struct hashtab *h)
99{
100 u32 i;
101 struct hashtab_node *cur, *temp;
102
103 if (!h)
104 return;
105
106 for (i = 0; i < h->size; i++) {
107 cur = h->htable[i];
Vesa-Matti Karidbc74c62008-08-07 03:18:20 +0300108 while (cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 temp = cur;
110 cur = cur->next;
Kyeongdon Kim7c620ec2017-09-06 18:50:19 +0900111 kmem_cache_free(hashtab_node_cachep, temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 }
113 h->htable[i] = NULL;
114 }
115
116 kfree(h->htable);
117 h->htable = NULL;
118
119 kfree(h);
120}
121
122int hashtab_map(struct hashtab *h,
123 int (*apply)(void *k, void *d, void *args),
124 void *args)
125{
126 u32 i;
127 int ret;
128 struct hashtab_node *cur;
129
130 if (!h)
131 return 0;
132
133 for (i = 0; i < h->size; i++) {
134 cur = h->htable[i];
Vesa-Matti Karidbc74c62008-08-07 03:18:20 +0300135 while (cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 ret = apply(cur->key, cur->datum, args);
137 if (ret)
138 return ret;
139 cur = cur->next;
140 }
141 }
142 return 0;
143}
144
145
146void hashtab_stat(struct hashtab *h, struct hashtab_info *info)
147{
148 u32 i, chain_len, slots_used, max_chain_len;
149 struct hashtab_node *cur;
150
151 slots_used = 0;
152 max_chain_len = 0;
153 for (slots_used = max_chain_len = i = 0; i < h->size; i++) {
154 cur = h->htable[i];
155 if (cur) {
156 slots_used++;
157 chain_len = 0;
158 while (cur) {
159 chain_len++;
160 cur = cur->next;
161 }
162
163 if (chain_len > max_chain_len)
164 max_chain_len = chain_len;
165 }
166 }
167
168 info->slots_used = slots_used;
169 info->max_chain_len = max_chain_len;
170}
Kyeongdon Kim7c620ec2017-09-06 18:50:19 +0900171void hashtab_cache_init(void)
172{
173 hashtab_node_cachep = kmem_cache_create("hashtab_node",
174 sizeof(struct hashtab_node),
175 0, SLAB_PANIC, NULL);
176}
177
178void hashtab_cache_destroy(void)
179{
180 kmem_cache_destroy(hashtab_node_cachep);
181}