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