xref: /onnv-gate/usr/src/cmd/fm/fminject/common/inj_hash.c (revision 1717:ef845d4a1074)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*1717Swesolows  * Common Development and Distribution License (the "License").
6*1717Swesolows  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*1717Swesolows 
220Sstevel@tonic-gate /*
23*1717Swesolows  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <string.h>
300Sstevel@tonic-gate #include <sys/types.h>
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <inj.h>
330Sstevel@tonic-gate #include <inj_err.h>
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #define	INJ_HASHSZ	211
360Sstevel@tonic-gate 
370Sstevel@tonic-gate struct inj_var {
380Sstevel@tonic-gate 	struct inj_var *v_next;
390Sstevel@tonic-gate 	uintmax_t v_uvalue;
400Sstevel@tonic-gate 	void *v_key;
410Sstevel@tonic-gate };
420Sstevel@tonic-gate 
430Sstevel@tonic-gate void
inj_hash_create(inj_hash_t * h,ulong_t (* hfn)(void *),int (* cfn)(void *,void *))440Sstevel@tonic-gate inj_hash_create(inj_hash_t *h, ulong_t (*hfn)(void *),
450Sstevel@tonic-gate     int (*cfn)(void *, void *))
460Sstevel@tonic-gate {
470Sstevel@tonic-gate 	h->h_hash = inj_zalloc(sizeof (inj_var_t *) * INJ_HASHSZ);
480Sstevel@tonic-gate 	h->h_hashsz = INJ_HASHSZ;
490Sstevel@tonic-gate 	h->h_nelems = 0;
500Sstevel@tonic-gate 
510Sstevel@tonic-gate 	h->h_hashfn = hfn;
520Sstevel@tonic-gate 	h->h_cmpfn = cfn;
530Sstevel@tonic-gate }
540Sstevel@tonic-gate 
550Sstevel@tonic-gate static inj_var_t *
inj_var_alloc(void * key,uintmax_t value,inj_var_t * next)560Sstevel@tonic-gate inj_var_alloc(void *key, uintmax_t value, inj_var_t *next)
570Sstevel@tonic-gate {
580Sstevel@tonic-gate 	inj_var_t *v = inj_alloc(sizeof (inj_var_t));
590Sstevel@tonic-gate 
600Sstevel@tonic-gate 	v->v_next = next;
610Sstevel@tonic-gate 	v->v_key = key;
620Sstevel@tonic-gate 	v->v_uvalue = value;
630Sstevel@tonic-gate 
640Sstevel@tonic-gate 	return (v);
650Sstevel@tonic-gate }
660Sstevel@tonic-gate 
670Sstevel@tonic-gate static void
inj_var_free(inj_var_t * v,void (* freefn)(inj_var_t *,void *),void * arg)680Sstevel@tonic-gate inj_var_free(inj_var_t *v, void (*freefn)(inj_var_t *, void *), void *arg)
690Sstevel@tonic-gate {
700Sstevel@tonic-gate 	if (freefn != NULL)
710Sstevel@tonic-gate 		freefn(v, arg);
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	inj_free(v, sizeof (inj_var_t));
740Sstevel@tonic-gate }
750Sstevel@tonic-gate 
760Sstevel@tonic-gate void
inj_hash_destroy(inj_hash_t * h,void (* freefn)(inj_var_t *,void *),void * arg)770Sstevel@tonic-gate inj_hash_destroy(inj_hash_t *h, void (*freefn)(inj_var_t *, void *), void *arg)
780Sstevel@tonic-gate {
790Sstevel@tonic-gate 	inj_var_t *v, *w;
800Sstevel@tonic-gate 	size_t i;
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 	for (i = 0; i < h->h_hashsz; i++) {
830Sstevel@tonic-gate 		for (v = h->h_hash[i]; v != NULL; v = w) {
840Sstevel@tonic-gate 			w = v->v_next;
850Sstevel@tonic-gate 			inj_var_free(v, freefn, arg);
860Sstevel@tonic-gate 		}
870Sstevel@tonic-gate 	}
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 	inj_free(h->h_hash, sizeof (inj_var_t *) * INJ_HASHSZ);
900Sstevel@tonic-gate }
910Sstevel@tonic-gate 
920Sstevel@tonic-gate int
inj_hash_insert(inj_hash_t * h,void * key,uintmax_t value)930Sstevel@tonic-gate inj_hash_insert(inj_hash_t *h, void *key, uintmax_t value)
940Sstevel@tonic-gate {
950Sstevel@tonic-gate 	size_t i = h->h_hashfn(key) % h->h_hashsz;
960Sstevel@tonic-gate 	inj_var_t *v;
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	for (v = h->h_hash[i]; v != NULL; v = v->v_next) {
990Sstevel@tonic-gate 		if (h->h_cmpfn(v->v_key, key) == 0)
1000Sstevel@tonic-gate 			return (-1);
1010Sstevel@tonic-gate 	}
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	/* not found - make a new one */
1040Sstevel@tonic-gate 	v = inj_var_alloc(key, value, h->h_hash[i]);
1050Sstevel@tonic-gate 	h->h_hash[i] = v;
1060Sstevel@tonic-gate 	h->h_nelems++;
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 	return (0);
1090Sstevel@tonic-gate }
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate inj_var_t *
inj_hash_lookup(inj_hash_t * h,void * key)1120Sstevel@tonic-gate inj_hash_lookup(inj_hash_t *h, void *key)
1130Sstevel@tonic-gate {
1140Sstevel@tonic-gate 	size_t i = h->h_hashfn(key) % h->h_hashsz;
1150Sstevel@tonic-gate 	inj_var_t *v;
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	for (v = h->h_hash[i]; v != NULL; v = v->v_next) {
1180Sstevel@tonic-gate 		if (h->h_cmpfn(v->v_key, key) == 0)
1190Sstevel@tonic-gate 			return (v);
1200Sstevel@tonic-gate 	}
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 	return (NULL);
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate void *
inj_hash_get_key(inj_var_t * v)1260Sstevel@tonic-gate inj_hash_get_key(inj_var_t *v)
1270Sstevel@tonic-gate {
1280Sstevel@tonic-gate 	return (v->v_key);
1290Sstevel@tonic-gate }
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate uintmax_t
inj_hash_get_value(inj_var_t * v)1320Sstevel@tonic-gate inj_hash_get_value(inj_var_t *v)
1330Sstevel@tonic-gate {
1340Sstevel@tonic-gate 	return (v->v_uvalue);
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate void *
inj_hash_get_cookie(inj_var_t * v)1380Sstevel@tonic-gate inj_hash_get_cookie(inj_var_t *v)
1390Sstevel@tonic-gate {
140*1717Swesolows 	return ((void *)(uintptr_t)v->v_uvalue);
1410Sstevel@tonic-gate }
142