X-Git-Url: https://git.openssl.org/?a=blobdiff_plain;f=crypto%2Flhash%2Flhash.c;h=47f748081bba2a3165bc981d179ad018bf53f245;hb=399aa6b5ffd37e2601af4524bb71d862cbee4a84;hp=7eb92a18bca2c81fe837c1a87fb2f0bf2a9b37d7;hpb=6e22639f4640b702d9d8636265c685448ca64145;p=openssl.git diff --git a/crypto/lhash/lhash.c b/crypto/lhash/lhash.c index 7eb92a18bc..47f748081b 100644 --- a/crypto/lhash/lhash.c +++ b/crypto/lhash/lhash.c @@ -100,30 +100,30 @@ #include #include -const char *lh_version="lhash" OPENSSL_VERSION_PTEXT; +const char lh_version[]="lhash" OPENSSL_VERSION_PTEXT; #undef MIN_NODES #define MIN_NODES 16 #define UP_LOAD (2*LH_LOAD_MULT) /* load times 256 (default 2) */ #define DOWN_LOAD (LH_LOAD_MULT) /* load times 256 (default 1) */ -static void expand(LHASH *lh); -static void contract(LHASH *lh); -static LHASH_NODE **getrn(LHASH *lh, void *data, unsigned long *rhash); +static void expand(_LHASH *lh); +static void contract(_LHASH *lh); +static LHASH_NODE **getrn(_LHASH *lh, const void *data, unsigned long *rhash); -LHASH *lh_new(unsigned long (*h)(), int (*c)()) +_LHASH *lh_new(LHASH_HASH_FN_TYPE h, LHASH_COMP_FN_TYPE c) { - LHASH *ret; + _LHASH *ret; int i; - if ((ret=(LHASH *)Malloc(sizeof(LHASH))) == NULL) + if ((ret=OPENSSL_malloc(sizeof(_LHASH))) == NULL) goto err0; - if ((ret->b=(LHASH_NODE **)Malloc(sizeof(LHASH_NODE *)*MIN_NODES)) == NULL) + if ((ret->b=OPENSSL_malloc(sizeof(LHASH_NODE *)*MIN_NODES)) == NULL) goto err1; for (i=0; ib[i]=NULL; - ret->comp=((c == NULL)?(int (*)())strcmp:c); - ret->hash=((h == NULL)?(unsigned long (*)())lh_strhash:h); + ret->comp=((c == NULL)?(LHASH_COMP_FN_TYPE)strcmp:c); + ret->hash=((h == NULL)?(LHASH_HASH_FN_TYPE)lh_strhash:h); ret->num_nodes=MIN_NODES/2; ret->num_alloc_nodes=MIN_NODES; ret->p=0; @@ -149,12 +149,12 @@ LHASH *lh_new(unsigned long (*h)(), int (*c)()) ret->error=0; return(ret); err1: - Free(ret); + OPENSSL_free(ret); err0: return(NULL); } -void lh_free(LHASH *lh) +void lh_free(_LHASH *lh) { unsigned int i; LHASH_NODE *n,*nn; @@ -168,15 +168,15 @@ void lh_free(LHASH *lh) while (n != NULL) { nn=n->next; - Free(n); + OPENSSL_free(n); n=nn; } } - Free(lh->b); - Free(lh); + OPENSSL_free(lh->b); + OPENSSL_free(lh); } -void *lh_insert(LHASH *lh, void *data) +void *lh_insert(_LHASH *lh, void *data) { unsigned long hash; LHASH_NODE *nn,**rn; @@ -190,14 +190,14 @@ void *lh_insert(LHASH *lh, void *data) if (*rn == NULL) { - if ((nn=(LHASH_NODE *)Malloc(sizeof(LHASH_NODE))) == NULL) + if ((nn=(LHASH_NODE *)OPENSSL_malloc(sizeof(LHASH_NODE))) == NULL) { lh->error++; return(NULL); } nn->data=data; nn->next=NULL; -#ifndef NO_HASH_COMP +#ifndef OPENSSL_NO_HASH_COMP nn->hash=hash; #endif *rn=nn; @@ -214,7 +214,7 @@ void *lh_insert(LHASH *lh, void *data) return(ret); } -void *lh_delete(LHASH *lh, void *data) +void *lh_delete(_LHASH *lh, const void *data) { unsigned long hash; LHASH_NODE *nn,**rn; @@ -233,7 +233,7 @@ void *lh_delete(LHASH *lh, void *data) nn= *rn; *rn=nn->next; ret=nn->data; - Free(nn); + OPENSSL_free(nn); lh->num_delete++; } @@ -245,7 +245,7 @@ void *lh_delete(LHASH *lh, void *data) return(ret); } -void *lh_retrieve(LHASH *lh, void *data) +void *lh_retrieve(_LHASH *lh, const void *data) { unsigned long hash; LHASH_NODE **rn; @@ -267,16 +267,15 @@ void *lh_retrieve(LHASH *lh, void *data) return(ret); } -void lh_doall(LHASH *lh, void (*func)()) - { - lh_doall_arg(lh,func,NULL); - } - -void lh_doall_arg(LHASH *lh, void (*func)(), void *arg) +static void doall_util_fn(_LHASH *lh, int use_arg, LHASH_DOALL_FN_TYPE func, + LHASH_DOALL_ARG_FN_TYPE func_arg, void *arg) { int i; LHASH_NODE *a,*n; + if (lh == NULL) + return; + /* reverse the order so we search from 'top to bottom' * We were having memory leaks otherwise */ for (i=lh->num_nodes-1; i>=0; i--) @@ -286,14 +285,29 @@ void lh_doall_arg(LHASH *lh, void (*func)(), void *arg) { /* 28/05/91 - eay - n added so items can be deleted * via lh_doall */ + /* 22/05/08 - ben - eh? since a is not passed, + * this should not be needed */ n=a->next; - func(a->data,arg); + if(use_arg) + func_arg(a->data,arg); + else + func(a->data); a=n; } } } -static void expand(LHASH *lh) +void lh_doall(_LHASH *lh, LHASH_DOALL_FN_TYPE func) + { + doall_util_fn(lh, 0, func, (LHASH_DOALL_ARG_FN_TYPE)0, NULL); + } + +void lh_doall_arg(_LHASH *lh, LHASH_DOALL_ARG_FN_TYPE func, void *arg) + { + doall_util_fn(lh, 1, (LHASH_DOALL_FN_TYPE)0, func, arg); + } + +static void expand(_LHASH *lh) { LHASH_NODE **n,**n1,**n2,*np; unsigned int p,i,j; @@ -309,10 +323,10 @@ static void expand(LHASH *lh) for (np= *n1; np != NULL; ) { -#ifndef NO_HASH_COMP +#ifndef OPENSSL_NO_HASH_COMP hash=np->hash; #else - hash=(*(lh->hash))(np->data); + hash=lh->hash(np->data); lh->num_hash_calls++; #endif if ((hash%nni) != p) @@ -329,8 +343,8 @@ static void expand(LHASH *lh) if ((lh->p) >= lh->pmax) { j=(int)lh->num_alloc_nodes*2; - n=(LHASH_NODE **)Realloc(lh->b, - (unsigned int)sizeof(LHASH_NODE *)*j); + n=(LHASH_NODE **)OPENSSL_realloc(lh->b, + (int)(sizeof(LHASH_NODE *)*j)); if (n == NULL) { /* fputs("realloc error in lhash",stderr); */ @@ -349,7 +363,7 @@ static void expand(LHASH *lh) } } -static void contract(LHASH *lh) +static void contract(_LHASH *lh) { LHASH_NODE **n,*n1,*np; @@ -357,7 +371,7 @@ static void contract(LHASH *lh) lh->b[lh->p+lh->pmax-1]=NULL; /* 24/07-92 - eay - weird but :-( */ if (lh->p == 0) { - n=(LHASH_NODE **)Realloc(lh->b, + n=(LHASH_NODE **)OPENSSL_realloc(lh->b, (unsigned int)(sizeof(LHASH_NODE *)*lh->pmax)); if (n == NULL) { @@ -388,11 +402,11 @@ static void contract(LHASH *lh) } } -static LHASH_NODE **getrn(LHASH *lh, void *data, unsigned long *rhash) +static LHASH_NODE **getrn(_LHASH *lh, const void *data, unsigned long *rhash) { LHASH_NODE **ret,*n1; unsigned long hash,nn; - int (*cf)(); + LHASH_COMP_FN_TYPE cf; hash=(*(lh->hash))(data); lh->num_hash_calls++; @@ -406,7 +420,7 @@ static LHASH_NODE **getrn(LHASH *lh, void *data, unsigned long *rhash) ret= &(lh->b[(int)nn]); for (n1= *ret; n1 != NULL; n1=n1->next) { -#ifndef NO_HASH_COMP +#ifndef OPENSSL_NO_HASH_COMP lh->num_hash_comps++; if (n1->hash != hash) { @@ -415,7 +429,7 @@ static LHASH_NODE **getrn(LHASH *lh, void *data, unsigned long *rhash) } #endif lh->num_comp_calls++; - if ((*cf)(n1->data,data) == 0) + if(cf(n1->data,data) == 0) break; ret= &(n1->next); } @@ -455,7 +469,7 @@ unsigned long lh_strhash(const char *c) return((ret>>16)^ret); } -unsigned long lh_num_items(LHASH *lh) +unsigned long lh_num_items(const _LHASH *lh) { return lh ? lh->num_items : 0; }