xref: /titanic_44/usr/src/lib/libpkg/common/nhash.c (revision 5c51f1241dbbdf2656d0e10011981411ed0c9673)
1*5c51f124SMoriah Waterland /*
2*5c51f124SMoriah Waterland  * CDDL HEADER START
3*5c51f124SMoriah Waterland  *
4*5c51f124SMoriah Waterland  * The contents of this file are subject to the terms of the
5*5c51f124SMoriah Waterland  * Common Development and Distribution License (the "License").
6*5c51f124SMoriah Waterland  * You may not use this file except in compliance with the License.
7*5c51f124SMoriah Waterland  *
8*5c51f124SMoriah Waterland  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*5c51f124SMoriah Waterland  * or http://www.opensolaris.org/os/licensing.
10*5c51f124SMoriah Waterland  * See the License for the specific language governing permissions
11*5c51f124SMoriah Waterland  * and limitations under the License.
12*5c51f124SMoriah Waterland  *
13*5c51f124SMoriah Waterland  * When distributing Covered Code, include this CDDL HEADER in each
14*5c51f124SMoriah Waterland  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*5c51f124SMoriah Waterland  * If applicable, add the following below this CDDL HEADER, with the
16*5c51f124SMoriah Waterland  * fields enclosed by brackets "[]" replaced with your own identifying
17*5c51f124SMoriah Waterland  * information: Portions Copyright [yyyy] [name of copyright owner]
18*5c51f124SMoriah Waterland  *
19*5c51f124SMoriah Waterland  * CDDL HEADER END
20*5c51f124SMoriah Waterland  */
21*5c51f124SMoriah Waterland 
22*5c51f124SMoriah Waterland /*
23*5c51f124SMoriah Waterland  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24*5c51f124SMoriah Waterland  * Use is subject to license terms.
25*5c51f124SMoriah Waterland  */
26*5c51f124SMoriah Waterland 
27*5c51f124SMoriah Waterland 
28*5c51f124SMoriah Waterland #include <stdio.h>
29*5c51f124SMoriah Waterland #include <string.h>
30*5c51f124SMoriah Waterland #include <stdlib.h>
31*5c51f124SMoriah Waterland #include <strings.h>
32*5c51f124SMoriah Waterland #include "pkglib.h"
33*5c51f124SMoriah Waterland #include "nhash.h"
34*5c51f124SMoriah Waterland #include "pkglocale.h"
35*5c51f124SMoriah Waterland 
36*5c51f124SMoriah Waterland #ifndef _KERNEL
37*5c51f124SMoriah Waterland #define	bcopy(a, b, c)	(void) memmove(b, a, c)
38*5c51f124SMoriah Waterland #define	bcmp		memcmp
39*5c51f124SMoriah Waterland #define	bzero(a, c)	(void) memset(a, '\0', c)
40*5c51f124SMoriah Waterland #else	/* _KERNEL */
41*5c51f124SMoriah Waterland #define	malloc		bkmem_alloc
42*5c51f124SMoriah Waterland #endif	/* _KERNEL */
43*5c51f124SMoriah Waterland 
44*5c51f124SMoriah Waterland #define	VERIFY_HASH_REALLOC
45*5c51f124SMoriah Waterland 
46*5c51f124SMoriah Waterland static int
BCMP(void * str1,void * str2,int len)47*5c51f124SMoriah Waterland BCMP(void *str1, void *str2, int len)
48*5c51f124SMoriah Waterland {
49*5c51f124SMoriah Waterland 	return (bcmp((char *)str1, (char *)str2, len));
50*5c51f124SMoriah Waterland }
51*5c51f124SMoriah Waterland 
52*5c51f124SMoriah Waterland static int
HASH(void * datap,int datalen,int hsz)53*5c51f124SMoriah Waterland HASH(void *datap, int datalen, int hsz)
54*5c51f124SMoriah Waterland {
55*5c51f124SMoriah Waterland 	char	*cp;
56*5c51f124SMoriah Waterland 	char	*np;
57*5c51f124SMoriah Waterland 	int	hv = 0;
58*5c51f124SMoriah Waterland 
59*5c51f124SMoriah Waterland 	/* determine starting and ending positions */
60*5c51f124SMoriah Waterland 
61*5c51f124SMoriah Waterland 	cp = (char *)datap;
62*5c51f124SMoriah Waterland 	np =  ((char *)cp + datalen);
63*5c51f124SMoriah Waterland 
64*5c51f124SMoriah Waterland 	/* compute hash over all characters from start to end */
65*5c51f124SMoriah Waterland 
66*5c51f124SMoriah Waterland 	while (cp != np) {
67*5c51f124SMoriah Waterland 		hv += ((int)*cp++);
68*5c51f124SMoriah Waterland 	}
69*5c51f124SMoriah Waterland 
70*5c51f124SMoriah Waterland 	/* return computed hash */
71*5c51f124SMoriah Waterland 
72*5c51f124SMoriah Waterland 	return (hv % hsz);
73*5c51f124SMoriah Waterland }
74*5c51f124SMoriah Waterland 
75*5c51f124SMoriah Waterland int
init_cache(Cache ** cp,int hsz,int bsz,int (* hfunc)(void *,int,int),int (* cfunc)(void *,void *,int))76*5c51f124SMoriah Waterland init_cache(Cache **cp, int hsz, int bsz,
77*5c51f124SMoriah Waterland 	    int (*hfunc)(void *, int, int), int (*cfunc)(void *, void *, int))
78*5c51f124SMoriah Waterland {
79*5c51f124SMoriah Waterland 	if ((*cp = (Cache *) malloc(sizeof (**cp))) == NULL) {
80*5c51f124SMoriah Waterland 		(void) fprintf(stderr, pkg_gt("malloc(Cache **cp)"));
81*5c51f124SMoriah Waterland 		return (-1);
82*5c51f124SMoriah Waterland 	}
83*5c51f124SMoriah Waterland 	if (((*cp)->bp =
84*5c51f124SMoriah Waterland 	    (Bucket *) malloc(sizeof (*(*cp)->bp) * hsz)) == NULL) {
85*5c51f124SMoriah Waterland 		(void) fprintf(stderr, pkg_gt("malloc(Bucket cp->bp)"));
86*5c51f124SMoriah Waterland 		return (-1);
87*5c51f124SMoriah Waterland 	}
88*5c51f124SMoriah Waterland 
89*5c51f124SMoriah Waterland 	(*cp)->hsz = hsz;
90*5c51f124SMoriah Waterland 	(*cp)->bsz = bsz;
91*5c51f124SMoriah Waterland 
92*5c51f124SMoriah Waterland 	bzero((*cp)->bp, sizeof (*(*cp)->bp) * hsz);
93*5c51f124SMoriah Waterland 
94*5c51f124SMoriah Waterland 	if (hfunc != (int (*)()) NULL) {
95*5c51f124SMoriah Waterland 		(*cp)->hfunc = hfunc;
96*5c51f124SMoriah Waterland 	} else {
97*5c51f124SMoriah Waterland 		(*cp)->hfunc = HASH;
98*5c51f124SMoriah Waterland 	}
99*5c51f124SMoriah Waterland 
100*5c51f124SMoriah Waterland 	if (cfunc != (int (*)()) NULL) {
101*5c51f124SMoriah Waterland 		(*cp)->cfunc = cfunc;
102*5c51f124SMoriah Waterland 	} else {
103*5c51f124SMoriah Waterland 		(*cp)->cfunc = BCMP;
104*5c51f124SMoriah Waterland 	}
105*5c51f124SMoriah Waterland 	return (0);
106*5c51f124SMoriah Waterland }
107*5c51f124SMoriah Waterland 
108*5c51f124SMoriah Waterland int
add_cache(Cache * cp,Item * itemp)109*5c51f124SMoriah Waterland add_cache(Cache *cp, Item *itemp)
110*5c51f124SMoriah Waterland {
111*5c51f124SMoriah Waterland 	Bucket *bp;
112*5c51f124SMoriah Waterland 	Item **titempp;
113*5c51f124SMoriah Waterland 
114*5c51f124SMoriah Waterland 	/*
115*5c51f124SMoriah Waterland 	 * If cp is NULL, then init_cache() wasn't called. Quietly return the
116*5c51f124SMoriah Waterland 	 * error code and let the caller deal with it.
117*5c51f124SMoriah Waterland 	 */
118*5c51f124SMoriah Waterland 	if (cp == NULL)
119*5c51f124SMoriah Waterland 		return (-1);
120*5c51f124SMoriah Waterland 
121*5c51f124SMoriah Waterland 	bp = &cp->bp[(*cp->hfunc)(itemp->key, itemp->keyl, cp->hsz)];
122*5c51f124SMoriah Waterland 	if (bp->nent >= bp->nalloc) {
123*5c51f124SMoriah Waterland 		if (bp->nalloc == 0) {
124*5c51f124SMoriah Waterland 			bp->itempp =
125*5c51f124SMoriah Waterland 			    (Item **) malloc(sizeof (*bp->itempp) * cp->bsz);
126*5c51f124SMoriah Waterland 		} else {
127*5c51f124SMoriah Waterland #ifdef	VERIFY_HASH_REALLOC
128*5c51f124SMoriah Waterland 			(void) fprintf(stderr,
129*5c51f124SMoriah Waterland 			    pkg_gt("realloc(%d) bucket=%d\n"),
130*5c51f124SMoriah Waterland 			    bp->nalloc + cp->bsz,
131*5c51f124SMoriah Waterland 			    (*cp->hfunc)(itemp->key, itemp->keyl, cp->hsz));
132*5c51f124SMoriah Waterland #endif	/* VERIFY_HASH_REALLOC */
133*5c51f124SMoriah Waterland 			if ((titempp =
134*5c51f124SMoriah Waterland 			    (Item **) malloc(sizeof (*bp->itempp) *
135*5c51f124SMoriah Waterland 			    (bp->nalloc + cp->bsz))) != NULL) {
136*5c51f124SMoriah Waterland 				bcopy((char *)bp->itempp, (char *)titempp,
137*5c51f124SMoriah Waterland 				    (sizeof (*bp->itempp) * bp->nalloc));
138*5c51f124SMoriah Waterland #ifdef _KERNEL
139*5c51f124SMoriah Waterland 				bkmem_free(bp->itempp,
140*5c51f124SMoriah Waterland 					(sizeof (*bp->itempp) * bp->nalloc));
141*5c51f124SMoriah Waterland #else	/* !_KERNEL */
142*5c51f124SMoriah Waterland 				free(bp->itempp);
143*5c51f124SMoriah Waterland #endif	/* _KERNEL */
144*5c51f124SMoriah Waterland 				bp->itempp = titempp;
145*5c51f124SMoriah Waterland 			} else
146*5c51f124SMoriah Waterland 				bp->itempp = NULL;
147*5c51f124SMoriah Waterland 		}
148*5c51f124SMoriah Waterland 		if (bp->itempp == NULL) {
149*5c51f124SMoriah Waterland 			(void) fprintf(stderr,
150*5c51f124SMoriah Waterland 			    pkg_gt("add_cache(): out of memory\n"));
151*5c51f124SMoriah Waterland 			return (-1);
152*5c51f124SMoriah Waterland 		}
153*5c51f124SMoriah Waterland 		bp->nalloc += cp->bsz;
154*5c51f124SMoriah Waterland 	}
155*5c51f124SMoriah Waterland 	bp->itempp[bp->nent] = itemp;
156*5c51f124SMoriah Waterland 	bp->nent++;
157*5c51f124SMoriah Waterland 	return (0);
158*5c51f124SMoriah Waterland }
159*5c51f124SMoriah Waterland 
160*5c51f124SMoriah Waterland Item *
lookup_cache(Cache * cp,void * datap,int datalen)161*5c51f124SMoriah Waterland lookup_cache(Cache *cp, void *datap, int datalen)
162*5c51f124SMoriah Waterland {
163*5c51f124SMoriah Waterland 	int	i;
164*5c51f124SMoriah Waterland 	Bucket *bp;
165*5c51f124SMoriah Waterland 
166*5c51f124SMoriah Waterland 	/*
167*5c51f124SMoriah Waterland 	 * If cp is NULL, then init_cache() wasn't called. Quietly return the
168*5c51f124SMoriah Waterland 	 * error code and let the caller deal with it.
169*5c51f124SMoriah Waterland 	 */
170*5c51f124SMoriah Waterland 	if (cp == NULL) {
171*5c51f124SMoriah Waterland 	    return (Null_Item);
172*5c51f124SMoriah Waterland 	}
173*5c51f124SMoriah Waterland 
174*5c51f124SMoriah Waterland 	bp = &cp->bp[(*cp->hfunc)(datap, datalen, cp->hsz)];
175*5c51f124SMoriah Waterland 
176*5c51f124SMoriah Waterland 	for (i = 0; i < bp->nent; i++) {
177*5c51f124SMoriah Waterland 		if (!(*cp->cfunc)((void *)bp->itempp[i]->key, datap, datalen)) {
178*5c51f124SMoriah Waterland 			return (bp->itempp[i]);
179*5c51f124SMoriah Waterland 		}
180*5c51f124SMoriah Waterland 	}
181*5c51f124SMoriah Waterland 	return (Null_Item);
182*5c51f124SMoriah Waterland }
183