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