]>
Commit | Line | Data |
---|---|---|
d7837182 TL |
1 | /* hash.h |
2 | ||
3 | Definitions for hashing... */ | |
4 | ||
5 | /* | |
c62871ba | 6 | * Copyright (c) 1995-2000 Internet Software Consortium. |
49733f31 | 7 | * All rights reserved. |
d7837182 | 8 | * |
49733f31 TL |
9 | * Redistribution and use in source and binary forms, with or without |
10 | * modification, are permitted provided that the following conditions | |
11 | * are met: | |
d7837182 | 12 | * |
49733f31 TL |
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. | |
d7837182 | 21 | * |
49733f31 TL |
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''. | |
d7837182 TL |
42 | */ |
43 | ||
c62871ba DN |
44 | #ifndef OMAPI_HASH_H |
45 | #define OMAPI_HASH_H | |
46 | ||
6b762886 | 47 | #define DEFAULT_HASH_SIZE 9973 |
d7837182 | 48 | |
20916cae TL |
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 | ||
d0411fbd TL |
54 | typedef void (*hash_foreach_func) (const unsigned char *, |
55 | unsigned, hashed_object_t *); | |
20916cae TL |
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); | |
287d577c | 59 | |
d7837182 TL |
60 | struct hash_bucket { |
61 | struct hash_bucket *next; | |
b1b7b521 TL |
62 | const unsigned char *name; |
63 | unsigned len; | |
20916cae | 64 | hashed_object_t *value; |
d7837182 TL |
65 | }; |
66 | ||
165bce70 | 67 | typedef int (*hash_comparator_t)(const void *, const void *, unsigned long); |
e67f8322 | 68 | |
d7837182 | 69 | struct hash_table { |
b1b7b521 | 70 | unsigned hash_count; |
d7837182 | 71 | struct hash_bucket *buckets [DEFAULT_HASH_SIZE]; |
287d577c TL |
72 | hash_reference referencer; |
73 | hash_dereference dereferencer; | |
e67f8322 | 74 | hash_comparator_t cmp; |
7d9784f6 | 75 | int (*do_hash) (const unsigned char *, unsigned, unsigned); |
d7837182 TL |
76 | }; |
77 | ||
ff404455 TL |
78 | struct named_hash { |
79 | struct named_hash *next; | |
b1b7b521 | 80 | const char *name; |
ff404455 TL |
81 | struct hash_table *hash; |
82 | }; | |
20916cae TL |
83 | |
84 | #define HASH_FUNCTIONS_DECL(name, bufarg, type) \ | |
85 | void name##_hash_add (struct hash_table *, bufarg, unsigned, type *, \ | |
86 | const char *, int); \ | |
87 | void name##_hash_delete (struct hash_table *, bufarg, unsigned, \ | |
88 | const char *, int); \ | |
89 | int name##_hash_lookup (type **, struct hash_table *, bufarg, unsigned, \ | |
d0411fbd TL |
90 | const char *, int); \ |
91 | int name##_hash_foreach (struct hash_table *, \ | |
92 | void (*) (bufarg, unsigned, type *)); | |
93 | ||
20916cae TL |
94 | |
95 | #define HASH_FUNCTIONS(name, bufarg, type) \ | |
96 | void name##_hash_add (struct hash_table *table, \ | |
97 | bufarg buf, unsigned len, type *ptr, \ | |
98 | const char *file, int line) \ | |
99 | { \ | |
100 | add_hash (table, \ | |
101 | (const unsigned char *)buf, \ | |
102 | len, (hashed_object_t *)ptr, file, line); \ | |
103 | } \ | |
104 | \ | |
105 | void name##_hash_delete (struct hash_table *table, \ | |
106 | bufarg buf, unsigned len, const char *file, int line)\ | |
107 | { \ | |
108 | delete_hash_entry (table, (const unsigned char *)buf, \ | |
109 | len, file, line); \ | |
110 | } \ | |
111 | \ | |
112 | int name##_hash_lookup (type **ptr, struct hash_table *table, \ | |
113 | bufarg buf, unsigned len, const char *file, int line) \ | |
114 | { \ | |
115 | return hash_lookup ((hashed_object_t **)ptr, table, \ | |
116 | (const unsigned char *)buf, len, file, line); \ | |
d0411fbd TL |
117 | } \ |
118 | \ | |
119 | int name##_hash_foreach (struct hash_table *table, \ | |
120 | void (*func) (bufarg, unsigned, type *)) \ | |
121 | { \ | |
122 | return hash_foreach (table, (hash_foreach_func)func); \ | |
20916cae TL |
123 | } |
124 | ||
c62871ba | 125 | |
d758ad8c | 126 | void relinquish_hash_bucket_hunks (void); |
c62871ba DN |
127 | struct hash_table *new_hash_table (int, const char *, int); |
128 | void free_hash_table (struct hash_table *, const char *, int); | |
129 | struct hash_bucket *new_hash_bucket (const char *, int); | |
130 | void free_hash_bucket (struct hash_bucket *, const char *, int); | |
d758ad8c TL |
131 | struct hash_table *new_hash (hash_reference, hash_dereference, int, |
132 | const char *, int); | |
c62871ba DN |
133 | void add_hash (struct hash_table *, |
134 | const unsigned char *, unsigned, hashed_object_t *, | |
135 | const char *, int); | |
136 | void delete_hash_entry (struct hash_table *, const unsigned char *, | |
137 | unsigned, const char *, int); | |
138 | int hash_lookup (hashed_object_t **, struct hash_table *, | |
139 | const unsigned char *, unsigned, const char *, int); | |
140 | int hash_foreach (struct hash_table *, hash_foreach_func); | |
141 | int casecmp (const void *s, const void *t, unsigned long len); | |
142 | ||
143 | #endif /* OMAPI_HASH_H */ |