Merge tag 'riscv-for-linus-4.15-rc2_cleanups' of git://git.kernel.org/pub/scm/linux...
[linux/fpc-iii.git] / security / selinux / ss / hashtab.c
blobfe25b3fb215406f0f3f45f5308533eda41937200
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Implementation of the hash table type.
5 * Author : Stephen Smalley, <sds@tycho.nsa.gov>
6 */
7 #include <linux/kernel.h>
8 #include <linux/slab.h>
9 #include <linux/errno.h>
10 #include <linux/sched.h>
11 #include "hashtab.h"
13 static struct kmem_cache *hashtab_node_cachep;
15 struct hashtab *hashtab_create(u32 (*hash_value)(struct hashtab *h, const void *key),
16 int (*keycmp)(struct hashtab *h, const void *key1, const void *key2),
17 u32 size)
19 struct hashtab *p;
20 u32 i;
22 p = kzalloc(sizeof(*p), GFP_KERNEL);
23 if (!p)
24 return p;
26 p->size = size;
27 p->nel = 0;
28 p->hash_value = hash_value;
29 p->keycmp = keycmp;
30 p->htable = kmalloc_array(size, sizeof(*p->htable), GFP_KERNEL);
31 if (!p->htable) {
32 kfree(p);
33 return NULL;
36 for (i = 0; i < size; i++)
37 p->htable[i] = NULL;
39 return p;
42 int hashtab_insert(struct hashtab *h, void *key, void *datum)
44 u32 hvalue;
45 struct hashtab_node *prev, *cur, *newnode;
47 cond_resched();
49 if (!h || h->nel == HASHTAB_MAX_NODES)
50 return -EINVAL;
52 hvalue = h->hash_value(h, key);
53 prev = NULL;
54 cur = h->htable[hvalue];
55 while (cur && h->keycmp(h, key, cur->key) > 0) {
56 prev = cur;
57 cur = cur->next;
60 if (cur && (h->keycmp(h, key, cur->key) == 0))
61 return -EEXIST;
63 newnode = kmem_cache_zalloc(hashtab_node_cachep, GFP_KERNEL);
64 if (!newnode)
65 return -ENOMEM;
66 newnode->key = key;
67 newnode->datum = datum;
68 if (prev) {
69 newnode->next = prev->next;
70 prev->next = newnode;
71 } else {
72 newnode->next = h->htable[hvalue];
73 h->htable[hvalue] = newnode;
76 h->nel++;
77 return 0;
80 void *hashtab_search(struct hashtab *h, const void *key)
82 u32 hvalue;
83 struct hashtab_node *cur;
85 if (!h)
86 return NULL;
88 hvalue = h->hash_value(h, key);
89 cur = h->htable[hvalue];
90 while (cur && h->keycmp(h, key, cur->key) > 0)
91 cur = cur->next;
93 if (!cur || (h->keycmp(h, key, cur->key) != 0))
94 return NULL;
96 return cur->datum;
99 void hashtab_destroy(struct hashtab *h)
101 u32 i;
102 struct hashtab_node *cur, *temp;
104 if (!h)
105 return;
107 for (i = 0; i < h->size; i++) {
108 cur = h->htable[i];
109 while (cur) {
110 temp = cur;
111 cur = cur->next;
112 kmem_cache_free(hashtab_node_cachep, temp);
114 h->htable[i] = NULL;
117 kfree(h->htable);
118 h->htable = NULL;
120 kfree(h);
123 int hashtab_map(struct hashtab *h,
124 int (*apply)(void *k, void *d, void *args),
125 void *args)
127 u32 i;
128 int ret;
129 struct hashtab_node *cur;
131 if (!h)
132 return 0;
134 for (i = 0; i < h->size; i++) {
135 cur = h->htable[i];
136 while (cur) {
137 ret = apply(cur->key, cur->datum, args);
138 if (ret)
139 return ret;
140 cur = cur->next;
143 return 0;
147 void hashtab_stat(struct hashtab *h, struct hashtab_info *info)
149 u32 i, chain_len, slots_used, max_chain_len;
150 struct hashtab_node *cur;
152 slots_used = 0;
153 max_chain_len = 0;
154 for (i = 0; i < h->size; i++) {
155 cur = h->htable[i];
156 if (cur) {
157 slots_used++;
158 chain_len = 0;
159 while (cur) {
160 chain_len++;
161 cur = cur->next;
164 if (chain_len > max_chain_len)
165 max_chain_len = chain_len;
169 info->slots_used = slots_used;
170 info->max_chain_len = max_chain_len;
172 void hashtab_cache_init(void)
174 hashtab_node_cachep = kmem_cache_create("hashtab_node",
175 sizeof(struct hashtab_node),
176 0, SLAB_PANIC, NULL);
179 void hashtab_cache_destroy(void)
181 kmem_cache_destroy(hashtab_node_cachep);