Blame doc/man3/OPENSSL_LH_COMPFUNC.pod

Packit c4476c
=pod
Packit c4476c
Packit c4476c
=head1 NAME
Packit c4476c
Packit c4476c
LHASH, DECLARE_LHASH_OF,
Packit c4476c
OPENSSL_LH_COMPFUNC, OPENSSL_LH_HASHFUNC, OPENSSL_LH_DOALL_FUNC,
Packit c4476c
LHASH_DOALL_ARG_FN_TYPE,
Packit c4476c
IMPLEMENT_LHASH_HASH_FN, IMPLEMENT_LHASH_COMP_FN,
Packit c4476c
lh_TYPE_new, lh_TYPE_free,
Packit c4476c
lh_TYPE_insert, lh_TYPE_delete, lh_TYPE_retrieve,
Packit c4476c
lh_TYPE_doall, lh_TYPE_doall_arg, lh_TYPE_error - dynamic hash table
Packit c4476c
Packit c4476c
=head1 SYNOPSIS
Packit c4476c
Packit c4476c
=for comment generic
Packit c4476c
Packit c4476c
 #include <openssl/lhash.h>
Packit c4476c
Packit c4476c
 DECLARE_LHASH_OF(TYPE);
Packit c4476c
Packit c4476c
 LHASH *lh_TYPE_new(OPENSSL_LH_HASHFUNC hash, OPENSSL_LH_COMPFUNC compare);
Packit c4476c
 void lh_TYPE_free(LHASH_OF(TYPE) *table);
Packit c4476c
Packit c4476c
 TYPE *lh_TYPE_insert(LHASH_OF(TYPE) *table, TYPE *data);
Packit c4476c
 TYPE *lh_TYPE_delete(LHASH_OF(TYPE) *table, TYPE *data);
Packit c4476c
 TYPE *lh_retrieve(LHASH_OF(TYPE) *table, TYPE *data);
Packit c4476c
Packit c4476c
 void lh_TYPE_doall(LHASH_OF(TYPE) *table, OPENSSL_LH_DOALL_FUNC func);
Packit c4476c
 void lh_TYPE_doall_arg(LHASH_OF(TYPE) *table, OPENSSL_LH_DOALL_FUNCARG func,
Packit c4476c
                        TYPE *arg);
Packit c4476c
Packit c4476c
 int lh_TYPE_error(LHASH_OF(TYPE) *table);
Packit c4476c
Packit c4476c
 typedef int (*OPENSSL_LH_COMPFUNC)(const void *, const void *);
Packit c4476c
 typedef unsigned long (*OPENSSL_LH_HASHFUNC)(const void *);
Packit c4476c
 typedef void (*OPENSSL_LH_DOALL_FUNC)(const void *);
Packit c4476c
 typedef void (*LHASH_DOALL_ARG_FN_TYPE)(const void *, const void *);
Packit c4476c
Packit c4476c
=head1 DESCRIPTION
Packit c4476c
Packit c4476c
This library implements type-checked dynamic hash tables. The hash
Packit c4476c
table entries can be arbitrary structures. Usually they consist of key
Packit c4476c
and value fields.  In the description here, I<TYPE> is used a placeholder
Packit c4476c
for any of the OpenSSL datatypes, such as I<SSL_SESSION>.
Packit c4476c
Packit c4476c
lh_TYPE_new() creates a new B<LHASH_OF(TYPE)> structure to store
Packit c4476c
arbitrary data entries, and specifies the 'hash' and 'compare'
Packit c4476c
callbacks to be used in organising the table's entries.  The B<hash>
Packit c4476c
callback takes a pointer to a table entry as its argument and returns
Packit c4476c
an unsigned long hash value for its key field.  The hash value is
Packit c4476c
normally truncated to a power of 2, so make sure that your hash
Packit c4476c
function returns well mixed low order bits.  The B<compare> callback
Packit c4476c
takes two arguments (pointers to two hash table entries), and returns
Packit c4476c
0 if their keys are equal, non-zero otherwise.
Packit c4476c
Packit c4476c
If your hash table
Packit c4476c
will contain items of some particular type and the B<hash> and
Packit c4476c
B<compare> callbacks hash/compare these types, then the
Packit c4476c
B<IMPLEMENT_LHASH_HASH_FN> and B<IMPLEMENT_LHASH_COMP_FN> macros can be
Packit c4476c
used to create callback wrappers of the prototypes required by
Packit c4476c
lh_TYPE_new() as shown in this example:
Packit c4476c
Packit c4476c
 /*
Packit c4476c
  * Implement the hash and compare functions; "stuff" can be any word.
Packit c4476c
  */
Packit c4476c
 static unsigned long stuff_hash(const TYPE *a)
Packit c4476c
 {
Packit c4476c
     ...
Packit c4476c
 }
Packit c4476c
 static int stuff_cmp(const TYPE *a, const TYPE *b)
Packit c4476c
 {
Packit c4476c
     ...
Packit c4476c
 }
Packit c4476c
Packit c4476c
 /*
Packit c4476c
  * Implement the wrapper functions.
Packit c4476c
  */
Packit c4476c
 static IMPLEMENT_LHASH_HASH_FN(stuff, TYPE)
Packit c4476c
 static IMPLEMENT_LHASH_COMP_FN(stuff, TYPE)
Packit c4476c
Packit c4476c
If the type is going to be used in several places, the following macros
Packit c4476c
can be used in a common header file to declare the function wrappers:
Packit c4476c
Packit c4476c
 DECLARE_LHASH_HASH_FN(stuff, TYPE)
Packit c4476c
 DECLARE_LHASH_COMP_FN(stuff, TYPE)
Packit c4476c
Packit c4476c
Then a hash table of TYPE objects can be created using this:
Packit c4476c
Packit c4476c
 LHASH_OF(TYPE) *htable;
Packit c4476c
Packit c4476c
 htable = lh_TYPE_new(LHASH_HASH_FN(stuff), LHASH_COMP_FN(stuff));
Packit c4476c
Packit c4476c
lh_TYPE_free() frees the B<LHASH_OF(TYPE)> structure
Packit c4476c
B. Allocated hash table entries will not be freed; consider
Packit c4476c
using lh_TYPE_doall() to deallocate any remaining entries in the
Packit c4476c
hash table (see below).
Packit c4476c
Packit c4476c
lh_TYPE_insert() inserts the structure pointed to by B<data> into
Packit c4476c
B.  If there already is an entry with the same key, the old
Packit c4476c
value is replaced. Note that lh_TYPE_insert() stores pointers, the
Packit c4476c
data are not copied.
Packit c4476c
Packit c4476c
lh_TYPE_delete() deletes an entry from B.
Packit c4476c
Packit c4476c
lh_TYPE_retrieve() looks up an entry in B. Normally, B<data>
Packit c4476c
is a structure with the key field(s) set; the function will return a
Packit c4476c
pointer to a fully populated structure.
Packit c4476c
Packit c4476c
lh_TYPE_doall() will, for every entry in the hash table, call
Packit c4476c
B<func> with the data item as its parameter.
Packit c4476c
For example:
Packit c4476c
Packit c4476c
 /* Cleans up resources belonging to 'a' (this is implemented elsewhere) */
Packit c4476c
 void TYPE_cleanup_doall(TYPE *a);
Packit c4476c
Packit c4476c
 /* Implement a prototype-compatible wrapper for "TYPE_cleanup" */
Packit c4476c
 IMPLEMENT_LHASH_DOALL_FN(TYPE_cleanup, TYPE)
Packit c4476c
Packit c4476c
 /* Call "TYPE_cleanup" against all items in a hash table. */
Packit c4476c
 lh_TYPE_doall(hashtable, LHASH_DOALL_FN(TYPE_cleanup));
Packit c4476c
Packit c4476c
 /* Then the hash table itself can be deallocated */
Packit c4476c
 lh_TYPE_free(hashtable);
Packit c4476c
Packit c4476c
When doing this, be careful if you delete entries from the hash table
Packit c4476c
in your callbacks: the table may decrease in size, moving the item
Packit c4476c
that you are currently on down lower in the hash table - this could
Packit c4476c
cause some entries to be skipped during the iteration.  The second
Packit c4476c
best solution to this problem is to set hash-E<gt>down_load=0 before
Packit c4476c
you start (which will stop the hash table ever decreasing in size).
Packit c4476c
The best solution is probably to avoid deleting items from the hash
Packit c4476c
table inside a "doall" callback!
Packit c4476c
Packit c4476c
lh_TYPE_doall_arg() is the same as lh_TYPE_doall() except that
Packit c4476c
B<func> will be called with B<arg> as the second argument and B<func>
Packit c4476c
should be of type B<LHASH_DOALL_ARG_FN_TYPE> (a callback prototype
Packit c4476c
that is passed both the table entry and an extra argument).  As with
Packit c4476c
lh_doall(), you can instead choose to declare your callback with a
Packit c4476c
prototype matching the types you are dealing with and use the
Packit c4476c
declare/implement macros to create compatible wrappers that cast
Packit c4476c
variables before calling your type-specific callbacks.  An example of
Packit c4476c
this is demonstrated here (printing all hash table entries to a BIO
Packit c4476c
that is provided by the caller):
Packit c4476c
Packit c4476c
 /* Prints item 'a' to 'output_bio' (this is implemented elsewhere) */
Packit c4476c
 void TYPE_print_doall_arg(const TYPE *a, BIO *output_bio);
Packit c4476c
Packit c4476c
 /* Implement a prototype-compatible wrapper for "TYPE_print" */
Packit c4476c
 static IMPLEMENT_LHASH_DOALL_ARG_FN(TYPE, const TYPE, BIO)
Packit c4476c
Packit c4476c
 /* Print out the entire hashtable to a particular BIO */
Packit c4476c
 lh_TYPE_doall_arg(hashtable, LHASH_DOALL_ARG_FN(TYPE_print), BIO,
Packit c4476c
                   logging_bio);
Packit c4476c
Packit c4476c
Packit c4476c
lh_TYPE_error() can be used to determine if an error occurred in the last
Packit c4476c
operation.
Packit c4476c
Packit c4476c
=head1 RETURN VALUES
Packit c4476c
Packit c4476c
lh_TYPE_new() returns B<NULL> on error, otherwise a pointer to the new
Packit c4476c
B<LHASH> structure.
Packit c4476c
Packit c4476c
When a hash table entry is replaced, lh_TYPE_insert() returns the value
Packit c4476c
being replaced. B<NULL> is returned on normal operation and on error.
Packit c4476c
Packit c4476c
lh_TYPE_delete() returns the entry being deleted.  B<NULL> is returned if
Packit c4476c
there is no such value in the hash table.
Packit c4476c
Packit c4476c
lh_TYPE_retrieve() returns the hash table entry if it has been found,
Packit c4476c
B<NULL> otherwise.
Packit c4476c
Packit c4476c
lh_TYPE_error() returns 1 if an error occurred in the last operation, 0
Packit c4476c
otherwise. It's meaningful only after non-retrieve operations.
Packit c4476c
Packit c4476c
lh_TYPE_free(), lh_TYPE_doall() and lh_TYPE_doall_arg() return no values.
Packit c4476c
Packit c4476c
=head1 NOTE
Packit c4476c
Packit c4476c
The LHASH code is not thread safe. All updating operations, as well as
Packit c4476c
lh_TYPE_error call must be performed under a write lock. All retrieve
Packit c4476c
operations should be performed under a read lock, I<unless> accurate
Packit c4476c
usage statistics are desired. In which case, a write lock should be used
Packit c4476c
for retrieve operations as well. For output of the usage statistics,
Packit c4476c
using the functions from L<OPENSSL_LH_stats(3)>, a read lock suffices.
Packit c4476c
Packit c4476c
The LHASH code regards table entries as constant data.  As such, it
Packit c4476c
internally represents lh_insert()'d items with a "const void *"
Packit c4476c
pointer type.  This is why callbacks such as those used by lh_doall()
Packit c4476c
and lh_doall_arg() declare their prototypes with "const", even for the
Packit c4476c
parameters that pass back the table items' data pointers - for
Packit c4476c
consistency, user-provided data is "const" at all times as far as the
Packit c4476c
LHASH code is concerned.  However, as callers are themselves providing
Packit c4476c
these pointers, they can choose whether they too should be treating
Packit c4476c
all such parameters as constant.
Packit c4476c
Packit c4476c
As an example, a hash table may be maintained by code that, for
Packit c4476c
reasons of encapsulation, has only "const" access to the data being
Packit c4476c
indexed in the hash table (ie. it is returned as "const" from
Packit c4476c
elsewhere in their code) - in this case the LHASH prototypes are
Packit c4476c
appropriate as-is.  Conversely, if the caller is responsible for the
Packit c4476c
life-time of the data in question, then they may well wish to make
Packit c4476c
modifications to table item passed back in the lh_doall() or
Packit c4476c
lh_doall_arg() callbacks (see the "TYPE_cleanup" example above).  If
Packit c4476c
so, the caller can either cast the "const" away (if they're providing
Packit c4476c
the raw callbacks themselves) or use the macros to declare/implement
Packit c4476c
the wrapper functions without "const" types.
Packit c4476c
Packit c4476c
Callers that only have "const" access to data they're indexing in a
Packit c4476c
table, yet declare callbacks without constant types (or cast the
Packit c4476c
"const" away themselves), are therefore creating their own risks/bugs
Packit c4476c
without being encouraged to do so by the API.  On a related note,
Packit c4476c
those auditing code should pay special attention to any instances of
Packit c4476c
DECLARE/IMPLEMENT_LHASH_DOALL_[ARG_]_FN macros that provide types
Packit c4476c
without any "const" qualifiers.
Packit c4476c
Packit c4476c
=head1 BUGS
Packit c4476c
Packit c4476c
lh_TYPE_insert() returns B<NULL> both for success and error.
Packit c4476c
Packit c4476c
=head1 SEE ALSO
Packit c4476c
Packit c4476c
L<OPENSSL_LH_stats(3)>
Packit c4476c
Packit c4476c
=head1 HISTORY
Packit c4476c
Packit c4476c
In OpenSSL 1.0.0, the lhash interface was revamped for better
Packit c4476c
type checking.
Packit c4476c
Packit c4476c
=head1 COPYRIGHT
Packit c4476c
Packit c4476c
Copyright 2000-2018 The OpenSSL Project Authors. All Rights Reserved.
Packit c4476c
Packit c4476c
Licensed under the OpenSSL license (the "License").  You may not use
Packit c4476c
this file except in compliance with the License.  You can obtain a copy
Packit c4476c
in the file LICENSE in the source distribution or at
Packit c4476c
L<https://www.openssl.org/source/license.html>.
Packit c4476c
Packit c4476c
=cut