1178481Sjb/*
2178481Sjb * CDDL HEADER START
3178481Sjb *
4178481Sjb * The contents of this file are subject to the terms of the
5178481Sjb * Common Development and Distribution License (the "License").
6178481Sjb * You may not use this file except in compliance with the License.
7178481Sjb *
8178481Sjb * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9178481Sjb * or http://www.opensolaris.org/os/licensing.
10178481Sjb * See the License for the specific language governing permissions
11178481Sjb * and limitations under the License.
12178481Sjb *
13178481Sjb * When distributing Covered Code, include this CDDL HEADER in each
14178481Sjb * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15178481Sjb * If applicable, add the following below this CDDL HEADER, with the
16178481Sjb * fields enclosed by brackets "[]" replaced with your own identifying
17178481Sjb * information: Portions Copyright [yyyy] [name of copyright owner]
18178481Sjb *
19178481Sjb * CDDL HEADER END
20178481Sjb */
21178481Sjb/*
22178481Sjb * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23178481Sjb * Use is subject to license terms.
24178481Sjb */
25178481Sjb
26178481Sjb#pragma ident	"%Z%%M%	%I%	%E% SMI"
27178481Sjb
28178481Sjb/*
29178481Sjb * Routines for manipulating iidesc_t structures
30178481Sjb */
31178481Sjb
32178481Sjb#include <stdio.h>
33178481Sjb#include <stdlib.h>
34178481Sjb#include <strings.h>
35178481Sjb
36178481Sjb#include "ctftools.h"
37178481Sjb#include "memory.h"
38178481Sjb#include "list.h"
39178481Sjb#include "hash.h"
40178481Sjb
41178481Sjbtypedef struct iidesc_find {
42178481Sjb	iidesc_t *iif_tgt;
43178481Sjb	iidesc_t *iif_ret;
44178481Sjb} iidesc_find_t;
45178481Sjb
46178481Sjbiidesc_t *
47178481Sjbiidesc_new(char *name)
48178481Sjb{
49178481Sjb	iidesc_t *ii;
50178481Sjb
51178481Sjb	ii = xcalloc(sizeof (iidesc_t));
52178481Sjb	if (name)
53178481Sjb		ii->ii_name = xstrdup(name);
54178481Sjb
55178481Sjb	return (ii);
56178481Sjb}
57178481Sjb
58178481Sjbint
59178481Sjbiidesc_hash(int nbuckets, void *arg)
60178481Sjb{
61178481Sjb	iidesc_t *ii = arg;
62178481Sjb	int h = 0;
63178481Sjb
64178481Sjb	if (ii->ii_name)
65178481Sjb		return (hash_name(nbuckets, ii->ii_name));
66178481Sjb
67178481Sjb	return (h);
68178481Sjb}
69178481Sjb
70178481Sjbstatic int
71178546Sjbiidesc_cmp(void *arg1, void *arg2)
72178481Sjb{
73178546Sjb	iidesc_t *src = arg1;
74178546Sjb	iidesc_find_t *find = arg2;
75178481Sjb	iidesc_t *tgt = find->iif_tgt;
76178481Sjb
77178481Sjb	if (src->ii_type != tgt->ii_type ||
78178481Sjb	    !streq(src->ii_name, tgt->ii_name))
79178481Sjb		return (0);
80178481Sjb
81178481Sjb	find->iif_ret = src;
82178481Sjb
83178481Sjb	return (-1);
84178481Sjb}
85178481Sjb
86178481Sjbvoid
87178481Sjbiidesc_add(hash_t *hash, iidesc_t *new)
88178481Sjb{
89178481Sjb	iidesc_find_t find;
90178481Sjb
91178481Sjb	find.iif_tgt = new;
92178481Sjb	find.iif_ret = NULL;
93178481Sjb
94178546Sjb	(void) hash_match(hash, new, iidesc_cmp, &find);
95178481Sjb
96178481Sjb	if (find.iif_ret != NULL) {
97178481Sjb		iidesc_t *old = find.iif_ret;
98178481Sjb		iidesc_t tmp;
99178481Sjb		/* replacing existing one */
100178481Sjb		bcopy(old, &tmp, sizeof (tmp));
101178481Sjb		bcopy(new, old, sizeof (*old));
102178481Sjb		bcopy(&tmp, new, sizeof (*new));
103178481Sjb
104178481Sjb		iidesc_free(new, NULL);
105178481Sjb		return;
106178481Sjb	}
107178481Sjb
108178481Sjb	hash_add(hash, new);
109178481Sjb}
110178481Sjb
111178481Sjbvoid
112178546Sjbiter_iidescs_by_name(tdata_t *td, char const *name,
113178546Sjb    int (*func)(void *, void *), void *data)
114178481Sjb{
115178481Sjb	iidesc_t tmpdesc;
116178546Sjb	bzero(&tmpdesc, sizeof(tmpdesc));
117178546Sjb	tmpdesc.ii_name = xstrdup(name);
118178546Sjb	(void) hash_match(td->td_iihash, &tmpdesc, func, data);
119178546Sjb	free(tmpdesc.ii_name);
120178481Sjb}
121178481Sjb
122178481Sjbiidesc_t *
123178481Sjbiidesc_dup(iidesc_t *src)
124178481Sjb{
125178481Sjb	iidesc_t *tgt;
126178481Sjb
127178481Sjb	tgt = xmalloc(sizeof (iidesc_t));
128178481Sjb	bcopy(src, tgt, sizeof (iidesc_t));
129178481Sjb
130178481Sjb	tgt->ii_name = src->ii_name ? xstrdup(src->ii_name) : NULL;
131178481Sjb	tgt->ii_owner = src->ii_owner ? xstrdup(src->ii_owner) : NULL;
132178481Sjb
133178481Sjb	if (tgt->ii_nargs) {
134178481Sjb		tgt->ii_args = xmalloc(sizeof (tdesc_t *) * tgt->ii_nargs);
135178481Sjb		bcopy(src->ii_args, tgt->ii_args,
136178481Sjb		    sizeof (tdesc_t *) * tgt->ii_nargs);
137178481Sjb	}
138178481Sjb
139178481Sjb	return (tgt);
140178481Sjb}
141178481Sjb
142178481Sjbiidesc_t *
143178481Sjbiidesc_dup_rename(iidesc_t *src, char const *name, char const *owner)
144178481Sjb{
145178481Sjb	iidesc_t *tgt = iidesc_dup(src);
146178481Sjb	free(tgt->ii_name);
147178481Sjb	free(tgt->ii_owner);
148178481Sjb
149178481Sjb	tgt->ii_name = name ? xstrdup(name) : NULL;
150178481Sjb	tgt->ii_owner = owner ? xstrdup(owner) : NULL;
151178481Sjb
152178481Sjb	return (tgt);
153178481Sjb}
154178481Sjb
155178481Sjb/*ARGSUSED*/
156178481Sjbvoid
157178546Sjbiidesc_free(void *arg, void *private __unused)
158178481Sjb{
159178546Sjb	iidesc_t *idp = arg;
160178481Sjb	if (idp->ii_name)
161178481Sjb		free(idp->ii_name);
162178481Sjb	if (idp->ii_nargs)
163178481Sjb		free(idp->ii_args);
164178481Sjb	if (idp->ii_owner)
165178481Sjb		free(idp->ii_owner);
166178481Sjb	free(idp);
167178481Sjb}
168178481Sjb
169178481Sjbint
170178481Sjbiidesc_dump(iidesc_t *ii)
171178481Sjb{
172178481Sjb	printf("type: %d  name %s\n", ii->ii_type,
173178481Sjb	    (ii->ii_name ? ii->ii_name : "(anon)"));
174178481Sjb
175178481Sjb	return (0);
176178481Sjb}
177178481Sjb
178178481Sjbint
179178481Sjbiidesc_count_type(void *data, void *private)
180178481Sjb{
181178481Sjb	iidesc_t *ii = data;
182178481Sjb	iitype_t match = (iitype_t)private;
183178481Sjb
184178481Sjb	return (ii->ii_type == match);
185178481Sjb}
186178481Sjb
187178481Sjbvoid
188178481Sjbiidesc_stats(hash_t *ii)
189178481Sjb{
190178481Sjb	printf("GFun: %5d SFun: %5d GVar: %5d SVar: %5d T %5d SOU: %5d\n",
191178481Sjb	    hash_iter(ii, iidesc_count_type, (void *)II_GFUN),
192178481Sjb	    hash_iter(ii, iidesc_count_type, (void *)II_SFUN),
193178481Sjb	    hash_iter(ii, iidesc_count_type, (void *)II_GVAR),
194178481Sjb	    hash_iter(ii, iidesc_count_type, (void *)II_SVAR),
195178481Sjb	    hash_iter(ii, iidesc_count_type, (void *)II_TYPE),
196178481Sjb	    hash_iter(ii, iidesc_count_type, (void *)II_SOU));
197178481Sjb}
198