xref: /freebsd/crypto/openssl/doc/man3/OPENSSL_LH_COMPFUNC.pod (revision 517e52b6c21ccff22c46df0dcd15c19baee3d86c)
1=pod
2
3=head1 NAME
4
5LHASH, DECLARE_LHASH_OF,
6OPENSSL_LH_COMPFUNC, OPENSSL_LH_HASHFUNC, OPENSSL_LH_DOALL_FUNC,
7LHASH_DOALL_ARG_FN_TYPE,
8IMPLEMENT_LHASH_HASH_FN, IMPLEMENT_LHASH_COMP_FN,
9lh_TYPE_new, lh_TYPE_free,
10lh_TYPE_insert, lh_TYPE_delete, lh_TYPE_retrieve,
11lh_TYPE_doall, lh_TYPE_doall_arg, lh_TYPE_error - dynamic hash table
12
13=head1 SYNOPSIS
14
15=for comment generic
16
17 #include <openssl/lhash.h>
18
19 DECLARE_LHASH_OF(TYPE);
20
21 LHASH *lh_TYPE_new(OPENSSL_LH_HASHFUNC hash, OPENSSL_LH_COMPFUNC compare);
22 void lh_TYPE_free(LHASH_OF(TYPE) *table);
23
24 TYPE *lh_TYPE_insert(LHASH_OF(TYPE) *table, TYPE *data);
25 TYPE *lh_TYPE_delete(LHASH_OF(TYPE) *table, TYPE *data);
26 TYPE *lh_retrieve(LHASH_OF(TYPE) *table, TYPE *data);
27
28 void lh_TYPE_doall(LHASH_OF(TYPE) *table, OPENSSL_LH_DOALL_FUNC func);
29 void lh_TYPE_doall_arg(LHASH_OF(TYPE) *table, OPENSSL_LH_DOALL_FUNCARG func,
30                        TYPE *arg);
31
32 int lh_TYPE_error(LHASH_OF(TYPE) *table);
33
34 typedef int (*OPENSSL_LH_COMPFUNC)(const void *, const void *);
35 typedef unsigned long (*OPENSSL_LH_HASHFUNC)(const void *);
36 typedef void (*OPENSSL_LH_DOALL_FUNC)(const void *);
37 typedef void (*LHASH_DOALL_ARG_FN_TYPE)(const void *, const void *);
38
39=head1 DESCRIPTION
40
41This library implements type-checked dynamic hash tables. The hash
42table entries can be arbitrary structures. Usually they consist of key
43and value fields.  In the description here, I<TYPE> is used a placeholder
44for any of the OpenSSL datatypes, such as I<SSL_SESSION>.
45
46lh_TYPE_new() creates a new B<LHASH_OF(TYPE)> structure to store
47arbitrary data entries, and specifies the 'hash' and 'compare'
48callbacks to be used in organising the table's entries.  The B<hash>
49callback takes a pointer to a table entry as its argument and returns
50an unsigned long hash value for its key field.  The hash value is
51normally truncated to a power of 2, so make sure that your hash
52function returns well mixed low order bits.  The B<compare> callback
53takes two arguments (pointers to two hash table entries), and returns
540 if their keys are equal, nonzero otherwise.
55
56If your hash table
57will contain items of some particular type and the B<hash> and
58B<compare> callbacks hash/compare these types, then the
59B<IMPLEMENT_LHASH_HASH_FN> and B<IMPLEMENT_LHASH_COMP_FN> macros can be
60used to create callback wrappers of the prototypes required by
61lh_TYPE_new() as shown in this example:
62
63 /*
64  * Implement the hash and compare functions; "stuff" can be any word.
65  */
66 static unsigned long stuff_hash(const TYPE *a)
67 {
68     ...
69 }
70 static int stuff_cmp(const TYPE *a, const TYPE *b)
71 {
72     ...
73 }
74
75 /*
76  * Implement the wrapper functions.
77  */
78 static IMPLEMENT_LHASH_HASH_FN(stuff, TYPE)
79 static IMPLEMENT_LHASH_COMP_FN(stuff, TYPE)
80
81If the type is going to be used in several places, the following macros
82can be used in a common header file to declare the function wrappers:
83
84 DECLARE_LHASH_HASH_FN(stuff, TYPE)
85 DECLARE_LHASH_COMP_FN(stuff, TYPE)
86
87Then a hash table of TYPE objects can be created using this:
88
89 LHASH_OF(TYPE) *htable;
90
91 htable = lh_TYPE_new(LHASH_HASH_FN(stuff), LHASH_COMP_FN(stuff));
92
93lh_TYPE_free() frees the B<LHASH_OF(TYPE)> structure
94B<table>. Allocated hash table entries will not be freed; consider
95using lh_TYPE_doall() to deallocate any remaining entries in the
96hash table (see below).
97
98lh_TYPE_insert() inserts the structure pointed to by B<data> into
99B<table>.  If there already is an entry with the same key, the old
100value is replaced. Note that lh_TYPE_insert() stores pointers, the
101data are not copied.
102
103lh_TYPE_delete() deletes an entry from B<table>.
104
105lh_TYPE_retrieve() looks up an entry in B<table>. Normally, B<data>
106is a structure with the key field(s) set; the function will return a
107pointer to a fully populated structure.
108
109lh_TYPE_doall() will, for every entry in the hash table, call
110B<func> with the data item as its parameter.
111For example:
112
113 /* Cleans up resources belonging to 'a' (this is implemented elsewhere) */
114 void TYPE_cleanup_doall(TYPE *a);
115
116 /* Implement a prototype-compatible wrapper for "TYPE_cleanup" */
117 IMPLEMENT_LHASH_DOALL_FN(TYPE_cleanup, TYPE)
118
119 /* Call "TYPE_cleanup" against all items in a hash table. */
120 lh_TYPE_doall(hashtable, LHASH_DOALL_FN(TYPE_cleanup));
121
122 /* Then the hash table itself can be deallocated */
123 lh_TYPE_free(hashtable);
124
125When doing this, be careful if you delete entries from the hash table
126in your callbacks: the table may decrease in size, moving the item
127that you are currently on down lower in the hash table - this could
128cause some entries to be skipped during the iteration.  The second
129best solution to this problem is to set hash-E<gt>down_load=0 before
130you start (which will stop the hash table ever decreasing in size).
131The best solution is probably to avoid deleting items from the hash
132table inside a "doall" callback!
133
134lh_TYPE_doall_arg() is the same as lh_TYPE_doall() except that
135B<func> will be called with B<arg> as the second argument and B<func>
136should be of type B<LHASH_DOALL_ARG_FN_TYPE> (a callback prototype
137that is passed both the table entry and an extra argument).  As with
138lh_doall(), you can instead choose to declare your callback with a
139prototype matching the types you are dealing with and use the
140declare/implement macros to create compatible wrappers that cast
141variables before calling your type-specific callbacks.  An example of
142this is demonstrated here (printing all hash table entries to a BIO
143that is provided by the caller):
144
145 /* Prints item 'a' to 'output_bio' (this is implemented elsewhere) */
146 void TYPE_print_doall_arg(const TYPE *a, BIO *output_bio);
147
148 /* Implement a prototype-compatible wrapper for "TYPE_print" */
149 static IMPLEMENT_LHASH_DOALL_ARG_FN(TYPE, const TYPE, BIO)
150
151 /* Print out the entire hashtable to a particular BIO */
152 lh_TYPE_doall_arg(hashtable, LHASH_DOALL_ARG_FN(TYPE_print), BIO,
153                   logging_bio);
154
155
156lh_TYPE_error() can be used to determine if an error occurred in the last
157operation.
158
159=head1 RETURN VALUES
160
161lh_TYPE_new() returns B<NULL> on error, otherwise a pointer to the new
162B<LHASH> structure.
163
164When a hash table entry is replaced, lh_TYPE_insert() returns the value
165being replaced. B<NULL> is returned on normal operation and on error.
166
167lh_TYPE_delete() returns the entry being deleted.  B<NULL> is returned if
168there is no such value in the hash table.
169
170lh_TYPE_retrieve() returns the hash table entry if it has been found,
171B<NULL> otherwise.
172
173lh_TYPE_error() returns 1 if an error occurred in the last operation, 0
174otherwise. It's meaningful only after non-retrieve operations.
175
176lh_TYPE_free(), lh_TYPE_doall() and lh_TYPE_doall_arg() return no values.
177
178=head1 NOTE
179
180The LHASH code is not thread safe. All updating operations, as well as
181lh_TYPE_error call must be performed under a write lock. All retrieve
182operations should be performed under a read lock, I<unless> accurate
183usage statistics are desired. In which case, a write lock should be used
184for retrieve operations as well. For output of the usage statistics,
185using the functions from L<OPENSSL_LH_stats(3)>, a read lock suffices.
186
187The LHASH code regards table entries as constant data.  As such, it
188internally represents lh_insert()'d items with a "const void *"
189pointer type.  This is why callbacks such as those used by lh_doall()
190and lh_doall_arg() declare their prototypes with "const", even for the
191parameters that pass back the table items' data pointers - for
192consistency, user-provided data is "const" at all times as far as the
193LHASH code is concerned.  However, as callers are themselves providing
194these pointers, they can choose whether they too should be treating
195all such parameters as constant.
196
197As an example, a hash table may be maintained by code that, for
198reasons of encapsulation, has only "const" access to the data being
199indexed in the hash table (i.e. it is returned as "const" from
200elsewhere in their code) - in this case the LHASH prototypes are
201appropriate as-is.  Conversely, if the caller is responsible for the
202life-time of the data in question, then they may well wish to make
203modifications to table item passed back in the lh_doall() or
204lh_doall_arg() callbacks (see the "TYPE_cleanup" example above).  If
205so, the caller can either cast the "const" away (if they're providing
206the raw callbacks themselves) or use the macros to declare/implement
207the wrapper functions without "const" types.
208
209Callers that only have "const" access to data they're indexing in a
210table, yet declare callbacks without constant types (or cast the
211"const" away themselves), are therefore creating their own risks/bugs
212without being encouraged to do so by the API.  On a related note,
213those auditing code should pay special attention to any instances of
214DECLARE/IMPLEMENT_LHASH_DOALL_[ARG_]_FN macros that provide types
215without any "const" qualifiers.
216
217=head1 BUGS
218
219lh_TYPE_insert() returns B<NULL> both for success and error.
220
221=head1 SEE ALSO
222
223L<OPENSSL_LH_stats(3)>
224
225=head1 HISTORY
226
227In OpenSSL 1.0.0, the lhash interface was revamped for better
228type checking.
229
230=head1 COPYRIGHT
231
232Copyright 2000-2020 The OpenSSL Project Authors. All Rights Reserved.
233
234Licensed under the OpenSSL license (the "License").  You may not use
235this file except in compliance with the License.  You can obtain a copy
236in the file LICENSE in the source distribution or at
237L<https://www.openssl.org/source/license.html>.
238
239=cut
240