xref: /freebsd-src/cddl/contrib/opensolaris/tools/ctf/cvt/iidesc.c (revision 1673e4046da975ab0e2bf67d45499930ebab0dbe)
1*1673e404SJohn Birrell /*
2*1673e404SJohn Birrell  * CDDL HEADER START
3*1673e404SJohn Birrell  *
4*1673e404SJohn Birrell  * The contents of this file are subject to the terms of the
5*1673e404SJohn Birrell  * Common Development and Distribution License (the "License").
6*1673e404SJohn Birrell  * You may not use this file except in compliance with the License.
7*1673e404SJohn Birrell  *
8*1673e404SJohn Birrell  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*1673e404SJohn Birrell  * or http://www.opensolaris.org/os/licensing.
10*1673e404SJohn Birrell  * See the License for the specific language governing permissions
11*1673e404SJohn Birrell  * and limitations under the License.
12*1673e404SJohn Birrell  *
13*1673e404SJohn Birrell  * When distributing Covered Code, include this CDDL HEADER in each
14*1673e404SJohn Birrell  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*1673e404SJohn Birrell  * If applicable, add the following below this CDDL HEADER, with the
16*1673e404SJohn Birrell  * fields enclosed by brackets "[]" replaced with your own identifying
17*1673e404SJohn Birrell  * information: Portions Copyright [yyyy] [name of copyright owner]
18*1673e404SJohn Birrell  *
19*1673e404SJohn Birrell  * CDDL HEADER END
20*1673e404SJohn Birrell  */
21*1673e404SJohn Birrell /*
22*1673e404SJohn Birrell  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23*1673e404SJohn Birrell  * Use is subject to license terms.
24*1673e404SJohn Birrell  */
25*1673e404SJohn Birrell 
26*1673e404SJohn Birrell #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*1673e404SJohn Birrell 
28*1673e404SJohn Birrell /*
29*1673e404SJohn Birrell  * Routines for manipulating iidesc_t structures
30*1673e404SJohn Birrell  */
31*1673e404SJohn Birrell 
32*1673e404SJohn Birrell #include <stdio.h>
33*1673e404SJohn Birrell #include <stdlib.h>
34*1673e404SJohn Birrell #include <strings.h>
35*1673e404SJohn Birrell 
36*1673e404SJohn Birrell #include "ctftools.h"
37*1673e404SJohn Birrell #include "memory.h"
38*1673e404SJohn Birrell #include "list.h"
39*1673e404SJohn Birrell #include "hash.h"
40*1673e404SJohn Birrell 
41*1673e404SJohn Birrell typedef struct iidesc_find {
42*1673e404SJohn Birrell 	iidesc_t *iif_tgt;
43*1673e404SJohn Birrell 	iidesc_t *iif_ret;
44*1673e404SJohn Birrell } iidesc_find_t;
45*1673e404SJohn Birrell 
46*1673e404SJohn Birrell iidesc_t *
47*1673e404SJohn Birrell iidesc_new(char *name)
48*1673e404SJohn Birrell {
49*1673e404SJohn Birrell 	iidesc_t *ii;
50*1673e404SJohn Birrell 
51*1673e404SJohn Birrell 	ii = xcalloc(sizeof (iidesc_t));
52*1673e404SJohn Birrell 	if (name)
53*1673e404SJohn Birrell 		ii->ii_name = xstrdup(name);
54*1673e404SJohn Birrell 
55*1673e404SJohn Birrell 	return (ii);
56*1673e404SJohn Birrell }
57*1673e404SJohn Birrell 
58*1673e404SJohn Birrell int
59*1673e404SJohn Birrell iidesc_hash(int nbuckets, void *arg)
60*1673e404SJohn Birrell {
61*1673e404SJohn Birrell 	iidesc_t *ii = arg;
62*1673e404SJohn Birrell 	int h = 0;
63*1673e404SJohn Birrell 
64*1673e404SJohn Birrell 	if (ii->ii_name)
65*1673e404SJohn Birrell 		return (hash_name(nbuckets, ii->ii_name));
66*1673e404SJohn Birrell 
67*1673e404SJohn Birrell 	return (h);
68*1673e404SJohn Birrell }
69*1673e404SJohn Birrell 
70*1673e404SJohn Birrell static int
71*1673e404SJohn Birrell iidesc_cmp(void *arg1, void *arg2)
72*1673e404SJohn Birrell {
73*1673e404SJohn Birrell 	iidesc_t *src = arg1;
74*1673e404SJohn Birrell 	iidesc_find_t *find = arg2;
75*1673e404SJohn Birrell 	iidesc_t *tgt = find->iif_tgt;
76*1673e404SJohn Birrell 
77*1673e404SJohn Birrell 	if (src->ii_type != tgt->ii_type ||
78*1673e404SJohn Birrell 	    !streq(src->ii_name, tgt->ii_name))
79*1673e404SJohn Birrell 		return (0);
80*1673e404SJohn Birrell 
81*1673e404SJohn Birrell 	find->iif_ret = src;
82*1673e404SJohn Birrell 
83*1673e404SJohn Birrell 	return (-1);
84*1673e404SJohn Birrell }
85*1673e404SJohn Birrell 
86*1673e404SJohn Birrell void
87*1673e404SJohn Birrell iidesc_add(hash_t *hash, iidesc_t *new)
88*1673e404SJohn Birrell {
89*1673e404SJohn Birrell 	iidesc_find_t find;
90*1673e404SJohn Birrell 
91*1673e404SJohn Birrell 	find.iif_tgt = new;
92*1673e404SJohn Birrell 	find.iif_ret = NULL;
93*1673e404SJohn Birrell 
94*1673e404SJohn Birrell 	(void) hash_match(hash, new, iidesc_cmp, &find);
95*1673e404SJohn Birrell 
96*1673e404SJohn Birrell 	if (find.iif_ret != NULL) {
97*1673e404SJohn Birrell 		iidesc_t *old = find.iif_ret;
98*1673e404SJohn Birrell 		iidesc_t tmp;
99*1673e404SJohn Birrell 		/* replacing existing one */
100*1673e404SJohn Birrell 		bcopy(old, &tmp, sizeof (tmp));
101*1673e404SJohn Birrell 		bcopy(new, old, sizeof (*old));
102*1673e404SJohn Birrell 		bcopy(&tmp, new, sizeof (*new));
103*1673e404SJohn Birrell 
104*1673e404SJohn Birrell 		iidesc_free(new, NULL);
105*1673e404SJohn Birrell 		return;
106*1673e404SJohn Birrell 	}
107*1673e404SJohn Birrell 
108*1673e404SJohn Birrell 	hash_add(hash, new);
109*1673e404SJohn Birrell }
110*1673e404SJohn Birrell 
111*1673e404SJohn Birrell void
112*1673e404SJohn Birrell iter_iidescs_by_name(tdata_t *td, char const *name,
113*1673e404SJohn Birrell     int (*func)(void *, void *), void *data)
114*1673e404SJohn Birrell {
115*1673e404SJohn Birrell 	iidesc_t tmpdesc;
116*1673e404SJohn Birrell 	bzero(&tmpdesc, sizeof(tmpdesc));
117*1673e404SJohn Birrell 	tmpdesc.ii_name = xstrdup(name);
118*1673e404SJohn Birrell 	(void) hash_match(td->td_iihash, &tmpdesc, func, data);
119*1673e404SJohn Birrell 	free(tmpdesc.ii_name);
120*1673e404SJohn Birrell }
121*1673e404SJohn Birrell 
122*1673e404SJohn Birrell iidesc_t *
123*1673e404SJohn Birrell iidesc_dup(iidesc_t *src)
124*1673e404SJohn Birrell {
125*1673e404SJohn Birrell 	iidesc_t *tgt;
126*1673e404SJohn Birrell 
127*1673e404SJohn Birrell 	tgt = xmalloc(sizeof (iidesc_t));
128*1673e404SJohn Birrell 	bcopy(src, tgt, sizeof (iidesc_t));
129*1673e404SJohn Birrell 
130*1673e404SJohn Birrell 	tgt->ii_name = src->ii_name ? xstrdup(src->ii_name) : NULL;
131*1673e404SJohn Birrell 	tgt->ii_owner = src->ii_owner ? xstrdup(src->ii_owner) : NULL;
132*1673e404SJohn Birrell 
133*1673e404SJohn Birrell 	if (tgt->ii_nargs) {
134*1673e404SJohn Birrell 		tgt->ii_args = xmalloc(sizeof (tdesc_t *) * tgt->ii_nargs);
135*1673e404SJohn Birrell 		bcopy(src->ii_args, tgt->ii_args,
136*1673e404SJohn Birrell 		    sizeof (tdesc_t *) * tgt->ii_nargs);
137*1673e404SJohn Birrell 	}
138*1673e404SJohn Birrell 
139*1673e404SJohn Birrell 	return (tgt);
140*1673e404SJohn Birrell }
141*1673e404SJohn Birrell 
142*1673e404SJohn Birrell iidesc_t *
143*1673e404SJohn Birrell iidesc_dup_rename(iidesc_t *src, char const *name, char const *owner)
144*1673e404SJohn Birrell {
145*1673e404SJohn Birrell 	iidesc_t *tgt = iidesc_dup(src);
146*1673e404SJohn Birrell 	free(tgt->ii_name);
147*1673e404SJohn Birrell 	free(tgt->ii_owner);
148*1673e404SJohn Birrell 
149*1673e404SJohn Birrell 	tgt->ii_name = name ? xstrdup(name) : NULL;
150*1673e404SJohn Birrell 	tgt->ii_owner = owner ? xstrdup(owner) : NULL;
151*1673e404SJohn Birrell 
152*1673e404SJohn Birrell 	return (tgt);
153*1673e404SJohn Birrell }
154*1673e404SJohn Birrell 
155*1673e404SJohn Birrell /*ARGSUSED*/
156*1673e404SJohn Birrell void
157*1673e404SJohn Birrell iidesc_free(void *arg, void *private __unused)
158*1673e404SJohn Birrell {
159*1673e404SJohn Birrell 	iidesc_t *idp = arg;
160*1673e404SJohn Birrell 	if (idp->ii_name)
161*1673e404SJohn Birrell 		free(idp->ii_name);
162*1673e404SJohn Birrell 	if (idp->ii_nargs)
163*1673e404SJohn Birrell 		free(idp->ii_args);
164*1673e404SJohn Birrell 	if (idp->ii_owner)
165*1673e404SJohn Birrell 		free(idp->ii_owner);
166*1673e404SJohn Birrell 	free(idp);
167*1673e404SJohn Birrell }
168*1673e404SJohn Birrell 
169*1673e404SJohn Birrell int
170*1673e404SJohn Birrell iidesc_dump(iidesc_t *ii)
171*1673e404SJohn Birrell {
172*1673e404SJohn Birrell 	printf("type: %d  name %s\n", ii->ii_type,
173*1673e404SJohn Birrell 	    (ii->ii_name ? ii->ii_name : "(anon)"));
174*1673e404SJohn Birrell 
175*1673e404SJohn Birrell 	return (0);
176*1673e404SJohn Birrell }
177*1673e404SJohn Birrell 
178*1673e404SJohn Birrell int
179*1673e404SJohn Birrell iidesc_count_type(void *data, void *private)
180*1673e404SJohn Birrell {
181*1673e404SJohn Birrell 	iidesc_t *ii = data;
182*1673e404SJohn Birrell 	iitype_t match = (iitype_t)private;
183*1673e404SJohn Birrell 
184*1673e404SJohn Birrell 	return (ii->ii_type == match);
185*1673e404SJohn Birrell }
186*1673e404SJohn Birrell 
187*1673e404SJohn Birrell void
188*1673e404SJohn Birrell iidesc_stats(hash_t *ii)
189*1673e404SJohn Birrell {
190*1673e404SJohn Birrell 	printf("GFun: %5d SFun: %5d GVar: %5d SVar: %5d T %5d SOU: %5d\n",
191*1673e404SJohn Birrell 	    hash_iter(ii, iidesc_count_type, (void *)II_GFUN),
192*1673e404SJohn Birrell 	    hash_iter(ii, iidesc_count_type, (void *)II_SFUN),
193*1673e404SJohn Birrell 	    hash_iter(ii, iidesc_count_type, (void *)II_GVAR),
194*1673e404SJohn Birrell 	    hash_iter(ii, iidesc_count_type, (void *)II_SVAR),
195*1673e404SJohn Birrell 	    hash_iter(ii, iidesc_count_type, (void *)II_TYPE),
196*1673e404SJohn Birrell 	    hash_iter(ii, iidesc_count_type, (void *)II_SOU));
197*1673e404SJohn Birrell }
198