xref: /titanic_51/usr/src/common/ctf/ctf_hash.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <ctf_impl.h>
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate static const ushort_t _CTF_EMPTY[1] = { 0 };
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate int
34*7c478bd9Sstevel@tonic-gate ctf_hash_create(ctf_hash_t *hp, ulong_t nelems)
35*7c478bd9Sstevel@tonic-gate {
36*7c478bd9Sstevel@tonic-gate 	if (nelems > USHRT_MAX)
37*7c478bd9Sstevel@tonic-gate 		return (EOVERFLOW);
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate 	/*
40*7c478bd9Sstevel@tonic-gate 	 * If the hash table is going to be empty, don't bother allocating any
41*7c478bd9Sstevel@tonic-gate 	 * memory and make the only bucket point to a zero so lookups fail.
42*7c478bd9Sstevel@tonic-gate 	 */
43*7c478bd9Sstevel@tonic-gate 	if (nelems == 0) {
44*7c478bd9Sstevel@tonic-gate 		bzero(hp, sizeof (ctf_hash_t));
45*7c478bd9Sstevel@tonic-gate 		hp->h_buckets = (ushort_t *)_CTF_EMPTY;
46*7c478bd9Sstevel@tonic-gate 		hp->h_nbuckets = 1;
47*7c478bd9Sstevel@tonic-gate 		return (0);
48*7c478bd9Sstevel@tonic-gate 	}
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate 	hp->h_nbuckets = 211;		/* use a prime number of hash buckets */
51*7c478bd9Sstevel@tonic-gate 	hp->h_nelems = nelems + 1;	/* we use index zero as a sentinel */
52*7c478bd9Sstevel@tonic-gate 	hp->h_free = 1;			/* first free element is index 1 */
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate 	hp->h_buckets = ctf_alloc(sizeof (ushort_t) * hp->h_nbuckets);
55*7c478bd9Sstevel@tonic-gate 	hp->h_chains = ctf_alloc(sizeof (ctf_helem_t) * hp->h_nelems);
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate 	if (hp->h_buckets == NULL || hp->h_chains == NULL) {
58*7c478bd9Sstevel@tonic-gate 		ctf_hash_destroy(hp);
59*7c478bd9Sstevel@tonic-gate 		return (EAGAIN);
60*7c478bd9Sstevel@tonic-gate 	}
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate 	bzero(hp->h_buckets, sizeof (ushort_t) * hp->h_nbuckets);
63*7c478bd9Sstevel@tonic-gate 	bzero(hp->h_chains, sizeof (ctf_helem_t) * hp->h_nelems);
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate 	return (0);
66*7c478bd9Sstevel@tonic-gate }
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate uint_t
69*7c478bd9Sstevel@tonic-gate ctf_hash_size(const ctf_hash_t *hp)
70*7c478bd9Sstevel@tonic-gate {
71*7c478bd9Sstevel@tonic-gate 	return (hp->h_nelems ? hp->h_nelems - 1 : 0);
72*7c478bd9Sstevel@tonic-gate }
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate static ulong_t
75*7c478bd9Sstevel@tonic-gate ctf_hash_compute(const char *key, size_t len)
76*7c478bd9Sstevel@tonic-gate {
77*7c478bd9Sstevel@tonic-gate 	ulong_t g, h = 0;
78*7c478bd9Sstevel@tonic-gate 	const char *p, *q = key + len;
79*7c478bd9Sstevel@tonic-gate 	size_t n = 0;
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate 	for (p = key; p < q; p++, n++) {
82*7c478bd9Sstevel@tonic-gate 		h = (h << 4) + *p;
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate 		if ((g = (h & 0xf0000000)) != 0) {
85*7c478bd9Sstevel@tonic-gate 			h ^= (g >> 24);
86*7c478bd9Sstevel@tonic-gate 			h ^= g;
87*7c478bd9Sstevel@tonic-gate 		}
88*7c478bd9Sstevel@tonic-gate 	}
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate 	return (h);
91*7c478bd9Sstevel@tonic-gate }
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate int
94*7c478bd9Sstevel@tonic-gate ctf_hash_insert(ctf_hash_t *hp, ctf_file_t *fp, ushort_t type, uint_t name)
95*7c478bd9Sstevel@tonic-gate {
96*7c478bd9Sstevel@tonic-gate 	ctf_strs_t *ctsp = &fp->ctf_str[CTF_NAME_STID(name)];
97*7c478bd9Sstevel@tonic-gate 	const char *str = ctsp->cts_strs + CTF_NAME_OFFSET(name);
98*7c478bd9Sstevel@tonic-gate 	ctf_helem_t *hep = &hp->h_chains[hp->h_free];
99*7c478bd9Sstevel@tonic-gate 	ulong_t h;
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 	if (type == 0)
102*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate 	if (hp->h_free >= hp->h_nelems)
105*7c478bd9Sstevel@tonic-gate 		return (EOVERFLOW);
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate 	if (ctsp->cts_strs == NULL)
108*7c478bd9Sstevel@tonic-gate 		return (ECTF_STRTAB);
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate 	if (ctsp->cts_len <= CTF_NAME_OFFSET(name))
111*7c478bd9Sstevel@tonic-gate 		return (ECTF_BADNAME);
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate 	if (str[0] == '\0')
114*7c478bd9Sstevel@tonic-gate 		return (0); /* just ignore empty strings on behalf of caller */
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate 	hep->h_name = name;
117*7c478bd9Sstevel@tonic-gate 	hep->h_type = type;
118*7c478bd9Sstevel@tonic-gate 	h = ctf_hash_compute(str, strlen(str)) % hp->h_nbuckets;
119*7c478bd9Sstevel@tonic-gate 	hep->h_next = hp->h_buckets[h];
120*7c478bd9Sstevel@tonic-gate 	hp->h_buckets[h] = hp->h_free++;
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate 	return (0);
123*7c478bd9Sstevel@tonic-gate }
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate ctf_helem_t *
126*7c478bd9Sstevel@tonic-gate ctf_hash_lookup(ctf_hash_t *hp, ctf_file_t *fp, const char *key, size_t len)
127*7c478bd9Sstevel@tonic-gate {
128*7c478bd9Sstevel@tonic-gate 	ctf_helem_t *hep;
129*7c478bd9Sstevel@tonic-gate 	ctf_strs_t *ctsp;
130*7c478bd9Sstevel@tonic-gate 	const char *str;
131*7c478bd9Sstevel@tonic-gate 	ushort_t i;
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 	ulong_t h = ctf_hash_compute(key, len) % hp->h_nbuckets;
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 	for (i = hp->h_buckets[h]; i != 0; i = hep->h_next) {
136*7c478bd9Sstevel@tonic-gate 		hep = &hp->h_chains[i];
137*7c478bd9Sstevel@tonic-gate 		ctsp = &fp->ctf_str[CTF_NAME_STID(hep->h_name)];
138*7c478bd9Sstevel@tonic-gate 		str = ctsp->cts_strs + CTF_NAME_OFFSET(hep->h_name);
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate 		if (strncmp(key, str, len) == 0 && str[len] == '\0')
141*7c478bd9Sstevel@tonic-gate 			return (hep);
142*7c478bd9Sstevel@tonic-gate 	}
143*7c478bd9Sstevel@tonic-gate 
144*7c478bd9Sstevel@tonic-gate 	return (NULL);
145*7c478bd9Sstevel@tonic-gate }
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate void
148*7c478bd9Sstevel@tonic-gate ctf_hash_destroy(ctf_hash_t *hp)
149*7c478bd9Sstevel@tonic-gate {
150*7c478bd9Sstevel@tonic-gate 	if (hp->h_buckets != NULL && hp->h_nbuckets != 1) {
151*7c478bd9Sstevel@tonic-gate 		ctf_free(hp->h_buckets, sizeof (ushort_t) * hp->h_nbuckets);
152*7c478bd9Sstevel@tonic-gate 		hp->h_buckets = NULL;
153*7c478bd9Sstevel@tonic-gate 	}
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate 	if (hp->h_chains != NULL) {
156*7c478bd9Sstevel@tonic-gate 		ctf_free(hp->h_chains, sizeof (ctf_helem_t) * hp->h_nelems);
157*7c478bd9Sstevel@tonic-gate 		hp->h_chains = NULL;
158*7c478bd9Sstevel@tonic-gate 	}
159*7c478bd9Sstevel@tonic-gate }
160