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