11673e404SJohn Birrell /*
21673e404SJohn Birrell * CDDL HEADER START
31673e404SJohn Birrell *
41673e404SJohn Birrell * The contents of this file are subject to the terms of the
51673e404SJohn Birrell * Common Development and Distribution License (the "License").
61673e404SJohn Birrell * You may not use this file except in compliance with the License.
71673e404SJohn Birrell *
81673e404SJohn Birrell * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
91673e404SJohn Birrell * or http://www.opensolaris.org/os/licensing.
101673e404SJohn Birrell * See the License for the specific language governing permissions
111673e404SJohn Birrell * and limitations under the License.
121673e404SJohn Birrell *
131673e404SJohn Birrell * When distributing Covered Code, include this CDDL HEADER in each
141673e404SJohn Birrell * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
151673e404SJohn Birrell * If applicable, add the following below this CDDL HEADER, with the
161673e404SJohn Birrell * fields enclosed by brackets "[]" replaced with your own identifying
171673e404SJohn Birrell * information: Portions Copyright [yyyy] [name of copyright owner]
181673e404SJohn Birrell *
191673e404SJohn Birrell * CDDL HEADER END
201673e404SJohn Birrell */
211673e404SJohn Birrell /*
221673e404SJohn Birrell * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
231673e404SJohn Birrell * Use is subject to license terms.
241673e404SJohn Birrell */
251673e404SJohn Birrell
261673e404SJohn Birrell #pragma ident "%Z%%M% %I% %E% SMI"
271673e404SJohn Birrell
281673e404SJohn Birrell /*
291673e404SJohn Birrell * Routines for manipulating iidesc_t structures
301673e404SJohn Birrell */
311673e404SJohn Birrell
321673e404SJohn Birrell #include <stdio.h>
331673e404SJohn Birrell #include <stdlib.h>
341673e404SJohn Birrell #include <strings.h>
351673e404SJohn Birrell
361673e404SJohn Birrell #include "ctftools.h"
371673e404SJohn Birrell #include "memory.h"
381673e404SJohn Birrell #include "list.h"
391673e404SJohn Birrell #include "hash.h"
401673e404SJohn Birrell
411673e404SJohn Birrell typedef struct iidesc_find {
421673e404SJohn Birrell iidesc_t *iif_tgt;
431673e404SJohn Birrell iidesc_t *iif_ret;
441673e404SJohn Birrell } iidesc_find_t;
451673e404SJohn Birrell
461673e404SJohn Birrell iidesc_t *
iidesc_new(char * name)471673e404SJohn Birrell iidesc_new(char *name)
481673e404SJohn Birrell {
491673e404SJohn Birrell iidesc_t *ii;
501673e404SJohn Birrell
511673e404SJohn Birrell ii = xcalloc(sizeof (iidesc_t));
521673e404SJohn Birrell if (name)
531673e404SJohn Birrell ii->ii_name = xstrdup(name);
541673e404SJohn Birrell
551673e404SJohn Birrell return (ii);
561673e404SJohn Birrell }
571673e404SJohn Birrell
581673e404SJohn Birrell int
iidesc_hash(int nbuckets,void * arg)591673e404SJohn Birrell iidesc_hash(int nbuckets, void *arg)
601673e404SJohn Birrell {
611673e404SJohn Birrell iidesc_t *ii = arg;
621673e404SJohn Birrell int h = 0;
631673e404SJohn Birrell
641673e404SJohn Birrell if (ii->ii_name)
651673e404SJohn Birrell return (hash_name(nbuckets, ii->ii_name));
661673e404SJohn Birrell
671673e404SJohn Birrell return (h);
681673e404SJohn Birrell }
691673e404SJohn Birrell
701673e404SJohn Birrell static int
iidesc_cmp(void * arg1,void * arg2)714cc75139SJohn Birrell iidesc_cmp(void *arg1, void *arg2)
721673e404SJohn Birrell {
734cc75139SJohn Birrell iidesc_t *src = arg1;
744cc75139SJohn Birrell iidesc_find_t *find = arg2;
751673e404SJohn Birrell iidesc_t *tgt = find->iif_tgt;
761673e404SJohn Birrell
771673e404SJohn Birrell if (src->ii_type != tgt->ii_type ||
781673e404SJohn Birrell !streq(src->ii_name, tgt->ii_name))
791673e404SJohn Birrell return (0);
801673e404SJohn Birrell
811673e404SJohn Birrell find->iif_ret = src;
821673e404SJohn Birrell
831673e404SJohn Birrell return (-1);
841673e404SJohn Birrell }
851673e404SJohn Birrell
861673e404SJohn Birrell void
iidesc_add(hash_t * hash,iidesc_t * new)871673e404SJohn Birrell iidesc_add(hash_t *hash, iidesc_t *new)
881673e404SJohn Birrell {
891673e404SJohn Birrell iidesc_find_t find;
901673e404SJohn Birrell
911673e404SJohn Birrell find.iif_tgt = new;
921673e404SJohn Birrell find.iif_ret = NULL;
931673e404SJohn Birrell
944cc75139SJohn Birrell (void) hash_match(hash, new, iidesc_cmp, &find);
951673e404SJohn Birrell
961673e404SJohn Birrell if (find.iif_ret != NULL) {
971673e404SJohn Birrell iidesc_t *old = find.iif_ret;
981673e404SJohn Birrell iidesc_t tmp;
991673e404SJohn Birrell /* replacing existing one */
1001673e404SJohn Birrell bcopy(old, &tmp, sizeof (tmp));
1011673e404SJohn Birrell bcopy(new, old, sizeof (*old));
1021673e404SJohn Birrell bcopy(&tmp, new, sizeof (*new));
1031673e404SJohn Birrell
1041673e404SJohn Birrell iidesc_free(new, NULL);
1051673e404SJohn Birrell return;
1061673e404SJohn Birrell }
1071673e404SJohn Birrell
1081673e404SJohn Birrell hash_add(hash, new);
1091673e404SJohn Birrell }
1101673e404SJohn Birrell
1111673e404SJohn Birrell void
iter_iidescs_by_name(tdata_t * td,char const * name,int (* func)(void *,void *),void * data)1124cc75139SJohn Birrell iter_iidescs_by_name(tdata_t *td, char const *name,
1134cc75139SJohn Birrell int (*func)(void *, void *), void *data)
1141673e404SJohn Birrell {
1151673e404SJohn Birrell iidesc_t tmpdesc;
1164cc75139SJohn Birrell bzero(&tmpdesc, sizeof(tmpdesc));
1174cc75139SJohn Birrell tmpdesc.ii_name = xstrdup(name);
1184cc75139SJohn Birrell (void) hash_match(td->td_iihash, &tmpdesc, func, data);
1194cc75139SJohn Birrell free(tmpdesc.ii_name);
1201673e404SJohn Birrell }
1211673e404SJohn Birrell
1221673e404SJohn Birrell iidesc_t *
iidesc_dup(iidesc_t * src)1231673e404SJohn Birrell iidesc_dup(iidesc_t *src)
1241673e404SJohn Birrell {
1251673e404SJohn Birrell iidesc_t *tgt;
1261673e404SJohn Birrell
1271673e404SJohn Birrell tgt = xmalloc(sizeof (iidesc_t));
1281673e404SJohn Birrell bcopy(src, tgt, sizeof (iidesc_t));
1291673e404SJohn Birrell
1301673e404SJohn Birrell tgt->ii_name = src->ii_name ? xstrdup(src->ii_name) : NULL;
1311673e404SJohn Birrell tgt->ii_owner = src->ii_owner ? xstrdup(src->ii_owner) : NULL;
1321673e404SJohn Birrell
1331673e404SJohn Birrell if (tgt->ii_nargs) {
1341673e404SJohn Birrell tgt->ii_args = xmalloc(sizeof (tdesc_t *) * tgt->ii_nargs);
1351673e404SJohn Birrell bcopy(src->ii_args, tgt->ii_args,
1361673e404SJohn Birrell sizeof (tdesc_t *) * tgt->ii_nargs);
1371673e404SJohn Birrell }
1381673e404SJohn Birrell
1391673e404SJohn Birrell return (tgt);
1401673e404SJohn Birrell }
1411673e404SJohn Birrell
1421673e404SJohn Birrell iidesc_t *
iidesc_dup_rename(iidesc_t * src,char const * name,char const * owner)1431673e404SJohn Birrell iidesc_dup_rename(iidesc_t *src, char const *name, char const *owner)
1441673e404SJohn Birrell {
1451673e404SJohn Birrell iidesc_t *tgt = iidesc_dup(src);
1461673e404SJohn Birrell free(tgt->ii_name);
1471673e404SJohn Birrell free(tgt->ii_owner);
1481673e404SJohn Birrell
1491673e404SJohn Birrell tgt->ii_name = name ? xstrdup(name) : NULL;
1501673e404SJohn Birrell tgt->ii_owner = owner ? xstrdup(owner) : NULL;
1511673e404SJohn Birrell
1521673e404SJohn Birrell return (tgt);
1531673e404SJohn Birrell }
1541673e404SJohn Birrell
1551673e404SJohn Birrell /*ARGSUSED*/
1561673e404SJohn Birrell void
iidesc_free(void * arg,void * private __unused)1574cc75139SJohn Birrell iidesc_free(void *arg, void *private __unused)
1581673e404SJohn Birrell {
1594cc75139SJohn Birrell iidesc_t *idp = arg;
1601673e404SJohn Birrell if (idp->ii_name)
1611673e404SJohn Birrell free(idp->ii_name);
1621673e404SJohn Birrell if (idp->ii_nargs)
1631673e404SJohn Birrell free(idp->ii_args);
1641673e404SJohn Birrell if (idp->ii_owner)
1651673e404SJohn Birrell free(idp->ii_owner);
1661673e404SJohn Birrell free(idp);
1671673e404SJohn Birrell }
1681673e404SJohn Birrell
1691673e404SJohn Birrell int
iidesc_dump(iidesc_t * ii)1701673e404SJohn Birrell iidesc_dump(iidesc_t *ii)
1711673e404SJohn Birrell {
1721673e404SJohn Birrell printf("type: %d name %s\n", ii->ii_type,
1731673e404SJohn Birrell (ii->ii_name ? ii->ii_name : "(anon)"));
1741673e404SJohn Birrell
1751673e404SJohn Birrell return (0);
1761673e404SJohn Birrell }
1771673e404SJohn Birrell
1781673e404SJohn Birrell int
iidesc_count_type(void * data,void * private)1791673e404SJohn Birrell iidesc_count_type(void *data, void *private)
1801673e404SJohn Birrell {
1811673e404SJohn Birrell iidesc_t *ii = data;
182*2938ecc8SBrooks Davis iitype_t match = (iitype_t)(uintptr_t)private;
1831673e404SJohn Birrell
1841673e404SJohn Birrell return (ii->ii_type == match);
1851673e404SJohn Birrell }
1861673e404SJohn Birrell
1871673e404SJohn Birrell void
iidesc_stats(hash_t * ii)1881673e404SJohn Birrell iidesc_stats(hash_t *ii)
1891673e404SJohn Birrell {
1901673e404SJohn Birrell printf("GFun: %5d SFun: %5d GVar: %5d SVar: %5d T %5d SOU: %5d\n",
1911673e404SJohn Birrell hash_iter(ii, iidesc_count_type, (void *)II_GFUN),
1921673e404SJohn Birrell hash_iter(ii, iidesc_count_type, (void *)II_SFUN),
1931673e404SJohn Birrell hash_iter(ii, iidesc_count_type, (void *)II_GVAR),
1941673e404SJohn Birrell hash_iter(ii, iidesc_count_type, (void *)II_SVAR),
1951673e404SJohn Birrell hash_iter(ii, iidesc_count_type, (void *)II_TYPE),
1961673e404SJohn Birrell hash_iter(ii, iidesc_count_type, (void *)II_SOU));
1971673e404SJohn Birrell }
198