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
5*e824d57fSjohnlev * Common Development and Distribution License (the "License").
6*e824d57fSjohnlev * 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*e824d57fSjohnlev * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate * Routines for manipulating iidesc_t structures
307c478bd9Sstevel@tonic-gate */
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate #include <stdio.h>
337c478bd9Sstevel@tonic-gate #include <stdlib.h>
347c478bd9Sstevel@tonic-gate #include <strings.h>
357c478bd9Sstevel@tonic-gate
367c478bd9Sstevel@tonic-gate #include "ctftools.h"
377c478bd9Sstevel@tonic-gate #include "memory.h"
387c478bd9Sstevel@tonic-gate #include "list.h"
397c478bd9Sstevel@tonic-gate #include "hash.h"
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate typedef struct iidesc_find {
427c478bd9Sstevel@tonic-gate iidesc_t *iif_tgt;
437c478bd9Sstevel@tonic-gate iidesc_t *iif_ret;
447c478bd9Sstevel@tonic-gate } iidesc_find_t;
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate iidesc_t *
iidesc_new(char * name)477c478bd9Sstevel@tonic-gate iidesc_new(char *name)
487c478bd9Sstevel@tonic-gate {
497c478bd9Sstevel@tonic-gate iidesc_t *ii;
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate ii = xcalloc(sizeof (iidesc_t));
527c478bd9Sstevel@tonic-gate if (name)
537c478bd9Sstevel@tonic-gate ii->ii_name = xstrdup(name);
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate return (ii);
567c478bd9Sstevel@tonic-gate }
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate int
iidesc_hash(int nbuckets,void * arg)597c478bd9Sstevel@tonic-gate iidesc_hash(int nbuckets, void *arg)
607c478bd9Sstevel@tonic-gate {
617c478bd9Sstevel@tonic-gate iidesc_t *ii = arg;
627c478bd9Sstevel@tonic-gate int h = 0;
637c478bd9Sstevel@tonic-gate
647c478bd9Sstevel@tonic-gate if (ii->ii_name)
657c478bd9Sstevel@tonic-gate return (hash_name(nbuckets, ii->ii_name));
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate return (h);
687c478bd9Sstevel@tonic-gate }
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gate static int
iidesc_cmp(iidesc_t * src,iidesc_find_t * find)717c478bd9Sstevel@tonic-gate iidesc_cmp(iidesc_t *src, iidesc_find_t *find)
727c478bd9Sstevel@tonic-gate {
737c478bd9Sstevel@tonic-gate iidesc_t *tgt = find->iif_tgt;
747c478bd9Sstevel@tonic-gate
757c478bd9Sstevel@tonic-gate if (src->ii_type != tgt->ii_type ||
767c478bd9Sstevel@tonic-gate !streq(src->ii_name, tgt->ii_name))
777c478bd9Sstevel@tonic-gate return (0);
787c478bd9Sstevel@tonic-gate
797c478bd9Sstevel@tonic-gate find->iif_ret = src;
807c478bd9Sstevel@tonic-gate
817c478bd9Sstevel@tonic-gate return (-1);
827c478bd9Sstevel@tonic-gate }
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate void
iidesc_add(hash_t * hash,iidesc_t * new)857c478bd9Sstevel@tonic-gate iidesc_add(hash_t *hash, iidesc_t *new)
867c478bd9Sstevel@tonic-gate {
877c478bd9Sstevel@tonic-gate iidesc_find_t find;
887c478bd9Sstevel@tonic-gate
897c478bd9Sstevel@tonic-gate find.iif_tgt = new;
907c478bd9Sstevel@tonic-gate find.iif_ret = NULL;
917c478bd9Sstevel@tonic-gate
927c478bd9Sstevel@tonic-gate (void) hash_match(hash, new, (int (*)())iidesc_cmp, &find);
937c478bd9Sstevel@tonic-gate
947c478bd9Sstevel@tonic-gate if (find.iif_ret != NULL) {
957c478bd9Sstevel@tonic-gate iidesc_t *old = find.iif_ret;
967c478bd9Sstevel@tonic-gate iidesc_t tmp;
977c478bd9Sstevel@tonic-gate /* replacing existing one */
987c478bd9Sstevel@tonic-gate bcopy(old, &tmp, sizeof (tmp));
997c478bd9Sstevel@tonic-gate bcopy(new, old, sizeof (*old));
1007c478bd9Sstevel@tonic-gate bcopy(&tmp, new, sizeof (*new));
1017c478bd9Sstevel@tonic-gate
1027c478bd9Sstevel@tonic-gate iidesc_free(new, NULL);
1037c478bd9Sstevel@tonic-gate return;
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate
1067c478bd9Sstevel@tonic-gate hash_add(hash, new);
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate
109*e824d57fSjohnlev void
iter_iidescs_by_name(tdata_t * td,const char * name,int (* func)(iidesc_t *,void *),void * data)110*e824d57fSjohnlev iter_iidescs_by_name(tdata_t *td, const char *name,
111*e824d57fSjohnlev int (*func)(iidesc_t *, void *), void *data)
112*e824d57fSjohnlev {
113*e824d57fSjohnlev iidesc_t tmpdesc;
114*e824d57fSjohnlev bzero(&tmpdesc, sizeof (iidesc_t));
115*e824d57fSjohnlev tmpdesc.ii_name = (char *)name;
116*e824d57fSjohnlev (void) hash_match(td->td_iihash, &tmpdesc, (int (*)())func, data);
117*e824d57fSjohnlev }
118*e824d57fSjohnlev
1197c478bd9Sstevel@tonic-gate iidesc_t *
iidesc_dup(iidesc_t * src)1207c478bd9Sstevel@tonic-gate iidesc_dup(iidesc_t *src)
1217c478bd9Sstevel@tonic-gate {
1227c478bd9Sstevel@tonic-gate iidesc_t *tgt;
1237c478bd9Sstevel@tonic-gate
1247c478bd9Sstevel@tonic-gate tgt = xmalloc(sizeof (iidesc_t));
1257c478bd9Sstevel@tonic-gate bcopy(src, tgt, sizeof (iidesc_t));
1267c478bd9Sstevel@tonic-gate
1277c478bd9Sstevel@tonic-gate tgt->ii_name = src->ii_name ? xstrdup(src->ii_name) : NULL;
1287c478bd9Sstevel@tonic-gate tgt->ii_owner = src->ii_owner ? xstrdup(src->ii_owner) : NULL;
1297c478bd9Sstevel@tonic-gate
1307c478bd9Sstevel@tonic-gate if (tgt->ii_nargs) {
1317c478bd9Sstevel@tonic-gate tgt->ii_args = xmalloc(sizeof (tdesc_t *) * tgt->ii_nargs);
1327c478bd9Sstevel@tonic-gate bcopy(src->ii_args, tgt->ii_args,
1337c478bd9Sstevel@tonic-gate sizeof (tdesc_t *) * tgt->ii_nargs);
1347c478bd9Sstevel@tonic-gate }
1357c478bd9Sstevel@tonic-gate
1367c478bd9Sstevel@tonic-gate return (tgt);
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate
1397c478bd9Sstevel@tonic-gate iidesc_t *
iidesc_dup_rename(iidesc_t * src,char const * name,char const * owner)1407c478bd9Sstevel@tonic-gate iidesc_dup_rename(iidesc_t *src, char const *name, char const *owner)
1417c478bd9Sstevel@tonic-gate {
1427c478bd9Sstevel@tonic-gate iidesc_t *tgt = iidesc_dup(src);
1437c478bd9Sstevel@tonic-gate free(tgt->ii_name);
1447c478bd9Sstevel@tonic-gate free(tgt->ii_owner);
1457c478bd9Sstevel@tonic-gate
1467c478bd9Sstevel@tonic-gate tgt->ii_name = name ? xstrdup(name) : NULL;
1477c478bd9Sstevel@tonic-gate tgt->ii_owner = owner ? xstrdup(owner) : NULL;
1487c478bd9Sstevel@tonic-gate
1497c478bd9Sstevel@tonic-gate return (tgt);
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate
1527c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1537c478bd9Sstevel@tonic-gate void
iidesc_free(iidesc_t * idp,void * private)1547c478bd9Sstevel@tonic-gate iidesc_free(iidesc_t *idp, void *private)
1557c478bd9Sstevel@tonic-gate {
1567c478bd9Sstevel@tonic-gate if (idp->ii_name)
1577c478bd9Sstevel@tonic-gate free(idp->ii_name);
1587c478bd9Sstevel@tonic-gate if (idp->ii_nargs)
1597c478bd9Sstevel@tonic-gate free(idp->ii_args);
1607c478bd9Sstevel@tonic-gate if (idp->ii_owner)
1617c478bd9Sstevel@tonic-gate free(idp->ii_owner);
1627c478bd9Sstevel@tonic-gate free(idp);
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate
1657c478bd9Sstevel@tonic-gate int
iidesc_dump(iidesc_t * ii)1667c478bd9Sstevel@tonic-gate iidesc_dump(iidesc_t *ii)
1677c478bd9Sstevel@tonic-gate {
1687c478bd9Sstevel@tonic-gate printf("type: %d name %s\n", ii->ii_type,
1697c478bd9Sstevel@tonic-gate (ii->ii_name ? ii->ii_name : "(anon)"));
1707c478bd9Sstevel@tonic-gate
1717c478bd9Sstevel@tonic-gate return (0);
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate
1747c478bd9Sstevel@tonic-gate int
iidesc_count_type(void * data,void * private)1757c478bd9Sstevel@tonic-gate iidesc_count_type(void *data, void *private)
1767c478bd9Sstevel@tonic-gate {
1777c478bd9Sstevel@tonic-gate iidesc_t *ii = data;
1787c478bd9Sstevel@tonic-gate iitype_t match = (iitype_t)private;
1797c478bd9Sstevel@tonic-gate
1807c478bd9Sstevel@tonic-gate return (ii->ii_type == match);
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate
1837c478bd9Sstevel@tonic-gate void
iidesc_stats(hash_t * ii)1847c478bd9Sstevel@tonic-gate iidesc_stats(hash_t *ii)
1857c478bd9Sstevel@tonic-gate {
1867c478bd9Sstevel@tonic-gate printf("GFun: %5d SFun: %5d GVar: %5d SVar: %5d T %5d SOU: %5d\n",
1877c478bd9Sstevel@tonic-gate hash_iter(ii, iidesc_count_type, (void *)II_GFUN),
1887c478bd9Sstevel@tonic-gate hash_iter(ii, iidesc_count_type, (void *)II_SFUN),
1897c478bd9Sstevel@tonic-gate hash_iter(ii, iidesc_count_type, (void *)II_GVAR),
1907c478bd9Sstevel@tonic-gate hash_iter(ii, iidesc_count_type, (void *)II_SVAR),
1917c478bd9Sstevel@tonic-gate hash_iter(ii, iidesc_count_type, (void *)II_TYPE),
1927c478bd9Sstevel@tonic-gate hash_iter(ii, iidesc_count_type, (void *)II_SOU));
1937c478bd9Sstevel@tonic-gate }
194