1 // SPDX-License-Identifier: GPL-2.0
3 * Implementation of the hash table type.
5 * Author : Stephen Smalley, <sds@tycho.nsa.gov>
7 #include <linux/kernel.h>
8 #include <linux/slab.h>
9 #include <linux/errno.h>
10 #include <linux/sched.h>
13 struct hashtab *hashtab_create(u32 (*hash_value)(struct hashtab *h, const void *key),
14 int (*keycmp)(struct hashtab *h, const void *key1, const void *key2),
20 p = kzalloc(sizeof(*p), GFP_KERNEL);
26 p->hash_value = hash_value;
28 p->htable = kmalloc_array(size, sizeof(*p->htable), GFP_KERNEL);
34 for (i = 0; i < size; i++)
40 int hashtab_insert(struct hashtab *h, void *key, void *datum)
43 struct hashtab_node *prev, *cur, *newnode;
47 if (!h || h->nel == HASHTAB_MAX_NODES)
50 hvalue = h->hash_value(h, key);
52 cur = h->htable[hvalue];
53 while (cur && h->keycmp(h, key, cur->key) > 0) {
58 if (cur && (h->keycmp(h, key, cur->key) == 0))
61 newnode = kzalloc(sizeof(*newnode), GFP_KERNEL);
65 newnode->datum = datum;
67 newnode->next = prev->next;
70 newnode->next = h->htable[hvalue];
71 h->htable[hvalue] = newnode;
78 void *hashtab_search(struct hashtab *h, const void *key)
81 struct hashtab_node *cur;
86 hvalue = h->hash_value(h, key);
87 cur = h->htable[hvalue];
88 while (cur && h->keycmp(h, key, cur->key) > 0)
91 if (!cur || (h->keycmp(h, key, cur->key) != 0))
97 void hashtab_destroy(struct hashtab *h)
100 struct hashtab_node *cur, *temp;
105 for (i = 0; i < h->size; i++) {
121 int hashtab_map(struct hashtab *h,
122 int (*apply)(void *k, void *d, void *args),
127 struct hashtab_node *cur;
132 for (i = 0; i < h->size; i++) {
135 ret = apply(cur->key, cur->datum, args);
145 void hashtab_stat(struct hashtab *h, struct hashtab_info *info)
147 u32 i, chain_len, slots_used, max_chain_len;
148 struct hashtab_node *cur;
152 for (slots_used = max_chain_len = i = 0; i < h->size; i++) {
162 if (chain_len > max_chain_len)
163 max_chain_len = chain_len;
167 info->slots_used = slots_used;
168 info->max_chain_len = max_chain_len;