VirtualBox

source: vbox/trunk/src/libs/openssl-3.1.7/doc/man3/OPENSSL_LH_COMPFUNC.pod@ 107278

最後變更 在這個檔案從107278是 104078,由 vboxsync 提交於 11 月 前

openssl-3.1.5: Applied and adjusted our OpenSSL changes to 3.1.4. bugref:10638

檔案大小: 13.4 KB
 
1=pod
2
3=head1 NAME
4
5LHASH, LHASH_OF, DEFINE_LHASH_OF_EX, DEFINE_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, lh_TYPE_flush,
10lh_TYPE_insert, lh_TYPE_delete, lh_TYPE_retrieve,
11lh_TYPE_doall, lh_TYPE_doall_arg, lh_TYPE_num_items, lh_TYPE_get_down_load,
12lh_TYPE_set_down_load, lh_TYPE_error,
13OPENSSL_LH_new, OPENSSL_LH_free, OPENSSL_LH_flush,
14OPENSSL_LH_insert, OPENSSL_LH_delete, OPENSSL_LH_retrieve,
15OPENSSL_LH_doall, OPENSSL_LH_doall_arg, OPENSSL_LH_num_items,
16OPENSSL_LH_get_down_load, OPENSSL_LH_set_down_load, OPENSSL_LH_error
17- dynamic hash table
18
19=head1 SYNOPSIS
20
21=for openssl generic
22
23 #include <openssl/lhash.h>
24
25 LHASH_OF(TYPE)
26
27 DEFINE_LHASH_OF_EX(TYPE);
28
29 LHASH_OF(TYPE) *lh_TYPE_new(OPENSSL_LH_HASHFUNC hash, OPENSSL_LH_COMPFUNC compare);
30 void lh_TYPE_free(LHASH_OF(TYPE) *table);
31 void lh_TYPE_flush(LHASH_OF(TYPE) *table);
32
33 TYPE *lh_TYPE_insert(LHASH_OF(TYPE) *table, TYPE *data);
34 TYPE *lh_TYPE_delete(LHASH_OF(TYPE) *table, TYPE *data);
35 TYPE *lh_TYPE_retrieve(LHASH_OF(TYPE) *table, TYPE *data);
36
37 void lh_TYPE_doall(LHASH_OF(TYPE) *table, OPENSSL_LH_DOALL_FUNC func);
38 void lh_TYPE_doall_arg(LHASH_OF(TYPE) *table, OPENSSL_LH_DOALL_FUNCARG func,
39 TYPE *arg);
40
41 unsigned long lh_TYPE_num_items(OPENSSL_LHASH *lh);
42 unsigned long lh_TYPE_get_down_load(OPENSSL_LHASH *lh);
43 void lh_TYPE_set_down_load(OPENSSL_LHASH *lh, unsigned long dl);
44
45 int lh_TYPE_error(LHASH_OF(TYPE) *table);
46
47 typedef int (*OPENSSL_LH_COMPFUNC)(const void *, const void *);
48 typedef unsigned long (*OPENSSL_LH_HASHFUNC)(const void *);
49 typedef void (*OPENSSL_LH_DOALL_FUNC)(const void *);
50 typedef void (*LHASH_DOALL_ARG_FN_TYPE)(const void *, const void *);
51
52 OPENSSL_LHASH *OPENSSL_LH_new(OPENSSL_LH_HASHFUNC h, OPENSSL_LH_COMPFUNC c);
53 void OPENSSL_LH_free(OPENSSL_LHASH *lh);
54 void OPENSSL_LH_flush(OPENSSL_LHASH *lh);
55
56 void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data);
57 void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data);
58 void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data);
59
60 void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func);
61 void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg);
62
63 unsigned long OPENSSL_LH_num_items(OPENSSL_LHASH *lh);
64 unsigned long OPENSSL_LH_get_down_load(OPENSSL_LHASH *lh);
65 void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long dl);
66
67 int OPENSSL_LH_error(OPENSSL_LHASH *lh);
68
69 #define LH_LOAD_MULT /* integer constant */
70
71The following macro is deprecated:
72
73 DEFINE_LHASH_OF(TYPE);
74
75=head1 DESCRIPTION
76
77This library implements type-checked dynamic hash tables. The hash
78table entries can be arbitrary structures. Usually they consist of key
79and value fields. In the description here, B<I<TYPE>> is used a placeholder
80for any of the OpenSSL datatypes, such as I<SSL_SESSION>.
81
82To define a new type-checked dynamic hash table, use B<DEFINE_LHASH_OF_EX>().
83B<DEFINE_LHASH_OF>() was previously used for this purpose, but is now
84deprecated. The B<DEFINE_LHASH_OF_EX>() macro provides all functionality of
85B<DEFINE_LHASH_OF>() except for certain deprecated statistics functions (see
86OPENSSL_LH_stats(3)).
87
88B<lh_I<TYPE>_new>() creates a new B<LHASH_OF>(B<I<TYPE>>) structure to store
89arbitrary data entries, and specifies the 'hash' and 'compare'
90callbacks to be used in organising the table's entries. The I<hash>
91callback takes a pointer to a table entry as its argument and returns
92an unsigned long hash value for its key field. The hash value is
93normally truncated to a power of 2, so make sure that your hash
94function returns well mixed low order bits. The I<compare> callback
95takes two arguments (pointers to two hash table entries), and returns
960 if their keys are equal, nonzero otherwise.
97
98If your hash table
99will contain items of some particular type and the I<hash> and
100I<compare> callbacks hash/compare these types, then the
101B<IMPLEMENT_LHASH_HASH_FN> and B<IMPLEMENT_LHASH_COMP_FN> macros can be
102used to create callback wrappers of the prototypes required by
103B<lh_I<TYPE>_new>() as shown in this example:
104
105 /*
106 * Implement the hash and compare functions; "stuff" can be any word.
107 */
108 static unsigned long stuff_hash(const TYPE *a)
109 {
110 ...
111 }
112 static int stuff_cmp(const TYPE *a, const TYPE *b)
113 {
114 ...
115 }
116
117 /*
118 * Implement the wrapper functions.
119 */
120 static IMPLEMENT_LHASH_HASH_FN(stuff, TYPE)
121 static IMPLEMENT_LHASH_COMP_FN(stuff, TYPE)
122
123If the type is going to be used in several places, the following macros
124can be used in a common header file to declare the function wrappers:
125
126 DECLARE_LHASH_HASH_FN(stuff, TYPE)
127 DECLARE_LHASH_COMP_FN(stuff, TYPE)
128
129Then a hash table of B<I<TYPE>> objects can be created using this:
130
131 LHASH_OF(TYPE) *htable;
132
133 htable = B<lh_I<TYPE>_new>(LHASH_HASH_FN(stuff), LHASH_COMP_FN(stuff));
134
135B<lh_I<TYPE>_free>() frees the B<LHASH_OF>(B<I<TYPE>>) structure
136I<table>. Allocated hash table entries will not be freed; consider
137using B<lh_I<TYPE>_doall>() to deallocate any remaining entries in the
138hash table (see below).
139
140B<lh_I<TYPE>_flush>() empties the B<LHASH_OF>(B<I<TYPE>>) structure I<table>. New
141entries can be added to the flushed table. Allocated hash table entries
142will not be freed; consider using B<lh_I<TYPE>_doall>() to deallocate any
143remaining entries in the hash table (see below).
144
145B<lh_I<TYPE>_insert>() inserts the structure pointed to by I<data> into
146I<table>. If there already is an entry with the same key, the old
147value is replaced. Note that B<lh_I<TYPE>_insert>() stores pointers, the
148data are not copied.
149
150B<lh_I<TYPE>_delete>() deletes an entry from I<table>.
151
152B<lh_I<TYPE>_retrieve>() looks up an entry in I<table>. Normally, I<data>
153is a structure with the key field(s) set; the function will return a
154pointer to a fully populated structure.
155
156B<lh_I<TYPE>_doall>() will, for every entry in the hash table, call
157I<func> with the data item as its parameter.
158For example:
159
160 /* Cleans up resources belonging to 'a' (this is implemented elsewhere) */
161 void TYPE_cleanup_doall(TYPE *a);
162
163 /* Implement a prototype-compatible wrapper for "TYPE_cleanup" */
164 IMPLEMENT_LHASH_DOALL_FN(TYPE_cleanup, TYPE)
165
166 /* Call "TYPE_cleanup" against all items in a hash table. */
167 lh_TYPE_doall(hashtable, LHASH_DOALL_FN(TYPE_cleanup));
168
169 /* Then the hash table itself can be deallocated */
170 lh_TYPE_free(hashtable);
171
172B<lh_I<TYPE>_doall_arg>() is the same as B<lh_I<TYPE>_doall>() except that
173I<func> will be called with I<arg> as the second argument and I<func>
174should be of type B<LHASH_DOALL_ARG_FN>(B<I<TYPE>>) (a callback prototype
175that is passed both the table entry and an extra argument). As with
176lh_doall(), you can instead choose to declare your callback with a
177prototype matching the types you are dealing with and use the
178declare/implement macros to create compatible wrappers that cast
179variables before calling your type-specific callbacks. An example of
180this is demonstrated here (printing all hash table entries to a BIO
181that is provided by the caller):
182
183 /* Prints item 'a' to 'output_bio' (this is implemented elsewhere) */
184 void TYPE_print_doall_arg(const TYPE *a, BIO *output_bio);
185
186 /* Implement a prototype-compatible wrapper for "TYPE_print" */
187 static IMPLEMENT_LHASH_DOALL_ARG_FN(TYPE, const TYPE, BIO)
188
189 /* Print out the entire hashtable to a particular BIO */
190 lh_TYPE_doall_arg(hashtable, LHASH_DOALL_ARG_FN(TYPE_print), BIO,
191 logging_bio);
192
193Note that it is by default B<not> safe to use B<lh_I<TYPE>_delete>() inside a
194callback passed to B<lh_I<TYPE>_doall>() or B<lh_I<TYPE>_doall_arg>(). The
195reason for this is that deleting an item from the hash table may result in the
196hash table being contracted to a smaller size and rehashed.
197B<lh_I<TYPE>_doall>() and B<lh_I<TYPE>_doall_arg>() are unsafe and will exhibit
198undefined behaviour under these conditions, as these functions assume the hash
199table size and bucket pointers do not change during the call.
200
201If it is desired to use B<lh_I<TYPE>_doall>() or B<lh_I<TYPE>_doall_arg>() with
202B<lh_I<TYPE>_delete>(), it is essential that you call
203B<lh_I<TYPE>_set_down_load>() with a I<down_load> argument of 0 first. This
204disables hash table contraction and guarantees that it will be safe to delete
205items from a hash table during a call to B<lh_I<TYPE>_doall>() or
206B<lh_I<TYPE>_doall_arg>().
207
208It is never safe to call B<lh_I<TYPE>_insert>() during a call to
209B<lh_I<TYPE>_doall>() or B<lh_I<TYPE>_doall_arg>().
210
211B<lh_I<TYPE>_error>() can be used to determine if an error occurred in the last
212operation.
213
214B<lh_I<TYPE>_num_items>() returns the number of items in the hash table.
215
216B<lh_I<TYPE>_get_down_load>() and B<lh_I<TYPE>_set_down_load>() get and set the
217factor used to determine when the hash table is contracted. The factor is the
218load factor at or below which hash table contraction will occur, multiplied by
219B<LH_LOAD_MULT>, where the load factor is the number of items divided by the
220number of nodes. Setting this value to 0 disables hash table contraction.
221
222OPENSSL_LH_new() is the same as the B<lh_I<TYPE>_new>() except that it is not
223type specific. So instead of returning an B<LHASH_OF(I<TYPE>)> value it returns
224a B<void *>. In the same way the functions OPENSSL_LH_free(),
225OPENSSL_LH_flush(), OPENSSL_LH_insert(), OPENSSL_LH_delete(),
226OPENSSL_LH_retrieve(), OPENSSL_LH_doall(), OPENSSL_LH_doall_arg(),
227OPENSSL_LH_num_items(), OPENSSL_LH_get_down_load(), OPENSSL_LH_set_down_load()
228and OPENSSL_LH_error() are equivalent to the similarly named B<lh_I<TYPE>>
229functions except that they return or use a B<void *> where the equivalent
230B<lh_I<TYPE>> function returns or uses a B<I<TYPE> *> or B<LHASH_OF(I<TYPE>) *>.
231B<lh_I<TYPE>> functions are implemented as type checked wrappers around the
232B<OPENSSL_LH> functions. Most applications should not call the B<OPENSSL_LH>
233functions directly.
234
235=head1 RETURN VALUES
236
237B<lh_I<TYPE>_new>() and OPENSSL_LH_new() return NULL on error, otherwise a
238pointer to the new B<LHASH> structure.
239
240When a hash table entry is replaced, B<lh_I<TYPE>_insert>() or
241OPENSSL_LH_insert() return the value being replaced. NULL is returned on normal
242operation and on error.
243
244B<lh_I<TYPE>_delete>() and OPENSSL_LH_delete() return the entry being deleted.
245NULL is returned if there is no such value in the hash table.
246
247B<lh_I<TYPE>_retrieve>() and OPENSSL_LH_retrieve() return the hash table entry
248if it has been found, NULL otherwise.
249
250B<lh_I<TYPE>_error>() and OPENSSL_LH_error() return 1 if an error occurred in
251the last operation, 0 otherwise. It's meaningful only after non-retrieve
252operations.
253
254B<lh_I<TYPE>_free>(), OPENSSL_LH_free(), B<lh_I<TYPE>_flush>(),
255OPENSSL_LH_flush(), B<lh_I<TYPE>_doall>() OPENSSL_LH_doall(),
256B<lh_I<TYPE>_doall_arg>() and OPENSSL_LH_doall_arg() return no values.
257
258=head1 NOTE
259
260The LHASH code is not thread safe. All updating operations, as well as
261B<lh_I<TYPE>_error>() or OPENSSL_LH_error() calls must be performed under
262a write lock. All retrieve operations should be performed under a read lock,
263I<unless> accurate usage statistics are desired. In which case, a write lock
264should be used for retrieve operations as well. For output of the usage
265statistics, using the functions from L<OPENSSL_LH_stats(3)>, a read lock
266suffices.
267
268The LHASH code regards table entries as constant data. As such, it
269internally represents lh_insert()'d items with a "const void *"
270pointer type. This is why callbacks such as those used by lh_doall()
271and lh_doall_arg() declare their prototypes with "const", even for the
272parameters that pass back the table items' data pointers - for
273consistency, user-provided data is "const" at all times as far as the
274LHASH code is concerned. However, as callers are themselves providing
275these pointers, they can choose whether they too should be treating
276all such parameters as constant.
277
278As an example, a hash table may be maintained by code that, for
279reasons of encapsulation, has only "const" access to the data being
280indexed in the hash table (i.e. it is returned as "const" from
281elsewhere in their code) - in this case the LHASH prototypes are
282appropriate as-is. Conversely, if the caller is responsible for the
283life-time of the data in question, then they may well wish to make
284modifications to table item passed back in the lh_doall() or
285lh_doall_arg() callbacks (see the "TYPE_cleanup" example above). If
286so, the caller can either cast the "const" away (if they're providing
287the raw callbacks themselves) or use the macros to declare/implement
288the wrapper functions without "const" types.
289
290Callers that only have "const" access to data they're indexing in a
291table, yet declare callbacks without constant types (or cast the
292"const" away themselves), are therefore creating their own risks/bugs
293without being encouraged to do so by the API. On a related note,
294those auditing code should pay special attention to any instances of
295DECLARE/IMPLEMENT_LHASH_DOALL_[ARG_]_FN macros that provide types
296without any "const" qualifiers.
297
298=head1 BUGS
299
300B<lh_I<TYPE>_insert>() and OPENSSL_LH_insert() return NULL both for success
301and error.
302
303=head1 SEE ALSO
304
305L<OPENSSL_LH_stats(3)>
306
307=head1 HISTORY
308
309In OpenSSL 1.0.0, the lhash interface was revamped for better
310type checking.
311
312In OpenSSL 3.1, B<DEFINE_LHASH_OF_EX>() was introduced and B<DEFINE_LHASH_OF>()
313was deprecated.
314
315=head1 COPYRIGHT
316
317Copyright 2000-2022 The OpenSSL Project Authors. All Rights Reserved.
318
319Licensed under the Apache License 2.0 (the "License"). You may not use
320this file except in compliance with the License. You can obtain a copy
321in the file LICENSE in the source distribution or at
322L<https://www.openssl.org/source/license.html>.
323
324=cut
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette