1lhash(3) OpenSSL lhash(3)
2
3
4
6 lh_new, lh_free, lh_insert, lh_delete, lh_retrieve, lh_doall,
7 lh_doall_arg, lh_error - dynamic hash table
8
10 #include <openssl/lhash.h>
11
12 DECLARE_LHASH_OF(<type>);
13
14 LHASH *lh_<type>_new();
15 void lh_<type>_free(LHASH_OF(<type> *table);
16
17 <type> *lh_<type>_insert(LHASH_OF(<type> *table, <type> *data);
18 <type> *lh_<type>_delete(LHASH_OF(<type> *table, <type> *data);
19 <type> *lh_retrieve(LHASH_OF<type> *table, <type> *data);
20
21 void lh_<type>_doall(LHASH_OF(<type> *table, LHASH_DOALL_FN_TYPE func);
22 void lh_<type>_doall_arg(LHASH_OF(<type> *table, LHASH_DOALL_ARG_FN_TYPE func,
23 <type2>, <type2> *arg);
24
25 int lh_<type>_error(LHASH_OF(<type> *table);
26
27 typedef int (*LHASH_COMP_FN_TYPE)(const void *, const void *);
28 typedef unsigned long (*LHASH_HASH_FN_TYPE)(const void *);
29 typedef void (*LHASH_DOALL_FN_TYPE)(const void *);
30 typedef void (*LHASH_DOALL_ARG_FN_TYPE)(const void *, const void *);
31
33 This library implements type-checked dynamic hash tables. The hash
34 table entries can be arbitrary structures. Usually they consist of key
35 and value fields.
36
37 lh_<type>_new() creates a new LHASH_OF(<type> structure to store
38 arbitrary data entries, and provides the 'hash' and 'compare' callbacks
39 to be used in organising the table's entries. The hash callback takes
40 a pointer to a table entry as its argument and returns an unsigned long
41 hash value for its key field. The hash value is normally truncated to
42 a power of 2, so make sure that your hash function returns well mixed
43 low order bits. The compare callback takes two arguments (pointers to
44 two hash table entries), and returns 0 if their keys are equal, non-
45 zero otherwise. If your hash table will contain items of some
46 particular type and the hash and compare callbacks hash/compare these
47 types, then the DECLARE_LHASH_HASH_FN and IMPLEMENT_LHASH_COMP_FN
48 macros can be used to create callback wrappers of the prototypes
49 required by lh_<type>_new(). These provide per-variable casts before
50 calling the type-specific callbacks written by the application author.
51 These macros, as well as those used for the "doall" callbacks, are
52 defined as;
53
54 #define DECLARE_LHASH_HASH_FN(name, o_type) \
55 unsigned long name##_LHASH_HASH(const void *);
56 #define IMPLEMENT_LHASH_HASH_FN(name, o_type) \
57 unsigned long name##_LHASH_HASH(const void *arg) { \
58 const o_type *a = arg; \
59 return name##_hash(a); }
60 #define LHASH_HASH_FN(name) name##_LHASH_HASH
61
62 #define DECLARE_LHASH_COMP_FN(name, o_type) \
63 int name##_LHASH_COMP(const void *, const void *);
64 #define IMPLEMENT_LHASH_COMP_FN(name, o_type) \
65 int name##_LHASH_COMP(const void *arg1, const void *arg2) { \
66 const o_type *a = arg1; \
67 const o_type *b = arg2; \
68 return name##_cmp(a,b); }
69 #define LHASH_COMP_FN(name) name##_LHASH_COMP
70
71 #define DECLARE_LHASH_DOALL_FN(name, o_type) \
72 void name##_LHASH_DOALL(void *);
73 #define IMPLEMENT_LHASH_DOALL_FN(name, o_type) \
74 void name##_LHASH_DOALL(void *arg) { \
75 o_type *a = arg; \
76 name##_doall(a); }
77 #define LHASH_DOALL_FN(name) name##_LHASH_DOALL
78
79 #define DECLARE_LHASH_DOALL_ARG_FN(name, o_type, a_type) \
80 void name##_LHASH_DOALL_ARG(void *, void *);
81 #define IMPLEMENT_LHASH_DOALL_ARG_FN(name, o_type, a_type) \
82 void name##_LHASH_DOALL_ARG(void *arg1, void *arg2) { \
83 o_type *a = arg1; \
84 a_type *b = arg2; \
85 name##_doall_arg(a, b); }
86 #define LHASH_DOALL_ARG_FN(name) name##_LHASH_DOALL_ARG
87
88 An example of a hash table storing (pointers to) structures of type 'STUFF'
89 could be defined as follows;
90
91 /* Calculates the hash value of 'tohash' (implemented elsewhere) */
92 unsigned long STUFF_hash(const STUFF *tohash);
93 /* Orders 'arg1' and 'arg2' (implemented elsewhere) */
94 int stuff_cmp(const STUFF *arg1, const STUFF *arg2);
95 /* Create the type-safe wrapper functions for use in the LHASH internals */
96 static IMPLEMENT_LHASH_HASH_FN(stuff, STUFF);
97 static IMPLEMENT_LHASH_COMP_FN(stuff, STUFF);
98 /* ... */
99 int main(int argc, char *argv[]) {
100 /* Create the new hash table using the hash/compare wrappers */
101 LHASH_OF(STUFF) *hashtable = lh_STUFF_new(LHASH_HASH_FN(STUFF_hash),
102 LHASH_COMP_FN(STUFF_cmp));
103 /* ... */
104 }
105
106 lh_<type>_free() frees the LHASH_OF(<type> structure table. Allocated
107 hash table entries will not be freed; consider using lh_<type>_doall()
108 to deallocate any remaining entries in the hash table (see below).
109
110 lh_<type>_insert() inserts the structure pointed to by data into table.
111 If there already is an entry with the same key, the old value is
112 replaced. Note that lh_<type>_insert() stores pointers, the data are
113 not copied.
114
115 lh_<type>_delete() deletes an entry from table.
116
117 lh_<type>_retrieve() looks up an entry in table. Normally, data is a
118 structure with the key field(s) set; the function will return a pointer
119 to a fully populated structure.
120
121 lh_<type>_doall() will, for every entry in the hash table, call func
122 with the data item as its parameter. For lh_<type>_doall() and
123 lh_<type>_doall_arg(), function pointer casting should be avoided in
124 the callbacks (see NOTE) - instead use the declare/implement macros to
125 create type-checked wrappers that cast variables prior to calling your
126 type-specific callbacks. An example of this is illustrated here where
127 the callback is used to cleanup resources for items in the hash table
128 prior to the hashtable itself being deallocated:
129
130 /* Cleans up resources belonging to 'a' (this is implemented elsewhere) */
131 void STUFF_cleanup_doall(STUFF *a);
132 /* Implement a prototype-compatible wrapper for "STUFF_cleanup" */
133 IMPLEMENT_LHASH_DOALL_FN(STUFF_cleanup, STUFF)
134 /* ... then later in the code ... */
135 /* So to run "STUFF_cleanup" against all items in a hash table ... */
136 lh_STUFF_doall(hashtable, LHASH_DOALL_FN(STUFF_cleanup));
137 /* Then the hash table itself can be deallocated */
138 lh_STUFF_free(hashtable);
139
140 When doing this, be careful if you delete entries from the hash table
141 in your callbacks: the table may decrease in size, moving the item that
142 you are currently on down lower in the hash table - this could cause
143 some entries to be skipped during the iteration. The second best
144 solution to this problem is to set hash->down_load=0 before you start
145 (which will stop the hash table ever decreasing in size). The best
146 solution is probably to avoid deleting items from the hash table inside
147 a "doall" callback!
148
149 lh_<type>_doall_arg() is the same as lh_<type>_doall() except that func
150 will be called with arg as the second argument and func should be of
151 type LHASH_DOALL_ARG_FN_TYPE (a callback prototype that is passed both
152 the table entry and an extra argument). As with lh_doall(), you can
153 instead choose to declare your callback with a prototype matching the
154 types you are dealing with and use the declare/implement macros to
155 create compatible wrappers that cast variables before calling your
156 type-specific callbacks. An example of this is demonstrated here
157 (printing all hash table entries to a BIO that is provided by the
158 caller):
159
160 /* Prints item 'a' to 'output_bio' (this is implemented elsewhere) */
161 void STUFF_print_doall_arg(const STUFF *a, BIO *output_bio);
162 /* Implement a prototype-compatible wrapper for "STUFF_print" */
163 static IMPLEMENT_LHASH_DOALL_ARG_FN(STUFF, const STUFF, BIO)
164 /* ... then later in the code ... */
165 /* Print out the entire hashtable to a particular BIO */
166 lh_STUFF_doall_arg(hashtable, LHASH_DOALL_ARG_FN(STUFF_print), BIO,
167 logging_bio);
168
169 lh_<type>_error() can be used to determine if an error occurred in the
170 last operation. lh_<type>_error() is a macro.
171
173 lh_<type>_new() returns NULL on error, otherwise a pointer to the new
174 LHASH structure.
175
176 When a hash table entry is replaced, lh_<type>_insert() returns the
177 value being replaced. NULL is returned on normal operation and on
178 error.
179
180 lh_<type>_delete() returns the entry being deleted. NULL is returned
181 if there is no such value in the hash table.
182
183 lh_<type>_retrieve() returns the hash table entry if it has been found,
184 NULL otherwise.
185
186 lh_<type>_error() returns 1 if an error occurred in the last operation,
187 0 otherwise.
188
189 lh_<type>_free(), lh_<type>_doall() and lh_<type>_doall_arg() return no
190 values.
191
193 The various LHASH macros and callback types exist to make it possible
194 to write type-checked code without resorting to function-prototype
195 casting - an evil that makes application code much harder to
196 audit/verify and also opens the window of opportunity for stack
197 corruption and other hard-to-find bugs. It also, apparently, violates
198 ANSI-C.
199
200 The LHASH code regards table entries as constant data. As such, it
201 internally represents lh_insert()'d items with a "const void *" pointer
202 type. This is why callbacks such as those used by lh_doall() and
203 lh_doall_arg() declare their prototypes with "const", even for the
204 parameters that pass back the table items' data pointers - for
205 consistency, user-provided data is "const" at all times as far as the
206 LHASH code is concerned. However, as callers are themselves providing
207 these pointers, they can choose whether they too should be treating all
208 such parameters as constant.
209
210 As an example, a hash table may be maintained by code that, for reasons
211 of encapsulation, has only "const" access to the data being indexed in
212 the hash table (ie. it is returned as "const" from elsewhere in their
213 code) - in this case the LHASH prototypes are appropriate as-is.
214 Conversely, if the caller is responsible for the life-time of the data
215 in question, then they may well wish to make modifications to table
216 item passed back in the lh_doall() or lh_doall_arg() callbacks (see the
217 "STUFF_cleanup" example above). If so, the caller can either cast the
218 "const" away (if they're providing the raw callbacks themselves) or use
219 the macros to declare/implement the wrapper functions without "const"
220 types.
221
222 Callers that only have "const" access to data they're indexing in a
223 table, yet declare callbacks without constant types (or cast the
224 "const" away themselves), are therefore creating their own risks/bugs
225 without being encouraged to do so by the API. On a related note, those
226 auditing code should pay special attention to any instances of
227 DECLARE/IMPLEMENT_LHASH_DOALL_[ARG_]_FN macros that provide types
228 without any "const" qualifiers.
229
231 lh_<type>_insert() returns NULL both for success and error.
232
234 The following description is based on the SSLeay documentation:
235
236 The lhash library implements a hash table described in the
237 Communications of the ACM in 1991. What makes this hash table
238 different is that as the table fills, the hash table is increased (or
239 decreased) in size via OPENSSL_realloc(). When a 'resize' is done,
240 instead of all hashes being redistributed over twice as many 'buckets',
241 one bucket is split. So when an 'expand' is done, there is only a
242 minimal cost to redistribute some values. Subsequent inserts will
243 cause more single 'bucket' redistributions but there will never be a
244 sudden large cost due to redistributing all the 'buckets'.
245
246 The state for a particular hash table is kept in the LHASH structure.
247 The decision to increase or decrease the hash table size is made
248 depending on the 'load' of the hash table. The load is the number of
249 items in the hash table divided by the size of the hash table. The
250 default values are as follows. If (hash->up_load < load) => expand.
251 if (hash->down_load > load) => contract. The up_load has a default
252 value of 1 and down_load has a default value of 2. These numbers can
253 be modified by the application by just playing with the up_load and
254 down_load variables. The 'load' is kept in a form which is multiplied
255 by 256. So hash->up_load=8*256; will cause a load of 8 to be set.
256
257 If you are interested in performance the field to watch is
258 num_comp_calls. The hash library keeps track of the 'hash' value for
259 each item so when a lookup is done, the 'hashes' are compared, if there
260 is a match, then a full compare is done, and hash->num_comp_calls is
261 incremented. If num_comp_calls is not equal to num_delete plus
262 num_retrieve it means that your hash function is generating hashes that
263 are the same for different values. It is probably worth changing your
264 hash function if this is the case because even if your hash table has
265 10 items in a 'bucket', it can be searched with 10 unsigned long
266 compares and 10 linked list traverses. This will be much less
267 expensive that 10 calls to your compare function.
268
269 lh_strhash() is a demo string hashing function:
270
271 unsigned long lh_strhash(const char *c);
272
273 Since the LHASH routines would normally be passed structures, this
274 routine would not normally be passed to lh_<type>_new(), rather it
275 would be used in the function passed to lh_<type>_new().
276
278 lh_stats(3)
279
281 The lhash library is available in all versions of SSLeay and OpenSSL.
282 lh_error() was added in SSLeay 0.9.1b.
283
284 This manpage is derived from the SSLeay documentation.
285
286 In OpenSSL 0.9.7, all lhash functions that were passed function
287 pointers were changed for better type safety, and the function types
288 LHASH_COMP_FN_TYPE, LHASH_HASH_FN_TYPE, LHASH_DOALL_FN_TYPE and
289 LHASH_DOALL_ARG_FN_TYPE became available.
290
291 In OpenSSL 1.0.0, the lhash interface was revamped for even better type
292 checking.
293
294
295
2961.0.2o 2018-03-27 lhash(3)