xref: /onnv-gate/usr/src/tools/ctf/cvt/alist.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2001-2003 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * Create, manage, and destroy association lists.  alists are arrays with
31*0Sstevel@tonic-gate  * arbitrary index types, and are also commonly known as associative arrays.
32*0Sstevel@tonic-gate  */
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate #include <stdio.h>
35*0Sstevel@tonic-gate #include <stdlib.h>
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate #include "alist.h"
38*0Sstevel@tonic-gate #include "memory.h"
39*0Sstevel@tonic-gate #include "hash.h"
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate #define	ALIST_HASH_SIZE	997
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate struct alist {
44*0Sstevel@tonic-gate 	hash_t *al_elements;
45*0Sstevel@tonic-gate 	void (*al_namefree)(void *);
46*0Sstevel@tonic-gate 	void (*al_valfree)(void *);
47*0Sstevel@tonic-gate };
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate typedef struct alist_el {
50*0Sstevel@tonic-gate 	void *ale_name;
51*0Sstevel@tonic-gate 	void *ale_value;
52*0Sstevel@tonic-gate } alist_el_t;
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate static int
alist_hash(int nbuckets,alist_el_t * el)55*0Sstevel@tonic-gate alist_hash(int nbuckets, alist_el_t *el)
56*0Sstevel@tonic-gate {
57*0Sstevel@tonic-gate 	uintptr_t num = (uintptr_t)el->ale_name;
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate 	return (num % nbuckets);
60*0Sstevel@tonic-gate }
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate static int
alist_cmp(alist_el_t * el1,alist_el_t * el2)63*0Sstevel@tonic-gate alist_cmp(alist_el_t *el1, alist_el_t *el2)
64*0Sstevel@tonic-gate {
65*0Sstevel@tonic-gate 	return ((uintptr_t)el1->ale_name != (uintptr_t)el2->ale_name);
66*0Sstevel@tonic-gate }
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate alist_t *
alist_xnew(int nbuckets,void (* namefree)(void *),void (* valfree)(void *),int (* hashfn)(int,void *),int (* cmpfn)(void *,void *))69*0Sstevel@tonic-gate alist_xnew(int nbuckets, void (*namefree)(void *),
70*0Sstevel@tonic-gate     void (*valfree)(void *), int (*hashfn)(int, void *),
71*0Sstevel@tonic-gate     int (*cmpfn)(void *, void *))
72*0Sstevel@tonic-gate {
73*0Sstevel@tonic-gate 	alist_t *alist;
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate 	alist = xcalloc(sizeof (alist_t));
76*0Sstevel@tonic-gate 	alist->al_elements = hash_new(nbuckets, hashfn, cmpfn);
77*0Sstevel@tonic-gate 	alist->al_namefree = namefree;
78*0Sstevel@tonic-gate 	alist->al_valfree = valfree;
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate 	return (alist);
81*0Sstevel@tonic-gate }
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate alist_t *
alist_new(void (* namefree)(void *),void (* valfree)(void *))84*0Sstevel@tonic-gate alist_new(void (*namefree)(void *), void (*valfree)(void *))
85*0Sstevel@tonic-gate {
86*0Sstevel@tonic-gate 	return (alist_xnew(ALIST_HASH_SIZE, namefree, valfree,
87*0Sstevel@tonic-gate 	    (int (*)())alist_hash, (int (*)())alist_cmp));
88*0Sstevel@tonic-gate }
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate static void
alist_free_cb(alist_el_t * el,alist_t * alist)91*0Sstevel@tonic-gate alist_free_cb(alist_el_t *el, alist_t *alist)
92*0Sstevel@tonic-gate {
93*0Sstevel@tonic-gate 	if (alist->al_namefree)
94*0Sstevel@tonic-gate 		alist->al_namefree(el->ale_name);
95*0Sstevel@tonic-gate 	if (alist->al_valfree)
96*0Sstevel@tonic-gate 		alist->al_valfree(el->ale_name);
97*0Sstevel@tonic-gate 	free(el);
98*0Sstevel@tonic-gate }
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate void
alist_free(alist_t * alist)101*0Sstevel@tonic-gate alist_free(alist_t *alist)
102*0Sstevel@tonic-gate {
103*0Sstevel@tonic-gate 	hash_free(alist->al_elements, (void (*)())alist_free_cb, alist);
104*0Sstevel@tonic-gate 	free(alist);
105*0Sstevel@tonic-gate }
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate void
alist_add(alist_t * alist,void * name,void * value)108*0Sstevel@tonic-gate alist_add(alist_t *alist, void *name, void *value)
109*0Sstevel@tonic-gate {
110*0Sstevel@tonic-gate 	alist_el_t *el;
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate 	el = xmalloc(sizeof (alist_el_t));
113*0Sstevel@tonic-gate 	el->ale_name = name;
114*0Sstevel@tonic-gate 	el->ale_value = value;
115*0Sstevel@tonic-gate 	hash_add(alist->al_elements, el);
116*0Sstevel@tonic-gate }
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate int
alist_find(alist_t * alist,void * name,void ** value)119*0Sstevel@tonic-gate alist_find(alist_t *alist, void *name, void **value)
120*0Sstevel@tonic-gate {
121*0Sstevel@tonic-gate 	alist_el_t template, *ret;
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate 	template.ale_name = name;
124*0Sstevel@tonic-gate 	if (!hash_find(alist->al_elements, &template, (void **)&ret))
125*0Sstevel@tonic-gate 		return (0);
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate 	if (value)
128*0Sstevel@tonic-gate 		*value = ret->ale_value;
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate 	return (1);
131*0Sstevel@tonic-gate }
132*0Sstevel@tonic-gate 
133*0Sstevel@tonic-gate typedef struct alist_iter_data {
134*0Sstevel@tonic-gate 	int (*aid_func)(void *, void *, void *);
135*0Sstevel@tonic-gate 	void *aid_priv;
136*0Sstevel@tonic-gate } alist_iter_data_t;
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate static int
alist_iter_cb(alist_el_t * el,alist_iter_data_t * aid)139*0Sstevel@tonic-gate alist_iter_cb(alist_el_t *el, alist_iter_data_t *aid)
140*0Sstevel@tonic-gate {
141*0Sstevel@tonic-gate 	return (aid->aid_func(el->ale_name, el->ale_value, aid->aid_priv));
142*0Sstevel@tonic-gate }
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate int
alist_iter(alist_t * alist,int (* func)(void *,void *,void *),void * private)145*0Sstevel@tonic-gate alist_iter(alist_t *alist, int (*func)(void *, void *, void *), void *private)
146*0Sstevel@tonic-gate {
147*0Sstevel@tonic-gate 	alist_iter_data_t aid;
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate 	aid.aid_func = func;
150*0Sstevel@tonic-gate 	aid.aid_priv = private;
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate 	return (hash_iter(alist->al_elements, (int (*)())alist_iter_cb, &aid));
153*0Sstevel@tonic-gate }
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate /*
156*0Sstevel@tonic-gate  * Debugging support.  Used to print the contents of an alist.
157*0Sstevel@tonic-gate  */
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate void
alist_stats(alist_t * alist,int verbose)160*0Sstevel@tonic-gate alist_stats(alist_t *alist, int verbose)
161*0Sstevel@tonic-gate {
162*0Sstevel@tonic-gate 	printf("Alist statistics\n");
163*0Sstevel@tonic-gate 	hash_stats(alist->al_elements, verbose);
164*0Sstevel@tonic-gate }
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate static int alist_def_print_cb_key_int = 1;
167*0Sstevel@tonic-gate static int alist_def_print_cb_value_int = 1;
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate static int
alist_def_print_cb(void * key,void * value)170*0Sstevel@tonic-gate alist_def_print_cb(void *key, void *value)
171*0Sstevel@tonic-gate {
172*0Sstevel@tonic-gate 	printf("Key: ");
173*0Sstevel@tonic-gate 	if (alist_def_print_cb_key_int == 1)
174*0Sstevel@tonic-gate 		printf("%5d ", (int)key);
175*0Sstevel@tonic-gate 	else
176*0Sstevel@tonic-gate 		printf("%s\n", (char *)key);
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate 	printf("Value: ");
179*0Sstevel@tonic-gate 	if (alist_def_print_cb_value_int == 1)
180*0Sstevel@tonic-gate 		printf("%5d\n", (int)value);
181*0Sstevel@tonic-gate 	else
182*0Sstevel@tonic-gate 		printf("%s\n", (char *)key);
183*0Sstevel@tonic-gate 
184*0Sstevel@tonic-gate 	return (1);
185*0Sstevel@tonic-gate }
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate static int
alist_dump_cb(void * node,void * private)188*0Sstevel@tonic-gate alist_dump_cb(void *node, void *private)
189*0Sstevel@tonic-gate {
190*0Sstevel@tonic-gate 	int (*printer)(void *, void *) = (int (*)())private;
191*0Sstevel@tonic-gate 	alist_el_t *el = node;
192*0Sstevel@tonic-gate 
193*0Sstevel@tonic-gate 	printer(el->ale_name, el->ale_value);
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate 	return (1);
196*0Sstevel@tonic-gate }
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate int
alist_dump(alist_t * alist,int (* printer)(void *,void *))199*0Sstevel@tonic-gate alist_dump(alist_t *alist, int (*printer)(void *, void *))
200*0Sstevel@tonic-gate {
201*0Sstevel@tonic-gate 	if (!printer)
202*0Sstevel@tonic-gate 		printer = alist_def_print_cb;
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate 	return (hash_iter(alist->al_elements, alist_dump_cb, (void *)printer));
205*0Sstevel@tonic-gate }
206