Path: blob/main/cddl/contrib/opensolaris/tools/ctf/cvt/alist.c
39586 views
/*1* CDDL HEADER START2*3* The contents of this file are subject to the terms of the4* Common Development and Distribution License, Version 1.0 only5* (the "License"). You may not use this file except in compliance6* with the License.7*8* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9* or http://www.opensolaris.org/os/licensing.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/21/*22* Copyright 2001-2003 Sun Microsystems, Inc. All rights reserved.23* Use is subject to license terms.24*/2526#pragma ident "%Z%%M% %I% %E% SMI"2728/*29* Create, manage, and destroy association lists. alists are arrays with30* arbitrary index types, and are also commonly known as associative arrays.31*/3233#include <stdio.h>34#include <stdlib.h>3536#include "alist.h"37#include "memory.h"38#include "hash.h"3940#define ALIST_HASH_SIZE 9974142struct alist {43hash_t *al_elements;44void (*al_namefree)(void *);45void (*al_valfree)(void *);46};4748typedef struct alist_el {49void *ale_name;50void *ale_value;51} alist_el_t;5253static int54alist_hash(int nbuckets, void *arg)55{56alist_el_t *el = arg;57uintptr_t num = (uintptr_t)el->ale_name;5859return (num % nbuckets);60}6162static int63alist_cmp(void *arg1, void *arg2)64{65alist_el_t *el1 = arg1;66alist_el_t *el2 = arg2;67return ((uintptr_t)el1->ale_name != (uintptr_t)el2->ale_name);68}6970alist_t *71alist_xnew(int nbuckets, void (*namefree)(void *),72void (*valfree)(void *), int (*hashfn)(int, void *),73int (*cmpfn)(void *, void *))74{75alist_t *alist;7677alist = xcalloc(sizeof (alist_t));78alist->al_elements = hash_new(nbuckets, hashfn, cmpfn);79alist->al_namefree = namefree;80alist->al_valfree = valfree;8182return (alist);83}8485alist_t *86alist_new(void (*namefree)(void *), void (*valfree)(void *))87{88return (alist_xnew(ALIST_HASH_SIZE, namefree, valfree,89alist_hash, alist_cmp));90}9192static void93alist_free_cb(void *arg1, void *arg2)94{95alist_el_t *el = arg1;96alist_t *alist = arg2;97if (alist->al_namefree)98alist->al_namefree(el->ale_name);99if (alist->al_valfree)100alist->al_valfree(el->ale_name);101free(el);102}103104void105alist_free(alist_t *alist)106{107hash_free(alist->al_elements, alist_free_cb, alist);108free(alist);109}110111void112alist_add(alist_t *alist, void *name, void *value)113{114alist_el_t *el;115116el = xmalloc(sizeof (alist_el_t));117el->ale_name = name;118el->ale_value = value;119hash_add(alist->al_elements, el);120}121122int123alist_find(alist_t *alist, void *name, void **value)124{125alist_el_t template, *retx;126void *ret;127128template.ale_name = name;129if (!hash_find(alist->al_elements, &template, &ret))130return (0);131132if (value) {133retx = ret;134*value = retx->ale_value;135}136137return (1);138}139140typedef struct alist_iter_data {141int (*aid_func)(void *, void *, void *);142void *aid_priv;143} alist_iter_data_t;144145static int146alist_iter_cb(void *arg1, void *arg2)147{148alist_el_t *el = arg1;149alist_iter_data_t *aid = arg2;150return (aid->aid_func(el->ale_name, el->ale_value, aid->aid_priv));151}152153int154alist_iter(alist_t *alist, int (*func)(void *, void *, void *), void *private)155{156alist_iter_data_t aid;157158aid.aid_func = func;159aid.aid_priv = private;160161return (hash_iter(alist->al_elements, alist_iter_cb, &aid));162}163164/*165* Debugging support. Used to print the contents of an alist.166*/167168void169alist_stats(alist_t *alist, int verbose)170{171printf("Alist statistics\n");172hash_stats(alist->al_elements, verbose);173}174175static int alist_def_print_cb_key_int = 1;176static int alist_def_print_cb_value_int = 1;177178static int179alist_def_print_cb(void *key, void *value)180{181printf("Key: ");182if (alist_def_print_cb_key_int == 1)183printf("%5lu ", (ulong_t)key);184else185printf("%s\n", (char *)key);186187printf("Value: ");188if (alist_def_print_cb_value_int == 1)189printf("%5lu\n", (ulong_t)value);190else191printf("%s\n", (char *)key);192193return (1);194}195196static int197alist_dump_cb(void *node, void *private)198{199int (*printer)(void *, void *) = private;200alist_el_t *el = node;201202printer(el->ale_name, el->ale_value);203204return (1);205}206207int208alist_dump(alist_t *alist, int (*printer)(void *, void *))209{210if (!printer)211printer = alist_def_print_cb;212213return (hash_iter(alist->al_elements, alist_dump_cb, (void *)printer));214}215216217