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