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