alist.c (178529) | alist.c (178546) |
---|---|
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 * --- 38 unchanged lines hidden (view full) --- 47}; 48 49typedef struct alist_el { 50 void *ale_name; 51 void *ale_value; 52} alist_el_t; 53 54static int | 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 * --- 38 unchanged lines hidden (view full) --- 47}; 48 49typedef struct alist_el { 50 void *ale_name; 51 void *ale_value; 52} alist_el_t; 53 54static int |
55alist_hash(int nbuckets, alist_el_t *el) | 55alist_hash(int nbuckets, void *arg) |
56{ | 56{ |
57 alist_el_t *el = arg; |
|
57 uintptr_t num = (uintptr_t)el->ale_name; 58 59 return (num % nbuckets); 60} 61 62static int | 58 uintptr_t num = (uintptr_t)el->ale_name; 59 60 return (num % nbuckets); 61} 62 63static int |
63alist_cmp(alist_el_t *el1, alist_el_t *el2) | 64alist_cmp(void *arg1, void *arg2) |
64{ | 65{ |
66 alist_el_t *el1 = arg1; 67 alist_el_t *el2 = arg2; |
|
65 return ((uintptr_t)el1->ale_name != (uintptr_t)el2->ale_name); 66} 67 68alist_t * 69alist_xnew(int nbuckets, void (*namefree)(void *), 70 void (*valfree)(void *), int (*hashfn)(int, void *), 71 int (*cmpfn)(void *, void *)) 72{ --- 6 unchanged lines hidden (view full) --- 79 80 return (alist); 81} 82 83alist_t * 84alist_new(void (*namefree)(void *), void (*valfree)(void *)) 85{ 86 return (alist_xnew(ALIST_HASH_SIZE, namefree, valfree, | 68 return ((uintptr_t)el1->ale_name != (uintptr_t)el2->ale_name); 69} 70 71alist_t * 72alist_xnew(int nbuckets, void (*namefree)(void *), 73 void (*valfree)(void *), int (*hashfn)(int, void *), 74 int (*cmpfn)(void *, void *)) 75{ --- 6 unchanged lines hidden (view full) --- 82 83 return (alist); 84} 85 86alist_t * 87alist_new(void (*namefree)(void *), void (*valfree)(void *)) 88{ 89 return (alist_xnew(ALIST_HASH_SIZE, namefree, valfree, |
87 (int (*)())alist_hash, (int (*)())alist_cmp)); | 90 alist_hash, alist_cmp)); |
88} 89 90static void | 91} 92 93static void |
91alist_free_cb(alist_el_t *el, alist_t *alist) | 94alist_free_cb(void *arg1, void *arg2) |
92{ | 95{ |
96 alist_el_t *el = arg1; 97 alist_t *alist = arg2; |
|
93 if (alist->al_namefree) 94 alist->al_namefree(el->ale_name); 95 if (alist->al_valfree) 96 alist->al_valfree(el->ale_name); 97 free(el); 98} 99 100void 101alist_free(alist_t *alist) 102{ | 98 if (alist->al_namefree) 99 alist->al_namefree(el->ale_name); 100 if (alist->al_valfree) 101 alist->al_valfree(el->ale_name); 102 free(el); 103} 104 105void 106alist_free(alist_t *alist) 107{ |
103 hash_free(alist->al_elements, (void (*)())alist_free_cb, alist); | 108 hash_free(alist->al_elements, alist_free_cb, alist); |
104 free(alist); 105} 106 107void 108alist_add(alist_t *alist, void *name, void *value) 109{ 110 alist_el_t *el; 111 112 el = xmalloc(sizeof (alist_el_t)); 113 el->ale_name = name; 114 el->ale_value = value; 115 hash_add(alist->al_elements, el); 116} 117 118int 119alist_find(alist_t *alist, void *name, void **value) 120{ | 109 free(alist); 110} 111 112void 113alist_add(alist_t *alist, void *name, void *value) 114{ 115 alist_el_t *el; 116 117 el = xmalloc(sizeof (alist_el_t)); 118 el->ale_name = name; 119 el->ale_value = value; 120 hash_add(alist->al_elements, el); 121} 122 123int 124alist_find(alist_t *alist, void *name, void **value) 125{ |
121 alist_el_t template, *ret; | 126 alist_el_t template, *retx; 127 void *ret; |
122 123 template.ale_name = name; | 128 129 template.ale_name = name; |
124 if (!hash_find(alist->al_elements, &template, (void **)&ret)) | 130 if (!hash_find(alist->al_elements, &template, &ret)) |
125 return (0); 126 | 131 return (0); 132 |
127 if (value) 128 *value = ret->ale_value; | 133 if (value) { 134 retx = ret; 135 *value = retx->ale_value; 136 } |
129 130 return (1); 131} 132 133typedef struct alist_iter_data { 134 int (*aid_func)(void *, void *, void *); 135 void *aid_priv; 136} alist_iter_data_t; 137 138static int | 137 138 return (1); 139} 140 141typedef struct alist_iter_data { 142 int (*aid_func)(void *, void *, void *); 143 void *aid_priv; 144} alist_iter_data_t; 145 146static int |
139alist_iter_cb(alist_el_t *el, alist_iter_data_t *aid) | 147alist_iter_cb(void *arg1, void *arg2) |
140{ | 148{ |
149 alist_el_t *el = arg1; 150 alist_iter_data_t *aid = arg2; |
|
141 return (aid->aid_func(el->ale_name, el->ale_value, aid->aid_priv)); 142} 143 144int 145alist_iter(alist_t *alist, int (*func)(void *, void *, void *), void *private) 146{ 147 alist_iter_data_t aid; 148 149 aid.aid_func = func; 150 aid.aid_priv = private; 151 | 151 return (aid->aid_func(el->ale_name, el->ale_value, aid->aid_priv)); 152} 153 154int 155alist_iter(alist_t *alist, int (*func)(void *, void *, void *), void *private) 156{ 157 alist_iter_data_t aid; 158 159 aid.aid_func = func; 160 aid.aid_priv = private; 161 |
152 return (hash_iter(alist->al_elements, (int (*)())alist_iter_cb, &aid)); | 162 return (hash_iter(alist->al_elements, alist_iter_cb, &aid)); |
153} 154 155/* 156 * Debugging support. Used to print the contents of an alist. 157 */ 158 159void 160alist_stats(alist_t *alist, int verbose) --- 5 unchanged lines hidden (view full) --- 166static int alist_def_print_cb_key_int = 1; 167static int alist_def_print_cb_value_int = 1; 168 169static int 170alist_def_print_cb(void *key, void *value) 171{ 172 printf("Key: "); 173 if (alist_def_print_cb_key_int == 1) | 163} 164 165/* 166 * Debugging support. Used to print the contents of an alist. 167 */ 168 169void 170alist_stats(alist_t *alist, int verbose) --- 5 unchanged lines hidden (view full) --- 176static int alist_def_print_cb_key_int = 1; 177static int alist_def_print_cb_value_int = 1; 178 179static int 180alist_def_print_cb(void *key, void *value) 181{ 182 printf("Key: "); 183 if (alist_def_print_cb_key_int == 1) |
174 printf("%5d ", (int)key); | 184 printf("%5lu ", (ulong_t)key); |
175 else 176 printf("%s\n", (char *)key); 177 178 printf("Value: "); 179 if (alist_def_print_cb_value_int == 1) | 185 else 186 printf("%s\n", (char *)key); 187 188 printf("Value: "); 189 if (alist_def_print_cb_value_int == 1) |
180 printf("%5d\n", (int)value); | 190 printf("%5lu\n", (ulong_t)value); |
181 else 182 printf("%s\n", (char *)key); 183 184 return (1); 185} 186 187static int 188alist_dump_cb(void *node, void *private) 189{ | 191 else 192 printf("%s\n", (char *)key); 193 194 return (1); 195} 196 197static int 198alist_dump_cb(void *node, void *private) 199{ |
190 int (*printer)(void *, void *) = (int (*)())private; | 200 int (*printer)(void *, void *) = private; |
191 alist_el_t *el = node; 192 193 printer(el->ale_name, el->ale_value); 194 195 return (1); 196} 197 198int 199alist_dump(alist_t *alist, int (*printer)(void *, void *)) 200{ 201 if (!printer) 202 printer = alist_def_print_cb; 203 204 return (hash_iter(alist->al_elements, alist_dump_cb, (void *)printer)); 205} | 201 alist_el_t *el = node; 202 203 printer(el->ale_name, el->ale_value); 204 205 return (1); 206} 207 208int 209alist_dump(alist_t *alist, int (*printer)(void *, void *)) 210{ 211 if (!printer) 212 printer = alist_def_print_cb; 213 214 return (hash_iter(alist->al_elements, alist_dump_cb, (void *)printer)); 215} |