1 /* SPDX-License-Identifier: GPL-2.0 */
3 * A hash table (hashtab) maintains associations between
4 * key values and datum values. The type of the key values
5 * and the type of the datum values is arbitrary. The
6 * functions for hash computation and key comparison are
7 * provided by the creator of the table.
9 * Author : Stephen Smalley, <sds@tycho.nsa.gov>
11 #ifndef _SS_HASHTAB_H_
12 #define _SS_HASHTAB_H_
14 #include <linux/types.h>
15 #include <linux/errno.h>
16 #include <linux/sched.h>
18 #define HASHTAB_MAX_NODES U32_MAX
20 struct hashtab_key_params {
21 u32 (*hash)(const void *key); /* hash function */
22 int (*cmp)(const void *key1, const void *key2);
23 /* key comparison function */
29 struct hashtab_node *next;
33 struct hashtab_node **htable; /* hash table */
34 u32 size; /* number of slots in hash table */
35 u32 nel; /* number of elements in hash table */
44 * Initializes a new hash table with the specified characteristics.
46 * Returns -ENOMEM if insufficient space is available or 0 otherwise.
48 int hashtab_init(struct hashtab *h, u32 nel_hint);
50 int __hashtab_insert(struct hashtab *h, struct hashtab_node **dst,
51 void *key, void *datum);
54 * Inserts the specified (key, datum) pair into the specified hash table.
56 * Returns -ENOMEM on memory allocation error,
57 * -EEXIST if there is already an entry with the same key,
58 * -EINVAL for general errors or
61 static inline int hashtab_insert(struct hashtab *h, void *key, void *datum,
62 struct hashtab_key_params key_params)
65 struct hashtab_node *prev, *cur;
69 if (!h->size || h->nel == HASHTAB_MAX_NODES)
72 hvalue = key_params.hash(key) & (h->size - 1);
74 cur = h->htable[hvalue];
76 int cmp = key_params.cmp(key, cur->key);
86 return __hashtab_insert(h, prev ? &prev->next : &h->htable[hvalue],
91 * Searches for the entry with the specified key in the hash table.
93 * Returns NULL if no entry has the specified key or
94 * the datum of the entry otherwise.
96 static inline void *hashtab_search(struct hashtab *h, const void *key,
97 struct hashtab_key_params key_params)
100 struct hashtab_node *cur;
105 hvalue = key_params.hash(key) & (h->size - 1);
106 cur = h->htable[hvalue];
108 int cmp = key_params.cmp(key, cur->key);
120 * Destroys the specified hash table.
122 void hashtab_destroy(struct hashtab *h);
125 * Applies the specified apply function to (key,datum,args)
126 * for each entry in the specified hash table.
128 * The order in which the function is applied to the entries
129 * is dependent upon the internal structure of the hash table.
131 * If apply returns a non-zero status, then hashtab_map will cease
132 * iterating through the hash table and will propagate the error
133 * return to its caller.
135 int hashtab_map(struct hashtab *h,
136 int (*apply)(void *k, void *d, void *args),
139 int hashtab_duplicate(struct hashtab *new, struct hashtab *orig,
140 int (*copy)(struct hashtab_node *new,
141 struct hashtab_node *orig, void *args),
142 int (*destroy)(void *k, void *d, void *args),
145 /* Fill info with some hash table statistics */
146 void hashtab_stat(struct hashtab *h, struct hashtab_info *info);
148 #endif /* _SS_HASHTAB_H */