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
50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
70Sstevel@tonic-gate * with the License.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate * See the License for the specific language governing permissions
120Sstevel@tonic-gate * and limitations under the License.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * CDDL HEADER END
210Sstevel@tonic-gate */
22*1222Smws
230Sstevel@tonic-gate /*
24*1222Smws * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
250Sstevel@tonic-gate * Use is subject to license terms.
260Sstevel@tonic-gate */
270Sstevel@tonic-gate
280Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
290Sstevel@tonic-gate
300Sstevel@tonic-gate #include <ctf_impl.h>
310Sstevel@tonic-gate
320Sstevel@tonic-gate static const ushort_t _CTF_EMPTY[1] = { 0 };
330Sstevel@tonic-gate
340Sstevel@tonic-gate int
ctf_hash_create(ctf_hash_t * hp,ulong_t nelems)350Sstevel@tonic-gate ctf_hash_create(ctf_hash_t *hp, ulong_t nelems)
360Sstevel@tonic-gate {
370Sstevel@tonic-gate if (nelems > USHRT_MAX)
380Sstevel@tonic-gate return (EOVERFLOW);
390Sstevel@tonic-gate
400Sstevel@tonic-gate /*
410Sstevel@tonic-gate * If the hash table is going to be empty, don't bother allocating any
420Sstevel@tonic-gate * memory and make the only bucket point to a zero so lookups fail.
430Sstevel@tonic-gate */
440Sstevel@tonic-gate if (nelems == 0) {
450Sstevel@tonic-gate bzero(hp, sizeof (ctf_hash_t));
460Sstevel@tonic-gate hp->h_buckets = (ushort_t *)_CTF_EMPTY;
470Sstevel@tonic-gate hp->h_nbuckets = 1;
480Sstevel@tonic-gate return (0);
490Sstevel@tonic-gate }
500Sstevel@tonic-gate
510Sstevel@tonic-gate hp->h_nbuckets = 211; /* use a prime number of hash buckets */
520Sstevel@tonic-gate hp->h_nelems = nelems + 1; /* we use index zero as a sentinel */
530Sstevel@tonic-gate hp->h_free = 1; /* first free element is index 1 */
540Sstevel@tonic-gate
550Sstevel@tonic-gate hp->h_buckets = ctf_alloc(sizeof (ushort_t) * hp->h_nbuckets);
560Sstevel@tonic-gate hp->h_chains = ctf_alloc(sizeof (ctf_helem_t) * hp->h_nelems);
570Sstevel@tonic-gate
580Sstevel@tonic-gate if (hp->h_buckets == NULL || hp->h_chains == NULL) {
590Sstevel@tonic-gate ctf_hash_destroy(hp);
600Sstevel@tonic-gate return (EAGAIN);
610Sstevel@tonic-gate }
620Sstevel@tonic-gate
630Sstevel@tonic-gate bzero(hp->h_buckets, sizeof (ushort_t) * hp->h_nbuckets);
640Sstevel@tonic-gate bzero(hp->h_chains, sizeof (ctf_helem_t) * hp->h_nelems);
650Sstevel@tonic-gate
660Sstevel@tonic-gate return (0);
670Sstevel@tonic-gate }
680Sstevel@tonic-gate
690Sstevel@tonic-gate uint_t
ctf_hash_size(const ctf_hash_t * hp)700Sstevel@tonic-gate ctf_hash_size(const ctf_hash_t *hp)
710Sstevel@tonic-gate {
720Sstevel@tonic-gate return (hp->h_nelems ? hp->h_nelems - 1 : 0);
730Sstevel@tonic-gate }
740Sstevel@tonic-gate
750Sstevel@tonic-gate static ulong_t
ctf_hash_compute(const char * key,size_t len)760Sstevel@tonic-gate ctf_hash_compute(const char *key, size_t len)
770Sstevel@tonic-gate {
780Sstevel@tonic-gate ulong_t g, h = 0;
790Sstevel@tonic-gate const char *p, *q = key + len;
800Sstevel@tonic-gate size_t n = 0;
810Sstevel@tonic-gate
820Sstevel@tonic-gate for (p = key; p < q; p++, n++) {
830Sstevel@tonic-gate h = (h << 4) + *p;
840Sstevel@tonic-gate
850Sstevel@tonic-gate if ((g = (h & 0xf0000000)) != 0) {
860Sstevel@tonic-gate h ^= (g >> 24);
870Sstevel@tonic-gate h ^= g;
880Sstevel@tonic-gate }
890Sstevel@tonic-gate }
900Sstevel@tonic-gate
910Sstevel@tonic-gate return (h);
920Sstevel@tonic-gate }
930Sstevel@tonic-gate
940Sstevel@tonic-gate int
ctf_hash_insert(ctf_hash_t * hp,ctf_file_t * fp,ushort_t type,uint_t name)950Sstevel@tonic-gate ctf_hash_insert(ctf_hash_t *hp, ctf_file_t *fp, ushort_t type, uint_t name)
960Sstevel@tonic-gate {
970Sstevel@tonic-gate ctf_strs_t *ctsp = &fp->ctf_str[CTF_NAME_STID(name)];
980Sstevel@tonic-gate const char *str = ctsp->cts_strs + CTF_NAME_OFFSET(name);
990Sstevel@tonic-gate ctf_helem_t *hep = &hp->h_chains[hp->h_free];
1000Sstevel@tonic-gate ulong_t h;
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate if (type == 0)
1030Sstevel@tonic-gate return (EINVAL);
1040Sstevel@tonic-gate
1050Sstevel@tonic-gate if (hp->h_free >= hp->h_nelems)
1060Sstevel@tonic-gate return (EOVERFLOW);
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate if (ctsp->cts_strs == NULL)
1090Sstevel@tonic-gate return (ECTF_STRTAB);
1100Sstevel@tonic-gate
1110Sstevel@tonic-gate if (ctsp->cts_len <= CTF_NAME_OFFSET(name))
1120Sstevel@tonic-gate return (ECTF_BADNAME);
1130Sstevel@tonic-gate
1140Sstevel@tonic-gate if (str[0] == '\0')
1150Sstevel@tonic-gate return (0); /* just ignore empty strings on behalf of caller */
1160Sstevel@tonic-gate
1170Sstevel@tonic-gate hep->h_name = name;
1180Sstevel@tonic-gate hep->h_type = type;
1190Sstevel@tonic-gate h = ctf_hash_compute(str, strlen(str)) % hp->h_nbuckets;
1200Sstevel@tonic-gate hep->h_next = hp->h_buckets[h];
1210Sstevel@tonic-gate hp->h_buckets[h] = hp->h_free++;
1220Sstevel@tonic-gate
1230Sstevel@tonic-gate return (0);
1240Sstevel@tonic-gate }
1250Sstevel@tonic-gate
126*1222Smws /*
127*1222Smws * Wrapper for ctf_hash_lookup/ctf_hash_insert: if the key is already in the
128*1222Smws * hash, override the previous definition with this new official definition.
129*1222Smws * If the key is not present, then call ctf_hash_insert() and hash it in.
130*1222Smws */
131*1222Smws int
ctf_hash_define(ctf_hash_t * hp,ctf_file_t * fp,ushort_t type,uint_t name)132*1222Smws ctf_hash_define(ctf_hash_t *hp, ctf_file_t *fp, ushort_t type, uint_t name)
133*1222Smws {
134*1222Smws const char *str = ctf_strptr(fp, name);
135*1222Smws ctf_helem_t *hep = ctf_hash_lookup(hp, fp, str, strlen(str));
136*1222Smws
137*1222Smws if (hep == NULL)
138*1222Smws return (ctf_hash_insert(hp, fp, type, name));
139*1222Smws
140*1222Smws hep->h_type = type;
141*1222Smws return (0);
142*1222Smws }
143*1222Smws
1440Sstevel@tonic-gate ctf_helem_t *
ctf_hash_lookup(ctf_hash_t * hp,ctf_file_t * fp,const char * key,size_t len)1450Sstevel@tonic-gate ctf_hash_lookup(ctf_hash_t *hp, ctf_file_t *fp, const char *key, size_t len)
1460Sstevel@tonic-gate {
1470Sstevel@tonic-gate ctf_helem_t *hep;
1480Sstevel@tonic-gate ctf_strs_t *ctsp;
1490Sstevel@tonic-gate const char *str;
1500Sstevel@tonic-gate ushort_t i;
1510Sstevel@tonic-gate
1520Sstevel@tonic-gate ulong_t h = ctf_hash_compute(key, len) % hp->h_nbuckets;
1530Sstevel@tonic-gate
1540Sstevel@tonic-gate for (i = hp->h_buckets[h]; i != 0; i = hep->h_next) {
1550Sstevel@tonic-gate hep = &hp->h_chains[i];
1560Sstevel@tonic-gate ctsp = &fp->ctf_str[CTF_NAME_STID(hep->h_name)];
1570Sstevel@tonic-gate str = ctsp->cts_strs + CTF_NAME_OFFSET(hep->h_name);
1580Sstevel@tonic-gate
1590Sstevel@tonic-gate if (strncmp(key, str, len) == 0 && str[len] == '\0')
1600Sstevel@tonic-gate return (hep);
1610Sstevel@tonic-gate }
1620Sstevel@tonic-gate
1630Sstevel@tonic-gate return (NULL);
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate
1660Sstevel@tonic-gate void
ctf_hash_destroy(ctf_hash_t * hp)1670Sstevel@tonic-gate ctf_hash_destroy(ctf_hash_t *hp)
1680Sstevel@tonic-gate {
1690Sstevel@tonic-gate if (hp->h_buckets != NULL && hp->h_nbuckets != 1) {
1700Sstevel@tonic-gate ctf_free(hp->h_buckets, sizeof (ushort_t) * hp->h_nbuckets);
1710Sstevel@tonic-gate hp->h_buckets = NULL;
1720Sstevel@tonic-gate }
1730Sstevel@tonic-gate
1740Sstevel@tonic-gate if (hp->h_chains != NULL) {
1750Sstevel@tonic-gate ctf_free(hp->h_chains, sizeof (ctf_helem_t) * hp->h_nelems);
1760Sstevel@tonic-gate hp->h_chains = NULL;
1770Sstevel@tonic-gate }
1780Sstevel@tonic-gate }
179