]>
Commit | Line | Data |
---|---|---|
718e3744 | 1 | /* Hash routine. |
896014f4 DL |
2 | * Copyright (C) 1998 Kunihiro Ishiguro |
3 | * | |
4 | * This file is part of GNU Zebra. | |
5 | * | |
6 | * GNU Zebra is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published | |
8 | * by the Free Software Foundation; either version 2, or (at your | |
9 | * option) any later version. | |
10 | * | |
11 | * GNU Zebra is distributed in the hope that it will be useful, but | |
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License along | |
17 | * with this program; see the file COPYING; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
718e3744 | 20 | |
21 | #ifndef _ZEBRA_HASH_H | |
22 | #define _ZEBRA_HASH_H | |
23 | ||
4a1ab8e4 | 24 | #include "memory.h" |
6f6f0010 | 25 | #include "frratomic.h" |
4a1ab8e4 | 26 | |
5e244469 RW |
27 | #ifdef __cplusplus |
28 | extern "C" { | |
29 | #endif | |
30 | ||
d62a17ae | 31 | /* Default hash table size. */ |
bed7ad83 QY |
32 | #define HASH_INITIAL_SIZE 256 |
33 | /* Expansion threshold */ | |
34 | #define HASH_THRESHOLD(used, size) ((used) > (size)) | |
718e3744 | 35 | |
3f9c7369 DS |
36 | #define HASHWALK_CONTINUE 0 |
37 | #define HASHWALK_ABORT -1 | |
38 | ||
0026ac15 QY |
39 | #if CONFDATE > 20200225 |
40 | CPP_NOTICE("hash.h: time to remove hash_backet #define") | |
41 | #endif | |
42 | #define hash_backet hash_bucket | |
43 | ||
e3b78da8 | 44 | struct hash_bucket { |
6fd8c487 | 45 | /* |
e3b78da8 | 46 | * if this bucket is the head of the linked listed, len denotes the |
6fd8c487 QY |
47 | * number of elements in the list |
48 | */ | |
d62a17ae | 49 | int len; |
6f6f0010 | 50 | |
d62a17ae | 51 | /* Linked list. */ |
e3b78da8 | 52 | struct hash_bucket *next; |
718e3744 | 53 | |
d62a17ae | 54 | /* Hash key. */ |
55 | unsigned int key; | |
718e3744 | 56 | |
d62a17ae | 57 | /* Data. */ |
58 | void *data; | |
718e3744 | 59 | }; |
60 | ||
d62a17ae | 61 | struct hashstats { |
62 | /* number of empty hash buckets */ | |
c8a65463 | 63 | atomic_uint_fast32_t empty; |
d62a17ae | 64 | /* sum of squares of bucket length */ |
c8a65463 | 65 | atomic_uint_fast32_t ssq; |
6f6f0010 QY |
66 | }; |
67 | ||
d62a17ae | 68 | struct hash { |
e3b78da8 TB |
69 | /* Hash bucket. */ |
70 | struct hash_bucket **index; | |
718e3744 | 71 | |
d62a17ae | 72 | /* Hash table size. Must be power of 2 */ |
73 | unsigned int size; | |
718e3744 | 74 | |
40520c36 DW |
75 | /* If max_size is 0 there is no limit */ |
76 | unsigned int max_size; | |
77 | ||
d62a17ae | 78 | /* Key make function. */ |
d8b87afe | 79 | unsigned int (*hash_key)(const void *); |
718e3744 | 80 | |
d62a17ae | 81 | /* Data compare function. */ |
74df8d6d | 82 | bool (*hash_cmp)(const void *, const void *); |
718e3744 | 83 | |
d62a17ae | 84 | /* Backet alloc. */ |
85 | unsigned long count; | |
4db0cff1 | 86 | |
d62a17ae | 87 | struct hashstats stats; |
6f6f0010 | 88 | |
d62a17ae | 89 | /* hash name */ |
90 | char *name; | |
718e3744 | 91 | }; |
92 | ||
8234a987 | 93 | #define hashcount(X) ((X)->count) |
94 | ||
6fd8c487 QY |
95 | /* |
96 | * Create a hash table. | |
97 | * | |
98 | * The created hash table uses chaining and a user-provided comparator function | |
99 | * to resolve collisions. For best performance use a perfect hash function. | |
100 | * Worst case lookup time is O(N) when using a constant hash function. Best | |
101 | * case lookup time is O(1) when using a perfect hash function. | |
102 | * | |
103 | * The initial size of the created hash table is HASH_INITIAL_SIZE. | |
104 | * | |
105 | * hash_key | |
106 | * hash function to use; should return a unique unsigned integer when called | |
107 | * with a data item. Collisions are acceptable. | |
108 | * | |
109 | * hash_cmp | |
110 | * comparison function used for resolving collisions; when called with two | |
111 | * data items, should return nonzero if the two items are equal and 0 | |
112 | * otherwise | |
113 | * | |
114 | * name | |
115 | * optional name for the hashtable; this is used when displaying global | |
116 | * hashtable statistics. If this parameter is NULL the hash's name will be | |
117 | * set to NULL and the default name will be displayed when showing | |
118 | * statistics. | |
119 | * | |
120 | * Returns: | |
121 | * a new hash table | |
122 | */ | |
d8b87afe | 123 | extern struct hash *hash_create(unsigned int (*hash_key)(const void *), |
74df8d6d | 124 | bool (*hash_cmp)(const void *, const void *), |
6fd8c487 QY |
125 | const char *name); |
126 | ||
127 | /* | |
128 | * Create a hash table. | |
129 | * | |
130 | * The created hash table uses chaining and a user-provided comparator function | |
131 | * to resolve collisions. For best performance use a perfect hash function. | |
132 | * Worst case lookup time is O(N) when using a constant hash function. Best | |
133 | * case lookup time is O(1) when using a perfect hash function. | |
134 | * | |
135 | * size | |
136 | * initial number of hash buckets to allocate; must be a power of 2 or the | |
137 | * program will assert | |
138 | * | |
139 | * hash_key | |
140 | * hash function to use; should return a unique unsigned integer when called | |
141 | * with a data item. Collisions are acceptable. | |
142 | * | |
143 | * hash_cmp | |
144 | * comparison function used for resolving collisions; when called with two | |
145 | * data items, should return nonzero if the two items are equal and 0 | |
146 | * otherwise | |
147 | * | |
148 | * name | |
149 | * optional name for the hashtable; this is used when displaying global | |
150 | * hashtable statistics. If this parameter is NULL the hash's name will be | |
151 | * set to NULL and the default name will be displayed when showing | |
152 | * statistics. | |
153 | * | |
154 | * Returns: | |
155 | * a new hash table | |
156 | */ | |
157 | extern struct hash * | |
d8b87afe | 158 | hash_create_size(unsigned int size, unsigned int (*hash_key)(const void *), |
74df8d6d DS |
159 | bool (*hash_cmp)(const void *, const void *), |
160 | const char *name); | |
6fd8c487 QY |
161 | |
162 | /* | |
163 | * Retrieve or insert data from / into a hash table. | |
164 | * | |
165 | * This function is somewhat counterintuitive in its usage. In order to look up | |
166 | * an element from its key, you must provide the data item itself, with the | |
167 | * portions used in the hash function set to the same values as the data item | |
168 | * to retrieve. To insert a data element, either provide the key as just | |
169 | * described and provide alloc_func as descrbied below to allocate the full | |
170 | * data element, or provide the full data element and pass 'hash_alloc_intern' | |
171 | * to alloc_func. | |
172 | * | |
173 | * hash | |
174 | * hash table to operate on | |
175 | * | |
176 | * data | |
e3b78da8 | 177 | * data to insert or retrieve - A hash bucket will not be created if |
d3ce24ef | 178 | * the alloc_func returns a NULL pointer and nothing will be added to |
e3b78da8 | 179 | * the hash. As such bucket->data will always be non-NULL. |
6fd8c487 QY |
180 | * |
181 | * alloc_func | |
182 | * function to call if the item is not found in the hash table. This | |
183 | * function is called with the value of 'data' and should create the data | |
184 | * item to insert and return a pointer to it. If the data has already been | |
185 | * completely created and provided in the 'data' parameter, passing | |
186 | * 'hash_alloc_intern' to this parameter will cause 'data' to be inserted. | |
187 | * If this parameter is NULL, then this call to hash_get is equivalent to | |
188 | * hash_lookup. | |
189 | * | |
190 | * Returns: | |
191 | * the data item found or inserted, or NULL if alloc_func is NULL and the | |
192 | * data is not found | |
193 | */ | |
194 | extern void *hash_get(struct hash *hash, void *data, | |
195 | void *(*alloc_func)(void *)); | |
196 | ||
197 | /* | |
198 | * Dummy element allocation function. | |
199 | * | |
200 | * See hash_get for details. | |
201 | * | |
202 | * data | |
203 | * data to insert into the hash table | |
204 | * | |
205 | * Returns: | |
206 | * data | |
207 | */ | |
208 | extern void *hash_alloc_intern(void *data); | |
209 | ||
210 | /* | |
211 | * Retrieve an item from a hash table. | |
212 | * | |
213 | * This function is equivalent to calling hash_get with alloc_func set to NULL. | |
214 | * | |
215 | * hash | |
216 | * hash table to operate on | |
217 | * | |
218 | * data | |
219 | * data element with values used for key computation set | |
220 | * | |
221 | * Returns: | |
222 | * the data element if found, or NULL if not found | |
223 | */ | |
224 | extern void *hash_lookup(struct hash *hash, void *data); | |
718e3744 | 225 | |
6fd8c487 QY |
226 | /* |
227 | * Remove an element from a hash table. | |
228 | * | |
229 | * hash | |
230 | * hash table to operate on | |
231 | * | |
232 | * data | |
233 | * data element to remove with values used for key computation set | |
234 | * | |
235 | * Returns: | |
236 | * the removed element if found, or NULL if not found | |
237 | */ | |
238 | extern void *hash_release(struct hash *hash, void *data); | |
718e3744 | 239 | |
6fd8c487 QY |
240 | /* |
241 | * Iterate over the elements in a hash table. | |
242 | * | |
243 | * It is safe to delete items passed to the iteration function from the hash | |
8b52179d DS |
244 | * table during iteration. Please note that adding entries to the hash |
245 | * during the walk will cause undefined behavior in that some new entries | |
246 | * will be walked and some will not. So do not do this. | |
6fd8c487 | 247 | * |
e3b78da8 | 248 | * The bucket passed to func will have a non-NULL data pointer. |
d3ce24ef | 249 | * |
6fd8c487 QY |
250 | * hash |
251 | * hash table to operate on | |
252 | * | |
253 | * func | |
254 | * function to call with each data item | |
255 | * | |
256 | * arg | |
257 | * arbitrary argument passed as the second parameter in each call to 'func' | |
258 | */ | |
259 | extern void hash_iterate(struct hash *hash, | |
e3b78da8 | 260 | void (*func)(struct hash_bucket *, void *), void *arg); |
718e3744 | 261 | |
6fd8c487 QY |
262 | /* |
263 | * Iterate over the elements in a hash table, stopping on condition. | |
264 | * | |
265 | * It is safe to delete items passed to the iteration function from the hash | |
8b52179d DS |
266 | * table during iteration. Please note that adding entries to the hash |
267 | * during the walk will cause undefined behavior in that some new entries | |
268 | * will be walked and some will not. So do not do this. | |
6fd8c487 | 269 | * |
e3b78da8 | 270 | * The bucket passed to func will have a non-NULL data pointer. |
d3ce24ef | 271 | * |
6fd8c487 QY |
272 | * hash |
273 | * hash table to operate on | |
274 | * | |
275 | * func | |
276 | * function to call with each data item. If this function returns | |
277 | * HASHWALK_ABORT then the iteration stops. | |
278 | * | |
279 | * arg | |
280 | * arbitrary argument passed as the second parameter in each call to 'func' | |
281 | */ | |
282 | extern void hash_walk(struct hash *hash, | |
e3b78da8 | 283 | int (*func)(struct hash_bucket *, void *), void *arg); |
3f9c7369 | 284 | |
6fd8c487 QY |
285 | /* |
286 | * Remove all elements from a hash table. | |
287 | * | |
288 | * hash | |
289 | * hash table to operate on | |
290 | * | |
291 | * free_func | |
292 | * function to call with each removed item; intended to free the data | |
293 | */ | |
294 | extern void hash_clean(struct hash *hash, void (*free_func)(void *)); | |
295 | ||
296 | /* | |
297 | * Delete a hash table. | |
298 | * | |
299 | * This function assumes the table is empty. Call hash_clean to delete the | |
300 | * hashtable contents if necessary. | |
301 | * | |
302 | * hash | |
303 | * hash table to delete | |
304 | */ | |
305 | extern void hash_free(struct hash *hash); | |
718e3744 | 306 | |
91f10370 QY |
307 | /* |
308 | * Converts a hash table to an unsorted linked list. | |
309 | * Does not modify the hash table in any way. | |
310 | * | |
311 | * hash | |
6fd8c487 | 312 | * hash table to convert |
91f10370 QY |
313 | */ |
314 | extern struct list *hash_to_list(struct hash *hash); | |
315 | ||
6fd8c487 QY |
316 | /* |
317 | * Hash a string using the modified Bernstein hash. | |
318 | * | |
319 | * This is not a perfect hash function. | |
320 | * | |
321 | * str | |
322 | * string to hash | |
323 | * | |
324 | * Returns: | |
325 | * modified Bernstein hash of the string | |
326 | */ | |
d62a17ae | 327 | extern unsigned int string_hash_make(const char *); |
6392aa83 | 328 | |
6fd8c487 QY |
329 | /* |
330 | * Install CLI commands for viewing global hash table statistics. | |
331 | */ | |
d62a17ae | 332 | extern void hash_cmd_init(void); |
4db0cff1 | 333 | |
5e244469 RW |
334 | #ifdef __cplusplus |
335 | } | |
336 | #endif | |
337 | ||
718e3744 | 338 | #endif /* _ZEBRA_HASH_H */ |