xref: /titanic_41/usr/src/cmd/lvm/rpc.metamedd/med_hash.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1994, 2000 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <string.h>
30*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
31*7c478bd9Sstevel@tonic-gate #include <stdio.h>
32*7c478bd9Sstevel@tonic-gate #include "med_hash.h"
33*7c478bd9Sstevel@tonic-gate #include "med_local.h"
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #ifdef _KERNEL
36*7c478bd9Sstevel@tonic-gate #define	memmove(a, b, c)		bcopy(b, a, c)
37*7c478bd9Sstevel@tonic-gate #define	memcmp				bcmp
38*7c478bd9Sstevel@tonic-gate #define	memset(a, '\0', c)		bzero(a, c)
39*7c478bd9Sstevel@tonic-gate #define	Malloc				bkmem_alloc
40*7c478bd9Sstevel@tonic-gate #endif	/* _KERNEL */
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate #define	VERIFY_HASH_REALLOC
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate static int
BCMP(void * str1,void * str2,int len)45*7c478bd9Sstevel@tonic-gate BCMP(void *str1, void *str2, int len)
46*7c478bd9Sstevel@tonic-gate {
47*7c478bd9Sstevel@tonic-gate 	return (memcmp((char *)str1, (char *)str2, len));
48*7c478bd9Sstevel@tonic-gate }
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate static int
HASH(void * datap,int datalen,int hsz)51*7c478bd9Sstevel@tonic-gate HASH(void *datap, int datalen, int hsz)
52*7c478bd9Sstevel@tonic-gate {
53*7c478bd9Sstevel@tonic-gate 	char		*cp;
54*7c478bd9Sstevel@tonic-gate 	int		hv = 0;
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate 	for (cp = (char *)datap; cp != ((char *)datap + datalen); hv += *cp++)
57*7c478bd9Sstevel@tonic-gate 		;
58*7c478bd9Sstevel@tonic-gate 	return (hv % hsz);
59*7c478bd9Sstevel@tonic-gate }
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate int
init_cache(Cache ** cp,int hsz,int bsz,int (* hfunc)(void *,int,int),int (* cfunc)(void *,void *,int),void (* kffunc)(void *),void (* dffunc)(void *))62*7c478bd9Sstevel@tonic-gate init_cache(
63*7c478bd9Sstevel@tonic-gate 	Cache	**cp,
64*7c478bd9Sstevel@tonic-gate 	int	hsz,
65*7c478bd9Sstevel@tonic-gate 	int	bsz,
66*7c478bd9Sstevel@tonic-gate 	int	(*hfunc)(void *, int, int),
67*7c478bd9Sstevel@tonic-gate 	int	(*cfunc)(void *, void *, int),
68*7c478bd9Sstevel@tonic-gate 	void	(*kffunc)(void *),
69*7c478bd9Sstevel@tonic-gate 	void	(*dffunc)(void *)
70*7c478bd9Sstevel@tonic-gate )
71*7c478bd9Sstevel@tonic-gate {
72*7c478bd9Sstevel@tonic-gate 	int			i;
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate 	if ((*cp = (Cache *) Malloc(sizeof (**cp))) == NULL) {
75*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "Malloc(Cache **cp)");
76*7c478bd9Sstevel@tonic-gate 		return (-1);
77*7c478bd9Sstevel@tonic-gate 	}
78*7c478bd9Sstevel@tonic-gate 	(*cp)->bp = (Bucket *) Malloc(sizeof (*(*cp)->bp) * hsz);
79*7c478bd9Sstevel@tonic-gate 	if ((*cp)->bp == NULL) {
80*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "Malloc(Bucket cp->bp)");
81*7c478bd9Sstevel@tonic-gate 		return (-1);
82*7c478bd9Sstevel@tonic-gate 	}
83*7c478bd9Sstevel@tonic-gate 	(*cp)->hsz = hsz;
84*7c478bd9Sstevel@tonic-gate 	(*cp)->bsz = bsz;
85*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < (*cp)->hsz; i++) {
86*7c478bd9Sstevel@tonic-gate 		(*cp)->bp[i].nent = 0;
87*7c478bd9Sstevel@tonic-gate 		(*cp)->bp[i].nalloc = 0;
88*7c478bd9Sstevel@tonic-gate 		(*cp)->bp[i].itempp = NULL;
89*7c478bd9Sstevel@tonic-gate 	}
90*7c478bd9Sstevel@tonic-gate 	/* Hash function */
91*7c478bd9Sstevel@tonic-gate 	if (hfunc != (int (*)()) NULL)
92*7c478bd9Sstevel@tonic-gate 		(*cp)->hfunc = hfunc;
93*7c478bd9Sstevel@tonic-gate 	else
94*7c478bd9Sstevel@tonic-gate 		(*cp)->hfunc = HASH;
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate 	/* Compare function */
97*7c478bd9Sstevel@tonic-gate 	if (cfunc != (int (*)()) NULL)
98*7c478bd9Sstevel@tonic-gate 		(*cp)->cfunc = cfunc;
99*7c478bd9Sstevel@tonic-gate 	else
100*7c478bd9Sstevel@tonic-gate 		(*cp)->cfunc = BCMP;
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate 	/* Key free function */
103*7c478bd9Sstevel@tonic-gate 	if (kffunc != (void (*)()) NULL)
104*7c478bd9Sstevel@tonic-gate 		(*cp)->kffunc = kffunc;
105*7c478bd9Sstevel@tonic-gate 	else
106*7c478bd9Sstevel@tonic-gate 		(*cp)->kffunc = Free;
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate 	/* Data free function */
109*7c478bd9Sstevel@tonic-gate 	if (dffunc != (void (*)()) NULL)
110*7c478bd9Sstevel@tonic-gate 		(*cp)->dffunc = dffunc;
111*7c478bd9Sstevel@tonic-gate 	else
112*7c478bd9Sstevel@tonic-gate 		(*cp)->dffunc = Free;
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 	return (0);
115*7c478bd9Sstevel@tonic-gate }
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate int
add_cache(Cache * cp,Item * itemp)118*7c478bd9Sstevel@tonic-gate add_cache(Cache *cp, Item *itemp)
119*7c478bd9Sstevel@tonic-gate {
120*7c478bd9Sstevel@tonic-gate 	Bucket			*bp;
121*7c478bd9Sstevel@tonic-gate 	Item			**titempp;
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate 	if (cp == NULL) {
124*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
125*7c478bd9Sstevel@tonic-gate 		    "add_cache(): init_cache() not called.\n");
126*7c478bd9Sstevel@tonic-gate 		return (-1);
127*7c478bd9Sstevel@tonic-gate 	}
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate 	bp = &cp->bp[(*cp->hfunc)(itemp->key, itemp->keyl, cp->hsz)];
130*7c478bd9Sstevel@tonic-gate 	if (bp->nent >= bp->nalloc) {
131*7c478bd9Sstevel@tonic-gate 		if (bp->nalloc == 0) {
132*7c478bd9Sstevel@tonic-gate 			bp->itempp =
133*7c478bd9Sstevel@tonic-gate 			    (Item **) Malloc(sizeof (*bp->itempp) * cp->bsz);
134*7c478bd9Sstevel@tonic-gate 		} else {
135*7c478bd9Sstevel@tonic-gate #ifdef	VERIFY_HASH_REALLOC
136*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
137*7c478bd9Sstevel@tonic-gate 			    "realloc(%d) bucket=%d\n", bp->nalloc + cp->bsz,
138*7c478bd9Sstevel@tonic-gate 			    (*cp->hfunc)(itemp->key, itemp->keyl, cp->hsz));
139*7c478bd9Sstevel@tonic-gate #endif	/* VERIFY_HASH_REALLOC */
140*7c478bd9Sstevel@tonic-gate 			titempp =
141*7c478bd9Sstevel@tonic-gate 			    (Item **) Malloc(sizeof (*bp->itempp) *
142*7c478bd9Sstevel@tonic-gate 			    (bp->nalloc + cp->bsz));
143*7c478bd9Sstevel@tonic-gate 			if (titempp != NULL) {
144*7c478bd9Sstevel@tonic-gate 				(void) memmove((char *)titempp,
145*7c478bd9Sstevel@tonic-gate 				    (char *)bp->itempp,
146*7c478bd9Sstevel@tonic-gate 				    (sizeof (*bp->itempp) * bp->nalloc));
147*7c478bd9Sstevel@tonic-gate #ifdef _KERNEL
148*7c478bd9Sstevel@tonic-gate 				bkmem_free(bp->itempp,
149*7c478bd9Sstevel@tonic-gate 				    (sizeof (*bp->itempp) * bp->nalloc));
150*7c478bd9Sstevel@tonic-gate #else	/* !_KERNEL */
151*7c478bd9Sstevel@tonic-gate 				Free(bp->itempp);
152*7c478bd9Sstevel@tonic-gate #endif	/* _KERNEL */
153*7c478bd9Sstevel@tonic-gate 				bp->itempp = titempp;
154*7c478bd9Sstevel@tonic-gate 			} else
155*7c478bd9Sstevel@tonic-gate 				bp->itempp = NULL;
156*7c478bd9Sstevel@tonic-gate 		}
157*7c478bd9Sstevel@tonic-gate 		if (bp->itempp == NULL) {
158*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
159*7c478bd9Sstevel@tonic-gate 			    "add_cache(): out of memory\n");
160*7c478bd9Sstevel@tonic-gate 			return (-1);
161*7c478bd9Sstevel@tonic-gate 		}
162*7c478bd9Sstevel@tonic-gate 		bp->nalloc += cp->bsz;
163*7c478bd9Sstevel@tonic-gate 	}
164*7c478bd9Sstevel@tonic-gate 	bp->itempp[bp->nent] = itemp;
165*7c478bd9Sstevel@tonic-gate 	bp->nent++;
166*7c478bd9Sstevel@tonic-gate 	return (0);
167*7c478bd9Sstevel@tonic-gate }
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate Item *
lookup_cache(Cache * cp,void * datap,int datalen)170*7c478bd9Sstevel@tonic-gate lookup_cache(Cache *cp, void *datap, int datalen)
171*7c478bd9Sstevel@tonic-gate {
172*7c478bd9Sstevel@tonic-gate 	int			i;
173*7c478bd9Sstevel@tonic-gate 	Bucket			*bp;
174*7c478bd9Sstevel@tonic-gate 
175*7c478bd9Sstevel@tonic-gate 	if (cp == NULL) {
176*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
177*7c478bd9Sstevel@tonic-gate 		    "lookup_cache(): init_cache() not called.\n");
178*7c478bd9Sstevel@tonic-gate 		return (Null_Item);
179*7c478bd9Sstevel@tonic-gate 	}
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate 	bp = &cp->bp[(*cp->hfunc)(datap, datalen, cp->hsz)];
182*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < bp->nent; i++)
183*7c478bd9Sstevel@tonic-gate 		if (!(*cp->cfunc)((void *)bp->itempp[i]->key, datap, datalen))
184*7c478bd9Sstevel@tonic-gate 			    return (bp->itempp[i]);
185*7c478bd9Sstevel@tonic-gate 	return (Null_Item);
186*7c478bd9Sstevel@tonic-gate }
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate Item *
first_item(Cache * cp,int * bidx,int * iidx)189*7c478bd9Sstevel@tonic-gate first_item(Cache *cp, int *bidx, int *iidx)
190*7c478bd9Sstevel@tonic-gate {
191*7c478bd9Sstevel@tonic-gate 	Item			*itemp = Null_Item;
192*7c478bd9Sstevel@tonic-gate 
193*7c478bd9Sstevel@tonic-gate 	if (cp == NULL) {
194*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
195*7c478bd9Sstevel@tonic-gate 		    "first_item(): init_cache() not called.\n");
196*7c478bd9Sstevel@tonic-gate 		return (Null_Item);
197*7c478bd9Sstevel@tonic-gate 	}
198*7c478bd9Sstevel@tonic-gate 
199*7c478bd9Sstevel@tonic-gate 	for (*bidx = 0; *bidx < cp->hsz && (cp->bp[*bidx].nalloc == 0 ||
200*7c478bd9Sstevel@tonic-gate 	    cp->bp[*bidx].nent == 0); (*bidx)++)
201*7c478bd9Sstevel@tonic-gate 		/* void */;
202*7c478bd9Sstevel@tonic-gate 
203*7c478bd9Sstevel@tonic-gate 	if (*bidx < cp->hsz && cp->bp[*bidx].nent > 0) {
204*7c478bd9Sstevel@tonic-gate 		itemp = cp->bp[*bidx].itempp[0];
205*7c478bd9Sstevel@tonic-gate 		*iidx = 0;
206*7c478bd9Sstevel@tonic-gate 	} else {
207*7c478bd9Sstevel@tonic-gate 		*bidx = -1;
208*7c478bd9Sstevel@tonic-gate 		*iidx = -1;
209*7c478bd9Sstevel@tonic-gate 	}
210*7c478bd9Sstevel@tonic-gate 	return (itemp);
211*7c478bd9Sstevel@tonic-gate }
212*7c478bd9Sstevel@tonic-gate 
213*7c478bd9Sstevel@tonic-gate Item *
next_item(Cache * cp,int * bidx,int * iidx)214*7c478bd9Sstevel@tonic-gate next_item(Cache *cp, int *bidx, int *iidx)
215*7c478bd9Sstevel@tonic-gate {
216*7c478bd9Sstevel@tonic-gate 	Item			*itemp = Null_Item;
217*7c478bd9Sstevel@tonic-gate 
218*7c478bd9Sstevel@tonic-gate 	if (cp == NULL) {
219*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
220*7c478bd9Sstevel@tonic-gate 		    "next_item(): init_cache() not called.\n");
221*7c478bd9Sstevel@tonic-gate 		return (Null_Item);
222*7c478bd9Sstevel@tonic-gate 	}
223*7c478bd9Sstevel@tonic-gate 
224*7c478bd9Sstevel@tonic-gate 	if (*bidx < cp->hsz && *bidx >= 0) {
225*7c478bd9Sstevel@tonic-gate 		if ((*iidx + 1) < cp->bp[*bidx].nent) {
226*7c478bd9Sstevel@tonic-gate 			itemp = cp->bp[*bidx].itempp[++(*iidx)];
227*7c478bd9Sstevel@tonic-gate 		} else {
228*7c478bd9Sstevel@tonic-gate 			for (++(*bidx);
229*7c478bd9Sstevel@tonic-gate 			    *bidx < cp->hsz && (cp->bp[*bidx].nalloc == 0 ||
230*7c478bd9Sstevel@tonic-gate 			    cp->bp[*bidx].nent == 0);
231*7c478bd9Sstevel@tonic-gate 			    (*bidx)++)
232*7c478bd9Sstevel@tonic-gate 				/* void */;
233*7c478bd9Sstevel@tonic-gate 			if (*bidx < cp->hsz && cp->bp[*bidx].nent > 0) {
234*7c478bd9Sstevel@tonic-gate 				*iidx = 0;
235*7c478bd9Sstevel@tonic-gate 				itemp = cp->bp[*bidx].itempp[(*iidx)++];
236*7c478bd9Sstevel@tonic-gate 			} else {
237*7c478bd9Sstevel@tonic-gate 				*bidx = -1;
238*7c478bd9Sstevel@tonic-gate 				*iidx = -1;
239*7c478bd9Sstevel@tonic-gate 			}
240*7c478bd9Sstevel@tonic-gate 		}
241*7c478bd9Sstevel@tonic-gate 	} else {
242*7c478bd9Sstevel@tonic-gate 		*bidx = -1;
243*7c478bd9Sstevel@tonic-gate 		*iidx = -1;
244*7c478bd9Sstevel@tonic-gate 	}
245*7c478bd9Sstevel@tonic-gate 	return (itemp);
246*7c478bd9Sstevel@tonic-gate }
247*7c478bd9Sstevel@tonic-gate 
248*7c478bd9Sstevel@tonic-gate void
des_cache(Cache ** cpp)249*7c478bd9Sstevel@tonic-gate des_cache(Cache **cpp)
250*7c478bd9Sstevel@tonic-gate {
251*7c478bd9Sstevel@tonic-gate 	Cache			*cp = *cpp;
252*7c478bd9Sstevel@tonic-gate 	Bucket			*bp;
253*7c478bd9Sstevel@tonic-gate 	Item			*itemp;
254*7c478bd9Sstevel@tonic-gate 	int			i;
255*7c478bd9Sstevel@tonic-gate 	int			j;
256*7c478bd9Sstevel@tonic-gate 
257*7c478bd9Sstevel@tonic-gate 	if (cp == NULL) {
258*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
259*7c478bd9Sstevel@tonic-gate 		    "des_cache(): init_cache() not called.\n");
260*7c478bd9Sstevel@tonic-gate 		return;
261*7c478bd9Sstevel@tonic-gate 	}
262*7c478bd9Sstevel@tonic-gate 
263*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < cp->hsz; i++) {
264*7c478bd9Sstevel@tonic-gate 		bp = &cp->bp[i];
265*7c478bd9Sstevel@tonic-gate 		if (bp->nalloc > 0) {
266*7c478bd9Sstevel@tonic-gate 			for (j = 0; j < bp->nent; j++) {
267*7c478bd9Sstevel@tonic-gate 				itemp = bp->itempp[j];
268*7c478bd9Sstevel@tonic-gate 				if (itemp->key)
269*7c478bd9Sstevel@tonic-gate 					(void) (*cp->kffunc)(itemp->key);
270*7c478bd9Sstevel@tonic-gate 				if (itemp->data)
271*7c478bd9Sstevel@tonic-gate 					(void) (*cp->dffunc)(itemp->data);
272*7c478bd9Sstevel@tonic-gate 			}
273*7c478bd9Sstevel@tonic-gate 		}
274*7c478bd9Sstevel@tonic-gate 		(void) Free(bp->itempp);
275*7c478bd9Sstevel@tonic-gate 	}
276*7c478bd9Sstevel@tonic-gate 	(void) Free(cp->bp);
277*7c478bd9Sstevel@tonic-gate 	(void) Free(cp);
278*7c478bd9Sstevel@tonic-gate 	*cpp = NULL;
279*7c478bd9Sstevel@tonic-gate }
280*7c478bd9Sstevel@tonic-gate 
281*7c478bd9Sstevel@tonic-gate int
del_cache(Cache * cp,Item * itemp)282*7c478bd9Sstevel@tonic-gate del_cache(Cache *cp, Item *itemp)
283*7c478bd9Sstevel@tonic-gate {
284*7c478bd9Sstevel@tonic-gate 	Bucket			*bp;
285*7c478bd9Sstevel@tonic-gate 	int			bidx;
286*7c478bd9Sstevel@tonic-gate 	int			iidx;
287*7c478bd9Sstevel@tonic-gate 	int			tidx;
288*7c478bd9Sstevel@tonic-gate 	int			retval = 0;
289*7c478bd9Sstevel@tonic-gate 	void			*datap = itemp->key;
290*7c478bd9Sstevel@tonic-gate 	int			datalen = itemp->keyl;
291*7c478bd9Sstevel@tonic-gate 	Item			*titemp;
292*7c478bd9Sstevel@tonic-gate 
293*7c478bd9Sstevel@tonic-gate 	if (cp == NULL) {
294*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
295*7c478bd9Sstevel@tonic-gate 		    "del_cache(): init_cache() not called.\n");
296*7c478bd9Sstevel@tonic-gate 		return (-1);
297*7c478bd9Sstevel@tonic-gate 	}
298*7c478bd9Sstevel@tonic-gate 
299*7c478bd9Sstevel@tonic-gate 	bidx = (*cp->hfunc)(datap, datalen, cp->hsz);
300*7c478bd9Sstevel@tonic-gate 	bp = &cp->bp[bidx];
301*7c478bd9Sstevel@tonic-gate 
302*7c478bd9Sstevel@tonic-gate 	for (iidx = 0; iidx < bp->nent; iidx++)
303*7c478bd9Sstevel@tonic-gate 		if (!(*cp->cfunc)((void *)bp->itempp[iidx]->key, datap,
304*7c478bd9Sstevel@tonic-gate 		    datalen)) {
305*7c478bd9Sstevel@tonic-gate 			titemp = bp->itempp[iidx];
306*7c478bd9Sstevel@tonic-gate 			break;
307*7c478bd9Sstevel@tonic-gate 		}
308*7c478bd9Sstevel@tonic-gate 	if (iidx < bp->nent) {
309*7c478bd9Sstevel@tonic-gate 		if (titemp->key)
310*7c478bd9Sstevel@tonic-gate 			(void) (*cp->kffunc)(titemp->key);
311*7c478bd9Sstevel@tonic-gate 		if (titemp->data)
312*7c478bd9Sstevel@tonic-gate 			(void) (*cp->dffunc)(titemp->data);
313*7c478bd9Sstevel@tonic-gate 		titemp->keyl = 0;
314*7c478bd9Sstevel@tonic-gate 		titemp->datal = 0;
315*7c478bd9Sstevel@tonic-gate 		bp->nent--;
316*7c478bd9Sstevel@tonic-gate 		if (bp->nent == 0) {
317*7c478bd9Sstevel@tonic-gate 			(void) Free(bp->itempp);
318*7c478bd9Sstevel@tonic-gate 			bp->itempp = NULL;
319*7c478bd9Sstevel@tonic-gate 			bp->nalloc = 0;
320*7c478bd9Sstevel@tonic-gate 		} else {
321*7c478bd9Sstevel@tonic-gate 			for (tidx = iidx + 1; tidx < (bp->nent + 1); tidx++) {
322*7c478bd9Sstevel@tonic-gate 				bp->itempp[iidx] = bp->itempp[tidx];
323*7c478bd9Sstevel@tonic-gate 				iidx = tidx;
324*7c478bd9Sstevel@tonic-gate 			}
325*7c478bd9Sstevel@tonic-gate 		}
326*7c478bd9Sstevel@tonic-gate 	} else {
327*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
328*7c478bd9Sstevel@tonic-gate 		    "del_cache(): item not found.\n");
329*7c478bd9Sstevel@tonic-gate 		retval = -1;
330*7c478bd9Sstevel@tonic-gate 	}
331*7c478bd9Sstevel@tonic-gate 	return (retval);
332*7c478bd9Sstevel@tonic-gate }
333*7c478bd9Sstevel@tonic-gate 
334*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
335*7c478bd9Sstevel@tonic-gate void
cache_stat(Cache * cp,char * tag)336*7c478bd9Sstevel@tonic-gate cache_stat(Cache *cp, char *tag)
337*7c478bd9Sstevel@tonic-gate {
338*7c478bd9Sstevel@tonic-gate 	Bucket			*bp;
339*7c478bd9Sstevel@tonic-gate 	int			bidx;
340*7c478bd9Sstevel@tonic-gate 
341*7c478bd9Sstevel@tonic-gate 	if (cp == NULL) {
342*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
343*7c478bd9Sstevel@tonic-gate 		    "cache_stat(): init_cache() not called.\n");
344*7c478bd9Sstevel@tonic-gate 		return;
345*7c478bd9Sstevel@tonic-gate 	}
346*7c478bd9Sstevel@tonic-gate 
347*7c478bd9Sstevel@tonic-gate 	if (tag && *tag)
348*7c478bd9Sstevel@tonic-gate 		(void) printf("%s", tag);
349*7c478bd9Sstevel@tonic-gate 
350*7c478bd9Sstevel@tonic-gate 	for (bidx = 0; bidx < cp->hsz; bidx++) {
351*7c478bd9Sstevel@tonic-gate 		bp = &cp->bp[bidx];
352*7c478bd9Sstevel@tonic-gate 		if (bp->nalloc > 0) {
353*7c478bd9Sstevel@tonic-gate 			(void) printf("Bucket #%d Alloc %d", bidx, bp->nalloc);
354*7c478bd9Sstevel@tonic-gate 			if (bp->nent > 0) {
355*7c478bd9Sstevel@tonic-gate 				(void) printf(
356*7c478bd9Sstevel@tonic-gate 				    " Entries %d Reallocs %d", bp->nent,
357*7c478bd9Sstevel@tonic-gate 				    (bp->nalloc / cp->hsz));
358*7c478bd9Sstevel@tonic-gate 				(void) printf(
359*7c478bd9Sstevel@tonic-gate 				    " Utilization %d%%",
360*7c478bd9Sstevel@tonic-gate 				    ((bp->nent * 100)/bp->nalloc));
361*7c478bd9Sstevel@tonic-gate 			}
362*7c478bd9Sstevel@tonic-gate 			(void) printf("\n");
363*7c478bd9Sstevel@tonic-gate 			(void) fflush(stdout);
364*7c478bd9Sstevel@tonic-gate 		}
365*7c478bd9Sstevel@tonic-gate 	}
366*7c478bd9Sstevel@tonic-gate }
367*7c478bd9Sstevel@tonic-gate 
368*7c478bd9Sstevel@tonic-gate void
pr_cache(Cache * cp,char * tag,void (* pfunc)(void *,int,void *,int))369*7c478bd9Sstevel@tonic-gate pr_cache(Cache *cp, char *tag, void (*pfunc)(void *, int, void *, int))
370*7c478bd9Sstevel@tonic-gate {
371*7c478bd9Sstevel@tonic-gate 	int			bidx;
372*7c478bd9Sstevel@tonic-gate 	int			iidx;
373*7c478bd9Sstevel@tonic-gate 	Bucket			*bp;
374*7c478bd9Sstevel@tonic-gate 	Item			*itemp;
375*7c478bd9Sstevel@tonic-gate 
376*7c478bd9Sstevel@tonic-gate 	if (cp == NULL) {
377*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
378*7c478bd9Sstevel@tonic-gate 		    "pr_cache(): init_cache() not called.\n");
379*7c478bd9Sstevel@tonic-gate 		return;
380*7c478bd9Sstevel@tonic-gate 	}
381*7c478bd9Sstevel@tonic-gate 
382*7c478bd9Sstevel@tonic-gate 	if (tag && *tag)
383*7c478bd9Sstevel@tonic-gate 		(void) printf("%s", tag);
384*7c478bd9Sstevel@tonic-gate 
385*7c478bd9Sstevel@tonic-gate 	for (bidx = 0; bidx < cp->hsz; bidx++) {
386*7c478bd9Sstevel@tonic-gate 		bp = &cp->bp[bidx];
387*7c478bd9Sstevel@tonic-gate 		if (bp->nent > 0)
388*7c478bd9Sstevel@tonic-gate 			for (iidx = 0; iidx < bp->nent; iidx++) {
389*7c478bd9Sstevel@tonic-gate 				itemp = bp->itempp[iidx];
390*7c478bd9Sstevel@tonic-gate 				(*pfunc)(itemp->key, itemp->keyl,
391*7c478bd9Sstevel@tonic-gate 				    itemp->data, itemp->datal);
392*7c478bd9Sstevel@tonic-gate 			}
393*7c478bd9Sstevel@tonic-gate 	}
394*7c478bd9Sstevel@tonic-gate }
395*7c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
396