LTKCPP-- LLRP Toolkit C Plus Plus Library
lhash.h
00001 /* crypto/lhash/lhash.h */
00002 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
00003  * All rights reserved.
00004  *
00005  * This package is an SSL implementation written
00006  * by Eric Young (eay@cryptsoft.com).
00007  * The implementation was written so as to conform with Netscapes SSL.
00008  *
00009  * This library is free for commercial and non-commercial use as long as
00010  * the following conditions are aheared to.  The following conditions
00011  * apply to all code found in this distribution, be it the RC4, RSA,
00012  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
00013  * included with this distribution is covered by the same copyright terms
00014  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
00015  *
00016  * Copyright remains Eric Young's, and as such any Copyright notices in
00017  * the code are not to be removed.
00018  * If this package is used in a product, Eric Young should be given attribution
00019  * as the author of the parts of the library used.
00020  * This can be in the form of a textual message at program startup or
00021  * in documentation (online or textual) provided with the package.
00022  *
00023  * Redistribution and use in source and binary forms, with or without
00024  * modification, are permitted provided that the following conditions
00025  * are met:
00026  * 1. Redistributions of source code must retain the copyright
00027  *    notice, this list of conditions and the following disclaimer.
00028  * 2. Redistributions in binary form must reproduce the above copyright
00029  *    notice, this list of conditions and the following disclaimer in the
00030  *    documentation and/or other materials provided with the distribution.
00031  * 3. All advertising materials mentioning features or use of this software
00032  *    must display the following acknowledgement:
00033  *    "This product includes cryptographic software written by
00034  *     Eric Young (eay@cryptsoft.com)"
00035  *    The word 'cryptographic' can be left out if the rouines from the library
00036  *    being used are not cryptographic related :-).
00037  * 4. If you include any Windows specific code (or a derivative thereof) from
00038  *    the apps directory (application code) you must include an acknowledgement:
00039  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
00040  *
00041  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
00042  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00043  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00044  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
00045  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00046  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00047  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00048  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00049  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00050  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00051  * SUCH DAMAGE.
00052  *
00053  * The licence and distribution terms for any publically available version or
00054  * derivative of this code cannot be changed.  i.e. this code cannot simply be
00055  * copied and put under another distribution licence
00056  * [including the GNU Public Licence.]
00057  */
00058 
00059 /*
00060  * Header for dynamic hash table routines Author - Eric Young
00061  */
00062 
00063 #ifndef HEADER_LHASH_H
00064 # define HEADER_LHASH_H
00065 
00066 # include <openssl/e_os2.h>
00067 # ifndef OPENSSL_NO_FP_API
00068 #  include <stdio.h>
00069 # endif
00070 
00071 # ifndef OPENSSL_NO_BIO
00072 #  include <openssl/bio.h>
00073 # endif
00074 
00075 #ifdef  __cplusplus
00076 extern "C" {
00077 #endif
00078 
00079 typedef struct lhash_node_st {
00080     void *data;
00081     struct lhash_node_st *next;
00082 # ifndef OPENSSL_NO_HASH_COMP
00083     unsigned long hash;
00084 # endif
00085 } LHASH_NODE;
00086 
00087 typedef int (*LHASH_COMP_FN_TYPE) (const void *, const void *);
00088 typedef unsigned long (*LHASH_HASH_FN_TYPE) (const void *);
00089 typedef void (*LHASH_DOALL_FN_TYPE) (void *);
00090 typedef void (*LHASH_DOALL_ARG_FN_TYPE) (void *, void *);
00091 
00092 /*
00093  * Macros for declaring and implementing type-safe wrappers for LHASH
00094  * callbacks. This way, callbacks can be provided to LHASH structures without
00095  * function pointer casting and the macro-defined callbacks provide
00096  * per-variable casting before deferring to the underlying type-specific
00097  * callbacks. NB: It is possible to place a "static" in front of both the
00098  * DECLARE and IMPLEMENT macros if the functions are strictly internal.
00099  */
00100 
00101 /* First: "hash" functions */
00102 # define DECLARE_LHASH_HASH_FN(name, o_type) \
00103         unsigned long name##_LHASH_HASH(const void *);
00104 # define IMPLEMENT_LHASH_HASH_FN(name, o_type) \
00105         unsigned long name##_LHASH_HASH(const void *arg) { \
00106                 const o_type *a = arg; \
00107                 return name##_hash(a); }
00108 # define LHASH_HASH_FN(name) name##_LHASH_HASH
00109 
00110 /* Second: "compare" functions */
00111 # define DECLARE_LHASH_COMP_FN(name, o_type) \
00112         int name##_LHASH_COMP(const void *, const void *);
00113 # define IMPLEMENT_LHASH_COMP_FN(name, o_type) \
00114         int name##_LHASH_COMP(const void *arg1, const void *arg2) { \
00115                 const o_type *a = arg1;             \
00116                 const o_type *b = arg2; \
00117                 return name##_cmp(a,b); }
00118 # define LHASH_COMP_FN(name) name##_LHASH_COMP
00119 
00120 /* Third: "doall" functions */
00121 # define DECLARE_LHASH_DOALL_FN(name, o_type) \
00122         void name##_LHASH_DOALL(void *);
00123 # define IMPLEMENT_LHASH_DOALL_FN(name, o_type) \
00124         void name##_LHASH_DOALL(void *arg) { \
00125                 o_type *a = arg; \
00126                 name##_doall(a); }
00127 # define LHASH_DOALL_FN(name) name##_LHASH_DOALL
00128 
00129 /* Fourth: "doall_arg" functions */
00130 # define DECLARE_LHASH_DOALL_ARG_FN(name, o_type, a_type) \
00131         void name##_LHASH_DOALL_ARG(void *, void *);
00132 # define IMPLEMENT_LHASH_DOALL_ARG_FN(name, o_type, a_type) \
00133         void name##_LHASH_DOALL_ARG(void *arg1, void *arg2) { \
00134                 o_type *a = arg1; \
00135                 a_type *b = arg2; \
00136                 name##_doall_arg(a, b); }
00137 # define LHASH_DOALL_ARG_FN(name) name##_LHASH_DOALL_ARG
00138 
00139 typedef struct lhash_st {
00140     LHASH_NODE **b;
00141     LHASH_COMP_FN_TYPE comp;
00142     LHASH_HASH_FN_TYPE hash;
00143     unsigned int num_nodes;
00144     unsigned int num_alloc_nodes;
00145     unsigned int p;
00146     unsigned int pmax;
00147     unsigned long up_load;      /* load times 256 */
00148     unsigned long down_load;    /* load times 256 */
00149     unsigned long num_items;
00150     unsigned long num_expands;
00151     unsigned long num_expand_reallocs;
00152     unsigned long num_contracts;
00153     unsigned long num_contract_reallocs;
00154     unsigned long num_hash_calls;
00155     unsigned long num_comp_calls;
00156     unsigned long num_insert;
00157     unsigned long num_replace;
00158     unsigned long num_delete;
00159     unsigned long num_no_delete;
00160     unsigned long num_retrieve;
00161     unsigned long num_retrieve_miss;
00162     unsigned long num_hash_comps;
00163     int error;
00164 } _LHASH;                       /* Do not use _LHASH directly, use LHASH_OF
00165                                  * and friends */
00166 
00167 # define LH_LOAD_MULT    256
00168 
00169 /*
00170  * Indicates a malloc() error in the last call, this is only bad in
00171  * lh_insert().
00172  */
00173 # define lh_error(lh)    ((lh)->error)
00174 
00175 _LHASH *lh_new(LHASH_HASH_FN_TYPE h, LHASH_COMP_FN_TYPE c);
00176 void lh_free(_LHASH *lh);
00177 void *lh_insert(_LHASH *lh, void *data);
00178 void *lh_delete(_LHASH *lh, const void *data);
00179 void *lh_retrieve(_LHASH *lh, const void *data);
00180 void lh_doall(_LHASH *lh, LHASH_DOALL_FN_TYPE func);
00181 void lh_doall_arg(_LHASH *lh, LHASH_DOALL_ARG_FN_TYPE func, void *arg);
00182 unsigned long lh_strhash(const char *c);
00183 unsigned long lh_num_items(const _LHASH *lh);
00184 
00185 # ifndef OPENSSL_NO_FP_API
00186 void lh_stats(const _LHASH *lh, FILE *out);
00187 void lh_node_stats(const _LHASH *lh, FILE *out);
00188 void lh_node_usage_stats(const _LHASH *lh, FILE *out);
00189 # endif
00190 
00191 # ifndef OPENSSL_NO_BIO
00192 void lh_stats_bio(const _LHASH *lh, BIO *out);
00193 void lh_node_stats_bio(const _LHASH *lh, BIO *out);
00194 void lh_node_usage_stats_bio(const _LHASH *lh, BIO *out);
00195 # endif
00196 
00197 /* Type checking... */
00198 
00199 # define LHASH_OF(type) struct lhash_st_##type
00200 
00201 # define DECLARE_LHASH_OF(type) LHASH_OF(type) { int dummy; }
00202 
00203 # define CHECKED_LHASH_OF(type,lh) \
00204   ((_LHASH *)CHECKED_PTR_OF(LHASH_OF(type),lh))
00205 
00206 /* Define wrapper functions. */
00207 # define LHM_lh_new(type, name) \
00208   ((LHASH_OF(type) *)lh_new(LHASH_HASH_FN(name), LHASH_COMP_FN(name)))
00209 # define LHM_lh_error(type, lh) \
00210   lh_error(CHECKED_LHASH_OF(type,lh))
00211 # define LHM_lh_insert(type, lh, inst) \
00212   ((type *)lh_insert(CHECKED_LHASH_OF(type, lh), \
00213                      CHECKED_PTR_OF(type, inst)))
00214 # define LHM_lh_retrieve(type, lh, inst) \
00215   ((type *)lh_retrieve(CHECKED_LHASH_OF(type, lh), \
00216                        CHECKED_PTR_OF(type, inst)))
00217 # define LHM_lh_delete(type, lh, inst) \
00218   ((type *)lh_delete(CHECKED_LHASH_OF(type, lh),                        \
00219                      CHECKED_PTR_OF(type, inst)))
00220 # define LHM_lh_doall(type, lh,fn) lh_doall(CHECKED_LHASH_OF(type, lh), fn)
00221 # define LHM_lh_doall_arg(type, lh, fn, arg_type, arg) \
00222   lh_doall_arg(CHECKED_LHASH_OF(type, lh), fn, CHECKED_PTR_OF(arg_type, arg))
00223 # define LHM_lh_num_items(type, lh) lh_num_items(CHECKED_LHASH_OF(type, lh))
00224 # define LHM_lh_down_load(type, lh) (CHECKED_LHASH_OF(type, lh)->down_load)
00225 # define LHM_lh_node_stats_bio(type, lh, out) \
00226   lh_node_stats_bio(CHECKED_LHASH_OF(type, lh), out)
00227 # define LHM_lh_node_usage_stats_bio(type, lh, out) \
00228   lh_node_usage_stats_bio(CHECKED_LHASH_OF(type, lh), out)
00229 # define LHM_lh_stats_bio(type, lh, out) \
00230   lh_stats_bio(CHECKED_LHASH_OF(type, lh), out)
00231 # define LHM_lh_free(type, lh) lh_free(CHECKED_LHASH_OF(type, lh))
00232 
00233 DECLARE_LHASH_OF(OPENSSL_STRING);
00234 DECLARE_LHASH_OF(OPENSSL_CSTRING);
00235 
00236 #ifdef  __cplusplus
00237 }
00238 #endif
00239 
00240 #endif