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