4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
34 #include "pkglocale.h"
37 #define bcopy(a, b, c) (void) memmove(b, a, c)
39 #define bzero(a, c) (void) memset(a, '\0', c)
41 #define malloc bkmem_alloc
44 #define VERIFY_HASH_REALLOC
47 BCMP(void *str1
, void *str2
, int len
)
49 return (bcmp((char *)str1
, (char *)str2
, len
));
53 HASH(void *datap
, int datalen
, int hsz
)
59 /* determine starting and ending positions */
62 np
= ((char *)cp
+ datalen
);
64 /* compute hash over all characters from start to end */
70 /* return computed hash */
76 init_cache(Cache
**cp
, int hsz
, int bsz
,
77 int (*hfunc
)(void *, int, int), int (*cfunc
)(void *, void *, int))
79 if ((*cp
= (Cache
*) malloc(sizeof (**cp
))) == NULL
) {
80 (void) fprintf(stderr
, pkg_gt("malloc(Cache **cp)"));
84 (Bucket
*) malloc(sizeof (*(*cp
)->bp
) * hsz
)) == NULL
) {
85 (void) fprintf(stderr
, pkg_gt("malloc(Bucket cp->bp)"));
92 bzero((*cp
)->bp
, sizeof (*(*cp
)->bp
) * hsz
);
94 if (hfunc
!= (int (*)()) NULL
) {
100 if (cfunc
!= (int (*)()) NULL
) {
101 (*cp
)->cfunc
= cfunc
;
109 add_cache(Cache
*cp
, Item
*itemp
)
115 * If cp is NULL, then init_cache() wasn't called. Quietly return the
116 * error code and let the caller deal with it.
121 bp
= &cp
->bp
[(*cp
->hfunc
)(itemp
->key
, itemp
->keyl
, cp
->hsz
)];
122 if (bp
->nent
>= bp
->nalloc
) {
123 if (bp
->nalloc
== 0) {
125 (Item
**) malloc(sizeof (*bp
->itempp
) * cp
->bsz
);
127 #ifdef VERIFY_HASH_REALLOC
128 (void) fprintf(stderr
,
129 pkg_gt("realloc(%d) bucket=%d\n"),
130 bp
->nalloc
+ cp
->bsz
,
131 (*cp
->hfunc
)(itemp
->key
, itemp
->keyl
, cp
->hsz
));
132 #endif /* VERIFY_HASH_REALLOC */
134 (Item
**) malloc(sizeof (*bp
->itempp
) *
135 (bp
->nalloc
+ cp
->bsz
))) != NULL
) {
136 bcopy((char *)bp
->itempp
, (char *)titempp
,
137 (sizeof (*bp
->itempp
) * bp
->nalloc
));
139 bkmem_free(bp
->itempp
,
140 (sizeof (*bp
->itempp
) * bp
->nalloc
));
144 bp
->itempp
= titempp
;
148 if (bp
->itempp
== NULL
) {
149 (void) fprintf(stderr
,
150 pkg_gt("add_cache(): out of memory\n"));
153 bp
->nalloc
+= cp
->bsz
;
155 bp
->itempp
[bp
->nent
] = itemp
;
161 lookup_cache(Cache
*cp
, void *datap
, int datalen
)
167 * If cp is NULL, then init_cache() wasn't called. Quietly return the
168 * error code and let the caller deal with it.
174 bp
= &cp
->bp
[(*cp
->hfunc
)(datap
, datalen
, cp
->hsz
)];
176 for (i
= 0; i
< bp
->nent
; i
++) {
177 if (!(*cp
->cfunc
)((void *)bp
->itempp
[i
]->key
, datap
, datalen
)) {
178 return (bp
->itempp
[i
]);