]> git.proxmox.com Git - mirror_frr.git/blob - lib/hash.h
Merge pull request #869 from donaldsharp/hash_a_roni
[mirror_frr.git] / lib / hash.h
1 /* Hash routine.
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 */
20
21 #ifndef _ZEBRA_HASH_H
22 #define _ZEBRA_HASH_H
23
24 #include "memory.h"
25 #include "frratomic.h"
26
27 DECLARE_MTYPE(HASH)
28 DECLARE_MTYPE(HASH_BACKET)
29
30 /* Default hash table size. */
31 #define HASH_INITIAL_SIZE 256 /* initial number of backets. */
32 #define HASH_THRESHOLD 10 /* expand when backet. */
33
34 #define HASHWALK_CONTINUE 0
35 #define HASHWALK_ABORT -1
36
37 struct hash_backet {
38 /* if this backet is the head of the linked listed, len denotes the
39 * number of
40 * elements in the list */
41 int len;
42
43 /* Linked list. */
44 struct hash_backet *next;
45
46 /* Hash key. */
47 unsigned int key;
48
49 /* Data. */
50 void *data;
51 };
52
53 struct hashstats {
54 /* number of empty hash buckets */
55 _Atomic uint_fast32_t empty;
56 /* sum of squares of bucket length */
57 _Atomic uint_fast32_t ssq;
58 };
59
60 struct hash {
61 /* Hash backet. */
62 struct hash_backet **index;
63
64 /* Hash table size. Must be power of 2 */
65 unsigned int size;
66
67 /* Key make function. */
68 unsigned int (*hash_key)(void *);
69
70 /* Data compare function. */
71 int (*hash_cmp)(const void *, const void *);
72
73 /* Backet alloc. */
74 unsigned long count;
75
76 struct hashstats stats;
77
78 /* hash name */
79 char *name;
80 };
81
82 #define hashcount(X) ((X)->count)
83
84 extern struct hash *hash_create(unsigned int (*)(void *),
85 int (*)(const void *, const void *),
86 const char *);
87 extern struct hash *hash_create_size(unsigned int, unsigned int (*)(void *),
88 int (*)(const void *, const void *),
89 const char *);
90
91 extern void *hash_get(struct hash *, void *, void *(*)(void *));
92 extern void *hash_alloc_intern(void *);
93 extern void *hash_lookup(struct hash *, void *);
94 extern void *hash_release(struct hash *, void *);
95
96 extern void hash_iterate(struct hash *, void (*)(struct hash_backet *, void *),
97 void *);
98
99 extern void hash_walk(struct hash *, int (*)(struct hash_backet *, void *),
100 void *);
101
102 extern void hash_clean(struct hash *, void (*)(void *));
103 extern void hash_free(struct hash *);
104
105 extern unsigned int string_hash_make(const char *);
106
107 extern void hash_cmd_init(void);
108
109 #endif /* _ZEBRA_HASH_H */