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