xref: /onnv-gate/usr/src/lib/libdtrace/common/dt_strtab.c (revision 2769:b2e3d55c6e12)
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
5*2769Sahl  * Common Development and Distribution License (the "License").
6*2769Sahl  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*2769Sahl 
220Sstevel@tonic-gate /*
23*2769Sahl  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/sysmacros.h>
310Sstevel@tonic-gate #include <strings.h>
320Sstevel@tonic-gate #include <stdlib.h>
330Sstevel@tonic-gate #include <assert.h>
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #include <dt_strtab.h>
360Sstevel@tonic-gate #include <dt_impl.h>
370Sstevel@tonic-gate 
380Sstevel@tonic-gate static int
dt_strtab_grow(dt_strtab_t * sp)390Sstevel@tonic-gate dt_strtab_grow(dt_strtab_t *sp)
400Sstevel@tonic-gate {
410Sstevel@tonic-gate 	char *ptr, **bufs;
420Sstevel@tonic-gate 
430Sstevel@tonic-gate 	if ((ptr = malloc(sp->str_bufsz)) == NULL)
440Sstevel@tonic-gate 		return (-1);
450Sstevel@tonic-gate 
460Sstevel@tonic-gate 	bufs = realloc(sp->str_bufs, (sp->str_nbufs + 1) * sizeof (char *));
470Sstevel@tonic-gate 
480Sstevel@tonic-gate 	if (bufs == NULL) {
490Sstevel@tonic-gate 		free(ptr);
500Sstevel@tonic-gate 		return (-1);
510Sstevel@tonic-gate 	}
520Sstevel@tonic-gate 
530Sstevel@tonic-gate 	sp->str_nbufs++;
540Sstevel@tonic-gate 	sp->str_bufs = bufs;
550Sstevel@tonic-gate 	sp->str_ptr = ptr;
560Sstevel@tonic-gate 	sp->str_bufs[sp->str_nbufs - 1] = sp->str_ptr;
570Sstevel@tonic-gate 
580Sstevel@tonic-gate 	return (0);
590Sstevel@tonic-gate }
600Sstevel@tonic-gate 
610Sstevel@tonic-gate dt_strtab_t *
dt_strtab_create(size_t bufsz)620Sstevel@tonic-gate dt_strtab_create(size_t bufsz)
630Sstevel@tonic-gate {
640Sstevel@tonic-gate 	dt_strtab_t *sp = malloc(sizeof (dt_strtab_t));
650Sstevel@tonic-gate 	uint_t nbuckets = _dtrace_strbuckets;
660Sstevel@tonic-gate 
670Sstevel@tonic-gate 	assert(bufsz != 0);
680Sstevel@tonic-gate 
690Sstevel@tonic-gate 	if (sp == NULL)
700Sstevel@tonic-gate 		return (NULL);
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 	bzero(sp, sizeof (dt_strtab_t));
730Sstevel@tonic-gate 	sp->str_hash = malloc(nbuckets * sizeof (dt_strhash_t *));
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 	if (sp->str_hash == NULL)
760Sstevel@tonic-gate 		goto err;
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 	bzero(sp->str_hash, nbuckets * sizeof (dt_strhash_t *));
790Sstevel@tonic-gate 	sp->str_hashsz = nbuckets;
800Sstevel@tonic-gate 	sp->str_bufs = NULL;
810Sstevel@tonic-gate 	sp->str_ptr = NULL;
820Sstevel@tonic-gate 	sp->str_nbufs = 0;
830Sstevel@tonic-gate 	sp->str_bufsz = bufsz;
840Sstevel@tonic-gate 	sp->str_nstrs = 1;
850Sstevel@tonic-gate 	sp->str_size = 1;
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 	if (dt_strtab_grow(sp) == -1)
880Sstevel@tonic-gate 		goto err;
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 	*sp->str_ptr++ = '\0';
910Sstevel@tonic-gate 	return (sp);
920Sstevel@tonic-gate 
930Sstevel@tonic-gate err:
940Sstevel@tonic-gate 	dt_strtab_destroy(sp);
950Sstevel@tonic-gate 	return (NULL);
960Sstevel@tonic-gate }
970Sstevel@tonic-gate 
980Sstevel@tonic-gate void
dt_strtab_destroy(dt_strtab_t * sp)990Sstevel@tonic-gate dt_strtab_destroy(dt_strtab_t *sp)
1000Sstevel@tonic-gate {
1010Sstevel@tonic-gate 	dt_strhash_t *hp, *hq;
1020Sstevel@tonic-gate 	ulong_t i;
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	for (i = 0; i < sp->str_hashsz; i++) {
1050Sstevel@tonic-gate 		for (hp = sp->str_hash[i]; hp != NULL; hp = hq) {
1060Sstevel@tonic-gate 			hq = hp->str_next;
1070Sstevel@tonic-gate 			free(hp);
1080Sstevel@tonic-gate 		}
1090Sstevel@tonic-gate 	}
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	for (i = 0; i < sp->str_nbufs; i++)
1120Sstevel@tonic-gate 		free(sp->str_bufs[i]);
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	if (sp->str_hash != NULL)
1150Sstevel@tonic-gate 		free(sp->str_hash);
1160Sstevel@tonic-gate 	if (sp->str_bufs != NULL)
1170Sstevel@tonic-gate 		free(sp->str_bufs);
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	free(sp);
1200Sstevel@tonic-gate }
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate ulong_t
dt_strtab_hash(const char * key,size_t * len)1230Sstevel@tonic-gate dt_strtab_hash(const char *key, size_t *len)
1240Sstevel@tonic-gate {
1250Sstevel@tonic-gate 	ulong_t g, h = 0;
1260Sstevel@tonic-gate 	const char *p;
1270Sstevel@tonic-gate 	size_t n = 0;
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 	for (p = key; *p != '\0'; p++, n++) {
1300Sstevel@tonic-gate 		h = (h << 4) + *p;
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 		if ((g = (h & 0xf0000000)) != 0) {
1330Sstevel@tonic-gate 			h ^= (g >> 24);
1340Sstevel@tonic-gate 			h ^= g;
1350Sstevel@tonic-gate 		}
1360Sstevel@tonic-gate 	}
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	if (len != NULL)
1390Sstevel@tonic-gate 		*len = n;
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 	return (h);
1420Sstevel@tonic-gate }
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate static int
dt_strtab_compare(dt_strtab_t * sp,dt_strhash_t * hp,const char * str,size_t len)1450Sstevel@tonic-gate dt_strtab_compare(dt_strtab_t *sp, dt_strhash_t *hp,
1460Sstevel@tonic-gate     const char *str, size_t len)
1470Sstevel@tonic-gate {
1480Sstevel@tonic-gate 	ulong_t b = hp->str_buf;
1490Sstevel@tonic-gate 	const char *buf = hp->str_data;
1500Sstevel@tonic-gate 	size_t resid, n;
1510Sstevel@tonic-gate 	int rv;
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	while (len != 0) {
1540Sstevel@tonic-gate 		if (buf == sp->str_bufs[b] + sp->str_bufsz)
1550Sstevel@tonic-gate 			buf = sp->str_bufs[++b];
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 		resid = sp->str_bufs[b] + sp->str_bufsz - buf;
1580Sstevel@tonic-gate 		n = MIN(resid, len);
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 		if ((rv = strncmp(buf, str, n)) != 0)
1610Sstevel@tonic-gate 			return (rv);
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 		buf += n;
1640Sstevel@tonic-gate 		str += n;
1650Sstevel@tonic-gate 		len -= n;
1660Sstevel@tonic-gate 	}
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	return (0);
1690Sstevel@tonic-gate }
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate static int
dt_strtab_copyin(dt_strtab_t * sp,const char * str,size_t len)1720Sstevel@tonic-gate dt_strtab_copyin(dt_strtab_t *sp, const char *str, size_t len)
1730Sstevel@tonic-gate {
1740Sstevel@tonic-gate 	char *old_p = sp->str_ptr;
1750Sstevel@tonic-gate 	ulong_t old_n = sp->str_nbufs;
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	ulong_t b = sp->str_nbufs - 1;
1780Sstevel@tonic-gate 	size_t resid, n;
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	while (len != 0) {
1810Sstevel@tonic-gate 		if (sp->str_ptr == sp->str_bufs[b] + sp->str_bufsz) {
1820Sstevel@tonic-gate 			if (dt_strtab_grow(sp) == -1)
1830Sstevel@tonic-gate 				goto err;
1840Sstevel@tonic-gate 			b++;
1850Sstevel@tonic-gate 		}
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 		resid = sp->str_bufs[b] + sp->str_bufsz - sp->str_ptr;
1880Sstevel@tonic-gate 		n = MIN(resid, len);
1890Sstevel@tonic-gate 		bcopy(str, sp->str_ptr, n);
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 		sp->str_ptr += n;
1920Sstevel@tonic-gate 		str += n;
1930Sstevel@tonic-gate 		len -= n;
1940Sstevel@tonic-gate 	}
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	return (0);
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate err:
1990Sstevel@tonic-gate 	while (sp->str_nbufs != old_n)
2000Sstevel@tonic-gate 		free(sp->str_bufs[--sp->str_nbufs]);
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	sp->str_ptr = old_p;
2030Sstevel@tonic-gate 	return (-1);
2040Sstevel@tonic-gate }
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate ssize_t
dt_strtab_index(dt_strtab_t * sp,const char * str)207*2769Sahl dt_strtab_index(dt_strtab_t *sp, const char *str)
2080Sstevel@tonic-gate {
2090Sstevel@tonic-gate 	dt_strhash_t *hp;
2100Sstevel@tonic-gate 	size_t len;
2110Sstevel@tonic-gate 	ulong_t h;
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	if (str == NULL || str[0] == '\0')
2140Sstevel@tonic-gate 		return (0); /* we keep a \0 at offset 0 to simplify things */
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	h = dt_strtab_hash(str, &len) % sp->str_hashsz;
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	for (hp = sp->str_hash[h]; hp != NULL; hp = hp->str_next) {
2190Sstevel@tonic-gate 		if (dt_strtab_compare(sp, hp, str, len + 1) == 0)
2200Sstevel@tonic-gate 			return (hp->str_off);
2210Sstevel@tonic-gate 	}
2220Sstevel@tonic-gate 
223*2769Sahl 	return (-1);
224*2769Sahl }
225*2769Sahl 
226*2769Sahl ssize_t
dt_strtab_insert(dt_strtab_t * sp,const char * str)227*2769Sahl dt_strtab_insert(dt_strtab_t *sp, const char *str)
228*2769Sahl {
229*2769Sahl 	dt_strhash_t *hp;
230*2769Sahl 	size_t len;
231*2769Sahl 	ssize_t off;
232*2769Sahl 	ulong_t h;
233*2769Sahl 
234*2769Sahl 	if ((off = dt_strtab_index(sp, str)) != -1)
235*2769Sahl 		return (off);
236*2769Sahl 
237*2769Sahl 	h = dt_strtab_hash(str, &len) % sp->str_hashsz;
238*2769Sahl 
2390Sstevel@tonic-gate 	/*
2400Sstevel@tonic-gate 	 * Create a new hash bucket, initialize it, and insert it at the front
2410Sstevel@tonic-gate 	 * of the hash chain for the appropriate bucket.
2420Sstevel@tonic-gate 	 */
2430Sstevel@tonic-gate 	if ((hp = malloc(sizeof (dt_strhash_t))) == NULL)
2440Sstevel@tonic-gate 		return (-1L);
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 	hp->str_data = sp->str_ptr;
2470Sstevel@tonic-gate 	hp->str_buf = sp->str_nbufs - 1;
2480Sstevel@tonic-gate 	hp->str_off = sp->str_size;
2490Sstevel@tonic-gate 	hp->str_len = len;
2500Sstevel@tonic-gate 	hp->str_next = sp->str_hash[h];
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 	/*
2530Sstevel@tonic-gate 	 * Now copy the string data into our buffer list, and then update
2540Sstevel@tonic-gate 	 * the global counts of strings and bytes.  Return str's byte offset.
2550Sstevel@tonic-gate 	 */
2560Sstevel@tonic-gate 	if (dt_strtab_copyin(sp, str, len + 1) == -1)
2570Sstevel@tonic-gate 		return (-1L);
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	sp->str_nstrs++;
2600Sstevel@tonic-gate 	sp->str_size += len + 1;
2610Sstevel@tonic-gate 	sp->str_hash[h] = hp;
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 	return (hp->str_off);
2640Sstevel@tonic-gate }
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate size_t
dt_strtab_size(const dt_strtab_t * sp)2670Sstevel@tonic-gate dt_strtab_size(const dt_strtab_t *sp)
2680Sstevel@tonic-gate {
2690Sstevel@tonic-gate 	return (sp->str_size);
2700Sstevel@tonic-gate }
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate ssize_t
dt_strtab_write(const dt_strtab_t * sp,dt_strtab_write_f * func,void * private)2730Sstevel@tonic-gate dt_strtab_write(const dt_strtab_t *sp, dt_strtab_write_f *func, void *private)
2740Sstevel@tonic-gate {
2750Sstevel@tonic-gate 	ssize_t res, total = 0;
2760Sstevel@tonic-gate 	ulong_t i;
2770Sstevel@tonic-gate 	size_t n;
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	for (i = 0; i < sp->str_nbufs; i++, total += res) {
2800Sstevel@tonic-gate 		if (i == sp->str_nbufs - 1)
2810Sstevel@tonic-gate 			n = sp->str_ptr - sp->str_bufs[i];
2820Sstevel@tonic-gate 		else
2830Sstevel@tonic-gate 			n = sp->str_bufsz;
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 		if ((res = func(sp->str_bufs[i], n, total, private)) <= 0)
2860Sstevel@tonic-gate 			break;
2870Sstevel@tonic-gate 	}
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	if (total == 0 && sp->str_size != 0)
2900Sstevel@tonic-gate 		return (-1);
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	return (total);
2930Sstevel@tonic-gate }
294