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