xref: /illumos-gate/usr/src/common/ctf/ctf_hash.c (revision 3dfdac06)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
22e4586ebfSmws 
237c478bd9Sstevel@tonic-gate /*
24e4586ebfSmws  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
26*3dfdac06SAndy Fiddaman  *
27*3dfdac06SAndy Fiddaman  * Copyright 2020 OmniOS Community Edition (OmniOSce) Association.
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <ctf_impl.h>
31bc1f688bSRobert Mustacchi #include <sys/debug.h>
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate static const ushort_t _CTF_EMPTY[1] = { 0 };
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate int
ctf_hash_create(ctf_hash_t * hp,ulong_t nelems)367c478bd9Sstevel@tonic-gate ctf_hash_create(ctf_hash_t *hp, ulong_t nelems)
377c478bd9Sstevel@tonic-gate {
387c478bd9Sstevel@tonic-gate 	if (nelems > USHRT_MAX)
397c478bd9Sstevel@tonic-gate 		return (EOVERFLOW);
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate 	/*
427c478bd9Sstevel@tonic-gate 	 * If the hash table is going to be empty, don't bother allocating any
437c478bd9Sstevel@tonic-gate 	 * memory and make the only bucket point to a zero so lookups fail.
447c478bd9Sstevel@tonic-gate 	 */
457c478bd9Sstevel@tonic-gate 	if (nelems == 0) {
467c478bd9Sstevel@tonic-gate 		bzero(hp, sizeof (ctf_hash_t));
477c478bd9Sstevel@tonic-gate 		hp->h_buckets = (ushort_t *)_CTF_EMPTY;
487c478bd9Sstevel@tonic-gate 		hp->h_nbuckets = 1;
497c478bd9Sstevel@tonic-gate 		return (0);
507c478bd9Sstevel@tonic-gate 	}
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate 	hp->h_nbuckets = 211;		/* use a prime number of hash buckets */
537c478bd9Sstevel@tonic-gate 	hp->h_nelems = nelems + 1;	/* we use index zero as a sentinel */
547c478bd9Sstevel@tonic-gate 	hp->h_free = 1;			/* first free element is index 1 */
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate 	hp->h_buckets = ctf_alloc(sizeof (ushort_t) * hp->h_nbuckets);
577c478bd9Sstevel@tonic-gate 	hp->h_chains = ctf_alloc(sizeof (ctf_helem_t) * hp->h_nelems);
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	if (hp->h_buckets == NULL || hp->h_chains == NULL) {
607c478bd9Sstevel@tonic-gate 		ctf_hash_destroy(hp);
617c478bd9Sstevel@tonic-gate 		return (EAGAIN);
627c478bd9Sstevel@tonic-gate 	}
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	bzero(hp->h_buckets, sizeof (ushort_t) * hp->h_nbuckets);
657c478bd9Sstevel@tonic-gate 	bzero(hp->h_chains, sizeof (ctf_helem_t) * hp->h_nelems);
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 	return (0);
687c478bd9Sstevel@tonic-gate }
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate uint_t
ctf_hash_size(const ctf_hash_t * hp)717c478bd9Sstevel@tonic-gate ctf_hash_size(const ctf_hash_t *hp)
727c478bd9Sstevel@tonic-gate {
737c478bd9Sstevel@tonic-gate 	return (hp->h_nelems ? hp->h_nelems - 1 : 0);
747c478bd9Sstevel@tonic-gate }
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate static ulong_t
ctf_hash_compute(const char * key,size_t len)777c478bd9Sstevel@tonic-gate ctf_hash_compute(const char *key, size_t len)
787c478bd9Sstevel@tonic-gate {
797c478bd9Sstevel@tonic-gate 	ulong_t g, h = 0;
807c478bd9Sstevel@tonic-gate 	const char *p, *q = key + len;
817c478bd9Sstevel@tonic-gate 	size_t n = 0;
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	for (p = key; p < q; p++, n++) {
847c478bd9Sstevel@tonic-gate 		h = (h << 4) + *p;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 		if ((g = (h & 0xf0000000)) != 0) {
877c478bd9Sstevel@tonic-gate 			h ^= (g >> 24);
887c478bd9Sstevel@tonic-gate 			h ^= g;
897c478bd9Sstevel@tonic-gate 		}
907c478bd9Sstevel@tonic-gate 	}
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	return (h);
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate int
ctf_hash_insert(ctf_hash_t * hp,ctf_file_t * fp,ushort_t type,uint_t name)967c478bd9Sstevel@tonic-gate ctf_hash_insert(ctf_hash_t *hp, ctf_file_t *fp, ushort_t type, uint_t name)
977c478bd9Sstevel@tonic-gate {
987c478bd9Sstevel@tonic-gate 	ctf_strs_t *ctsp = &fp->ctf_str[CTF_NAME_STID(name)];
997c478bd9Sstevel@tonic-gate 	const char *str = ctsp->cts_strs + CTF_NAME_OFFSET(name);
1007c478bd9Sstevel@tonic-gate 	ctf_helem_t *hep = &hp->h_chains[hp->h_free];
1017c478bd9Sstevel@tonic-gate 	ulong_t h;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	if (type == 0)
1047c478bd9Sstevel@tonic-gate 		return (EINVAL);
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	if (hp->h_free >= hp->h_nelems)
1077c478bd9Sstevel@tonic-gate 		return (EOVERFLOW);
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	if (ctsp->cts_strs == NULL)
1107c478bd9Sstevel@tonic-gate 		return (ECTF_STRTAB);
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	if (ctsp->cts_len <= CTF_NAME_OFFSET(name))
1137c478bd9Sstevel@tonic-gate 		return (ECTF_BADNAME);
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	if (str[0] == '\0')
1167c478bd9Sstevel@tonic-gate 		return (0); /* just ignore empty strings on behalf of caller */
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	hep->h_name = name;
1197c478bd9Sstevel@tonic-gate 	hep->h_type = type;
1207c478bd9Sstevel@tonic-gate 	h = ctf_hash_compute(str, strlen(str)) % hp->h_nbuckets;
1217c478bd9Sstevel@tonic-gate 	hep->h_next = hp->h_buckets[h];
1227c478bd9Sstevel@tonic-gate 	hp->h_buckets[h] = hp->h_free++;
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	return (0);
1257c478bd9Sstevel@tonic-gate }
1267c478bd9Sstevel@tonic-gate 
127e4586ebfSmws /*
128e4586ebfSmws  * Wrapper for ctf_hash_lookup/ctf_hash_insert: if the key is already in the
129e4586ebfSmws  * hash, override the previous definition with this new official definition.
130e4586ebfSmws  * If the key is not present, then call ctf_hash_insert() and hash it in.
131e4586ebfSmws  */
132e4586ebfSmws int
ctf_hash_define(ctf_hash_t * hp,ctf_file_t * fp,ushort_t type,uint_t name)133e4586ebfSmws ctf_hash_define(ctf_hash_t *hp, ctf_file_t *fp, ushort_t type, uint_t name)
134e4586ebfSmws {
135e4586ebfSmws 	const char *str = ctf_strptr(fp, name);
136e4586ebfSmws 	ctf_helem_t *hep = ctf_hash_lookup(hp, fp, str, strlen(str));
137e4586ebfSmws 
138e4586ebfSmws 	if (hep == NULL)
139e4586ebfSmws 		return (ctf_hash_insert(hp, fp, type, name));
140e4586ebfSmws 
141e4586ebfSmws 	hep->h_type = type;
142e4586ebfSmws 	return (0);
143e4586ebfSmws }
144e4586ebfSmws 
1457c478bd9Sstevel@tonic-gate ctf_helem_t *
ctf_hash_lookup(ctf_hash_t * hp,ctf_file_t * fp,const char * key,size_t len)1467c478bd9Sstevel@tonic-gate ctf_hash_lookup(ctf_hash_t *hp, ctf_file_t *fp, const char *key, size_t len)
1477c478bd9Sstevel@tonic-gate {
1487c478bd9Sstevel@tonic-gate 	ctf_helem_t *hep;
1497c478bd9Sstevel@tonic-gate 	ctf_strs_t *ctsp;
1507c478bd9Sstevel@tonic-gate 	const char *str;
1517c478bd9Sstevel@tonic-gate 	ushort_t i;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	ulong_t h = ctf_hash_compute(key, len) % hp->h_nbuckets;
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	for (i = hp->h_buckets[h]; i != 0; i = hep->h_next) {
1567c478bd9Sstevel@tonic-gate 		hep = &hp->h_chains[i];
1577c478bd9Sstevel@tonic-gate 		ctsp = &fp->ctf_str[CTF_NAME_STID(hep->h_name)];
1587c478bd9Sstevel@tonic-gate 		str = ctsp->cts_strs + CTF_NAME_OFFSET(hep->h_name);
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 		if (strncmp(key, str, len) == 0 && str[len] == '\0')
1617c478bd9Sstevel@tonic-gate 			return (hep);
1627c478bd9Sstevel@tonic-gate 	}
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	return (NULL);
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate void
ctf_hash_destroy(ctf_hash_t * hp)1687c478bd9Sstevel@tonic-gate ctf_hash_destroy(ctf_hash_t *hp)
1697c478bd9Sstevel@tonic-gate {
1707c478bd9Sstevel@tonic-gate 	if (hp->h_buckets != NULL && hp->h_nbuckets != 1) {
1717c478bd9Sstevel@tonic-gate 		ctf_free(hp->h_buckets, sizeof (ushort_t) * hp->h_nbuckets);
1727c478bd9Sstevel@tonic-gate 		hp->h_buckets = NULL;
1737c478bd9Sstevel@tonic-gate 	}
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	if (hp->h_chains != NULL) {
1767c478bd9Sstevel@tonic-gate 		ctf_free(hp->h_chains, sizeof (ctf_helem_t) * hp->h_nelems);
1777c478bd9Sstevel@tonic-gate 		hp->h_chains = NULL;
1787c478bd9Sstevel@tonic-gate 	}
1797c478bd9Sstevel@tonic-gate }
180*3dfdac06SAndy Fiddaman 
181*3dfdac06SAndy Fiddaman void
ctf_hash_dump(const char * tag,ctf_hash_t * hp,ctf_file_t * fp)182*3dfdac06SAndy Fiddaman ctf_hash_dump(const char *tag, ctf_hash_t *hp, ctf_file_t *fp)
183*3dfdac06SAndy Fiddaman {
184*3dfdac06SAndy Fiddaman 	ctf_dprintf("---------------\nHash dump - %s\n", tag);
185*3dfdac06SAndy Fiddaman 
186*3dfdac06SAndy Fiddaman 	for (ushort_t h = 0; h < hp->h_nbuckets; h++) {
187*3dfdac06SAndy Fiddaman 		ctf_helem_t *hep;
188*3dfdac06SAndy Fiddaman 
189*3dfdac06SAndy Fiddaman 		for (ushort_t i = hp->h_buckets[h]; i != 0; i = hep->h_next) {
190*3dfdac06SAndy Fiddaman 			ctf_strs_t *ctsp;
191*3dfdac06SAndy Fiddaman 			const char *str;
192*3dfdac06SAndy Fiddaman 
193*3dfdac06SAndy Fiddaman 			hep = &hp->h_chains[i];
194*3dfdac06SAndy Fiddaman 			ctsp = &fp->ctf_str[CTF_NAME_STID(hep->h_name)];
195*3dfdac06SAndy Fiddaman 			str = ctsp->cts_strs + CTF_NAME_OFFSET(hep->h_name);
196*3dfdac06SAndy Fiddaman 
197*3dfdac06SAndy Fiddaman 			ctf_dprintf(" - %3u/%3u  - '%s' type %u\n", h, i, str,
198*3dfdac06SAndy Fiddaman 			    hep->h_type);
199*3dfdac06SAndy Fiddaman 		}
200*3dfdac06SAndy Fiddaman 	}
201*3dfdac06SAndy Fiddaman }
202