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
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.
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]
23 * Copyright 2001-2003 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
30 * Create, manage, and destroy association lists. alists are arrays with
31 * arbitrary index types, and are also commonly known as associative arrays.
41 #define ALIST_HASH_SIZE 997
45 void (*al_namefree)(void *);
46 void (*al_valfree)(void *);
49 typedef struct alist_el {
55 alist_hash(int nbuckets, void *arg)
58 uintptr_t num = (uintptr_t)el->ale_name;
60 return (num % nbuckets);
64 alist_cmp(void *arg1, void *arg2)
66 alist_el_t *el1 = arg1;
67 alist_el_t *el2 = arg2;
68 return ((uintptr_t)el1->ale_name != (uintptr_t)el2->ale_name);
72 alist_xnew(int nbuckets, void (*namefree)(void *),
73 void (*valfree)(void *), int (*hashfn)(int, void *),
74 int (*cmpfn)(void *, void *))
78 alist = xcalloc(sizeof (alist_t));
79 alist->al_elements = hash_new(nbuckets, hashfn, cmpfn);
80 alist->al_namefree = namefree;
81 alist->al_valfree = valfree;
87 alist_new(void (*namefree)(void *), void (*valfree)(void *))
89 return (alist_xnew(ALIST_HASH_SIZE, namefree, valfree,
90 alist_hash, alist_cmp));
94 alist_free_cb(void *arg1, void *arg2)
96 alist_el_t *el = arg1;
97 alist_t *alist = arg2;
98 if (alist->al_namefree)
99 alist->al_namefree(el->ale_name);
100 if (alist->al_valfree)
101 alist->al_valfree(el->ale_name);
106 alist_free(alist_t *alist)
108 hash_free(alist->al_elements, alist_free_cb, alist);
113 alist_add(alist_t *alist, void *name, void *value)
117 el = xmalloc(sizeof (alist_el_t));
119 el->ale_value = value;
120 hash_add(alist->al_elements, el);
124 alist_find(alist_t *alist, void *name, void **value)
126 alist_el_t template, *retx;
129 template.ale_name = name;
130 if (!hash_find(alist->al_elements, &template, &ret))
135 *value = retx->ale_value;
141 typedef struct alist_iter_data {
142 int (*aid_func)(void *, void *, void *);
147 alist_iter_cb(void *arg1, void *arg2)
149 alist_el_t *el = arg1;
150 alist_iter_data_t *aid = arg2;
151 return (aid->aid_func(el->ale_name, el->ale_value, aid->aid_priv));
155 alist_iter(alist_t *alist, int (*func)(void *, void *, void *), void *private)
157 alist_iter_data_t aid;
160 aid.aid_priv = private;
162 return (hash_iter(alist->al_elements, alist_iter_cb, &aid));
166 * Debugging support. Used to print the contents of an alist.
170 alist_stats(alist_t *alist, int verbose)
172 printf("Alist statistics\n");
173 hash_stats(alist->al_elements, verbose);
176 static int alist_def_print_cb_key_int = 1;
177 static int alist_def_print_cb_value_int = 1;
180 alist_def_print_cb(void *key, void *value)
183 if (alist_def_print_cb_key_int == 1)
184 printf("%5lu ", (ulong_t)key);
186 printf("%s\n", (char *)key);
189 if (alist_def_print_cb_value_int == 1)
190 printf("%5lu\n", (ulong_t)value);
192 printf("%s\n", (char *)key);
198 alist_dump_cb(void *node, void *private)
200 int (*printer)(void *, void *) = private;
201 alist_el_t *el = node;
203 printer(el->ale_name, el->ale_value);
209 alist_dump(alist_t *alist, int (*printer)(void *, void *))
212 printer = alist_def_print_cb;
214 return (hash_iter(alist->al_elements, alist_dump_cb, (void *)printer));