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