1: /* hash.h
2:
3: Definitions for hashing... */
4:
5: /*
6: * Copyright (c) 2004,2009 by Internet Systems Consortium, Inc. ("ISC")
7: * Copyright (c) 1995-2003 by Internet Software Consortium
8: *
9: * Permission to use, copy, modify, and distribute this software for any
10: * purpose with or without fee is hereby granted, provided that the above
11: * copyright notice and this permission notice appear in all copies.
12: *
13: * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
14: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
16: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
19: * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20: *
21: * Internet Systems Consortium, Inc.
22: * 950 Charter Street
23: * Redwood City, CA 94063
24: * <info@isc.org>
25: * https://www.isc.org/
26: *
27: * This software has been written for Internet Systems Consortium
28: * by Ted Lemon in cooperation with Vixie Enterprises and Nominum, Inc.
29: * To learn more about Internet Systems Consortium, see
30: * ``https://www.isc.org/''. To learn more about Vixie Enterprises,
31: * see ``http://www.vix.com''. To learn more about Nominum, Inc., see
32: * ``http://www.nominum.com''.
33: */
34:
35: #ifndef OMAPI_HASH_H
36: #define OMAPI_HASH_H
37:
38: #if !defined (DEFAULT_HASH_SIZE)
39: # define DEFAULT_HASH_SIZE 9973
40: #endif
41:
42: #if !defined (KEY_HASH_SIZE)
43: # define KEY_HASH_SIZE 1009
44: #endif
45:
46: /* The purpose of the hashed_object_t struct is to not match anything else. */
47: typedef struct {
48: int foo;
49: } hashed_object_t;
50:
51: typedef isc_result_t (*hash_foreach_func)(const void *, unsigned, void *);
52: typedef int (*hash_reference) (hashed_object_t **, hashed_object_t *,
53: const char *, int);
54: typedef int (*hash_dereference) (hashed_object_t **, const char *, int);
55:
56: struct hash_bucket {
57: struct hash_bucket *next;
58: const unsigned char *name;
59: unsigned len;
60: hashed_object_t *value;
61: };
62:
63: typedef int (*hash_comparator_t)(const void *, const void *, size_t);
64:
65: struct hash_table {
66: unsigned hash_count;
67: hash_reference referencer;
68: hash_dereference dereferencer;
69: hash_comparator_t cmp;
70: unsigned (*do_hash)(const void *, unsigned, unsigned);
71:
72: /* This must remain the last entry in this table. */
73: struct hash_bucket *buckets [1];
74: };
75:
76: struct named_hash {
77: struct named_hash *next;
78: const char *name;
79: struct hash_table *hash;
80: };
81:
82: #define HASH_FUNCTIONS_DECL(name, bufarg, type, hashtype) \
83: void name##_hash_add (hashtype *, bufarg, unsigned, type *, \
84: const char *, int); \
85: void name##_hash_delete (hashtype *, bufarg, unsigned, \
86: const char *, int); \
87: int name##_hash_lookup (type **, hashtype *, bufarg, unsigned, \
88: const char *, int); \
89: unsigned char * name##_hash_report(hashtype *); \
90: int name##_hash_foreach (hashtype *, hash_foreach_func); \
91: int name##_new_hash (hashtype **, unsigned, const char *, int); \
92: void name##_free_hash_table (hashtype **, const char *, int);
93:
94:
95: #define HASH_FUNCTIONS(name, bufarg, type, hashtype, ref, deref, hasher) \
96: void name##_hash_add (hashtype *table, \
97: bufarg buf, unsigned len, type *ptr, \
98: const char *file, int line) \
99: { \
100: add_hash ((struct hash_table *)table, buf, \
101: len, (hashed_object_t *)ptr, file, line); \
102: } \
103: \
104: void name##_hash_delete (hashtype *table, bufarg buf, unsigned len, \
105: const char *file, int line) \
106: { \
107: delete_hash_entry ((struct hash_table *)table, buf, len, \
108: file, line); \
109: } \
110: \
111: int name##_hash_lookup (type **ptr, hashtype *table, \
112: bufarg buf, unsigned len, const char *file, int line) \
113: { \
114: return hash_lookup ((hashed_object_t **)ptr, \
115: (struct hash_table *)table, \
116: buf, len, file, line); \
117: } \
118: \
119: unsigned char * name##_hash_report(hashtype *table) \
120: { \
121: return hash_report((struct hash_table *)table); \
122: } \
123: \
124: int name##_hash_foreach (hashtype *table, hash_foreach_func func) \
125: { \
126: return hash_foreach ((struct hash_table *)table, \
127: func); \
128: } \
129: \
130: int name##_new_hash (hashtype **tp, unsigned c, const char *file, int line) \
131: { \
132: return new_hash ((struct hash_table **)tp, \
133: (hash_reference)ref, (hash_dereference)deref, c, \
134: hasher, 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 **, unsigned, 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, unsigned,
149: unsigned (*do_hash)(const void *, unsigned, unsigned),
150: const char *, int);
151: unsigned do_string_hash(const void *, unsigned, unsigned);
152: unsigned do_case_hash(const void *, unsigned, unsigned);
153: unsigned do_id_hash(const void *, unsigned, unsigned);
154: unsigned do_number_hash(const void *, unsigned, unsigned);
155: unsigned do_ip4_hash(const void *, unsigned, unsigned);
156: unsigned char *hash_report(struct hash_table *);
157: void add_hash (struct hash_table *,
158: const void *, unsigned, hashed_object_t *,
159: const char *, int);
160: void delete_hash_entry (struct hash_table *, const void *,
161: unsigned, const char *, int);
162: int hash_lookup (hashed_object_t **, struct hash_table *,
163: const void *, unsigned, const char *, int);
164: int hash_foreach (struct hash_table *, hash_foreach_func);
165: int casecmp (const void *s, const void *t, size_t len);
166:
167: #endif /* OMAPI_HASH_H */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>