1OPENSSL_LH_COMPFUNC(3ossl)          OpenSSL         OPENSSL_LH_COMPFUNC(3ossl)
2
3
4

NAME

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_flush, lh_TYPE_insert, lh_TYPE_delete,
10       lh_TYPE_retrieve, lh_TYPE_doall, lh_TYPE_doall_arg, lh_TYPE_error,
11       OPENSSL_LH_new, OPENSSL_LH_free,  OPENSSL_LH_flush, OPENSSL_LH_insert,
12       OPENSSL_LH_delete, OPENSSL_LH_retrieve, OPENSSL_LH_doall,
13       OPENSSL_LH_doall_arg, OPENSSL_LH_error - dynamic hash table
14

SYNOPSIS

16        #include <openssl/lhash.h>
17
18        DECLARE_LHASH_OF(TYPE);
19
20        LHASH_OF(TYPE) *lh_TYPE_new(OPENSSL_LH_HASHFUNC hash, OPENSSL_LH_COMPFUNC compare);
21        void lh_TYPE_free(LHASH_OF(TYPE) *table);
22        void lh_TYPE_flush(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        OPENSSL_LHASH *OPENSSL_LH_new(OPENSSL_LH_HASHFUNC h, OPENSSL_LH_COMPFUNC c);
40        void OPENSSL_LH_free(OPENSSL_LHASH *lh);
41        void OPENSSL_LH_flush(OPENSSL_LHASH *lh);
42
43        void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data);
44        void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data);
45        void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data);
46
47        void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func);
48        void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg);
49
50        int OPENSSL_LH_error(OPENSSL_LHASH *lh);
51

DESCRIPTION

53       This library implements type-checked dynamic hash tables. The hash
54       table entries can be arbitrary structures. Usually they consist of key
55       and value fields.  In the description here, TYPE is used a placeholder
56       for any of the OpenSSL datatypes, such as SSL_SESSION.
57
58       lh_TYPE_new() creates a new LHASH_OF(TYPE) structure to store arbitrary
59       data entries, and specifies the 'hash' and 'compare' callbacks to be
60       used in organising the table's entries.  The hash callback takes a
61       pointer to a table entry as its argument and returns an unsigned long
62       hash value for its key field.  The hash value is normally truncated to
63       a power of 2, so make sure that your hash function returns well mixed
64       low order bits.  The compare callback takes two arguments (pointers to
65       two hash table entries), and returns 0 if their keys are equal, nonzero
66       otherwise.
67
68       If your hash table will contain items of some particular type and the
69       hash and compare callbacks hash/compare these types, then the
70       IMPLEMENT_LHASH_HASH_FN and IMPLEMENT_LHASH_COMP_FN macros can be used
71       to create callback wrappers of the prototypes required by lh_TYPE_new()
72       as shown in this example:
73
74        /*
75         * Implement the hash and compare functions; "stuff" can be any word.
76         */
77        static unsigned long stuff_hash(const TYPE *a)
78        {
79            ...
80        }
81        static int stuff_cmp(const TYPE *a, const TYPE *b)
82        {
83            ...
84        }
85
86        /*
87         * Implement the wrapper functions.
88         */
89        static IMPLEMENT_LHASH_HASH_FN(stuff, TYPE)
90        static IMPLEMENT_LHASH_COMP_FN(stuff, TYPE)
91
92       If the type is going to be used in several places, the following macros
93       can be used in a common header file to declare the function wrappers:
94
95        DECLARE_LHASH_HASH_FN(stuff, TYPE)
96        DECLARE_LHASH_COMP_FN(stuff, TYPE)
97
98       Then a hash table of TYPE objects can be created using this:
99
100        LHASH_OF(TYPE) *htable;
101
102        htable = B<lh_I<TYPE>_new>(LHASH_HASH_FN(stuff), LHASH_COMP_FN(stuff));
103
104       lh_TYPE_free() frees the LHASH_OF(TYPE) structure table. Allocated hash
105       table entries will not be freed; consider using lh_TYPE_doall() to
106       deallocate any remaining entries in the hash table (see below).
107
108       lh_TYPE_flush() empties the LHASH_OF(TYPE) structure table. New entries
109       can be added to the flushed table.  Allocated hash table entries will
110       not be freed; consider using lh_TYPE_doall() to deallocate any
111       remaining entries in the hash table (see below).
112
113       lh_TYPE_insert() inserts the structure pointed to by data into table.
114       If there already is an entry with the same key, the old value is
115       replaced. Note that lh_TYPE_insert() stores pointers, the data are not
116       copied.
117
118       lh_TYPE_delete() deletes an entry from table.
119
120       lh_TYPE_retrieve() looks up an entry in table. Normally, data is a
121       structure with the key field(s) set; the function will return a pointer
122       to a fully populated structure.
123
124       lh_TYPE_doall() will, for every entry in the hash table, call func with
125       the data item as its parameter.  For example:
126
127        /* Cleans up resources belonging to 'a' (this is implemented elsewhere) */
128        void TYPE_cleanup_doall(TYPE *a);
129
130        /* Implement a prototype-compatible wrapper for "TYPE_cleanup" */
131        IMPLEMENT_LHASH_DOALL_FN(TYPE_cleanup, TYPE)
132
133        /* Call "TYPE_cleanup" against all items in a hash table. */
134        lh_TYPE_doall(hashtable, LHASH_DOALL_FN(TYPE_cleanup));
135
136        /* Then the hash table itself can be deallocated */
137        lh_TYPE_free(hashtable);
138
139       When doing this, be careful if you delete entries from the hash table
140       in your callbacks: the table may decrease in size, moving the item that
141       you are currently on down lower in the hash table - this could cause
142       some entries to be skipped during the iteration.  The second best
143       solution to this problem is to set hash->down_load=0 before you start
144       (which will stop the hash table ever decreasing in size).  The best
145       solution is probably to avoid deleting items from the hash table inside
146       a "doall" callback!
147
148       lh_TYPE_doall_arg() is the same as lh_TYPE_doall() except that func
149       will be called with arg as the second argument and func should be of
150       type LHASH_DOALL_ARG_FN(TYPE) (a callback prototype that is passed both
151       the table entry and an extra argument).  As with lh_doall(), you can
152       instead choose to declare your callback with a prototype matching the
153       types you are dealing with and use the declare/implement macros to
154       create compatible wrappers that cast variables before calling your
155       type-specific callbacks.  An example of this is demonstrated here
156       (printing all hash table entries to a BIO that is provided by the
157       caller):
158
159        /* Prints item 'a' to 'output_bio' (this is implemented elsewhere) */
160        void TYPE_print_doall_arg(const TYPE *a, BIO *output_bio);
161
162        /* Implement a prototype-compatible wrapper for "TYPE_print" */
163        static IMPLEMENT_LHASH_DOALL_ARG_FN(TYPE, const TYPE, BIO)
164
165        /* Print out the entire hashtable to a particular BIO */
166        lh_TYPE_doall_arg(hashtable, LHASH_DOALL_ARG_FN(TYPE_print), BIO,
167                          logging_bio);
168
169       lh_TYPE_error() can be used to determine if an error occurred in the
170       last operation.
171
172       OPENSSL_LH_new() is the same as the lh_TYPE_new() except that it is not
173       type specific. So instead of returning an LHASH_OF(TYPE) value it
174       returns a void *. In the same way the functions OPENSSL_LH_free(),
175       OPENSSL_LH_flush(), OPENSSL_LH_insert(), OPENSSL_LH_delete(),
176       OPENSSL_LH_retrieve(), OPENSSL_LH_doall(), OPENSSL_LH_doall_arg(), and
177       OPENSSL_LH_error() are equivalent to the similarly named lh_TYPE
178       functions except that they return or use a void * where the equivalent
179       lh_TYPE function returns or uses a TYPE * or LHASH_OF(TYPE) *. lh_TYPE
180       functions are implemented as type checked wrappers around the
181       OPENSSL_LH functions. Most applications should not call the OPENSSL_LH
182       functions directly.
183

RETURN VALUES

185       lh_TYPE_new() and OPENSSL_LH_new() return NULL on error, otherwise a
186       pointer to the new LHASH structure.
187
188       When a hash table entry is replaced, lh_TYPE_insert() or
189       OPENSSL_LH_insert() return the value being replaced. NULL is returned
190       on normal operation and on error.
191
192       lh_TYPE_delete() and OPENSSL_LH_delete() return the entry being
193       deleted.  NULL is returned if there is no such value in the hash table.
194
195       lh_TYPE_retrieve() and OPENSSL_LH_retrieve() return the hash table
196       entry if it has been found, NULL otherwise.
197
198       lh_TYPE_error() and OPENSSL_LH_error() return 1 if an error occurred in
199       the last operation, 0 otherwise. It's meaningful only after non-
200       retrieve operations.
201
202       lh_TYPE_free(), OPENSSL_LH_free(), lh_TYPE_flush(), OPENSSL_LH_flush(),
203       lh_TYPE_doall() OPENSSL_LH_doall(), lh_TYPE_doall_arg() and
204       OPENSSL_LH_doall_arg() return no values.
205

NOTE

207       The LHASH code is not thread safe. All updating operations, as well as
208       lh_TYPE_error() or OPENSSL_LH_error() calls must be performed under a
209       write lock. All retrieve operations should be performed under a read
210       lock, unless accurate usage statistics are desired. In which case, a
211       write lock should be used for retrieve operations as well. For output
212       of the usage statistics, using the functions from OPENSSL_LH_stats(3),
213       a read lock suffices.
214
215       The LHASH code regards table entries as constant data.  As such, it
216       internally represents lh_insert()'d items with a "const void *" pointer
217       type.  This is why callbacks such as those used by lh_doall() and
218       lh_doall_arg() declare their prototypes with "const", even for the
219       parameters that pass back the table items' data pointers - for
220       consistency, user-provided data is "const" at all times as far as the
221       LHASH code is concerned.  However, as callers are themselves providing
222       these pointers, they can choose whether they too should be treating all
223       such parameters as constant.
224
225       As an example, a hash table may be maintained by code that, for reasons
226       of encapsulation, has only "const" access to the data being indexed in
227       the hash table (i.e. it is returned as "const" from elsewhere in their
228       code) - in this case the LHASH prototypes are appropriate as-is.
229       Conversely, if the caller is responsible for the life-time of the data
230       in question, then they may well wish to make modifications to table
231       item passed back in the lh_doall() or lh_doall_arg() callbacks (see the
232       "TYPE_cleanup" example above).  If so, the caller can either cast the
233       "const" away (if they're providing the raw callbacks themselves) or use
234       the macros to declare/implement the wrapper functions without "const"
235       types.
236
237       Callers that only have "const" access to data they're indexing in a
238       table, yet declare callbacks without constant types (or cast the
239       "const" away themselves), are therefore creating their own risks/bugs
240       without being encouraged to do so by the API.  On a related note, those
241       auditing code should pay special attention to any instances of
242       DECLARE/IMPLEMENT_LHASH_DOALL_[ARG_]_FN macros that provide types
243       without any "const" qualifiers.
244

BUGS

246       lh_TYPE_insert() and OPENSSL_LH_insert() return NULL both for success
247       and error.
248

SEE ALSO

250       OPENSSL_LH_stats(3)
251

HISTORY

253       In OpenSSL 1.0.0, the lhash interface was revamped for better type
254       checking.
255
257       Copyright 2000-2021 The OpenSSL Project Authors. All Rights Reserved.
258
259       Licensed under the Apache License 2.0 (the "License").  You may not use
260       this file except in compliance with the License.  You can obtain a copy
261       in the file LICENSE in the source distribution or at
262       <https://www.openssl.org/source/license.html>.
263
264
265
2663.0.5                             2022-07-05        OPENSSL_LH_COMPFUNC(3ossl)
Impressum