17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*80ab886dSwesolows * Common Development and Distribution License (the "License").
6*80ab886dSwesolows * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
21*80ab886dSwesolows
227c478bd9Sstevel@tonic-gate /*
23*80ab886dSwesolows * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate #include <string.h>
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate #include <inj.h>
337c478bd9Sstevel@tonic-gate #include <inj_err.h>
347c478bd9Sstevel@tonic-gate
357c478bd9Sstevel@tonic-gate #define INJ_HASHSZ 211
367c478bd9Sstevel@tonic-gate
377c478bd9Sstevel@tonic-gate struct inj_var {
387c478bd9Sstevel@tonic-gate struct inj_var *v_next;
397c478bd9Sstevel@tonic-gate uintmax_t v_uvalue;
407c478bd9Sstevel@tonic-gate void *v_key;
417c478bd9Sstevel@tonic-gate };
427c478bd9Sstevel@tonic-gate
437c478bd9Sstevel@tonic-gate void
inj_hash_create(inj_hash_t * h,ulong_t (* hfn)(void *),int (* cfn)(void *,void *))447c478bd9Sstevel@tonic-gate inj_hash_create(inj_hash_t *h, ulong_t (*hfn)(void *),
457c478bd9Sstevel@tonic-gate int (*cfn)(void *, void *))
467c478bd9Sstevel@tonic-gate {
477c478bd9Sstevel@tonic-gate h->h_hash = inj_zalloc(sizeof (inj_var_t *) * INJ_HASHSZ);
487c478bd9Sstevel@tonic-gate h->h_hashsz = INJ_HASHSZ;
497c478bd9Sstevel@tonic-gate h->h_nelems = 0;
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate h->h_hashfn = hfn;
527c478bd9Sstevel@tonic-gate h->h_cmpfn = cfn;
537c478bd9Sstevel@tonic-gate }
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate static inj_var_t *
inj_var_alloc(void * key,uintmax_t value,inj_var_t * next)567c478bd9Sstevel@tonic-gate inj_var_alloc(void *key, uintmax_t value, inj_var_t *next)
577c478bd9Sstevel@tonic-gate {
587c478bd9Sstevel@tonic-gate inj_var_t *v = inj_alloc(sizeof (inj_var_t));
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate v->v_next = next;
617c478bd9Sstevel@tonic-gate v->v_key = key;
627c478bd9Sstevel@tonic-gate v->v_uvalue = value;
637c478bd9Sstevel@tonic-gate
647c478bd9Sstevel@tonic-gate return (v);
657c478bd9Sstevel@tonic-gate }
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate static void
inj_var_free(inj_var_t * v,void (* freefn)(inj_var_t *,void *),void * arg)687c478bd9Sstevel@tonic-gate inj_var_free(inj_var_t *v, void (*freefn)(inj_var_t *, void *), void *arg)
697c478bd9Sstevel@tonic-gate {
707c478bd9Sstevel@tonic-gate if (freefn != NULL)
717c478bd9Sstevel@tonic-gate freefn(v, arg);
727c478bd9Sstevel@tonic-gate
737c478bd9Sstevel@tonic-gate inj_free(v, sizeof (inj_var_t));
747c478bd9Sstevel@tonic-gate }
757c478bd9Sstevel@tonic-gate
767c478bd9Sstevel@tonic-gate void
inj_hash_destroy(inj_hash_t * h,void (* freefn)(inj_var_t *,void *),void * arg)777c478bd9Sstevel@tonic-gate inj_hash_destroy(inj_hash_t *h, void (*freefn)(inj_var_t *, void *), void *arg)
787c478bd9Sstevel@tonic-gate {
797c478bd9Sstevel@tonic-gate inj_var_t *v, *w;
807c478bd9Sstevel@tonic-gate size_t i;
817c478bd9Sstevel@tonic-gate
827c478bd9Sstevel@tonic-gate for (i = 0; i < h->h_hashsz; i++) {
837c478bd9Sstevel@tonic-gate for (v = h->h_hash[i]; v != NULL; v = w) {
847c478bd9Sstevel@tonic-gate w = v->v_next;
857c478bd9Sstevel@tonic-gate inj_var_free(v, freefn, arg);
867c478bd9Sstevel@tonic-gate }
877c478bd9Sstevel@tonic-gate }
887c478bd9Sstevel@tonic-gate
897c478bd9Sstevel@tonic-gate inj_free(h->h_hash, sizeof (inj_var_t *) * INJ_HASHSZ);
907c478bd9Sstevel@tonic-gate }
917c478bd9Sstevel@tonic-gate
927c478bd9Sstevel@tonic-gate int
inj_hash_insert(inj_hash_t * h,void * key,uintmax_t value)937c478bd9Sstevel@tonic-gate inj_hash_insert(inj_hash_t *h, void *key, uintmax_t value)
947c478bd9Sstevel@tonic-gate {
957c478bd9Sstevel@tonic-gate size_t i = h->h_hashfn(key) % h->h_hashsz;
967c478bd9Sstevel@tonic-gate inj_var_t *v;
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate for (v = h->h_hash[i]; v != NULL; v = v->v_next) {
997c478bd9Sstevel@tonic-gate if (h->h_cmpfn(v->v_key, key) == 0)
1007c478bd9Sstevel@tonic-gate return (-1);
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate
1037c478bd9Sstevel@tonic-gate /* not found - make a new one */
1047c478bd9Sstevel@tonic-gate v = inj_var_alloc(key, value, h->h_hash[i]);
1057c478bd9Sstevel@tonic-gate h->h_hash[i] = v;
1067c478bd9Sstevel@tonic-gate h->h_nelems++;
1077c478bd9Sstevel@tonic-gate
1087c478bd9Sstevel@tonic-gate return (0);
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate
1117c478bd9Sstevel@tonic-gate inj_var_t *
inj_hash_lookup(inj_hash_t * h,void * key)1127c478bd9Sstevel@tonic-gate inj_hash_lookup(inj_hash_t *h, void *key)
1137c478bd9Sstevel@tonic-gate {
1147c478bd9Sstevel@tonic-gate size_t i = h->h_hashfn(key) % h->h_hashsz;
1157c478bd9Sstevel@tonic-gate inj_var_t *v;
1167c478bd9Sstevel@tonic-gate
1177c478bd9Sstevel@tonic-gate for (v = h->h_hash[i]; v != NULL; v = v->v_next) {
1187c478bd9Sstevel@tonic-gate if (h->h_cmpfn(v->v_key, key) == 0)
1197c478bd9Sstevel@tonic-gate return (v);
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate
1227c478bd9Sstevel@tonic-gate return (NULL);
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate
1257c478bd9Sstevel@tonic-gate void *
inj_hash_get_key(inj_var_t * v)1267c478bd9Sstevel@tonic-gate inj_hash_get_key(inj_var_t *v)
1277c478bd9Sstevel@tonic-gate {
1287c478bd9Sstevel@tonic-gate return (v->v_key);
1297c478bd9Sstevel@tonic-gate }
1307c478bd9Sstevel@tonic-gate
1317c478bd9Sstevel@tonic-gate uintmax_t
inj_hash_get_value(inj_var_t * v)1327c478bd9Sstevel@tonic-gate inj_hash_get_value(inj_var_t *v)
1337c478bd9Sstevel@tonic-gate {
1347c478bd9Sstevel@tonic-gate return (v->v_uvalue);
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate
1377c478bd9Sstevel@tonic-gate void *
inj_hash_get_cookie(inj_var_t * v)1387c478bd9Sstevel@tonic-gate inj_hash_get_cookie(inj_var_t *v)
1397c478bd9Sstevel@tonic-gate {
140*80ab886dSwesolows return ((void *)(uintptr_t)v->v_uvalue);
1417c478bd9Sstevel@tonic-gate }
142