ctf-hash.c revision 1.1 1 1.1 christos /* Interface to hashtable implementations.
2 1.1 christos Copyright (C) 2006-2020 Free Software Foundation, Inc.
3 1.1 christos
4 1.1 christos This file is part of libctf.
5 1.1 christos
6 1.1 christos libctf is free software; you can redistribute it and/or modify it under
7 1.1 christos the terms of the GNU General Public License as published by the Free
8 1.1 christos Software Foundation; either version 3, or (at your option) any later
9 1.1 christos version.
10 1.1 christos
11 1.1 christos This program is distributed in the hope that it will be useful, but
12 1.1 christos WITHOUT ANY WARRANTY; without even the implied warranty of
13 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 1.1 christos See the GNU General Public License for more details.
15 1.1 christos
16 1.1 christos You should have received a copy of the GNU General Public License
17 1.1 christos along with this program; see the file COPYING. If not see
18 1.1 christos <http://www.gnu.org/licenses/>. */
19 1.1 christos
20 1.1 christos #include <ctf-impl.h>
21 1.1 christos #include <string.h>
22 1.1 christos #include "libiberty.h"
23 1.1 christos #include "hashtab.h"
24 1.1 christos
25 1.1 christos /* We have three hashtable implementations:
26 1.1 christos
27 1.1 christos - ctf_hash_* is an interface to a fixed-size hash from const char * ->
28 1.1 christos ctf_id_t with number of elements specified at creation time, that should
29 1.1 christos support addition of items but need not support removal.
30 1.1 christos
31 1.1 christos - ctf_dynhash_* is an interface to a dynamically-expanding hash with
32 1.1 christos unknown size that should support addition of large numbers of items, and
33 1.1 christos removal as well, and is used only at type-insertion time and during
34 1.1 christos linking.
35 1.1 christos
36 1.1 christos - ctf_dynset_* is an interface to a dynamically-expanding hash that contains
37 1.1 christos only keys: no values.
38 1.1 christos
39 1.1 christos These can be implemented by the same underlying hashmap if you wish. */
40 1.1 christos
41 1.1 christos /* The helem is used for general key/value mappings in both the ctf_hash and
42 1.1 christos ctf_dynhash: the owner may not have space allocated for it, and will be
43 1.1 christos garbage (not NULL!) in that case. */
44 1.1 christos
45 1.1 christos typedef struct ctf_helem
46 1.1 christos {
47 1.1 christos void *key; /* Either a pointer, or a coerced ctf_id_t. */
48 1.1 christos void *value; /* The value (possibly a coerced int). */
49 1.1 christos ctf_dynhash_t *owner; /* The hash that owns us. */
50 1.1 christos } ctf_helem_t;
51 1.1 christos
52 1.1 christos /* Equally, the key_free and value_free may not exist. */
53 1.1 christos
54 1.1 christos struct ctf_dynhash
55 1.1 christos {
56 1.1 christos struct htab *htab;
57 1.1 christos ctf_hash_free_fun key_free;
58 1.1 christos ctf_hash_free_fun value_free;
59 1.1 christos };
60 1.1 christos
61 1.1 christos /* Hash and eq functions for the dynhash and hash. */
62 1.1 christos
63 1.1 christos unsigned int
64 1.1 christos ctf_hash_integer (const void *ptr)
65 1.1 christos {
66 1.1 christos ctf_helem_t *hep = (ctf_helem_t *) ptr;
67 1.1 christos
68 1.1 christos return htab_hash_pointer (hep->key);
69 1.1 christos }
70 1.1 christos
71 1.1 christos int
72 1.1 christos ctf_hash_eq_integer (const void *a, const void *b)
73 1.1 christos {
74 1.1 christos ctf_helem_t *hep_a = (ctf_helem_t *) a;
75 1.1 christos ctf_helem_t *hep_b = (ctf_helem_t *) b;
76 1.1 christos
77 1.1 christos return htab_eq_pointer (hep_a->key, hep_b->key);
78 1.1 christos }
79 1.1 christos
80 1.1 christos unsigned int
81 1.1 christos ctf_hash_string (const void *ptr)
82 1.1 christos {
83 1.1 christos ctf_helem_t *hep = (ctf_helem_t *) ptr;
84 1.1 christos
85 1.1 christos return htab_hash_string (hep->key);
86 1.1 christos }
87 1.1 christos
88 1.1 christos int
89 1.1 christos ctf_hash_eq_string (const void *a, const void *b)
90 1.1 christos {
91 1.1 christos ctf_helem_t *hep_a = (ctf_helem_t *) a;
92 1.1 christos ctf_helem_t *hep_b = (ctf_helem_t *) b;
93 1.1 christos
94 1.1 christos return !strcmp((const char *) hep_a->key, (const char *) hep_b->key);
95 1.1 christos }
96 1.1 christos
97 1.1 christos /* Hash a type_key. */
98 1.1 christos unsigned int
99 1.1 christos ctf_hash_type_key (const void *ptr)
100 1.1 christos {
101 1.1 christos ctf_helem_t *hep = (ctf_helem_t *) ptr;
102 1.1 christos ctf_link_type_key_t *k = (ctf_link_type_key_t *) hep->key;
103 1.1 christos
104 1.1 christos return htab_hash_pointer (k->cltk_fp) + 59
105 1.1 christos * htab_hash_pointer ((void *) (uintptr_t) k->cltk_idx);
106 1.1 christos }
107 1.1 christos
108 1.1 christos int
109 1.1 christos ctf_hash_eq_type_key (const void *a, const void *b)
110 1.1 christos {
111 1.1 christos ctf_helem_t *hep_a = (ctf_helem_t *) a;
112 1.1 christos ctf_helem_t *hep_b = (ctf_helem_t *) b;
113 1.1 christos ctf_link_type_key_t *key_a = (ctf_link_type_key_t *) hep_a->key;
114 1.1 christos ctf_link_type_key_t *key_b = (ctf_link_type_key_t *) hep_b->key;
115 1.1 christos
116 1.1 christos return (key_a->cltk_fp == key_b->cltk_fp)
117 1.1 christos && (key_a->cltk_idx == key_b->cltk_idx);
118 1.1 christos }
119 1.1 christos
120 1.1 christos /* Hash a type_id_key. */
121 1.1 christos unsigned int
122 1.1 christos ctf_hash_type_id_key (const void *ptr)
123 1.1 christos {
124 1.1 christos ctf_helem_t *hep = (ctf_helem_t *) ptr;
125 1.1 christos ctf_type_id_key_t *k = (ctf_type_id_key_t *) hep->key;
126 1.1 christos
127 1.1 christos return htab_hash_pointer ((void *) (uintptr_t) k->ctii_input_num)
128 1.1 christos + 59 * htab_hash_pointer ((void *) (uintptr_t) k->ctii_type);
129 1.1 christos }
130 1.1 christos
131 1.1 christos int
132 1.1 christos ctf_hash_eq_type_id_key (const void *a, const void *b)
133 1.1 christos {
134 1.1 christos ctf_helem_t *hep_a = (ctf_helem_t *) a;
135 1.1 christos ctf_helem_t *hep_b = (ctf_helem_t *) b;
136 1.1 christos ctf_type_id_key_t *key_a = (ctf_type_id_key_t *) hep_a->key;
137 1.1 christos ctf_type_id_key_t *key_b = (ctf_type_id_key_t *) hep_b->key;
138 1.1 christos
139 1.1 christos return (key_a->ctii_input_num == key_b->ctii_input_num)
140 1.1 christos && (key_a->ctii_type == key_b->ctii_type);
141 1.1 christos }
142 1.1 christos
143 1.1 christos /* Hash and eq functions for the dynset. Most of these can just use the
144 1.1 christos underlying hashtab functions directly. */
145 1.1 christos
146 1.1 christos int
147 1.1 christos ctf_dynset_eq_string (const void *a, const void *b)
148 1.1 christos {
149 1.1 christos return !strcmp((const char *) a, (const char *) b);
150 1.1 christos }
151 1.1 christos
152 1.1 christos /* The dynhash, used for hashes whose size is not known at creation time. */
153 1.1 christos
154 1.1 christos /* Free a single ctf_helem with arbitrary key/value functions. */
155 1.1 christos
156 1.1 christos static void
157 1.1 christos ctf_dynhash_item_free (void *item)
158 1.1 christos {
159 1.1 christos ctf_helem_t *helem = item;
160 1.1 christos
161 1.1 christos if (helem->owner->key_free && helem->key)
162 1.1 christos helem->owner->key_free (helem->key);
163 1.1 christos if (helem->owner->value_free && helem->value)
164 1.1 christos helem->owner->value_free (helem->value);
165 1.1 christos free (helem);
166 1.1 christos }
167 1.1 christos
168 1.1 christos ctf_dynhash_t *
169 1.1 christos ctf_dynhash_create (ctf_hash_fun hash_fun, ctf_hash_eq_fun eq_fun,
170 1.1 christos ctf_hash_free_fun key_free, ctf_hash_free_fun value_free)
171 1.1 christos {
172 1.1 christos ctf_dynhash_t *dynhash;
173 1.1 christos htab_del del = ctf_dynhash_item_free;
174 1.1 christos
175 1.1 christos if (key_free || value_free)
176 1.1 christos dynhash = malloc (sizeof (ctf_dynhash_t));
177 1.1 christos else
178 1.1 christos dynhash = malloc (offsetof (ctf_dynhash_t, key_free));
179 1.1 christos if (!dynhash)
180 1.1 christos return NULL;
181 1.1 christos
182 1.1 christos if (key_free == NULL && value_free == NULL)
183 1.1 christos del = free;
184 1.1 christos
185 1.1 christos /* 7 is arbitrary and untested for now. */
186 1.1 christos if ((dynhash->htab = htab_create_alloc (7, (htab_hash) hash_fun, eq_fun,
187 1.1 christos del, xcalloc, free)) == NULL)
188 1.1 christos {
189 1.1 christos free (dynhash);
190 1.1 christos return NULL;
191 1.1 christos }
192 1.1 christos
193 1.1 christos if (key_free || value_free)
194 1.1 christos {
195 1.1 christos dynhash->key_free = key_free;
196 1.1 christos dynhash->value_free = value_free;
197 1.1 christos }
198 1.1 christos
199 1.1 christos return dynhash;
200 1.1 christos }
201 1.1 christos
202 1.1 christos static ctf_helem_t **
203 1.1 christos ctf_hashtab_lookup (struct htab *htab, const void *key, enum insert_option insert)
204 1.1 christos {
205 1.1 christos ctf_helem_t tmp = { .key = (void *) key };
206 1.1 christos return (ctf_helem_t **) htab_find_slot (htab, &tmp, insert);
207 1.1 christos }
208 1.1 christos
209 1.1 christos static ctf_helem_t *
210 1.1 christos ctf_hashtab_insert (struct htab *htab, void *key, void *value,
211 1.1 christos ctf_hash_free_fun key_free,
212 1.1 christos ctf_hash_free_fun value_free)
213 1.1 christos {
214 1.1 christos ctf_helem_t **slot;
215 1.1 christos
216 1.1 christos slot = ctf_hashtab_lookup (htab, key, INSERT);
217 1.1 christos
218 1.1 christos if (!slot)
219 1.1 christos {
220 1.1 christos errno = ENOMEM;
221 1.1 christos return NULL;
222 1.1 christos }
223 1.1 christos
224 1.1 christos if (!*slot)
225 1.1 christos {
226 1.1 christos /* Only spend space on the owner if we're going to use it: if there is a
227 1.1 christos key or value freeing function. */
228 1.1 christos if (key_free || value_free)
229 1.1 christos *slot = malloc (sizeof (ctf_helem_t));
230 1.1 christos else
231 1.1 christos *slot = malloc (offsetof (ctf_helem_t, owner));
232 1.1 christos if (!*slot)
233 1.1 christos return NULL;
234 1.1 christos (*slot)->key = key;
235 1.1 christos }
236 1.1 christos else
237 1.1 christos {
238 1.1 christos if (key_free)
239 1.1 christos key_free (key);
240 1.1 christos if (value_free)
241 1.1 christos value_free ((*slot)->value);
242 1.1 christos }
243 1.1 christos (*slot)->value = value;
244 1.1 christos return *slot;
245 1.1 christos }
246 1.1 christos
247 1.1 christos int
248 1.1 christos ctf_dynhash_insert (ctf_dynhash_t *hp, void *key, void *value)
249 1.1 christos {
250 1.1 christos ctf_helem_t *slot;
251 1.1 christos ctf_hash_free_fun key_free = NULL, value_free = NULL;
252 1.1 christos
253 1.1 christos if (hp->htab->del_f == ctf_dynhash_item_free)
254 1.1 christos {
255 1.1 christos key_free = hp->key_free;
256 1.1 christos value_free = hp->value_free;
257 1.1 christos }
258 1.1 christos slot = ctf_hashtab_insert (hp->htab, key, value,
259 1.1 christos key_free, value_free);
260 1.1 christos
261 1.1 christos if (!slot)
262 1.1 christos return errno;
263 1.1 christos
264 1.1 christos /* Keep track of the owner, so that the del function can get at the key_free
265 1.1 christos and value_free functions. Only do this if one of those functions is set:
266 1.1 christos if not, the owner is not even present in the helem. */
267 1.1 christos
268 1.1 christos if (key_free || value_free)
269 1.1 christos slot->owner = hp;
270 1.1 christos
271 1.1 christos return 0;
272 1.1 christos }
273 1.1 christos
274 1.1 christos void
275 1.1 christos ctf_dynhash_remove (ctf_dynhash_t *hp, const void *key)
276 1.1 christos {
277 1.1 christos ctf_helem_t hep = { (void *) key, NULL, NULL };
278 1.1 christos htab_remove_elt (hp->htab, &hep);
279 1.1 christos }
280 1.1 christos
281 1.1 christos void
282 1.1 christos ctf_dynhash_empty (ctf_dynhash_t *hp)
283 1.1 christos {
284 1.1 christos htab_empty (hp->htab);
285 1.1 christos }
286 1.1 christos
287 1.1 christos size_t
288 1.1 christos ctf_dynhash_elements (ctf_dynhash_t *hp)
289 1.1 christos {
290 1.1 christos return htab_elements (hp->htab);
291 1.1 christos }
292 1.1 christos
293 1.1 christos void *
294 1.1 christos ctf_dynhash_lookup (ctf_dynhash_t *hp, const void *key)
295 1.1 christos {
296 1.1 christos ctf_helem_t **slot;
297 1.1 christos
298 1.1 christos slot = ctf_hashtab_lookup (hp->htab, key, NO_INSERT);
299 1.1 christos
300 1.1 christos if (slot)
301 1.1 christos return (*slot)->value;
302 1.1 christos
303 1.1 christos return NULL;
304 1.1 christos }
305 1.1 christos
306 1.1 christos /* TRUE/FALSE return. */
307 1.1 christos int
308 1.1 christos ctf_dynhash_lookup_kv (ctf_dynhash_t *hp, const void *key,
309 1.1 christos const void **orig_key, void **value)
310 1.1 christos {
311 1.1 christos ctf_helem_t **slot;
312 1.1 christos
313 1.1 christos slot = ctf_hashtab_lookup (hp->htab, key, NO_INSERT);
314 1.1 christos
315 1.1 christos if (slot)
316 1.1 christos {
317 1.1 christos if (orig_key)
318 1.1 christos *orig_key = (*slot)->key;
319 1.1 christos if (value)
320 1.1 christos *value = (*slot)->value;
321 1.1 christos return 1;
322 1.1 christos }
323 1.1 christos return 0;
324 1.1 christos }
325 1.1 christos
326 1.1 christos typedef struct ctf_traverse_cb_arg
327 1.1 christos {
328 1.1 christos ctf_hash_iter_f fun;
329 1.1 christos void *arg;
330 1.1 christos } ctf_traverse_cb_arg_t;
331 1.1 christos
332 1.1 christos static int
333 1.1 christos ctf_hashtab_traverse (void **slot, void *arg_)
334 1.1 christos {
335 1.1 christos ctf_helem_t *helem = *((ctf_helem_t **) slot);
336 1.1 christos ctf_traverse_cb_arg_t *arg = (ctf_traverse_cb_arg_t *) arg_;
337 1.1 christos
338 1.1 christos arg->fun (helem->key, helem->value, arg->arg);
339 1.1 christos return 1;
340 1.1 christos }
341 1.1 christos
342 1.1 christos void
343 1.1 christos ctf_dynhash_iter (ctf_dynhash_t *hp, ctf_hash_iter_f fun, void *arg_)
344 1.1 christos {
345 1.1 christos ctf_traverse_cb_arg_t arg = { fun, arg_ };
346 1.1 christos htab_traverse (hp->htab, ctf_hashtab_traverse, &arg);
347 1.1 christos }
348 1.1 christos
349 1.1 christos typedef struct ctf_traverse_find_cb_arg
350 1.1 christos {
351 1.1 christos ctf_hash_iter_find_f fun;
352 1.1 christos void *arg;
353 1.1 christos void *found_key;
354 1.1 christos } ctf_traverse_find_cb_arg_t;
355 1.1 christos
356 1.1 christos static int
357 1.1 christos ctf_hashtab_traverse_find (void **slot, void *arg_)
358 1.1 christos {
359 1.1 christos ctf_helem_t *helem = *((ctf_helem_t **) slot);
360 1.1 christos ctf_traverse_find_cb_arg_t *arg = (ctf_traverse_find_cb_arg_t *) arg_;
361 1.1 christos
362 1.1 christos if (arg->fun (helem->key, helem->value, arg->arg))
363 1.1 christos {
364 1.1 christos arg->found_key = helem->key;
365 1.1 christos return 0;
366 1.1 christos }
367 1.1 christos return 1;
368 1.1 christos }
369 1.1 christos
370 1.1 christos void *
371 1.1 christos ctf_dynhash_iter_find (ctf_dynhash_t *hp, ctf_hash_iter_find_f fun, void *arg_)
372 1.1 christos {
373 1.1 christos ctf_traverse_find_cb_arg_t arg = { fun, arg_, NULL };
374 1.1 christos htab_traverse (hp->htab, ctf_hashtab_traverse_find, &arg);
375 1.1 christos return arg.found_key;
376 1.1 christos }
377 1.1 christos
378 1.1 christos typedef struct ctf_traverse_remove_cb_arg
379 1.1 christos {
380 1.1 christos struct htab *htab;
381 1.1 christos ctf_hash_iter_remove_f fun;
382 1.1 christos void *arg;
383 1.1 christos } ctf_traverse_remove_cb_arg_t;
384 1.1 christos
385 1.1 christos static int
386 1.1 christos ctf_hashtab_traverse_remove (void **slot, void *arg_)
387 1.1 christos {
388 1.1 christos ctf_helem_t *helem = *((ctf_helem_t **) slot);
389 1.1 christos ctf_traverse_remove_cb_arg_t *arg = (ctf_traverse_remove_cb_arg_t *) arg_;
390 1.1 christos
391 1.1 christos if (arg->fun (helem->key, helem->value, arg->arg))
392 1.1 christos htab_clear_slot (arg->htab, slot);
393 1.1 christos return 1;
394 1.1 christos }
395 1.1 christos
396 1.1 christos void
397 1.1 christos ctf_dynhash_iter_remove (ctf_dynhash_t *hp, ctf_hash_iter_remove_f fun,
398 1.1 christos void *arg_)
399 1.1 christos {
400 1.1 christos ctf_traverse_remove_cb_arg_t arg = { hp->htab, fun, arg_ };
401 1.1 christos htab_traverse (hp->htab, ctf_hashtab_traverse_remove, &arg);
402 1.1 christos }
403 1.1 christos
404 1.1 christos /* Traverse a dynhash in arbitrary order, in _next iterator form.
405 1.1 christos
406 1.1 christos Mutating the dynhash while iterating is not supported (just as it isn't for
407 1.1 christos htab_traverse).
408 1.1 christos
409 1.1 christos Note: unusually, this returns zero on success and a *positive* value on
410 1.1 christos error, because it does not take an fp, taking an error pointer would be
411 1.1 christos incredibly clunky, and nearly all error-handling ends up stuffing the result
412 1.1 christos of this into some sort of errno or ctf_errno, which is invariably
413 1.1 christos positive. So doing this simplifies essentially all callers. */
414 1.1 christos int
415 1.1 christos ctf_dynhash_next (ctf_dynhash_t *h, ctf_next_t **it, void **key, void **value)
416 1.1 christos {
417 1.1 christos ctf_next_t *i = *it;
418 1.1 christos ctf_helem_t *slot;
419 1.1 christos
420 1.1 christos if (!i)
421 1.1 christos {
422 1.1 christos size_t size = htab_size (h->htab);
423 1.1 christos
424 1.1 christos /* If the table has too many entries to fit in an ssize_t, just give up.
425 1.1 christos This might be spurious, but if any type-related hashtable has ever been
426 1.1 christos nearly as large as that then something very odd is going on. */
427 1.1 christos if (((ssize_t) size) < 0)
428 1.1 christos return EDOM;
429 1.1 christos
430 1.1 christos if ((i = ctf_next_create ()) == NULL)
431 1.1 christos return ENOMEM;
432 1.1 christos
433 1.1 christos i->u.ctn_hash_slot = h->htab->entries;
434 1.1 christos i->cu.ctn_h = h;
435 1.1 christos i->ctn_n = 0;
436 1.1 christos i->ctn_size = (ssize_t) size;
437 1.1 christos i->ctn_iter_fun = (void (*) (void)) ctf_dynhash_next;
438 1.1 christos *it = i;
439 1.1 christos }
440 1.1 christos
441 1.1 christos if ((void (*) (void)) ctf_dynhash_next != i->ctn_iter_fun)
442 1.1 christos return ECTF_NEXT_WRONGFUN;
443 1.1 christos
444 1.1 christos if (h != i->cu.ctn_h)
445 1.1 christos return ECTF_NEXT_WRONGFP;
446 1.1 christos
447 1.1 christos if ((ssize_t) i->ctn_n == i->ctn_size)
448 1.1 christos goto hash_end;
449 1.1 christos
450 1.1 christos while ((ssize_t) i->ctn_n < i->ctn_size
451 1.1 christos && (*i->u.ctn_hash_slot == HTAB_EMPTY_ENTRY
452 1.1 christos || *i->u.ctn_hash_slot == HTAB_DELETED_ENTRY))
453 1.1 christos {
454 1.1 christos i->u.ctn_hash_slot++;
455 1.1 christos i->ctn_n++;
456 1.1 christos }
457 1.1 christos
458 1.1 christos if ((ssize_t) i->ctn_n == i->ctn_size)
459 1.1 christos goto hash_end;
460 1.1 christos
461 1.1 christos slot = *i->u.ctn_hash_slot;
462 1.1 christos
463 1.1 christos if (key)
464 1.1 christos *key = slot->key;
465 1.1 christos if (value)
466 1.1 christos *value = slot->value;
467 1.1 christos
468 1.1 christos i->u.ctn_hash_slot++;
469 1.1 christos i->ctn_n++;
470 1.1 christos
471 1.1 christos return 0;
472 1.1 christos
473 1.1 christos hash_end:
474 1.1 christos ctf_next_destroy (i);
475 1.1 christos *it = NULL;
476 1.1 christos return ECTF_NEXT_END;
477 1.1 christos }
478 1.1 christos
479 1.1 christos /* Traverse a sorted dynhash, in _next iterator form.
480 1.1 christos
481 1.1 christos See ctf_dynhash_next for notes on error returns, etc.
482 1.1 christos
483 1.1 christos Sort keys before iterating over them using the SORT_FUN and SORT_ARG.
484 1.1 christos
485 1.1 christos If SORT_FUN is null, thunks to ctf_dynhash_next. */
486 1.1 christos int
487 1.1 christos ctf_dynhash_next_sorted (ctf_dynhash_t *h, ctf_next_t **it, void **key,
488 1.1 christos void **value, ctf_hash_sort_f sort_fun, void *sort_arg)
489 1.1 christos {
490 1.1 christos ctf_next_t *i = *it;
491 1.1 christos
492 1.1 christos if (sort_fun == NULL)
493 1.1 christos return ctf_dynhash_next (h, it, key, value);
494 1.1 christos
495 1.1 christos if (!i)
496 1.1 christos {
497 1.1 christos size_t els = ctf_dynhash_elements (h);
498 1.1 christos ctf_next_t *accum_i = NULL;
499 1.1 christos void *key, *value;
500 1.1 christos int err;
501 1.1 christos ctf_next_hkv_t *walk;
502 1.1 christos
503 1.1 christos if (((ssize_t) els) < 0)
504 1.1 christos return EDOM;
505 1.1 christos
506 1.1 christos if ((i = ctf_next_create ()) == NULL)
507 1.1 christos return ENOMEM;
508 1.1 christos
509 1.1 christos if ((i->u.ctn_sorted_hkv = calloc (els, sizeof (ctf_next_hkv_t))) == NULL)
510 1.1 christos {
511 1.1 christos ctf_next_destroy (i);
512 1.1 christos return ENOMEM;
513 1.1 christos }
514 1.1 christos walk = i->u.ctn_sorted_hkv;
515 1.1 christos
516 1.1 christos i->cu.ctn_h = h;
517 1.1 christos
518 1.1 christos while ((err = ctf_dynhash_next (h, &accum_i, &key, &value)) == 0)
519 1.1 christos {
520 1.1 christos walk->hkv_key = key;
521 1.1 christos walk->hkv_value = value;
522 1.1 christos walk++;
523 1.1 christos }
524 1.1 christos if (err != ECTF_NEXT_END)
525 1.1 christos {
526 1.1 christos ctf_next_destroy (i);
527 1.1 christos return err;
528 1.1 christos }
529 1.1 christos
530 1.1 christos if (sort_fun)
531 1.1 christos ctf_qsort_r (i->u.ctn_sorted_hkv, els, sizeof (ctf_next_hkv_t),
532 1.1 christos (int (*) (const void *, const void *, void *)) sort_fun,
533 1.1 christos sort_arg);
534 1.1 christos i->ctn_n = 0;
535 1.1 christos i->ctn_size = (ssize_t) els;
536 1.1 christos i->ctn_iter_fun = (void (*) (void)) ctf_dynhash_next_sorted;
537 1.1 christos *it = i;
538 1.1 christos }
539 1.1 christos
540 1.1 christos if ((void (*) (void)) ctf_dynhash_next_sorted != i->ctn_iter_fun)
541 1.1 christos return ECTF_NEXT_WRONGFUN;
542 1.1 christos
543 1.1 christos if (h != i->cu.ctn_h)
544 1.1 christos return ECTF_NEXT_WRONGFP;
545 1.1 christos
546 1.1 christos if ((ssize_t) i->ctn_n == i->ctn_size)
547 1.1 christos {
548 1.1 christos ctf_next_destroy (i);
549 1.1 christos *it = NULL;
550 1.1 christos return ECTF_NEXT_END;
551 1.1 christos }
552 1.1 christos
553 1.1 christos if (key)
554 1.1 christos *key = i->u.ctn_sorted_hkv[i->ctn_n].hkv_key;
555 1.1 christos if (value)
556 1.1 christos *value = i->u.ctn_sorted_hkv[i->ctn_n].hkv_value;
557 1.1 christos i->ctn_n++;
558 1.1 christos return 0;
559 1.1 christos }
560 1.1 christos
561 1.1 christos void
562 1.1 christos ctf_dynhash_destroy (ctf_dynhash_t *hp)
563 1.1 christos {
564 1.1 christos if (hp != NULL)
565 1.1 christos htab_delete (hp->htab);
566 1.1 christos free (hp);
567 1.1 christos }
568 1.1 christos
569 1.1 christos /* The dynset, used for sets of keys with no value. The implementation of this
570 1.1 christos can be much simpler, because without a value the slot can simply be the
571 1.1 christos stored key, which means we don't need to store the freeing functions and the
572 1.1 christos dynset itself is just a htab. */
573 1.1 christos
574 1.1 christos ctf_dynset_t *
575 1.1 christos ctf_dynset_create (htab_hash hash_fun, htab_eq eq_fun,
576 1.1 christos ctf_hash_free_fun key_free)
577 1.1 christos {
578 1.1 christos /* 7 is arbitrary and untested for now. */
579 1.1 christos return (ctf_dynset_t *) htab_create_alloc (7, (htab_hash) hash_fun, eq_fun,
580 1.1 christos key_free, xcalloc, free);
581 1.1 christos }
582 1.1 christos
583 1.1 christos /* The dynset has one complexity: the underlying implementation reserves two
584 1.1 christos values for internal hash table implementation details (empty versus deleted
585 1.1 christos entries). These values are otherwise very useful for pointers cast to ints,
586 1.1 christos so transform the ctf_dynset_inserted value to allow for it. (This
587 1.1 christos introduces an ambiguity in that one can no longer store these two values in
588 1.1 christos the dynset, but if we pick high enough values this is very unlikely to be a
589 1.1 christos problem.)
590 1.1 christos
591 1.1 christos We leak this implementation detail to the freeing functions on the grounds
592 1.1 christos that any use of these functions is overwhelmingly likely to be in sets using
593 1.1 christos real pointers, which will be unaffected. */
594 1.1 christos
595 1.1 christos #define DYNSET_EMPTY_ENTRY_REPLACEMENT ((void *) (uintptr_t) -64)
596 1.1 christos #define DYNSET_DELETED_ENTRY_REPLACEMENT ((void *) (uintptr_t) -63)
597 1.1 christos
598 1.1 christos static void *
599 1.1 christos key_to_internal (const void *key)
600 1.1 christos {
601 1.1 christos if (key == HTAB_EMPTY_ENTRY)
602 1.1 christos return DYNSET_EMPTY_ENTRY_REPLACEMENT;
603 1.1 christos else if (key == HTAB_DELETED_ENTRY)
604 1.1 christos return DYNSET_DELETED_ENTRY_REPLACEMENT;
605 1.1 christos
606 1.1 christos return (void *) key;
607 1.1 christos }
608 1.1 christos
609 1.1 christos static void *
610 1.1 christos internal_to_key (const void *internal)
611 1.1 christos {
612 1.1 christos if (internal == DYNSET_EMPTY_ENTRY_REPLACEMENT)
613 1.1 christos return HTAB_EMPTY_ENTRY;
614 1.1 christos else if (internal == DYNSET_DELETED_ENTRY_REPLACEMENT)
615 1.1 christos return HTAB_DELETED_ENTRY;
616 1.1 christos return (void *) internal;
617 1.1 christos }
618 1.1 christos
619 1.1 christos int
620 1.1 christos ctf_dynset_insert (ctf_dynset_t *hp, void *key)
621 1.1 christos {
622 1.1 christos struct htab *htab = (struct htab *) hp;
623 1.1 christos void **slot;
624 1.1 christos
625 1.1 christos slot = htab_find_slot (htab, key, INSERT);
626 1.1 christos
627 1.1 christos if (!slot)
628 1.1 christos {
629 1.1 christos errno = ENOMEM;
630 1.1 christos return -errno;
631 1.1 christos }
632 1.1 christos
633 1.1 christos if (*slot)
634 1.1 christos {
635 1.1 christos if (htab->del_f)
636 1.1 christos (*htab->del_f) (*slot);
637 1.1 christos }
638 1.1 christos
639 1.1 christos *slot = key_to_internal (key);
640 1.1 christos
641 1.1 christos return 0;
642 1.1 christos }
643 1.1 christos
644 1.1 christos void
645 1.1 christos ctf_dynset_remove (ctf_dynset_t *hp, const void *key)
646 1.1 christos {
647 1.1 christos htab_remove_elt ((struct htab *) hp, key_to_internal (key));
648 1.1 christos }
649 1.1 christos
650 1.1 christos void
651 1.1 christos ctf_dynset_destroy (ctf_dynset_t *hp)
652 1.1 christos {
653 1.1 christos if (hp != NULL)
654 1.1 christos htab_delete ((struct htab *) hp);
655 1.1 christos }
656 1.1 christos
657 1.1 christos void *
658 1.1 christos ctf_dynset_lookup (ctf_dynset_t *hp, const void *key)
659 1.1 christos {
660 1.1 christos void **slot = htab_find_slot ((struct htab *) hp,
661 1.1 christos key_to_internal (key), NO_INSERT);
662 1.1 christos
663 1.1 christos if (slot)
664 1.1 christos return internal_to_key (*slot);
665 1.1 christos return NULL;
666 1.1 christos }
667 1.1 christos
668 1.1 christos /* TRUE/FALSE return. */
669 1.1 christos int
670 1.1 christos ctf_dynset_exists (ctf_dynset_t *hp, const void *key, const void **orig_key)
671 1.1 christos {
672 1.1 christos void **slot = htab_find_slot ((struct htab *) hp,
673 1.1 christos key_to_internal (key), NO_INSERT);
674 1.1 christos
675 1.1 christos if (orig_key && slot)
676 1.1 christos *orig_key = internal_to_key (*slot);
677 1.1 christos return (slot != NULL);
678 1.1 christos }
679 1.1 christos
680 1.1 christos /* Look up a completely random value from the set, if any exist.
681 1.1 christos Keys with value zero cannot be distinguished from a nonexistent key. */
682 1.1 christos void *
683 1.1 christos ctf_dynset_lookup_any (ctf_dynset_t *hp)
684 1.1 christos {
685 1.1 christos struct htab *htab = (struct htab *) hp;
686 1.1 christos void **slot = htab->entries;
687 1.1 christos void **limit = slot + htab_size (htab);
688 1.1 christos
689 1.1 christos while (slot < limit
690 1.1 christos && (*slot == HTAB_EMPTY_ENTRY || *slot == HTAB_DELETED_ENTRY))
691 1.1 christos slot++;
692 1.1 christos
693 1.1 christos if (slot < limit)
694 1.1 christos return internal_to_key (*slot);
695 1.1 christos return NULL;
696 1.1 christos }
697 1.1 christos
698 1.1 christos /* Traverse a dynset in arbitrary order, in _next iterator form.
699 1.1 christos
700 1.1 christos Otherwise, just like ctf_dynhash_next. */
701 1.1 christos int
702 1.1 christos ctf_dynset_next (ctf_dynset_t *hp, ctf_next_t **it, void **key)
703 1.1 christos {
704 1.1 christos struct htab *htab = (struct htab *) hp;
705 1.1 christos ctf_next_t *i = *it;
706 1.1 christos void *slot;
707 1.1 christos
708 1.1 christos if (!i)
709 1.1 christos {
710 1.1 christos size_t size = htab_size (htab);
711 1.1 christos
712 1.1 christos /* If the table has too many entries to fit in an ssize_t, just give up.
713 1.1 christos This might be spurious, but if any type-related hashtable has ever been
714 1.1 christos nearly as large as that then somthing very odd is going on. */
715 1.1 christos
716 1.1 christos if (((ssize_t) size) < 0)
717 1.1 christos return EDOM;
718 1.1 christos
719 1.1 christos if ((i = ctf_next_create ()) == NULL)
720 1.1 christos return ENOMEM;
721 1.1 christos
722 1.1 christos i->u.ctn_hash_slot = htab->entries;
723 1.1 christos i->cu.ctn_s = hp;
724 1.1 christos i->ctn_n = 0;
725 1.1 christos i->ctn_size = (ssize_t) size;
726 1.1 christos i->ctn_iter_fun = (void (*) (void)) ctf_dynset_next;
727 1.1 christos *it = i;
728 1.1 christos }
729 1.1 christos
730 1.1 christos if ((void (*) (void)) ctf_dynset_next != i->ctn_iter_fun)
731 1.1 christos return ECTF_NEXT_WRONGFUN;
732 1.1 christos
733 1.1 christos if (hp != i->cu.ctn_s)
734 1.1 christos return ECTF_NEXT_WRONGFP;
735 1.1 christos
736 1.1 christos if ((ssize_t) i->ctn_n == i->ctn_size)
737 1.1 christos goto set_end;
738 1.1 christos
739 1.1 christos while ((ssize_t) i->ctn_n < i->ctn_size
740 1.1 christos && (*i->u.ctn_hash_slot == HTAB_EMPTY_ENTRY
741 1.1 christos || *i->u.ctn_hash_slot == HTAB_DELETED_ENTRY))
742 1.1 christos {
743 1.1 christos i->u.ctn_hash_slot++;
744 1.1 christos i->ctn_n++;
745 1.1 christos }
746 1.1 christos
747 1.1 christos if ((ssize_t) i->ctn_n == i->ctn_size)
748 1.1 christos goto set_end;
749 1.1 christos
750 1.1 christos slot = *i->u.ctn_hash_slot;
751 1.1 christos
752 1.1 christos if (key)
753 1.1 christos *key = internal_to_key (slot);
754 1.1 christos
755 1.1 christos i->u.ctn_hash_slot++;
756 1.1 christos i->ctn_n++;
757 1.1 christos
758 1.1 christos return 0;
759 1.1 christos
760 1.1 christos set_end:
761 1.1 christos ctf_next_destroy (i);
762 1.1 christos *it = NULL;
763 1.1 christos return ECTF_NEXT_END;
764 1.1 christos }
765 1.1 christos
766 1.1 christos /* ctf_hash, used for fixed-size maps from const char * -> ctf_id_t without
767 1.1 christos removal. This is a straight cast of a hashtab. */
768 1.1 christos
769 1.1 christos ctf_hash_t *
770 1.1 christos ctf_hash_create (unsigned long nelems, ctf_hash_fun hash_fun,
771 1.1 christos ctf_hash_eq_fun eq_fun)
772 1.1 christos {
773 1.1 christos return (ctf_hash_t *) htab_create_alloc (nelems, (htab_hash) hash_fun,
774 1.1 christos eq_fun, free, xcalloc, free);
775 1.1 christos }
776 1.1 christos
777 1.1 christos uint32_t
778 1.1 christos ctf_hash_size (const ctf_hash_t *hp)
779 1.1 christos {
780 1.1 christos return htab_elements ((struct htab *) hp);
781 1.1 christos }
782 1.1 christos
783 1.1 christos int
784 1.1 christos ctf_hash_insert_type (ctf_hash_t *hp, ctf_file_t *fp, uint32_t type,
785 1.1 christos uint32_t name)
786 1.1 christos {
787 1.1 christos const char *str = ctf_strraw (fp, name);
788 1.1 christos
789 1.1 christos if (type == 0)
790 1.1 christos return EINVAL;
791 1.1 christos
792 1.1 christos if (str == NULL
793 1.1 christos && CTF_NAME_STID (name) == CTF_STRTAB_1
794 1.1 christos && fp->ctf_syn_ext_strtab == NULL
795 1.1 christos && fp->ctf_str[CTF_NAME_STID (name)].cts_strs == NULL)
796 1.1 christos return ECTF_STRTAB;
797 1.1 christos
798 1.1 christos if (str == NULL)
799 1.1 christos return ECTF_BADNAME;
800 1.1 christos
801 1.1 christos if (str[0] == '\0')
802 1.1 christos return 0; /* Just ignore empty strings on behalf of caller. */
803 1.1 christos
804 1.1 christos if (ctf_hashtab_insert ((struct htab *) hp, (char *) str,
805 1.1 christos (void *) (ptrdiff_t) type, NULL, NULL) != NULL)
806 1.1 christos return 0;
807 1.1 christos return errno;
808 1.1 christos }
809 1.1 christos
810 1.1 christos /* if the key is already in the hash, override the previous definition with
811 1.1 christos this new official definition. If the key is not present, then call
812 1.1 christos ctf_hash_insert_type and hash it in. */
813 1.1 christos int
814 1.1 christos ctf_hash_define_type (ctf_hash_t *hp, ctf_file_t *fp, uint32_t type,
815 1.1 christos uint32_t name)
816 1.1 christos {
817 1.1 christos /* This matches the semantics of ctf_hash_insert_type in this
818 1.1 christos implementation anyway. */
819 1.1 christos
820 1.1 christos return ctf_hash_insert_type (hp, fp, type, name);
821 1.1 christos }
822 1.1 christos
823 1.1 christos ctf_id_t
824 1.1 christos ctf_hash_lookup_type (ctf_hash_t *hp, ctf_file_t *fp __attribute__ ((__unused__)),
825 1.1 christos const char *key)
826 1.1 christos {
827 1.1 christos ctf_helem_t **slot;
828 1.1 christos
829 1.1 christos slot = ctf_hashtab_lookup ((struct htab *) hp, key, NO_INSERT);
830 1.1 christos
831 1.1 christos if (slot)
832 1.1 christos return (ctf_id_t) (uintptr_t) ((*slot)->value);
833 1.1 christos
834 1.1 christos return 0;
835 1.1 christos }
836 1.1 christos
837 1.1 christos void
838 1.1 christos ctf_hash_destroy (ctf_hash_t *hp)
839 1.1 christos {
840 1.1 christos if (hp != NULL)
841 1.1 christos htab_delete ((struct htab *) hp);
842 1.1 christos }
843