Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / isc-dhcp / includes / omapip / hash.h
1 /* hash.h
2
3    Definitions for hashing... */
4
5 /*
6  * Copyright (c) 1995-2001 Internet Software Consortium.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of The Internet Software Consortium nor the names
19  *    of its contributors may be used to endorse or promote products derived
20  *    from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
23  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED.  IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
27  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
30  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  * This software has been written for the Internet Software Consortium
37  * by Ted Lemon in cooperation with Vixie Enterprises and Nominum, Inc.
38  * To learn more about the Internet Software Consortium, see
39  * ``http://www.isc.org/''.  To learn more about Vixie Enterprises,
40  * see ``http://www.vix.com''.   To learn more about Nominum, Inc., see
41  * ``http://www.nominum.com''.
42  */
43
44 #ifndef OMAPI_HASH_H
45 #define OMAPI_HASH_H
46
47 #define DEFAULT_HASH_SIZE       9973
48
49 /* The purpose of the hashed_object_t struct is to not match anything else. */
50 typedef struct {
51         int foo;
52 } hashed_object_t;
53
54 typedef void (*hash_foreach_func) (const unsigned char *,
55                                    unsigned, hashed_object_t *);
56 typedef int (*hash_reference) (hashed_object_t **, hashed_object_t *,
57                                const char *, int);
58 typedef int (*hash_dereference) (hashed_object_t **, const char *, int);
59
60 struct hash_bucket {
61         struct hash_bucket *next;
62         const unsigned char *name;
63         unsigned len;
64         hashed_object_t *value;
65 };
66
67 typedef int (*hash_comparator_t)(const void *, const void *, unsigned long);
68
69 struct hash_table {
70         unsigned hash_count;
71         struct hash_bucket *buckets [DEFAULT_HASH_SIZE];
72         hash_reference referencer;
73         hash_dereference dereferencer;
74         hash_comparator_t cmp;
75         int (*do_hash) (const unsigned char *, unsigned, unsigned);
76 };
77
78 struct named_hash {
79         struct named_hash *next;
80         const char *name;
81         struct hash_table *hash;
82 };
83
84 #define HASH_FUNCTIONS_DECL(name, bufarg, type, hashtype)                     \
85 void name##_hash_add (hashtype *, bufarg, unsigned, type *,                   \
86                       const char *, int);                                     \
87 void name##_hash_delete (hashtype *, bufarg, unsigned,                        \
88                          const char *, int);                                  \
89 int name##_hash_lookup (type **, hashtype *, bufarg, unsigned,                \
90                         const char *, int);                                   \
91 int name##_hash_foreach (hashtype *,                                          \
92                          void (*) (bufarg, unsigned, type *));                \
93 int name##_new_hash (hashtype **, int, const char *, int);                    \
94 void name##_free_hash_table (hashtype **, const char *, int);
95
96
97 #define HASH_FUNCTIONS(name, bufarg, type, hashtype, ref, deref)              \
98 void name##_hash_add (hashtype *table,                                        \
99                       bufarg buf, unsigned len, type *ptr,                    \
100                       const char *file, int line)                             \
101 {                                                                             \
102         add_hash ((struct hash_table *)table,                                 \
103                   (const unsigned char *)buf,                                 \
104                   len, (hashed_object_t *)ptr, file, line);                   \
105 }                                                                             \
106                                                                               \
107 void name##_hash_delete (hashtype *table,                                     \
108                          bufarg buf, unsigned len, const char *file, int line)\
109 {                                                                             \
110         delete_hash_entry ((struct hash_table *)table,                        \
111                            (const unsigned char *)buf,                        \
112                            len, file, line);                                  \
113 }                                                                             \
114                                                                               \
115 int name##_hash_lookup (type **ptr, hashtype *table,                          \
116                         bufarg buf, unsigned len, const char *file, int line) \
117 {                                                                             \
118         return hash_lookup ((hashed_object_t **)ptr,                          \
119                             (struct hash_table *)table,                       \
120                             (const unsigned char *)buf, len, file, line);     \
121 }                                                                             \
122                                                                               \
123 int name##_hash_foreach (hashtype *table,                                     \
124                          void (*func) (bufarg, unsigned, type *))             \
125 {                                                                             \
126         return hash_foreach ((struct hash_table *)table,                      \
127                              (hash_foreach_func)func);                        \
128 }                                                                             \
129                                                                               \
130 int name##_new_hash (hashtype **tp, int c, const char *file, int line)        \
131 {                                                                             \
132         return new_hash ((struct hash_table **)tp,                            \
133                          (hash_reference)ref, (hash_dereference)deref, c,     \
134                          file, line);                                         \
135 }                                                                             \
136                                                                               \
137 void name##_free_hash_table (hashtype **table, const char *file, int line)    \
138 {                                                                             \
139         free_hash_table ((struct hash_table **)table, file, line);            \
140 }
141
142 void relinquish_hash_bucket_hunks (void);
143 int new_hash_table (struct hash_table **, int, const char *, int);
144 void free_hash_table (struct hash_table **, const char *, int);
145 struct hash_bucket *new_hash_bucket (const char *, int);
146 void free_hash_bucket (struct hash_bucket *, const char *, int);
147 int new_hash (struct hash_table **,
148               hash_reference, hash_dereference, int, const char *, int);
149 void add_hash (struct hash_table *,
150                       const unsigned char *, unsigned, hashed_object_t *,
151                       const char *, int);
152 void delete_hash_entry (struct hash_table *, const unsigned char *,
153                                unsigned, const char *, int);
154 int hash_lookup (hashed_object_t **, struct hash_table *,
155                         const unsigned char *, unsigned, const char *, int);
156 int hash_foreach (struct hash_table *, hash_foreach_func);
157 int casecmp (const void *s, const void *t, unsigned long len);
158
159 #endif /* OMAPI_HASH_H */