8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / tools / ctf / cvt / iidesc.c
blobb6b9a0c7f235105c94c960da633cf5bd45bce6b2
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
29 * Routines for manipulating iidesc_t structures
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <strings.h>
36 #include "ctftools.h"
37 #include "memory.h"
38 #include "list.h"
39 #include "hash.h"
41 typedef struct iidesc_find {
42 iidesc_t *iif_tgt;
43 iidesc_t *iif_ret;
44 } iidesc_find_t;
46 iidesc_t *
47 iidesc_new(char *name)
49 iidesc_t *ii;
51 ii = xcalloc(sizeof (iidesc_t));
52 if (name)
53 ii->ii_name = xstrdup(name);
55 return (ii);
58 int
59 iidesc_hash(int nbuckets, void *arg)
61 iidesc_t *ii = arg;
62 int h = 0;
64 if (ii->ii_name)
65 return (hash_name(nbuckets, ii->ii_name));
67 return (h);
70 static int
71 iidesc_cmp(iidesc_t *src, iidesc_find_t *find)
73 iidesc_t *tgt = find->iif_tgt;
75 if (src->ii_type != tgt->ii_type ||
76 !streq(src->ii_name, tgt->ii_name))
77 return (0);
79 find->iif_ret = src;
81 return (-1);
84 void
85 iidesc_add(hash_t *hash, iidesc_t *new)
87 iidesc_find_t find;
89 find.iif_tgt = new;
90 find.iif_ret = NULL;
92 (void) hash_match(hash, new, (int (*)())iidesc_cmp, &find);
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));
102 iidesc_free(new, NULL);
103 return;
106 hash_add(hash, new);
109 void
110 iter_iidescs_by_name(tdata_t *td, const char *name,
111 int (*func)(iidesc_t *, void *), void *data)
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);
119 iidesc_t *
120 iidesc_dup(iidesc_t *src)
122 iidesc_t *tgt;
124 tgt = xmalloc(sizeof (iidesc_t));
125 bcopy(src, tgt, sizeof (iidesc_t));
127 tgt->ii_name = src->ii_name ? xstrdup(src->ii_name) : NULL;
128 tgt->ii_owner = src->ii_owner ? xstrdup(src->ii_owner) : NULL;
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);
136 return (tgt);
139 iidesc_t *
140 iidesc_dup_rename(iidesc_t *src, char const *name, char const *owner)
142 iidesc_t *tgt = iidesc_dup(src);
143 free(tgt->ii_name);
144 free(tgt->ii_owner);
146 tgt->ii_name = name ? xstrdup(name) : NULL;
147 tgt->ii_owner = owner ? xstrdup(owner) : NULL;
149 return (tgt);
152 /*ARGSUSED*/
153 void
154 iidesc_free(iidesc_t *idp, void *private)
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);
166 iidesc_dump(iidesc_t *ii)
168 printf("type: %d name %s\n", ii->ii_type,
169 (ii->ii_name ? ii->ii_name : "(anon)"));
171 return (0);
175 iidesc_count_type(void *data, void *private)
177 iidesc_t *ii = data;
178 iitype_t match = (iitype_t)private;
180 return (ii->ii_type == match);
183 void
184 iidesc_stats(hash_t *ii)
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));