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