11673e404SJohn Birrell /*
21673e404SJohn Birrell * CDDL HEADER START
31673e404SJohn Birrell *
41673e404SJohn Birrell * The contents of this file are subject to the terms of the
51673e404SJohn Birrell * Common Development and Distribution License, Version 1.0 only
61673e404SJohn Birrell * (the "License"). You may not use this file except in compliance
71673e404SJohn Birrell * with the License.
81673e404SJohn Birrell *
91673e404SJohn Birrell * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
101673e404SJohn Birrell * or http://www.opensolaris.org/os/licensing.
111673e404SJohn Birrell * See the License for the specific language governing permissions
121673e404SJohn Birrell * and limitations under the License.
131673e404SJohn Birrell *
141673e404SJohn Birrell * When distributing Covered Code, include this CDDL HEADER in each
151673e404SJohn Birrell * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
161673e404SJohn Birrell * If applicable, add the following below this CDDL HEADER, with the
171673e404SJohn Birrell * fields enclosed by brackets "[]" replaced with your own identifying
181673e404SJohn Birrell * information: Portions Copyright [yyyy] [name of copyright owner]
191673e404SJohn Birrell *
201673e404SJohn Birrell * CDDL HEADER END
211673e404SJohn Birrell */
221673e404SJohn Birrell /*
231673e404SJohn Birrell * Copyright 2001-2003 Sun Microsystems, Inc. All rights reserved.
241673e404SJohn Birrell * Use is subject to license terms.
251673e404SJohn Birrell */
261673e404SJohn Birrell
271673e404SJohn Birrell #pragma ident "%Z%%M% %I% %E% SMI"
281673e404SJohn Birrell
291673e404SJohn Birrell /*
301673e404SJohn Birrell * Create, manage, and destroy association lists. alists are arrays with
311673e404SJohn Birrell * arbitrary index types, and are also commonly known as associative arrays.
321673e404SJohn Birrell */
331673e404SJohn Birrell
341673e404SJohn Birrell #include <stdio.h>
351673e404SJohn Birrell #include <stdlib.h>
361673e404SJohn Birrell
371673e404SJohn Birrell #include "alist.h"
381673e404SJohn Birrell #include "memory.h"
391673e404SJohn Birrell #include "hash.h"
401673e404SJohn Birrell
411673e404SJohn Birrell #define ALIST_HASH_SIZE 997
421673e404SJohn Birrell
431673e404SJohn Birrell struct alist {
441673e404SJohn Birrell hash_t *al_elements;
451673e404SJohn Birrell void (*al_namefree)(void *);
461673e404SJohn Birrell void (*al_valfree)(void *);
471673e404SJohn Birrell };
481673e404SJohn Birrell
491673e404SJohn Birrell typedef struct alist_el {
501673e404SJohn Birrell void *ale_name;
511673e404SJohn Birrell void *ale_value;
521673e404SJohn Birrell } alist_el_t;
531673e404SJohn Birrell
541673e404SJohn Birrell static int
alist_hash(int nbuckets,void * arg)554cc75139SJohn Birrell alist_hash(int nbuckets, void *arg)
561673e404SJohn Birrell {
574cc75139SJohn Birrell alist_el_t *el = arg;
581673e404SJohn Birrell uintptr_t num = (uintptr_t)el->ale_name;
591673e404SJohn Birrell
601673e404SJohn Birrell return (num % nbuckets);
611673e404SJohn Birrell }
621673e404SJohn Birrell
631673e404SJohn Birrell static int
alist_cmp(void * arg1,void * arg2)644cc75139SJohn Birrell alist_cmp(void *arg1, void *arg2)
651673e404SJohn Birrell {
664cc75139SJohn Birrell alist_el_t *el1 = arg1;
674cc75139SJohn Birrell alist_el_t *el2 = arg2;
681673e404SJohn Birrell return ((uintptr_t)el1->ale_name != (uintptr_t)el2->ale_name);
691673e404SJohn Birrell }
701673e404SJohn Birrell
711673e404SJohn Birrell alist_t *
alist_xnew(int nbuckets,void (* namefree)(void *),void (* valfree)(void *),int (* hashfn)(int,void *),int (* cmpfn)(void *,void *))721673e404SJohn Birrell alist_xnew(int nbuckets, void (*namefree)(void *),
731673e404SJohn Birrell void (*valfree)(void *), int (*hashfn)(int, void *),
741673e404SJohn Birrell int (*cmpfn)(void *, void *))
751673e404SJohn Birrell {
761673e404SJohn Birrell alist_t *alist;
771673e404SJohn Birrell
781673e404SJohn Birrell alist = xcalloc(sizeof (alist_t));
791673e404SJohn Birrell alist->al_elements = hash_new(nbuckets, hashfn, cmpfn);
801673e404SJohn Birrell alist->al_namefree = namefree;
811673e404SJohn Birrell alist->al_valfree = valfree;
821673e404SJohn Birrell
831673e404SJohn Birrell return (alist);
841673e404SJohn Birrell }
851673e404SJohn Birrell
861673e404SJohn Birrell alist_t *
alist_new(void (* namefree)(void *),void (* valfree)(void *))871673e404SJohn Birrell alist_new(void (*namefree)(void *), void (*valfree)(void *))
881673e404SJohn Birrell {
891673e404SJohn Birrell return (alist_xnew(ALIST_HASH_SIZE, namefree, valfree,
904cc75139SJohn Birrell alist_hash, alist_cmp));
911673e404SJohn Birrell }
921673e404SJohn Birrell
931673e404SJohn Birrell static void
alist_free_cb(void * arg1,void * arg2)944cc75139SJohn Birrell alist_free_cb(void *arg1, void *arg2)
951673e404SJohn Birrell {
964cc75139SJohn Birrell alist_el_t *el = arg1;
974cc75139SJohn Birrell alist_t *alist = arg2;
981673e404SJohn Birrell if (alist->al_namefree)
991673e404SJohn Birrell alist->al_namefree(el->ale_name);
1001673e404SJohn Birrell if (alist->al_valfree)
1011673e404SJohn Birrell alist->al_valfree(el->ale_name);
1021673e404SJohn Birrell free(el);
1031673e404SJohn Birrell }
1041673e404SJohn Birrell
1051673e404SJohn Birrell void
alist_free(alist_t * alist)1061673e404SJohn Birrell alist_free(alist_t *alist)
1071673e404SJohn Birrell {
1084cc75139SJohn Birrell hash_free(alist->al_elements, alist_free_cb, alist);
1091673e404SJohn Birrell free(alist);
1101673e404SJohn Birrell }
1111673e404SJohn Birrell
1121673e404SJohn Birrell void
alist_add(alist_t * alist,void * name,void * value)1131673e404SJohn Birrell alist_add(alist_t *alist, void *name, void *value)
1141673e404SJohn Birrell {
1151673e404SJohn Birrell alist_el_t *el;
1161673e404SJohn Birrell
1171673e404SJohn Birrell el = xmalloc(sizeof (alist_el_t));
1181673e404SJohn Birrell el->ale_name = name;
1191673e404SJohn Birrell el->ale_value = value;
1201673e404SJohn Birrell hash_add(alist->al_elements, el);
1211673e404SJohn Birrell }
1221673e404SJohn Birrell
1231673e404SJohn Birrell int
alist_find(alist_t * alist,void * name,void ** value)1241673e404SJohn Birrell alist_find(alist_t *alist, void *name, void **value)
1251673e404SJohn Birrell {
1264cc75139SJohn Birrell alist_el_t template, *retx;
1274cc75139SJohn Birrell void *ret;
1281673e404SJohn Birrell
1291673e404SJohn Birrell template.ale_name = name;
1304cc75139SJohn Birrell if (!hash_find(alist->al_elements, &template, &ret))
1311673e404SJohn Birrell return (0);
1321673e404SJohn Birrell
1334cc75139SJohn Birrell if (value) {
1344cc75139SJohn Birrell retx = ret;
1354cc75139SJohn Birrell *value = retx->ale_value;
1364cc75139SJohn Birrell }
1371673e404SJohn Birrell
1381673e404SJohn Birrell return (1);
1391673e404SJohn Birrell }
1401673e404SJohn Birrell
1411673e404SJohn Birrell typedef struct alist_iter_data {
1421673e404SJohn Birrell int (*aid_func)(void *, void *, void *);
1431673e404SJohn Birrell void *aid_priv;
1441673e404SJohn Birrell } alist_iter_data_t;
1451673e404SJohn Birrell
1461673e404SJohn Birrell static int
alist_iter_cb(void * arg1,void * arg2)1474cc75139SJohn Birrell alist_iter_cb(void *arg1, void *arg2)
1481673e404SJohn Birrell {
1494cc75139SJohn Birrell alist_el_t *el = arg1;
1504cc75139SJohn Birrell alist_iter_data_t *aid = arg2;
1511673e404SJohn Birrell return (aid->aid_func(el->ale_name, el->ale_value, aid->aid_priv));
1521673e404SJohn Birrell }
1531673e404SJohn Birrell
1541673e404SJohn Birrell int
alist_iter(alist_t * alist,int (* func)(void *,void *,void *),void * private)1551673e404SJohn Birrell alist_iter(alist_t *alist, int (*func)(void *, void *, void *), void *private)
1561673e404SJohn Birrell {
1571673e404SJohn Birrell alist_iter_data_t aid;
1581673e404SJohn Birrell
1591673e404SJohn Birrell aid.aid_func = func;
1601673e404SJohn Birrell aid.aid_priv = private;
1611673e404SJohn Birrell
1624cc75139SJohn Birrell return (hash_iter(alist->al_elements, alist_iter_cb, &aid));
1631673e404SJohn Birrell }
1641673e404SJohn Birrell
1651673e404SJohn Birrell /*
1661673e404SJohn Birrell * Debugging support. Used to print the contents of an alist.
1671673e404SJohn Birrell */
1681673e404SJohn Birrell
1691673e404SJohn Birrell void
alist_stats(alist_t * alist,int verbose)1701673e404SJohn Birrell alist_stats(alist_t *alist, int verbose)
1711673e404SJohn Birrell {
1721673e404SJohn Birrell printf("Alist statistics\n");
1731673e404SJohn Birrell hash_stats(alist->al_elements, verbose);
1741673e404SJohn Birrell }
1751673e404SJohn Birrell
1761673e404SJohn Birrell static int alist_def_print_cb_key_int = 1;
1771673e404SJohn Birrell static int alist_def_print_cb_value_int = 1;
1781673e404SJohn Birrell
1791673e404SJohn Birrell static int
alist_def_print_cb(void * key,void * value)1801673e404SJohn Birrell alist_def_print_cb(void *key, void *value)
1811673e404SJohn Birrell {
1821673e404SJohn Birrell printf("Key: ");
1831673e404SJohn Birrell if (alist_def_print_cb_key_int == 1)
1844cc75139SJohn Birrell printf("%5lu ", (ulong_t)key);
1851673e404SJohn Birrell else
1861673e404SJohn Birrell printf("%s\n", (char *)key);
1871673e404SJohn Birrell
1881673e404SJohn Birrell printf("Value: ");
1891673e404SJohn Birrell if (alist_def_print_cb_value_int == 1)
1904cc75139SJohn Birrell printf("%5lu\n", (ulong_t)value);
1911673e404SJohn Birrell else
1921673e404SJohn Birrell printf("%s\n", (char *)key);
1931673e404SJohn Birrell
1941673e404SJohn Birrell return (1);
1951673e404SJohn Birrell }
1961673e404SJohn Birrell
1971673e404SJohn Birrell static int
alist_dump_cb(void * node,void * private)1981673e404SJohn Birrell alist_dump_cb(void *node, void *private)
1991673e404SJohn Birrell {
2004cc75139SJohn Birrell int (*printer)(void *, void *) = private;
2011673e404SJohn Birrell alist_el_t *el = node;
2021673e404SJohn Birrell
2031673e404SJohn Birrell printer(el->ale_name, el->ale_value);
2041673e404SJohn Birrell
2051673e404SJohn Birrell return (1);
2061673e404SJohn Birrell }
2071673e404SJohn Birrell
2081673e404SJohn Birrell int
alist_dump(alist_t * alist,int (* printer)(void *,void *))2091673e404SJohn Birrell alist_dump(alist_t *alist, int (*printer)(void *, void *))
2101673e404SJohn Birrell {
2111673e404SJohn Birrell if (!printer)
2121673e404SJohn Birrell printer = alist_def_print_cb;
2131673e404SJohn Birrell
2141673e404SJohn Birrell return (hash_iter(alist->al_elements, alist_dump_cb, (void *)printer));
2151673e404SJohn Birrell }
216