xref: /titanic_44/usr/src/tools/ctf/cvt/tdata.c (revision 4d232658ec6831daceb12672726ce4cabe270c23)
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
5*4d232658Sjohnlev  * Common Development and Distribution License (the "License").
6*4d232658Sjohnlev  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*4d232658Sjohnlev  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * Routines for manipulating tdesc and tdata structures
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <stdio.h>
337c478bd9Sstevel@tonic-gate #include <stdlib.h>
347c478bd9Sstevel@tonic-gate #include <strings.h>
357c478bd9Sstevel@tonic-gate #include <pthread.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include "ctftools.h"
387c478bd9Sstevel@tonic-gate #include "memory.h"
397c478bd9Sstevel@tonic-gate #include "traverse.h"
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate  * The layout hash is used during the equivalency checking.  We have a node in
437c478bd9Sstevel@tonic-gate  * the child graph that may be equivalent to a node in the parent graph.  To
447c478bd9Sstevel@tonic-gate  * find the corresponding node (if any) in the parent, we need a quick way to
457c478bd9Sstevel@tonic-gate  * get to all nodes in the parent that look like the node in the child.  Since a
467c478bd9Sstevel@tonic-gate  * large number of nodes don't have names, we need to incorporate the layout of
477c478bd9Sstevel@tonic-gate  * the node into the hash.  If we don't, we'll end up with the vast majority of
487c478bd9Sstevel@tonic-gate  * nodes in bucket zero, with one or two nodes in each of the remaining buckets.
497c478bd9Sstevel@tonic-gate  *
507c478bd9Sstevel@tonic-gate  * There are a couple of constraints, both of which concern forward
517c478bd9Sstevel@tonic-gate  * declarations.  Recall that a forward declaration tdesc is equivalent to a
527c478bd9Sstevel@tonic-gate  * tdesc that actually defines the structure or union.  As such, we cannot
537c478bd9Sstevel@tonic-gate  * incorporate anything into the hash for a named struct or union node that
547c478bd9Sstevel@tonic-gate  * couldn't be found by looking at the forward, and vice versa.
557c478bd9Sstevel@tonic-gate  */
567c478bd9Sstevel@tonic-gate int
577c478bd9Sstevel@tonic-gate tdesc_layouthash(int nbuckets, void *node)
587c478bd9Sstevel@tonic-gate {
597c478bd9Sstevel@tonic-gate 	tdesc_t *tdp = node;
607c478bd9Sstevel@tonic-gate 	char *name = NULL;
617c478bd9Sstevel@tonic-gate 	ulong_t h = 0;
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 	if (tdp->t_name)
647c478bd9Sstevel@tonic-gate 		name = tdp->t_name;
657c478bd9Sstevel@tonic-gate 	else {
667c478bd9Sstevel@tonic-gate 		switch (tdp->t_type) {
677c478bd9Sstevel@tonic-gate 		case POINTER:
687c478bd9Sstevel@tonic-gate 		case TYPEDEF:
697c478bd9Sstevel@tonic-gate 		case VOLATILE:
707c478bd9Sstevel@tonic-gate 		case CONST:
717c478bd9Sstevel@tonic-gate 		case RESTRICT:
727c478bd9Sstevel@tonic-gate 			name = tdp->t_tdesc->t_name;
737c478bd9Sstevel@tonic-gate 			break;
747c478bd9Sstevel@tonic-gate 		case FUNCTION:
757c478bd9Sstevel@tonic-gate 			h = tdp->t_fndef->fn_nargs +
767c478bd9Sstevel@tonic-gate 			    tdp->t_fndef->fn_vargs;
777c478bd9Sstevel@tonic-gate 			name = tdp->t_fndef->fn_ret->t_name;
787c478bd9Sstevel@tonic-gate 			break;
797c478bd9Sstevel@tonic-gate 		case ARRAY:
807c478bd9Sstevel@tonic-gate 			h = tdp->t_ardef->ad_nelems;
817c478bd9Sstevel@tonic-gate 			name = tdp->t_ardef->ad_contents->t_name;
827c478bd9Sstevel@tonic-gate 			break;
837c478bd9Sstevel@tonic-gate 		case STRUCT:
847c478bd9Sstevel@tonic-gate 		case UNION:
857c478bd9Sstevel@tonic-gate 			/*
867c478bd9Sstevel@tonic-gate 			 * Unnamed structures, which cannot have forward
877c478bd9Sstevel@tonic-gate 			 * declarations pointing to them.  We can therefore
887c478bd9Sstevel@tonic-gate 			 * incorporate the name of the first member into
897c478bd9Sstevel@tonic-gate 			 * the hash value.
907c478bd9Sstevel@tonic-gate 			 */
917c478bd9Sstevel@tonic-gate 			name = tdp->t_members->ml_name;
927c478bd9Sstevel@tonic-gate 			break;
937c478bd9Sstevel@tonic-gate 		case ENUM:
947c478bd9Sstevel@tonic-gate 			/* Use the first element in the hash value */
957c478bd9Sstevel@tonic-gate 			name = tdp->t_emem->el_name;
967c478bd9Sstevel@tonic-gate 			break;
977c478bd9Sstevel@tonic-gate 		default:
987c478bd9Sstevel@tonic-gate 			/*
997c478bd9Sstevel@tonic-gate 			 * Intrinsics, forwards, and typedefs all have
1007c478bd9Sstevel@tonic-gate 			 * names.
1017c478bd9Sstevel@tonic-gate 			 */
1027c478bd9Sstevel@tonic-gate 			warning("Unexpected unnamed %d tdesc (ID %d)\n",
1037c478bd9Sstevel@tonic-gate 			    tdp->t_type, tdp->t_id);
1047c478bd9Sstevel@tonic-gate 		}
1057c478bd9Sstevel@tonic-gate 	}
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	if (name)
1087c478bd9Sstevel@tonic-gate 		return (hash_name(nbuckets, name));
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	return (h % nbuckets);
1117c478bd9Sstevel@tonic-gate }
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate int
1147c478bd9Sstevel@tonic-gate tdesc_layoutcmp(void *arg1, void *arg2)
1157c478bd9Sstevel@tonic-gate {
1167c478bd9Sstevel@tonic-gate 	tdesc_t *tdp1 = arg1, *tdp2 = arg2;
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	if (tdp1->t_name == NULL) {
1197c478bd9Sstevel@tonic-gate 		if (tdp2->t_name == NULL)
1207c478bd9Sstevel@tonic-gate 			return (0);
1217c478bd9Sstevel@tonic-gate 		else
1227c478bd9Sstevel@tonic-gate 			return (-1);
1237c478bd9Sstevel@tonic-gate 	} else if (tdp2->t_name == NULL)
1247c478bd9Sstevel@tonic-gate 		return (1);
1257c478bd9Sstevel@tonic-gate 	else
1267c478bd9Sstevel@tonic-gate 		return (strcmp(tdp1->t_name, tdp2->t_name));
1277c478bd9Sstevel@tonic-gate }
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate int
1307c478bd9Sstevel@tonic-gate tdesc_idhash(int nbuckets, void *data)
1317c478bd9Sstevel@tonic-gate {
1327c478bd9Sstevel@tonic-gate 	tdesc_t *tdp = data;
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	return (tdp->t_id % nbuckets);
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate int
1387c478bd9Sstevel@tonic-gate tdesc_idcmp(void *arg1, void *arg2)
1397c478bd9Sstevel@tonic-gate {
1407c478bd9Sstevel@tonic-gate 	tdesc_t *tdp1 = arg1, *tdp2 = arg2;
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	if (tdp1->t_id == tdp2->t_id)
1437c478bd9Sstevel@tonic-gate 		return (0);
1447c478bd9Sstevel@tonic-gate 	else
1457c478bd9Sstevel@tonic-gate 		return (tdp1->t_id > tdp2->t_id ? 1 : -1);
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate int
1497c478bd9Sstevel@tonic-gate tdesc_namehash(int nbuckets, void *data)
1507c478bd9Sstevel@tonic-gate {
1517c478bd9Sstevel@tonic-gate 	tdesc_t *tdp = data;
1527c478bd9Sstevel@tonic-gate 	ulong_t h, g;
1537c478bd9Sstevel@tonic-gate 	char *c;
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	if (tdp->t_name == NULL)
1567c478bd9Sstevel@tonic-gate 		return (0);
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	for (h = 0, c = tdp->t_name; *c; c++) {
1597c478bd9Sstevel@tonic-gate 		h = (h << 4) + *c;
1607c478bd9Sstevel@tonic-gate 		if ((g = (h & 0xf0000000)) != 0) {
1617c478bd9Sstevel@tonic-gate 			h ^= (g >> 24);
1627c478bd9Sstevel@tonic-gate 			h ^= g;
1637c478bd9Sstevel@tonic-gate 		}
1647c478bd9Sstevel@tonic-gate 	}
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	return (h % nbuckets);
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate int
1707c478bd9Sstevel@tonic-gate tdesc_namecmp(void *arg1, void *arg2)
1717c478bd9Sstevel@tonic-gate {
1727c478bd9Sstevel@tonic-gate 	tdesc_t *tdp1 = arg1, *tdp2 = arg2;
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	return (!streq(tdp1->t_name, tdp2->t_name));
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
1787c478bd9Sstevel@tonic-gate int
1797c478bd9Sstevel@tonic-gate tdesc_print(void *data, void *private)
1807c478bd9Sstevel@tonic-gate {
1817c478bd9Sstevel@tonic-gate 	tdesc_t *tdp = data;
1827c478bd9Sstevel@tonic-gate 
183*4d232658Sjohnlev 	printf("%7d %s\n", tdp->t_id, tdesc_name(tdp));
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	return (1);
1867c478bd9Sstevel@tonic-gate }
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate static void
1897c478bd9Sstevel@tonic-gate free_intr(tdesc_t *tdp)
1907c478bd9Sstevel@tonic-gate {
1917c478bd9Sstevel@tonic-gate 	free(tdp->t_intr);
1927c478bd9Sstevel@tonic-gate }
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate static void
1957c478bd9Sstevel@tonic-gate free_ardef(tdesc_t *tdp)
1967c478bd9Sstevel@tonic-gate {
1977c478bd9Sstevel@tonic-gate 	free(tdp->t_ardef);
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate static void
2017c478bd9Sstevel@tonic-gate free_mlist(tdesc_t *tdp)
2027c478bd9Sstevel@tonic-gate {
2037c478bd9Sstevel@tonic-gate 	mlist_t *ml = tdp->t_members;
2047c478bd9Sstevel@tonic-gate 	mlist_t *oml;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	while (ml) {
2077c478bd9Sstevel@tonic-gate 		oml = ml;
2087c478bd9Sstevel@tonic-gate 		ml = ml->ml_next;
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 		if (oml->ml_name)
2117c478bd9Sstevel@tonic-gate 			free(oml->ml_name);
2127c478bd9Sstevel@tonic-gate 		free(oml);
2137c478bd9Sstevel@tonic-gate 	}
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate static void
2177c478bd9Sstevel@tonic-gate free_elist(tdesc_t *tdp)
2187c478bd9Sstevel@tonic-gate {
2197c478bd9Sstevel@tonic-gate 	elist_t *el = tdp->t_emem;
2207c478bd9Sstevel@tonic-gate 	elist_t *oel;
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	while (el) {
2237c478bd9Sstevel@tonic-gate 		oel = el;
2247c478bd9Sstevel@tonic-gate 		el = el->el_next;
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 		if (oel->el_name)
2277c478bd9Sstevel@tonic-gate 			free(oel->el_name);
2287c478bd9Sstevel@tonic-gate 		free(oel);
2297c478bd9Sstevel@tonic-gate 	}
2307c478bd9Sstevel@tonic-gate }
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate static void (*free_cbs[])(tdesc_t *) = {
2337c478bd9Sstevel@tonic-gate 	NULL,
2347c478bd9Sstevel@tonic-gate 	free_intr,
2357c478bd9Sstevel@tonic-gate 	NULL,
2367c478bd9Sstevel@tonic-gate 	free_ardef,
2377c478bd9Sstevel@tonic-gate 	NULL,
2387c478bd9Sstevel@tonic-gate 	free_mlist,
2397c478bd9Sstevel@tonic-gate 	free_mlist,
2407c478bd9Sstevel@tonic-gate 	free_elist,
2417c478bd9Sstevel@tonic-gate 	NULL,
2427c478bd9Sstevel@tonic-gate 	NULL,
2437c478bd9Sstevel@tonic-gate 	NULL,
2447c478bd9Sstevel@tonic-gate 	NULL,
2457c478bd9Sstevel@tonic-gate 	NULL,
2467c478bd9Sstevel@tonic-gate 	NULL
2477c478bd9Sstevel@tonic-gate };
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
2507c478bd9Sstevel@tonic-gate static int
2517c478bd9Sstevel@tonic-gate tdesc_free_cb(tdesc_t *tdp, void *private)
2527c478bd9Sstevel@tonic-gate {
2537c478bd9Sstevel@tonic-gate 	if (tdp->t_name)
2547c478bd9Sstevel@tonic-gate 		free(tdp->t_name);
2557c478bd9Sstevel@tonic-gate 	if (free_cbs[tdp->t_type])
2567c478bd9Sstevel@tonic-gate 		free_cbs[tdp->t_type](tdp);
2577c478bd9Sstevel@tonic-gate 	free(tdp);
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	return (1);
2607c478bd9Sstevel@tonic-gate }
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate void
2637c478bd9Sstevel@tonic-gate tdesc_free(tdesc_t *tdp)
2647c478bd9Sstevel@tonic-gate {
2657c478bd9Sstevel@tonic-gate 	(void) tdesc_free_cb(tdp, NULL);
2667c478bd9Sstevel@tonic-gate }
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate static int
2697c478bd9Sstevel@tonic-gate tdata_label_cmp(labelent_t *le1, labelent_t *le2)
2707c478bd9Sstevel@tonic-gate {
2717c478bd9Sstevel@tonic-gate 	return (le1->le_idx - le2->le_idx);
2727c478bd9Sstevel@tonic-gate }
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate void
2757c478bd9Sstevel@tonic-gate tdata_label_add(tdata_t *td, char *label, int idx)
2767c478bd9Sstevel@tonic-gate {
2777c478bd9Sstevel@tonic-gate 	labelent_t *le = xmalloc(sizeof (*le));
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	le->le_name = xstrdup(label);
2807c478bd9Sstevel@tonic-gate 	le->le_idx = (idx == -1 ? td->td_nextid - 1 : idx);
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	slist_add(&td->td_labels, le, (int (*)())tdata_label_cmp);
2837c478bd9Sstevel@tonic-gate }
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate static int
2867c478bd9Sstevel@tonic-gate tdata_label_top_cb(void *data, void *arg)
2877c478bd9Sstevel@tonic-gate {
2887c478bd9Sstevel@tonic-gate 	labelent_t *le = data;
2897c478bd9Sstevel@tonic-gate 	labelent_t **topp = arg;
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	*topp = le;
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	return (1);
2947c478bd9Sstevel@tonic-gate }
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate labelent_t *
2977c478bd9Sstevel@tonic-gate tdata_label_top(tdata_t *td)
2987c478bd9Sstevel@tonic-gate {
2997c478bd9Sstevel@tonic-gate 	labelent_t *top = NULL;
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	(void) list_iter(td->td_labels, tdata_label_top_cb, &top);
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	return (top);
3047c478bd9Sstevel@tonic-gate }
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate static int
3077c478bd9Sstevel@tonic-gate tdata_label_find_cb(labelent_t *le, labelent_t *tmpl)
3087c478bd9Sstevel@tonic-gate {
3097c478bd9Sstevel@tonic-gate 	return (streq(le->le_name, tmpl->le_name));
3107c478bd9Sstevel@tonic-gate }
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate int
3137c478bd9Sstevel@tonic-gate tdata_label_find(tdata_t *td, char *label)
3147c478bd9Sstevel@tonic-gate {
3157c478bd9Sstevel@tonic-gate 	labelent_t let;
3167c478bd9Sstevel@tonic-gate 	labelent_t *ret;
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	if (streq(label, "BASE")) {
3197c478bd9Sstevel@tonic-gate 		ret = (labelent_t *)list_first(td->td_labels);
3207c478bd9Sstevel@tonic-gate 		return (ret ? ret->le_idx : -1);
3217c478bd9Sstevel@tonic-gate 	}
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	let.le_name = label;
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	if (!(ret = (labelent_t *)list_find(td->td_labels, &let,
3267c478bd9Sstevel@tonic-gate 	    (int (*)())tdata_label_find_cb)))
3277c478bd9Sstevel@tonic-gate 		return (-1);
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	return (ret->le_idx);
3307c478bd9Sstevel@tonic-gate }
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate static int
3337c478bd9Sstevel@tonic-gate tdata_label_newmax_cb(void *data, void *arg)
3347c478bd9Sstevel@tonic-gate {
3357c478bd9Sstevel@tonic-gate 	labelent_t *le = data;
3367c478bd9Sstevel@tonic-gate 	int *newmaxp = arg;
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 	if (le->le_idx > *newmaxp) {
3397c478bd9Sstevel@tonic-gate 		le->le_idx = *newmaxp;
3407c478bd9Sstevel@tonic-gate 		return (1);
3417c478bd9Sstevel@tonic-gate 	}
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	return (0);
3447c478bd9Sstevel@tonic-gate }
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate void
3477c478bd9Sstevel@tonic-gate tdata_label_newmax(tdata_t *td, int newmax)
3487c478bd9Sstevel@tonic-gate {
3497c478bd9Sstevel@tonic-gate 	(void) list_iter(td->td_labels, tdata_label_newmax_cb, &newmax);
3507c478bd9Sstevel@tonic-gate }
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
3537c478bd9Sstevel@tonic-gate static void
3547c478bd9Sstevel@tonic-gate tdata_label_free_cb(labelent_t *le, void *private)
3557c478bd9Sstevel@tonic-gate {
3567c478bd9Sstevel@tonic-gate 	if (le->le_name)
3577c478bd9Sstevel@tonic-gate 		free(le->le_name);
3587c478bd9Sstevel@tonic-gate 	free(le);
3597c478bd9Sstevel@tonic-gate }
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate void
3627c478bd9Sstevel@tonic-gate tdata_label_free(tdata_t *td)
3637c478bd9Sstevel@tonic-gate {
3647c478bd9Sstevel@tonic-gate 	list_free(td->td_labels, (void (*)())tdata_label_free_cb, NULL);
3657c478bd9Sstevel@tonic-gate 	td->td_labels = NULL;
3667c478bd9Sstevel@tonic-gate }
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate tdata_t *
3697c478bd9Sstevel@tonic-gate tdata_new(void)
3707c478bd9Sstevel@tonic-gate {
3717c478bd9Sstevel@tonic-gate 	tdata_t *new = xcalloc(sizeof (tdata_t));
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	new->td_layouthash = hash_new(TDATA_LAYOUT_HASH_SIZE, tdesc_layouthash,
3747c478bd9Sstevel@tonic-gate 	    tdesc_layoutcmp);
3757c478bd9Sstevel@tonic-gate 	new->td_idhash = hash_new(TDATA_ID_HASH_SIZE, tdesc_idhash,
3767c478bd9Sstevel@tonic-gate 	    tdesc_idcmp);
3777c478bd9Sstevel@tonic-gate 	/*
3787c478bd9Sstevel@tonic-gate 	 * This is also traversed as a list, but amortized O(1)
3797c478bd9Sstevel@tonic-gate 	 * lookup massively impacts part of the merge phase, so
3807c478bd9Sstevel@tonic-gate 	 * we store the iidescs as a hash.
3817c478bd9Sstevel@tonic-gate 	 */
3827c478bd9Sstevel@tonic-gate 	new->td_iihash = hash_new(IIDESC_HASH_SIZE, iidesc_hash, NULL);
3837c478bd9Sstevel@tonic-gate 	new->td_nextid = 1;
3847c478bd9Sstevel@tonic-gate 	new->td_curvgen = 1;
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	pthread_mutex_init(&new->td_mergelock, NULL);
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	return (new);
3897c478bd9Sstevel@tonic-gate }
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate void
3927c478bd9Sstevel@tonic-gate tdata_free(tdata_t *td)
3937c478bd9Sstevel@tonic-gate {
3947c478bd9Sstevel@tonic-gate 	hash_free(td->td_iihash, (void (*)())iidesc_free, NULL);
3957c478bd9Sstevel@tonic-gate 	hash_free(td->td_layouthash, (void (*)())tdesc_free_cb, NULL);
3967c478bd9Sstevel@tonic-gate 	hash_free(td->td_idhash, NULL, NULL);
3977c478bd9Sstevel@tonic-gate 	list_free(td->td_fwdlist, NULL, NULL);
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 	tdata_label_free(td);
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	free(td->td_parlabel);
4027c478bd9Sstevel@tonic-gate 	free(td->td_parname);
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	pthread_mutex_destroy(&td->td_mergelock);
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	free(td);
4077c478bd9Sstevel@tonic-gate }
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
4107c478bd9Sstevel@tonic-gate static int
4117c478bd9Sstevel@tonic-gate build_hashes(tdesc_t *ctdp, tdesc_t **ctdpp, void *private)
4127c478bd9Sstevel@tonic-gate {
4137c478bd9Sstevel@tonic-gate 	tdata_t *td = private;
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	hash_add(td->td_idhash, ctdp);
4167c478bd9Sstevel@tonic-gate 	hash_add(td->td_layouthash, ctdp);
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	return (1);
4197c478bd9Sstevel@tonic-gate }
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate static tdtrav_cb_f build_hashes_cbs[] = {
4227c478bd9Sstevel@tonic-gate 	NULL,
4237c478bd9Sstevel@tonic-gate 	build_hashes,	/* intrinsic */
4247c478bd9Sstevel@tonic-gate 	build_hashes,	/* pointer */
4257c478bd9Sstevel@tonic-gate 	build_hashes,	/* array */
4267c478bd9Sstevel@tonic-gate 	build_hashes,	/* function */
4277c478bd9Sstevel@tonic-gate 	build_hashes,	/* struct */
4287c478bd9Sstevel@tonic-gate 	build_hashes,	/* union */
4297c478bd9Sstevel@tonic-gate 	build_hashes,	/* enum */
4307c478bd9Sstevel@tonic-gate 	build_hashes,	/* forward */
4317c478bd9Sstevel@tonic-gate 	build_hashes,	/* typedef */
4327c478bd9Sstevel@tonic-gate 	tdtrav_assert,	/* typedef_unres */
4337c478bd9Sstevel@tonic-gate 	build_hashes,	/* volatile */
4347c478bd9Sstevel@tonic-gate 	build_hashes,	/* const */
4357c478bd9Sstevel@tonic-gate 	build_hashes	/* restrict */
4367c478bd9Sstevel@tonic-gate };
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate static void
4397c478bd9Sstevel@tonic-gate tdata_build_hashes_common(tdata_t *td, hash_t *hash)
4407c478bd9Sstevel@tonic-gate {
4417c478bd9Sstevel@tonic-gate 	(void) iitraverse_hash(hash, &td->td_curvgen, NULL, NULL,
4427c478bd9Sstevel@tonic-gate 	    build_hashes_cbs, td);
4437c478bd9Sstevel@tonic-gate }
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate void
4467c478bd9Sstevel@tonic-gate tdata_build_hashes(tdata_t *td)
4477c478bd9Sstevel@tonic-gate {
4487c478bd9Sstevel@tonic-gate 	tdata_build_hashes_common(td, td->td_iihash);
4497c478bd9Sstevel@tonic-gate }
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate /* Merge td2 into td1.  td2 is destroyed by the merge */
4527c478bd9Sstevel@tonic-gate void
4537c478bd9Sstevel@tonic-gate tdata_merge(tdata_t *td1, tdata_t *td2)
4547c478bd9Sstevel@tonic-gate {
4557c478bd9Sstevel@tonic-gate 	td1->td_curemark = MAX(td1->td_curemark, td2->td_curemark);
4567c478bd9Sstevel@tonic-gate 	td1->td_curvgen = MAX(td1->td_curvgen, td2->td_curvgen);
4577c478bd9Sstevel@tonic-gate 	td1->td_nextid = MAX(td1->td_nextid, td2->td_nextid);
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	hash_merge(td1->td_iihash, td2->td_iihash);
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	/* Add td2's type tree to the hashes */
4627c478bd9Sstevel@tonic-gate 	tdata_build_hashes_common(td1, td2->td_iihash);
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	list_concat(&td1->td_fwdlist, td2->td_fwdlist);
4657c478bd9Sstevel@tonic-gate 	td2->td_fwdlist = NULL;
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	slist_merge(&td1->td_labels, td2->td_labels,
4687c478bd9Sstevel@tonic-gate 	    (int (*)())tdata_label_cmp);
4697c478bd9Sstevel@tonic-gate 	td2->td_labels = NULL;
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 	/* free the td2 hashes (data is now part of td1) */
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	hash_free(td2->td_layouthash, NULL, NULL);
4747c478bd9Sstevel@tonic-gate 	td2->td_layouthash = NULL;
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	hash_free(td2->td_iihash, NULL, NULL);
4777c478bd9Sstevel@tonic-gate 	td2->td_iihash = NULL;
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	tdata_free(td2);
4807c478bd9Sstevel@tonic-gate }
481