4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
29 * Routines for manipulating tdesc and tdata structures
42 * The layout hash is used during the equivalency checking. We have a node in
43 * the child graph that may be equivalent to a node in the parent graph. To
44 * find the corresponding node (if any) in the parent, we need a quick way to
45 * get to all nodes in the parent that look like the node in the child. Since a
46 * large number of nodes don't have names, we need to incorporate the layout of
47 * the node into the hash. If we don't, we'll end up with the vast majority of
48 * nodes in bucket zero, with one or two nodes in each of the remaining buckets.
50 * There are a couple of constraints, both of which concern forward
51 * declarations. Recall that a forward declaration tdesc is equivalent to a
52 * tdesc that actually defines the structure or union. As such, we cannot
53 * incorporate anything into the hash for a named struct or union node that
54 * couldn't be found by looking at the forward, and vice versa.
57 tdesc_layouthash(int nbuckets, void *node)
66 switch (tdp->t_type) {
72 name = tdp->t_tdesc->t_name;
75 h = tdp->t_fndef->fn_nargs +
76 tdp->t_fndef->fn_vargs;
77 name = tdp->t_fndef->fn_ret->t_name;
80 h = tdp->t_ardef->ad_nelems;
81 name = tdp->t_ardef->ad_contents->t_name;
86 * Unnamed structures, which cannot have forward
87 * declarations pointing to them. We can therefore
88 * incorporate the name of the first member into
91 name = tdp->t_members->ml_name;
94 /* Use the first element in the hash value */
95 name = tdp->t_emem->el_name;
99 * Intrinsics, forwards, and typedefs all have
102 warning("Unexpected unnamed %d tdesc (ID %d)\n",
103 tdp->t_type, tdp->t_id);
108 return (hash_name(nbuckets, name));
110 return (h % nbuckets);
114 tdesc_layoutcmp(void *arg1, void *arg2)
116 tdesc_t *tdp1 = arg1, *tdp2 = arg2;
118 if (tdp1->t_name == NULL) {
119 if (tdp2->t_name == NULL)
123 } else if (tdp2->t_name == NULL)
126 return (strcmp(tdp1->t_name, tdp2->t_name));
130 tdesc_idhash(int nbuckets, void *data)
134 return (tdp->t_id % nbuckets);
138 tdesc_idcmp(void *arg1, void *arg2)
140 tdesc_t *tdp1 = arg1, *tdp2 = arg2;
142 if (tdp1->t_id == tdp2->t_id)
145 return (tdp1->t_id > tdp2->t_id ? 1 : -1);
149 tdesc_namehash(int nbuckets, void *data)
155 if (tdp->t_name == NULL)
158 for (h = 0, c = tdp->t_name; *c; c++) {
160 if ((g = (h & 0xf0000000)) != 0) {
166 return (h % nbuckets);
170 tdesc_namecmp(void *arg1, void *arg2)
172 tdesc_t *tdp1 = arg1, *tdp2 = arg2;
174 return (!streq(tdp1->t_name, tdp2->t_name));
180 tdesc_print(void *data, void *private __unused)
184 printf("%7d %s\n", tdp->t_id, tdesc_name(tdp));
191 free_intr(tdesc_t *tdp)
197 free_ardef(tdesc_t *tdp)
203 free_mlist(tdesc_t *tdp)
205 mlist_t *ml = tdp->t_members;
219 free_elist(tdesc_t *tdp)
221 elist_t *el = tdp->t_emem;
234 static void (*free_cbs[])(tdesc_t *) = {
253 tdesc_free_cb(void *arg, void *private __unused)
258 if (free_cbs[tdp->t_type])
259 free_cbs[tdp->t_type](tdp);
266 tdesc_free(tdesc_t *tdp)
268 tdesc_free_cb(tdp, NULL);
272 tdata_label_cmp(void *arg1, void *arg2)
274 labelent_t *le1 = arg1;
275 labelent_t *le2 = arg2;
276 return (le1->le_idx - le2->le_idx);
280 tdata_label_add(tdata_t *td, const char *label, int idx)
282 labelent_t *le = xmalloc(sizeof (*le));
284 le->le_name = xstrdup(label);
285 le->le_idx = (idx == -1 ? td->td_nextid - 1 : idx);
287 slist_add(&td->td_labels, le, tdata_label_cmp);
291 tdata_label_top_cb(void *data, void *arg)
293 labelent_t *le = data;
294 labelent_t **topp = arg;
302 tdata_label_top(tdata_t *td)
304 labelent_t *top = NULL;
306 (void) list_iter(td->td_labels, tdata_label_top_cb, &top);
312 tdata_label_find_cb(void *arg1, void *arg2)
314 labelent_t *le = arg1;
315 labelent_t *tmpl = arg2;
316 return (streq(le->le_name, tmpl->le_name));
320 tdata_label_find(tdata_t *td, char *label)
325 if (streq(label, "BASE")) {
326 ret = (labelent_t *)list_first(td->td_labels);
327 return (ret ? ret->le_idx : -1);
332 if (!(ret = (labelent_t *)list_find(td->td_labels, &let,
333 tdata_label_find_cb)))
336 return (ret->le_idx);
340 tdata_label_newmax_cb(void *data, void *arg)
342 labelent_t *le = data;
345 if (le->le_idx > *newmaxp) {
346 le->le_idx = *newmaxp;
354 tdata_label_newmax(tdata_t *td, int newmax)
356 (void) list_iter(td->td_labels, tdata_label_newmax_cb, &newmax);
361 tdata_label_free_cb(void *arg, void *private __unused)
363 labelent_t *le = arg;
370 tdata_label_free(tdata_t *td)
372 list_free(td->td_labels, tdata_label_free_cb, NULL);
373 td->td_labels = NULL;
379 tdata_t *new = xcalloc(sizeof (tdata_t));
381 new->td_layouthash = hash_new(TDATA_LAYOUT_HASH_SIZE, tdesc_layouthash,
383 new->td_idhash = hash_new(TDATA_ID_HASH_SIZE, tdesc_idhash,
386 * This is also traversed as a list, but amortized O(1)
387 * lookup massively impacts part of the merge phase, so
388 * we store the iidescs as a hash.
390 new->td_iihash = hash_new(IIDESC_HASH_SIZE, iidesc_hash, NULL);
394 pthread_mutex_init(&new->td_mergelock, NULL);
400 tdata_free(tdata_t *td)
402 hash_free(td->td_iihash, iidesc_free, NULL);
403 hash_free(td->td_layouthash, tdesc_free_cb, NULL);
404 hash_free(td->td_idhash, NULL, NULL);
405 list_free(td->td_fwdlist, NULL, NULL);
407 tdata_label_free(td);
409 free(td->td_parlabel);
410 free(td->td_parname);
412 pthread_mutex_destroy(&td->td_mergelock);
419 build_hashes(tdesc_t *ctdp, tdesc_t **ctdpp __unused, void *private)
421 tdata_t *td = private;
423 hash_add(td->td_idhash, ctdp);
424 hash_add(td->td_layouthash, ctdp);
429 static tdtrav_cb_f build_hashes_cbs[] = {
431 build_hashes, /* intrinsic */
432 build_hashes, /* pointer */
433 build_hashes, /* array */
434 build_hashes, /* function */
435 build_hashes, /* struct */
436 build_hashes, /* union */
437 build_hashes, /* enum */
438 build_hashes, /* forward */
439 build_hashes, /* typedef */
440 tdtrav_assert, /* typedef_unres */
441 build_hashes, /* volatile */
442 build_hashes, /* const */
443 build_hashes /* restrict */
447 tdata_build_hashes_common(tdata_t *td, hash_t *hash)
449 (void) iitraverse_hash(hash, &td->td_curvgen, NULL, NULL,
450 build_hashes_cbs, td);
454 tdata_build_hashes(tdata_t *td)
456 tdata_build_hashes_common(td, td->td_iihash);
459 /* Merge td2 into td1. td2 is destroyed by the merge */
461 tdata_merge(tdata_t *td1, tdata_t *td2)
463 td1->td_curemark = MAX(td1->td_curemark, td2->td_curemark);
464 td1->td_curvgen = MAX(td1->td_curvgen, td2->td_curvgen);
465 td1->td_nextid = MAX(td1->td_nextid, td2->td_nextid);
467 hash_merge(td1->td_iihash, td2->td_iihash);
469 /* Add td2's type tree to the hashes */
470 tdata_build_hashes_common(td1, td2->td_iihash);
472 list_concat(&td1->td_fwdlist, td2->td_fwdlist);
473 td2->td_fwdlist = NULL;
475 slist_merge(&td1->td_labels, td2->td_labels,
477 td2->td_labels = NULL;
479 /* free the td2 hashes (data is now part of td1) */
481 hash_free(td2->td_layouthash, NULL, NULL);
482 td2->td_layouthash = NULL;
484 hash_free(td2->td_iihash, NULL, NULL);
485 td2->td_iihash = NULL;