1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 1998 Kunihiro Ishiguro
10 #include "frratomic.h"
16 /* Default hash table size. */
17 #define HASH_INITIAL_SIZE 256
18 /* Expansion threshold */
19 #define HASH_THRESHOLD(used, size) ((used) > (size))
21 #define HASHWALK_CONTINUE 0
22 #define HASHWALK_ABORT -1
26 * if this bucket is the head of the linked listed, len denotes the
27 * number of elements in the list
32 struct hash_bucket
*next
;
42 /* number of empty hash buckets */
43 atomic_uint_fast32_t empty
;
44 /* sum of squares of bucket length */
45 atomic_uint_fast32_t ssq
;
50 struct hash_bucket
**index
;
52 /* Hash table size. Must be power of 2 */
55 /* If max_size is 0 there is no limit */
56 unsigned int max_size
;
58 /* Key make function. */
59 unsigned int (*hash_key
)(const void *);
61 /* Data compare function. */
62 bool (*hash_cmp
)(const void *, const void *);
67 struct hashstats stats
;
73 #define hashcount(X) ((X)->count)
76 * Create a hash table.
78 * The created hash table uses chaining and a user-provided comparator function
79 * to resolve collisions. For best performance use a perfect hash function.
80 * Worst case lookup time is O(N) when using a constant hash function. Best
81 * case lookup time is O(1) when using a perfect hash function.
83 * The initial size of the created hash table is HASH_INITIAL_SIZE.
86 * hash function to use; should return a unique unsigned integer when called
87 * with a data item. Collisions are acceptable.
90 * comparison function used for resolving collisions; when called with two
91 * data items, should return true if the two items are equal and false
95 * optional name for the hashtable; this is used when displaying global
96 * hashtable statistics. If this parameter is NULL the hash's name will be
97 * set to NULL and the default name will be displayed when showing
103 extern struct hash
*hash_create(unsigned int (*hash_key
)(const void *),
104 bool (*hash_cmp
)(const void *, const void *),
108 * Create a hash table.
110 * The created hash table uses chaining and a user-provided comparator function
111 * to resolve collisions. For best performance use a perfect hash function.
112 * Worst case lookup time is O(N) when using a constant hash function. Best
113 * case lookup time is O(1) when using a perfect hash function.
116 * initial number of hash buckets to allocate; must be a power of 2 or the
117 * program will assert
120 * hash function to use; should return a unique unsigned integer when called
121 * with a data item. Collisions are acceptable.
124 * comparison function used for resolving collisions; when called with two
125 * data items, should return true if the two items are equal and false
129 * optional name for the hashtable; this is used when displaying global
130 * hashtable statistics. If this parameter is NULL the hash's name will be
131 * set to NULL and the default name will be displayed when showing
138 hash_create_size(unsigned int size
, unsigned int (*hash_key
)(const void *),
139 bool (*hash_cmp
)(const void *, const void *),
143 * Retrieve or insert data from / into a hash table.
145 * This function is somewhat counterintuitive in its usage. In order to look up
146 * an element from its key, you must provide the data item itself, with the
147 * portions used in the hash function set to the same values as the data item
148 * to retrieve. To insert a data element, either provide the key as just
149 * described and provide alloc_func as described below to allocate the full
150 * data element, or provide the full data element and pass 'hash_alloc_intern'
154 * hash table to operate on
157 * data to insert or retrieve - A hash bucket will not be created if
158 * the alloc_func returns a NULL pointer and nothing will be added to
159 * the hash. As such bucket->data will always be non-NULL.
162 * function to call if the item is not found in the hash table. This
163 * function is called with the value of 'data' and should create the data
164 * item to insert and return a pointer to it. If the data has already been
165 * completely created and provided in the 'data' parameter, passing
166 * 'hash_alloc_intern' to this parameter will cause 'data' to be inserted.
167 * If this parameter is NULL, then this call to hash_get is equivalent to
171 * the data item found or inserted, or NULL if alloc_func is NULL and the
174 extern void *hash_get(struct hash
*hash
, void *data
,
175 void *(*alloc_func
)(void *));
178 * Dummy element allocation function.
180 * See hash_get for details.
183 * data to insert into the hash table
188 extern void *hash_alloc_intern(void *data
);
191 * Retrieve an item from a hash table.
193 * This function is equivalent to calling hash_get with alloc_func set to NULL.
196 * hash table to operate on
199 * data element with values used for key computation set
202 * the data element if found, or NULL if not found
204 extern void *hash_lookup(struct hash
*hash
, void *data
);
207 * Remove an element from a hash table.
210 * hash table to operate on
213 * data element to remove with values used for key computation set
216 * the removed element if found, or NULL if not found
218 extern void *hash_release(struct hash
*hash
, void *data
);
221 * Iterate over the elements in a hash table.
223 * The passed in arg to the handler function is the only safe
224 * item to delete from the hash.
226 * Please note that adding entries to the hash
227 * during the walk will cause undefined behavior in that some new entries
228 * will be walked and some will not. So do not do this.
230 * The bucket passed to func will have a non-NULL data pointer.
233 * hash table to operate on
236 * function to call with each data item
239 * arbitrary argument passed as the second parameter in each call to 'func'
241 extern void hash_iterate(struct hash
*hash
,
242 void (*func
)(struct hash_bucket
*, void *), void *arg
);
245 * Iterate over the elements in a hash table, stopping on condition.
247 * The passed in arg to the handler function is the only safe item
248 * to delete from the hash.
250 * Please note that adding entries to the hash
251 * during the walk will cause undefined behavior in that some new entries
252 * will be walked and some will not. So do not do this.
254 * The bucket passed to func will have a non-NULL data pointer.
257 * hash table to operate on
260 * function to call with each data item. If this function returns
261 * HASHWALK_ABORT then the iteration stops.
264 * arbitrary argument passed as the second parameter in each call to 'func'
266 extern void hash_walk(struct hash
*hash
,
267 int (*func
)(struct hash_bucket
*, void *), void *arg
);
270 * Remove all elements from a hash table.
273 * hash table to operate on
276 * function to call with each removed item; intended to free the data
278 extern void hash_clean(struct hash
*hash
, void (*free_func
)(void *));
281 * Delete a hash table.
283 * This function assumes the table is empty. Call hash_clean to delete the
284 * hashtable contents if necessary.
287 * hash table to delete
289 extern void hash_free(struct hash
*hash
);
292 * Converts a hash table to an unsorted linked list.
293 * Does not modify the hash table in any way.
296 * hash table to convert
298 extern struct list
*hash_to_list(struct hash
*hash
);
301 * Hash a string using the modified Bernstein hash.
303 * This is not a perfect hash function.
309 * modified Bernstein hash of the string
311 extern unsigned int string_hash_make(const char *);
314 * Install CLI commands for viewing global hash table statistics.
316 extern void hash_cmd_init(void);
322 #endif /* _ZEBRA_HASH_H */