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