xref: /onnv-gate/usr/src/tools/ctf/cvt/iidesc.c (revision 2722)
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*2722Sjohnlev  * Common Development and Distribution License (the "License").
6*2722Sjohnlev  * 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*2722Sjohnlev  * 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 iidesc_t 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 
360Sstevel@tonic-gate #include "ctftools.h"
370Sstevel@tonic-gate #include "memory.h"
380Sstevel@tonic-gate #include "list.h"
390Sstevel@tonic-gate #include "hash.h"
400Sstevel@tonic-gate 
410Sstevel@tonic-gate typedef struct iidesc_find {
420Sstevel@tonic-gate 	iidesc_t *iif_tgt;
430Sstevel@tonic-gate 	iidesc_t *iif_ret;
440Sstevel@tonic-gate } iidesc_find_t;
450Sstevel@tonic-gate 
460Sstevel@tonic-gate iidesc_t *
470Sstevel@tonic-gate iidesc_new(char *name)
480Sstevel@tonic-gate {
490Sstevel@tonic-gate 	iidesc_t *ii;
500Sstevel@tonic-gate 
510Sstevel@tonic-gate 	ii = xcalloc(sizeof (iidesc_t));
520Sstevel@tonic-gate 	if (name)
530Sstevel@tonic-gate 		ii->ii_name = xstrdup(name);
540Sstevel@tonic-gate 
550Sstevel@tonic-gate 	return (ii);
560Sstevel@tonic-gate }
570Sstevel@tonic-gate 
580Sstevel@tonic-gate int
590Sstevel@tonic-gate iidesc_hash(int nbuckets, void *arg)
600Sstevel@tonic-gate {
610Sstevel@tonic-gate 	iidesc_t *ii = arg;
620Sstevel@tonic-gate 	int h = 0;
630Sstevel@tonic-gate 
640Sstevel@tonic-gate 	if (ii->ii_name)
650Sstevel@tonic-gate 		return (hash_name(nbuckets, ii->ii_name));
660Sstevel@tonic-gate 
670Sstevel@tonic-gate 	return (h);
680Sstevel@tonic-gate }
690Sstevel@tonic-gate 
700Sstevel@tonic-gate static int
710Sstevel@tonic-gate iidesc_cmp(iidesc_t *src, iidesc_find_t *find)
720Sstevel@tonic-gate {
730Sstevel@tonic-gate 	iidesc_t *tgt = find->iif_tgt;
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 	if (src->ii_type != tgt->ii_type ||
760Sstevel@tonic-gate 	    !streq(src->ii_name, tgt->ii_name))
770Sstevel@tonic-gate 		return (0);
780Sstevel@tonic-gate 
790Sstevel@tonic-gate 	find->iif_ret = src;
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 	return (-1);
820Sstevel@tonic-gate }
830Sstevel@tonic-gate 
840Sstevel@tonic-gate void
850Sstevel@tonic-gate iidesc_add(hash_t *hash, iidesc_t *new)
860Sstevel@tonic-gate {
870Sstevel@tonic-gate 	iidesc_find_t find;
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 	find.iif_tgt = new;
900Sstevel@tonic-gate 	find.iif_ret = NULL;
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 	(void) hash_match(hash, new, (int (*)())iidesc_cmp, &find);
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 	if (find.iif_ret != NULL) {
950Sstevel@tonic-gate 		iidesc_t *old = find.iif_ret;
960Sstevel@tonic-gate 		iidesc_t tmp;
970Sstevel@tonic-gate 		/* replacing existing one */
980Sstevel@tonic-gate 		bcopy(old, &tmp, sizeof (tmp));
990Sstevel@tonic-gate 		bcopy(new, old, sizeof (*old));
1000Sstevel@tonic-gate 		bcopy(&tmp, new, sizeof (*new));
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 		iidesc_free(new, NULL);
1030Sstevel@tonic-gate 		return;
1040Sstevel@tonic-gate 	}
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	hash_add(hash, new);
1070Sstevel@tonic-gate }
1080Sstevel@tonic-gate 
109*2722Sjohnlev void
110*2722Sjohnlev iter_iidescs_by_name(tdata_t *td, const char *name,
111*2722Sjohnlev     int (*func)(iidesc_t *, void *), void *data)
112*2722Sjohnlev {
113*2722Sjohnlev 	iidesc_t tmpdesc;
114*2722Sjohnlev 	bzero(&tmpdesc, sizeof (iidesc_t));
115*2722Sjohnlev 	tmpdesc.ii_name = (char *)name;
116*2722Sjohnlev 	(void) hash_match(td->td_iihash, &tmpdesc, (int (*)())func, data);
117*2722Sjohnlev }
118*2722Sjohnlev 
1190Sstevel@tonic-gate iidesc_t *
1200Sstevel@tonic-gate iidesc_dup(iidesc_t *src)
1210Sstevel@tonic-gate {
1220Sstevel@tonic-gate 	iidesc_t *tgt;
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	tgt = xmalloc(sizeof (iidesc_t));
1250Sstevel@tonic-gate 	bcopy(src, tgt, sizeof (iidesc_t));
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	tgt->ii_name = src->ii_name ? xstrdup(src->ii_name) : NULL;
1280Sstevel@tonic-gate 	tgt->ii_owner = src->ii_owner ? xstrdup(src->ii_owner) : NULL;
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 	if (tgt->ii_nargs) {
1310Sstevel@tonic-gate 		tgt->ii_args = xmalloc(sizeof (tdesc_t *) * tgt->ii_nargs);
1320Sstevel@tonic-gate 		bcopy(src->ii_args, tgt->ii_args,
1330Sstevel@tonic-gate 		    sizeof (tdesc_t *) * tgt->ii_nargs);
1340Sstevel@tonic-gate 	}
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	return (tgt);
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate iidesc_t *
1400Sstevel@tonic-gate iidesc_dup_rename(iidesc_t *src, char const *name, char const *owner)
1410Sstevel@tonic-gate {
1420Sstevel@tonic-gate 	iidesc_t *tgt = iidesc_dup(src);
1430Sstevel@tonic-gate 	free(tgt->ii_name);
1440Sstevel@tonic-gate 	free(tgt->ii_owner);
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	tgt->ii_name = name ? xstrdup(name) : NULL;
1470Sstevel@tonic-gate 	tgt->ii_owner = owner ? xstrdup(owner) : NULL;
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	return (tgt);
1500Sstevel@tonic-gate }
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate /*ARGSUSED*/
1530Sstevel@tonic-gate void
1540Sstevel@tonic-gate iidesc_free(iidesc_t *idp, void *private)
1550Sstevel@tonic-gate {
1560Sstevel@tonic-gate 	if (idp->ii_name)
1570Sstevel@tonic-gate 		free(idp->ii_name);
1580Sstevel@tonic-gate 	if (idp->ii_nargs)
1590Sstevel@tonic-gate 		free(idp->ii_args);
1600Sstevel@tonic-gate 	if (idp->ii_owner)
1610Sstevel@tonic-gate 		free(idp->ii_owner);
1620Sstevel@tonic-gate 	free(idp);
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate int
1660Sstevel@tonic-gate iidesc_dump(iidesc_t *ii)
1670Sstevel@tonic-gate {
1680Sstevel@tonic-gate 	printf("type: %d  name %s\n", ii->ii_type,
1690Sstevel@tonic-gate 	    (ii->ii_name ? ii->ii_name : "(anon)"));
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	return (0);
1720Sstevel@tonic-gate }
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate int
1750Sstevel@tonic-gate iidesc_count_type(void *data, void *private)
1760Sstevel@tonic-gate {
1770Sstevel@tonic-gate 	iidesc_t *ii = data;
1780Sstevel@tonic-gate 	iitype_t match = (iitype_t)private;
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	return (ii->ii_type == match);
1810Sstevel@tonic-gate }
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate void
1840Sstevel@tonic-gate iidesc_stats(hash_t *ii)
1850Sstevel@tonic-gate {
1860Sstevel@tonic-gate 	printf("GFun: %5d SFun: %5d GVar: %5d SVar: %5d T %5d SOU: %5d\n",
1870Sstevel@tonic-gate 	    hash_iter(ii, iidesc_count_type, (void *)II_GFUN),
1880Sstevel@tonic-gate 	    hash_iter(ii, iidesc_count_type, (void *)II_SFUN),
1890Sstevel@tonic-gate 	    hash_iter(ii, iidesc_count_type, (void *)II_GVAR),
1900Sstevel@tonic-gate 	    hash_iter(ii, iidesc_count_type, (void *)II_SVAR),
1910Sstevel@tonic-gate 	    hash_iter(ii, iidesc_count_type, (void *)II_TYPE),
1920Sstevel@tonic-gate 	    hash_iter(ii, iidesc_count_type, (void *)II_SOU));
1930Sstevel@tonic-gate }
194