#include "memory.h"
#include "frratomic.h"
+#ifdef __cplusplus
+extern "C" {
+#endif
+
DECLARE_MTYPE(HASH)
DECLARE_MTYPE(HASH_BACKET)
#define HASHWALK_CONTINUE 0
#define HASHWALK_ABORT -1
-struct hash_backet {
+#if CONFDATE > 20200225
+CPP_NOTICE("hash.h: time to remove hash_backet #define")
+#endif
+#define hash_backet hash_bucket
+
+struct hash_bucket {
/*
- * if this backet is the head of the linked listed, len denotes the
+ * if this bucket is the head of the linked listed, len denotes the
* number of elements in the list
*/
int len;
/* Linked list. */
- struct hash_backet *next;
+ struct hash_bucket *next;
/* Hash key. */
unsigned int key;
struct hashstats {
/* number of empty hash buckets */
- _Atomic uint_fast32_t empty;
+ atomic_uint_fast32_t empty;
/* sum of squares of bucket length */
- _Atomic uint_fast32_t ssq;
+ atomic_uint_fast32_t ssq;
};
struct hash {
- /* Hash backet. */
- struct hash_backet **index;
+ /* Hash bucket. */
+ struct hash_bucket **index;
/* Hash table size. Must be power of 2 */
unsigned int size;
unsigned int (*hash_key)(void *);
/* Data compare function. */
- int (*hash_cmp)(const void *, const void *);
+ bool (*hash_cmp)(const void *, const void *);
/* Backet alloc. */
unsigned long count;
* a new hash table
*/
extern struct hash *hash_create(unsigned int (*hash_key)(void *),
- int (*hash_cmp)(const void *, const void *),
+ bool (*hash_cmp)(const void *, const void *),
const char *name);
/*
*/
extern struct hash *
hash_create_size(unsigned int size, unsigned int (*hash_key)(void *),
- int (*hash_cmp)(const void *, const void *), const char *name);
+ bool (*hash_cmp)(const void *, const void *),
+ const char *name);
/*
* Retrieve or insert data from / into a hash table.
* hash table to operate on
*
* data
- * data to insert or retrieve
+ * data to insert or retrieve - A hash bucket will not be created if
+ * the alloc_func returns a NULL pointer and nothing will be added to
+ * the hash. As such bucket->data will always be non-NULL.
*
* alloc_func
* function to call if the item is not found in the hash table. This
* Iterate over the elements in a hash table.
*
* It is safe to delete items passed to the iteration function from the hash
- * table during iteration.
+ * table during iteration. Please note that adding entries to the hash
+ * during the walk will cause undefined behavior in that some new entries
+ * will be walked and some will not. So do not do this.
+ *
+ * The bucket passed to func will have a non-NULL data pointer.
*
* hash
* hash table to operate on
* arbitrary argument passed as the second parameter in each call to 'func'
*/
extern void hash_iterate(struct hash *hash,
- void (*func)(struct hash_backet *, void *), void *arg);
+ void (*func)(struct hash_bucket *, void *), void *arg);
/*
* Iterate over the elements in a hash table, stopping on condition.
*
* It is safe to delete items passed to the iteration function from the hash
- * table during iteration.
+ * table during iteration. Please note that adding entries to the hash
+ * during the walk will cause undefined behavior in that some new entries
+ * will be walked and some will not. So do not do this.
+ *
+ * The bucket passed to func will have a non-NULL data pointer.
*
* hash
* hash table to operate on
* arbitrary argument passed as the second parameter in each call to 'func'
*/
extern void hash_walk(struct hash *hash,
- int (*func)(struct hash_backet *, void *), void *arg);
+ int (*func)(struct hash_bucket *, void *), void *arg);
/*
* Remove all elements from a hash table.
*/
extern void hash_cmd_init(void);
+#ifdef __cplusplus
+}
+#endif
+
#endif /* _ZEBRA_HASH_H */