]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - security/selinux/ss/hashtab.c
e0443f4afea50a5a8ad14d3afd956e81c511ca87
2 * Implementation of the hash table type.
4 * Author : Stephen Smalley, <sds@tycho.nsa.gov>
6 #include <linux/kernel.h>
7 #include <linux/slab.h>
8 #include <linux/errno.h>
9 #include <linux/sched.h>
12 static struct kmem_cache
*hashtab_node_cachep
;
14 struct hashtab
*hashtab_create(u32 (*hash_value
)(struct hashtab
*h
, const void *key
),
15 int (*keycmp
)(struct hashtab
*h
, const void *key1
, const void *key2
),
21 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
27 p
->hash_value
= hash_value
;
29 p
->htable
= kmalloc_array(size
, sizeof(*p
->htable
), GFP_KERNEL
);
35 for (i
= 0; i
< size
; i
++)
41 int hashtab_insert(struct hashtab
*h
, void *key
, void *datum
)
44 struct hashtab_node
*prev
, *cur
, *newnode
;
48 if (!h
|| h
->nel
== HASHTAB_MAX_NODES
)
51 hvalue
= h
->hash_value(h
, key
);
53 cur
= h
->htable
[hvalue
];
54 while (cur
&& h
->keycmp(h
, key
, cur
->key
) > 0) {
59 if (cur
&& (h
->keycmp(h
, key
, cur
->key
) == 0))
62 newnode
= kmem_cache_zalloc(hashtab_node_cachep
, GFP_KERNEL
);
66 newnode
->datum
= datum
;
68 newnode
->next
= prev
->next
;
71 newnode
->next
= h
->htable
[hvalue
];
72 h
->htable
[hvalue
] = newnode
;
79 void *hashtab_search(struct hashtab
*h
, const void *key
)
82 struct hashtab_node
*cur
;
87 hvalue
= h
->hash_value(h
, key
);
88 cur
= h
->htable
[hvalue
];
89 while (cur
&& h
->keycmp(h
, key
, cur
->key
) > 0)
92 if (!cur
|| (h
->keycmp(h
, key
, cur
->key
) != 0))
98 void hashtab_destroy(struct hashtab
*h
)
101 struct hashtab_node
*cur
, *temp
;
106 for (i
= 0; i
< h
->size
; i
++) {
111 kmem_cache_free(hashtab_node_cachep
, temp
);
122 int hashtab_map(struct hashtab
*h
,
123 int (*apply
)(void *k
, void *d
, void *args
),
128 struct hashtab_node
*cur
;
133 for (i
= 0; i
< h
->size
; i
++) {
136 ret
= apply(cur
->key
, cur
->datum
, args
);
146 void hashtab_stat(struct hashtab
*h
, struct hashtab_info
*info
)
148 u32 i
, chain_len
, slots_used
, max_chain_len
;
149 struct hashtab_node
*cur
;
153 for (i
= 0; i
< h
->size
; i
++) {
163 if (chain_len
> max_chain_len
)
164 max_chain_len
= chain_len
;
168 info
->slots_used
= slots_used
;
169 info
->max_chain_len
= max_chain_len
;
171 void hashtab_cache_init(void)
173 hashtab_node_cachep
= kmem_cache_create("hashtab_node",
174 sizeof(struct hashtab_node
),
175 0, SLAB_PANIC
, NULL
);
178 void hashtab_cache_destroy(void)
180 kmem_cache_destroy(hashtab_node_cachep
);