xref: /onnv-gate/usr/src/tools/ctf/cvt/tdata.c (revision 11432:c1c450bf62f2)
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
51882Sjohnlev  * Common Development and Distribution License (the "License").
61882Sjohnlev  * 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  */
210Sstevel@tonic-gate /*
22*11432Sjohn.levon@sun.com  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * Routines for manipulating tdesc and tdata structures
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <stdio.h>
310Sstevel@tonic-gate #include <stdlib.h>
320Sstevel@tonic-gate #include <strings.h>
330Sstevel@tonic-gate #include <pthread.h>
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #include "ctftools.h"
360Sstevel@tonic-gate #include "memory.h"
370Sstevel@tonic-gate #include "traverse.h"
380Sstevel@tonic-gate 
390Sstevel@tonic-gate /*
400Sstevel@tonic-gate  * The layout hash is used during the equivalency checking.  We have a node in
410Sstevel@tonic-gate  * the child graph that may be equivalent to a node in the parent graph.  To
420Sstevel@tonic-gate  * find the corresponding node (if any) in the parent, we need a quick way to
430Sstevel@tonic-gate  * get to all nodes in the parent that look like the node in the child.  Since a
440Sstevel@tonic-gate  * large number of nodes don't have names, we need to incorporate the layout of
450Sstevel@tonic-gate  * the node into the hash.  If we don't, we'll end up with the vast majority of
460Sstevel@tonic-gate  * nodes in bucket zero, with one or two nodes in each of the remaining buckets.
470Sstevel@tonic-gate  *
480Sstevel@tonic-gate  * There are a couple of constraints, both of which concern forward
490Sstevel@tonic-gate  * declarations.  Recall that a forward declaration tdesc is equivalent to a
500Sstevel@tonic-gate  * tdesc that actually defines the structure or union.  As such, we cannot
510Sstevel@tonic-gate  * incorporate anything into the hash for a named struct or union node that
520Sstevel@tonic-gate  * couldn't be found by looking at the forward, and vice versa.
530Sstevel@tonic-gate  */
540Sstevel@tonic-gate int
tdesc_layouthash(int nbuckets,void * node)550Sstevel@tonic-gate tdesc_layouthash(int nbuckets, void *node)
560Sstevel@tonic-gate {
570Sstevel@tonic-gate 	tdesc_t *tdp = node;
580Sstevel@tonic-gate 	char *name = NULL;
590Sstevel@tonic-gate 	ulong_t h = 0;
600Sstevel@tonic-gate 
610Sstevel@tonic-gate 	if (tdp->t_name)
620Sstevel@tonic-gate 		name = tdp->t_name;
630Sstevel@tonic-gate 	else {
640Sstevel@tonic-gate 		switch (tdp->t_type) {
650Sstevel@tonic-gate 		case POINTER:
660Sstevel@tonic-gate 		case TYPEDEF:
670Sstevel@tonic-gate 		case VOLATILE:
680Sstevel@tonic-gate 		case CONST:
690Sstevel@tonic-gate 		case RESTRICT:
700Sstevel@tonic-gate 			name = tdp->t_tdesc->t_name;
710Sstevel@tonic-gate 			break;
720Sstevel@tonic-gate 		case FUNCTION:
730Sstevel@tonic-gate 			h = tdp->t_fndef->fn_nargs +
740Sstevel@tonic-gate 			    tdp->t_fndef->fn_vargs;
750Sstevel@tonic-gate 			name = tdp->t_fndef->fn_ret->t_name;
760Sstevel@tonic-gate 			break;
770Sstevel@tonic-gate 		case ARRAY:
780Sstevel@tonic-gate 			h = tdp->t_ardef->ad_nelems;
790Sstevel@tonic-gate 			name = tdp->t_ardef->ad_contents->t_name;
800Sstevel@tonic-gate 			break;
810Sstevel@tonic-gate 		case STRUCT:
820Sstevel@tonic-gate 		case UNION:
830Sstevel@tonic-gate 			/*
840Sstevel@tonic-gate 			 * Unnamed structures, which cannot have forward
850Sstevel@tonic-gate 			 * declarations pointing to them.  We can therefore
860Sstevel@tonic-gate 			 * incorporate the name of the first member into
87*11432Sjohn.levon@sun.com 			 * the hash value, assuming there are any.
880Sstevel@tonic-gate 			 */
89*11432Sjohn.levon@sun.com 			if (tdp->t_members != NULL)
90*11432Sjohn.levon@sun.com 				name = tdp->t_members->ml_name;
910Sstevel@tonic-gate 			break;
920Sstevel@tonic-gate 		case ENUM:
930Sstevel@tonic-gate 			/* Use the first element in the hash value */
940Sstevel@tonic-gate 			name = tdp->t_emem->el_name;
950Sstevel@tonic-gate 			break;
960Sstevel@tonic-gate 		default:
970Sstevel@tonic-gate 			/*
980Sstevel@tonic-gate 			 * Intrinsics, forwards, and typedefs all have
990Sstevel@tonic-gate 			 * names.
1000Sstevel@tonic-gate 			 */
1010Sstevel@tonic-gate 			warning("Unexpected unnamed %d tdesc (ID %d)\n",
1020Sstevel@tonic-gate 			    tdp->t_type, tdp->t_id);
1030Sstevel@tonic-gate 		}
1040Sstevel@tonic-gate 	}
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	if (name)
1070Sstevel@tonic-gate 		return (hash_name(nbuckets, name));
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	return (h % nbuckets);
1100Sstevel@tonic-gate }
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate int
tdesc_layoutcmp(void * arg1,void * arg2)1130Sstevel@tonic-gate tdesc_layoutcmp(void *arg1, void *arg2)
1140Sstevel@tonic-gate {
1150Sstevel@tonic-gate 	tdesc_t *tdp1 = arg1, *tdp2 = arg2;
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	if (tdp1->t_name == NULL) {
1180Sstevel@tonic-gate 		if (tdp2->t_name == NULL)
1190Sstevel@tonic-gate 			return (0);
1200Sstevel@tonic-gate 		else
1210Sstevel@tonic-gate 			return (-1);
1220Sstevel@tonic-gate 	} else if (tdp2->t_name == NULL)
1230Sstevel@tonic-gate 		return (1);
1240Sstevel@tonic-gate 	else
1250Sstevel@tonic-gate 		return (strcmp(tdp1->t_name, tdp2->t_name));
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate int
tdesc_idhash(int nbuckets,void * data)1290Sstevel@tonic-gate tdesc_idhash(int nbuckets, void *data)
1300Sstevel@tonic-gate {
1310Sstevel@tonic-gate 	tdesc_t *tdp = data;
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 	return (tdp->t_id % nbuckets);
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate int
tdesc_idcmp(void * arg1,void * arg2)1370Sstevel@tonic-gate tdesc_idcmp(void *arg1, void *arg2)
1380Sstevel@tonic-gate {
1390Sstevel@tonic-gate 	tdesc_t *tdp1 = arg1, *tdp2 = arg2;
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 	if (tdp1->t_id == tdp2->t_id)
1420Sstevel@tonic-gate 		return (0);
1430Sstevel@tonic-gate 	else
1440Sstevel@tonic-gate 		return (tdp1->t_id > tdp2->t_id ? 1 : -1);
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate int
tdesc_namehash(int nbuckets,void * data)1480Sstevel@tonic-gate tdesc_namehash(int nbuckets, void *data)
1490Sstevel@tonic-gate {
1500Sstevel@tonic-gate 	tdesc_t *tdp = data;
1510Sstevel@tonic-gate 	ulong_t h, g;
1520Sstevel@tonic-gate 	char *c;
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	if (tdp->t_name == NULL)
1550Sstevel@tonic-gate 		return (0);
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	for (h = 0, c = tdp->t_name; *c; c++) {
1580Sstevel@tonic-gate 		h = (h << 4) + *c;
1590Sstevel@tonic-gate 		if ((g = (h & 0xf0000000)) != 0) {
1600Sstevel@tonic-gate 			h ^= (g >> 24);
1610Sstevel@tonic-gate 			h ^= g;
1620Sstevel@tonic-gate 		}
1630Sstevel@tonic-gate 	}
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	return (h % nbuckets);
1660Sstevel@tonic-gate }
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate int
tdesc_namecmp(void * arg1,void * arg2)1690Sstevel@tonic-gate tdesc_namecmp(void *arg1, void *arg2)
1700Sstevel@tonic-gate {
1710Sstevel@tonic-gate 	tdesc_t *tdp1 = arg1, *tdp2 = arg2;
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	return (!streq(tdp1->t_name, tdp2->t_name));
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate /*ARGSUSED1*/
1770Sstevel@tonic-gate int
tdesc_print(void * data,void * private)1780Sstevel@tonic-gate tdesc_print(void *data, void *private)
1790Sstevel@tonic-gate {
1800Sstevel@tonic-gate 	tdesc_t *tdp = data;
1810Sstevel@tonic-gate 
1821882Sjohnlev 	printf("%7d %s\n", tdp->t_id, tdesc_name(tdp));
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	return (1);
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate static void
free_intr(tdesc_t * tdp)1880Sstevel@tonic-gate free_intr(tdesc_t *tdp)
1890Sstevel@tonic-gate {
1900Sstevel@tonic-gate 	free(tdp->t_intr);
1910Sstevel@tonic-gate }
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate static void
free_ardef(tdesc_t * tdp)1940Sstevel@tonic-gate free_ardef(tdesc_t *tdp)
1950Sstevel@tonic-gate {
1960Sstevel@tonic-gate 	free(tdp->t_ardef);
1970Sstevel@tonic-gate }
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate static void
free_mlist(tdesc_t * tdp)2000Sstevel@tonic-gate free_mlist(tdesc_t *tdp)
2010Sstevel@tonic-gate {
2020Sstevel@tonic-gate 	mlist_t *ml = tdp->t_members;
2030Sstevel@tonic-gate 	mlist_t *oml;
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 	while (ml) {
2060Sstevel@tonic-gate 		oml = ml;
2070Sstevel@tonic-gate 		ml = ml->ml_next;
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 		if (oml->ml_name)
2100Sstevel@tonic-gate 			free(oml->ml_name);
2110Sstevel@tonic-gate 		free(oml);
2120Sstevel@tonic-gate 	}
2130Sstevel@tonic-gate }
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate static void
free_elist(tdesc_t * tdp)2160Sstevel@tonic-gate free_elist(tdesc_t *tdp)
2170Sstevel@tonic-gate {
2180Sstevel@tonic-gate 	elist_t *el = tdp->t_emem;
2190Sstevel@tonic-gate 	elist_t *oel;
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	while (el) {
2220Sstevel@tonic-gate 		oel = el;
2230Sstevel@tonic-gate 		el = el->el_next;
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 		if (oel->el_name)
2260Sstevel@tonic-gate 			free(oel->el_name);
2270Sstevel@tonic-gate 		free(oel);
2280Sstevel@tonic-gate 	}
2290Sstevel@tonic-gate }
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate static void (*free_cbs[])(tdesc_t *) = {
2320Sstevel@tonic-gate 	NULL,
2330Sstevel@tonic-gate 	free_intr,
2340Sstevel@tonic-gate 	NULL,
2350Sstevel@tonic-gate 	free_ardef,
2360Sstevel@tonic-gate 	NULL,
2370Sstevel@tonic-gate 	free_mlist,
2380Sstevel@tonic-gate 	free_mlist,
2390Sstevel@tonic-gate 	free_elist,
2400Sstevel@tonic-gate 	NULL,
2410Sstevel@tonic-gate 	NULL,
2420Sstevel@tonic-gate 	NULL,
2430Sstevel@tonic-gate 	NULL,
2440Sstevel@tonic-gate 	NULL,
2450Sstevel@tonic-gate 	NULL
2460Sstevel@tonic-gate };
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate /*ARGSUSED1*/
2490Sstevel@tonic-gate static int
tdesc_free_cb(tdesc_t * tdp,void * private)2500Sstevel@tonic-gate tdesc_free_cb(tdesc_t *tdp, void *private)
2510Sstevel@tonic-gate {
2520Sstevel@tonic-gate 	if (tdp->t_name)
2530Sstevel@tonic-gate 		free(tdp->t_name);
2540Sstevel@tonic-gate 	if (free_cbs[tdp->t_type])
2550Sstevel@tonic-gate 		free_cbs[tdp->t_type](tdp);
2560Sstevel@tonic-gate 	free(tdp);
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	return (1);
2590Sstevel@tonic-gate }
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate void
tdesc_free(tdesc_t * tdp)2620Sstevel@tonic-gate tdesc_free(tdesc_t *tdp)
2630Sstevel@tonic-gate {
2640Sstevel@tonic-gate 	(void) tdesc_free_cb(tdp, NULL);
2650Sstevel@tonic-gate }
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate static int
tdata_label_cmp(labelent_t * le1,labelent_t * le2)2680Sstevel@tonic-gate tdata_label_cmp(labelent_t *le1, labelent_t *le2)
2690Sstevel@tonic-gate {
2700Sstevel@tonic-gate 	return (le1->le_idx - le2->le_idx);
2710Sstevel@tonic-gate }
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate void
tdata_label_add(tdata_t * td,char * label,int idx)2740Sstevel@tonic-gate tdata_label_add(tdata_t *td, char *label, int idx)
2750Sstevel@tonic-gate {
2760Sstevel@tonic-gate 	labelent_t *le = xmalloc(sizeof (*le));
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	le->le_name = xstrdup(label);
2790Sstevel@tonic-gate 	le->le_idx = (idx == -1 ? td->td_nextid - 1 : idx);
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 	slist_add(&td->td_labels, le, (int (*)())tdata_label_cmp);
2820Sstevel@tonic-gate }
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate static int
tdata_label_top_cb(void * data,void * arg)2850Sstevel@tonic-gate tdata_label_top_cb(void *data, void *arg)
2860Sstevel@tonic-gate {
2870Sstevel@tonic-gate 	labelent_t *le = data;
2880Sstevel@tonic-gate 	labelent_t **topp = arg;
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	*topp = le;
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	return (1);
2930Sstevel@tonic-gate }
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate labelent_t *
tdata_label_top(tdata_t * td)2960Sstevel@tonic-gate tdata_label_top(tdata_t *td)
2970Sstevel@tonic-gate {
2980Sstevel@tonic-gate 	labelent_t *top = NULL;
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	(void) list_iter(td->td_labels, tdata_label_top_cb, &top);
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 	return (top);
3030Sstevel@tonic-gate }
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate static int
tdata_label_find_cb(labelent_t * le,labelent_t * tmpl)3060Sstevel@tonic-gate tdata_label_find_cb(labelent_t *le, labelent_t *tmpl)
3070Sstevel@tonic-gate {
3080Sstevel@tonic-gate 	return (streq(le->le_name, tmpl->le_name));
3090Sstevel@tonic-gate }
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate int
tdata_label_find(tdata_t * td,char * label)3120Sstevel@tonic-gate tdata_label_find(tdata_t *td, char *label)
3130Sstevel@tonic-gate {
3140Sstevel@tonic-gate 	labelent_t let;
3150Sstevel@tonic-gate 	labelent_t *ret;
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	if (streq(label, "BASE")) {
3180Sstevel@tonic-gate 		ret = (labelent_t *)list_first(td->td_labels);
3190Sstevel@tonic-gate 		return (ret ? ret->le_idx : -1);
3200Sstevel@tonic-gate 	}
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 	let.le_name = label;
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 	if (!(ret = (labelent_t *)list_find(td->td_labels, &let,
3250Sstevel@tonic-gate 	    (int (*)())tdata_label_find_cb)))
3260Sstevel@tonic-gate 		return (-1);
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 	return (ret->le_idx);
3290Sstevel@tonic-gate }
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate static int
tdata_label_newmax_cb(void * data,void * arg)3320Sstevel@tonic-gate tdata_label_newmax_cb(void *data, void *arg)
3330Sstevel@tonic-gate {
3340Sstevel@tonic-gate 	labelent_t *le = data;
3350Sstevel@tonic-gate 	int *newmaxp = arg;
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 	if (le->le_idx > *newmaxp) {
3380Sstevel@tonic-gate 		le->le_idx = *newmaxp;
3390Sstevel@tonic-gate 		return (1);
3400Sstevel@tonic-gate 	}
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 	return (0);
3430Sstevel@tonic-gate }
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate void
tdata_label_newmax(tdata_t * td,int newmax)3460Sstevel@tonic-gate tdata_label_newmax(tdata_t *td, int newmax)
3470Sstevel@tonic-gate {
3480Sstevel@tonic-gate 	(void) list_iter(td->td_labels, tdata_label_newmax_cb, &newmax);
3490Sstevel@tonic-gate }
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate /*ARGSUSED1*/
3520Sstevel@tonic-gate static void
tdata_label_free_cb(labelent_t * le,void * private)3530Sstevel@tonic-gate tdata_label_free_cb(labelent_t *le, void *private)
3540Sstevel@tonic-gate {
3550Sstevel@tonic-gate 	if (le->le_name)
3560Sstevel@tonic-gate 		free(le->le_name);
3570Sstevel@tonic-gate 	free(le);
3580Sstevel@tonic-gate }
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate void
tdata_label_free(tdata_t * td)3610Sstevel@tonic-gate tdata_label_free(tdata_t *td)
3620Sstevel@tonic-gate {
3630Sstevel@tonic-gate 	list_free(td->td_labels, (void (*)())tdata_label_free_cb, NULL);
3640Sstevel@tonic-gate 	td->td_labels = NULL;
3650Sstevel@tonic-gate }
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate tdata_t *
tdata_new(void)3680Sstevel@tonic-gate tdata_new(void)
3690Sstevel@tonic-gate {
3700Sstevel@tonic-gate 	tdata_t *new = xcalloc(sizeof (tdata_t));
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 	new->td_layouthash = hash_new(TDATA_LAYOUT_HASH_SIZE, tdesc_layouthash,
3730Sstevel@tonic-gate 	    tdesc_layoutcmp);
3740Sstevel@tonic-gate 	new->td_idhash = hash_new(TDATA_ID_HASH_SIZE, tdesc_idhash,
3750Sstevel@tonic-gate 	    tdesc_idcmp);
3760Sstevel@tonic-gate 	/*
3770Sstevel@tonic-gate 	 * This is also traversed as a list, but amortized O(1)
3780Sstevel@tonic-gate 	 * lookup massively impacts part of the merge phase, so
3790Sstevel@tonic-gate 	 * we store the iidescs as a hash.
3800Sstevel@tonic-gate 	 */
3810Sstevel@tonic-gate 	new->td_iihash = hash_new(IIDESC_HASH_SIZE, iidesc_hash, NULL);
3820Sstevel@tonic-gate 	new->td_nextid = 1;
3830Sstevel@tonic-gate 	new->td_curvgen = 1;
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate 	pthread_mutex_init(&new->td_mergelock, NULL);
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 	return (new);
3880Sstevel@tonic-gate }
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate void
tdata_free(tdata_t * td)3910Sstevel@tonic-gate tdata_free(tdata_t *td)
3920Sstevel@tonic-gate {
3930Sstevel@tonic-gate 	hash_free(td->td_iihash, (void (*)())iidesc_free, NULL);
3940Sstevel@tonic-gate 	hash_free(td->td_layouthash, (void (*)())tdesc_free_cb, NULL);
3950Sstevel@tonic-gate 	hash_free(td->td_idhash, NULL, NULL);
3960Sstevel@tonic-gate 	list_free(td->td_fwdlist, NULL, NULL);
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 	tdata_label_free(td);
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 	free(td->td_parlabel);
4010Sstevel@tonic-gate 	free(td->td_parname);
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 	pthread_mutex_destroy(&td->td_mergelock);
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 	free(td);
4060Sstevel@tonic-gate }
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate /*ARGSUSED1*/
4090Sstevel@tonic-gate static int
build_hashes(tdesc_t * ctdp,tdesc_t ** ctdpp,void * private)4100Sstevel@tonic-gate build_hashes(tdesc_t *ctdp, tdesc_t **ctdpp, void *private)
4110Sstevel@tonic-gate {
4120Sstevel@tonic-gate 	tdata_t *td = private;
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 	hash_add(td->td_idhash, ctdp);
4150Sstevel@tonic-gate 	hash_add(td->td_layouthash, ctdp);
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	return (1);
4180Sstevel@tonic-gate }
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate static tdtrav_cb_f build_hashes_cbs[] = {
4210Sstevel@tonic-gate 	NULL,
4220Sstevel@tonic-gate 	build_hashes,	/* intrinsic */
4230Sstevel@tonic-gate 	build_hashes,	/* pointer */
4240Sstevel@tonic-gate 	build_hashes,	/* array */
4250Sstevel@tonic-gate 	build_hashes,	/* function */
4260Sstevel@tonic-gate 	build_hashes,	/* struct */
4270Sstevel@tonic-gate 	build_hashes,	/* union */
4280Sstevel@tonic-gate 	build_hashes,	/* enum */
4290Sstevel@tonic-gate 	build_hashes,	/* forward */
4300Sstevel@tonic-gate 	build_hashes,	/* typedef */
4310Sstevel@tonic-gate 	tdtrav_assert,	/* typedef_unres */
4320Sstevel@tonic-gate 	build_hashes,	/* volatile */
4330Sstevel@tonic-gate 	build_hashes,	/* const */
4340Sstevel@tonic-gate 	build_hashes	/* restrict */
4350Sstevel@tonic-gate };
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate static void
tdata_build_hashes_common(tdata_t * td,hash_t * hash)4380Sstevel@tonic-gate tdata_build_hashes_common(tdata_t *td, hash_t *hash)
4390Sstevel@tonic-gate {
4400Sstevel@tonic-gate 	(void) iitraverse_hash(hash, &td->td_curvgen, NULL, NULL,
4410Sstevel@tonic-gate 	    build_hashes_cbs, td);
4420Sstevel@tonic-gate }
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate void
tdata_build_hashes(tdata_t * td)4450Sstevel@tonic-gate tdata_build_hashes(tdata_t *td)
4460Sstevel@tonic-gate {
4470Sstevel@tonic-gate 	tdata_build_hashes_common(td, td->td_iihash);
4480Sstevel@tonic-gate }
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate /* Merge td2 into td1.  td2 is destroyed by the merge */
4510Sstevel@tonic-gate void
tdata_merge(tdata_t * td1,tdata_t * td2)4520Sstevel@tonic-gate tdata_merge(tdata_t *td1, tdata_t *td2)
4530Sstevel@tonic-gate {
4540Sstevel@tonic-gate 	td1->td_curemark = MAX(td1->td_curemark, td2->td_curemark);
4550Sstevel@tonic-gate 	td1->td_curvgen = MAX(td1->td_curvgen, td2->td_curvgen);
4560Sstevel@tonic-gate 	td1->td_nextid = MAX(td1->td_nextid, td2->td_nextid);
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 	hash_merge(td1->td_iihash, td2->td_iihash);
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 	/* Add td2's type tree to the hashes */
4610Sstevel@tonic-gate 	tdata_build_hashes_common(td1, td2->td_iihash);
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 	list_concat(&td1->td_fwdlist, td2->td_fwdlist);
4640Sstevel@tonic-gate 	td2->td_fwdlist = NULL;
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 	slist_merge(&td1->td_labels, td2->td_labels,
4670Sstevel@tonic-gate 	    (int (*)())tdata_label_cmp);
4680Sstevel@tonic-gate 	td2->td_labels = NULL;
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 	/* free the td2 hashes (data is now part of td1) */
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 	hash_free(td2->td_layouthash, NULL, NULL);
4730Sstevel@tonic-gate 	td2->td_layouthash = NULL;
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 	hash_free(td2->td_iihash, NULL, NULL);
4760Sstevel@tonic-gate 	td2->td_iihash = NULL;
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 	tdata_free(td2);
4790Sstevel@tonic-gate }
480