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