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 #if HAVE_NBTOOL_CONFIG_H
33 # include "nbtool_config.h"
34 #endif
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <strings.h>
39
40 #include "ctftools.h"
41 #include "memory.h"
42 #include "list.h"
43 #include "hash.h"
44
45 typedef struct iidesc_find {
46 iidesc_t *iif_tgt;
47 iidesc_t *iif_ret;
48 } iidesc_find_t;
49
50 iidesc_t *
iidesc_new(char * name)51 iidesc_new(char *name)
52 {
53 iidesc_t *ii;
54
55 ii = xcalloc(sizeof (iidesc_t));
56 if (name)
57 ii->ii_name = xstrdup(name);
58
59 return (ii);
60 }
61
62 int
iidesc_hash(int nbuckets,void * arg)63 iidesc_hash(int nbuckets, void *arg)
64 {
65 iidesc_t *ii = arg;
66 int h = 0;
67
68 if (ii->ii_name)
69 return (hash_name(nbuckets, ii->ii_name));
70
71 return (h);
72 }
73
74 static int
iidesc_cmp(void * arg1,void * arg2)75 iidesc_cmp(void *arg1, void *arg2)
76 {
77 iidesc_t *src = arg1;
78 iidesc_find_t *find = arg2;
79 iidesc_t *tgt = find->iif_tgt;
80
81 if (src->ii_type != tgt->ii_type ||
82 !streq(src->ii_name, tgt->ii_name))
83 return (0);
84
85 find->iif_ret = src;
86
87 return (-1);
88 }
89
90 void
iidesc_add(hash_t * hash,iidesc_t * new)91 iidesc_add(hash_t *hash, iidesc_t *new)
92 {
93 iidesc_find_t find;
94
95 find.iif_tgt = new;
96 find.iif_ret = NULL;
97
98 (void) hash_match(hash, new, iidesc_cmp, &find);
99
100 if (find.iif_ret != NULL) {
101 iidesc_t *old = find.iif_ret;
102 iidesc_t tmp;
103 /* replacing existing one */
104 bcopy(old, &tmp, sizeof (tmp));
105 bcopy(new, old, sizeof (*old));
106 bcopy(&tmp, new, sizeof (*new));
107
108 iidesc_free(new, NULL);
109 return;
110 }
111
112 hash_add(hash, new);
113 }
114
115 void
iter_iidescs_by_name(tdata_t * td,char const * name,int (* func)(void *,void *),void * data)116 iter_iidescs_by_name(tdata_t *td, char const *name,
117 int (*func)(void *, void *), void *data)
118 {
119 iidesc_t tmpdesc;
120 bzero(&tmpdesc, sizeof(tmpdesc));
121 tmpdesc.ii_name = xstrdup(name);
122 (void) hash_match(td->td_iihash, &tmpdesc, func, data);
123 free(tmpdesc.ii_name);
124 }
125
126 iidesc_t *
iidesc_dup(iidesc_t * src)127 iidesc_dup(iidesc_t *src)
128 {
129 iidesc_t *tgt;
130
131 tgt = xmalloc(sizeof (iidesc_t));
132 bcopy(src, tgt, sizeof (iidesc_t));
133
134 tgt->ii_name = src->ii_name ? xstrdup(src->ii_name) : NULL;
135 tgt->ii_owner = src->ii_owner ? xstrdup(src->ii_owner) : NULL;
136
137 if (tgt->ii_nargs) {
138 tgt->ii_args = xmalloc(sizeof (tdesc_t *) * tgt->ii_nargs);
139 bcopy(src->ii_args, tgt->ii_args,
140 sizeof (tdesc_t *) * tgt->ii_nargs);
141 }
142
143 return (tgt);
144 }
145
146 iidesc_t *
iidesc_dup_rename(iidesc_t * src,char const * name,char const * owner)147 iidesc_dup_rename(iidesc_t *src, char const *name, char const *owner)
148 {
149 iidesc_t *tgt = iidesc_dup(src);
150 free(tgt->ii_name);
151 free(tgt->ii_owner);
152
153 tgt->ii_name = name ? xstrdup(name) : NULL;
154 tgt->ii_owner = owner ? xstrdup(owner) : NULL;
155
156 return (tgt);
157 }
158
159 /*ARGSUSED*/
160 void
iidesc_free(void * arg,void * private __unused)161 iidesc_free(void *arg, void *private __unused)
162 {
163 iidesc_t *idp = arg;
164 if (idp->ii_name)
165 free(idp->ii_name);
166 if (idp->ii_nargs)
167 free(idp->ii_args);
168 if (idp->ii_owner)
169 free(idp->ii_owner);
170 free(idp);
171 }
172
173 int
iidesc_dump(iidesc_t * ii)174 iidesc_dump(iidesc_t *ii)
175 {
176 printf("type: %d name %s\n", ii->ii_type,
177 (ii->ii_name ? ii->ii_name : "(anon)"));
178
179 return (0);
180 }
181
182 int
iidesc_count_type(void * data,void * private)183 iidesc_count_type(void *data, void *private)
184 {
185 iidesc_t *ii = data;
186 iitype_t match = (iitype_t)(uintptr_t)private;
187
188 return (ii->ii_type == match);
189 }
190
191 void
iidesc_stats(hash_t * ii)192 iidesc_stats(hash_t *ii)
193 {
194 printf("GFun: %5d SFun: %5d GVar: %5d SVar: %5d T %5d SOU: %5d\n",
195 hash_iter(ii, iidesc_count_type, (void *)II_GFUN),
196 hash_iter(ii, iidesc_count_type, (void *)II_SFUN),
197 hash_iter(ii, iidesc_count_type, (void *)II_GVAR),
198 hash_iter(ii, iidesc_count_type, (void *)II_SVAR),
199 hash_iter(ii, iidesc_count_type, (void *)II_TYPE),
200 hash_iter(ii, iidesc_count_type, (void *)II_SOU));
201 }
202