xref: /netbsd-src/external/gpl3/gdb.old/dist/libctf/ctf-hash.c (revision 6881a4007f077b54e5f51159c52b9b25f57deb0d)
17d62b00eSchristos /* Interface to hashtable implementations.
2*6881a400Schristos    Copyright (C) 2006-2022 Free Software Foundation, Inc.
37d62b00eSchristos 
47d62b00eSchristos    This file is part of libctf.
57d62b00eSchristos 
67d62b00eSchristos    libctf is free software; you can redistribute it and/or modify it under
77d62b00eSchristos    the terms of the GNU General Public License as published by the Free
87d62b00eSchristos    Software Foundation; either version 3, or (at your option) any later
97d62b00eSchristos    version.
107d62b00eSchristos 
117d62b00eSchristos    This program is distributed in the hope that it will be useful, but
127d62b00eSchristos    WITHOUT ANY WARRANTY; without even the implied warranty of
137d62b00eSchristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
147d62b00eSchristos    See the GNU General Public License for more details.
157d62b00eSchristos 
167d62b00eSchristos    You should have received a copy of the GNU General Public License
177d62b00eSchristos    along with this program; see the file COPYING.  If not see
187d62b00eSchristos    <http://www.gnu.org/licenses/>.  */
197d62b00eSchristos 
207d62b00eSchristos #include <ctf-impl.h>
217d62b00eSchristos #include <string.h>
227d62b00eSchristos #include "libiberty.h"
237d62b00eSchristos #include "hashtab.h"
247d62b00eSchristos 
257d62b00eSchristos /* We have three hashtable implementations:
267d62b00eSchristos 
277d62b00eSchristos    - ctf_hash_* is an interface to a fixed-size hash from const char * ->
287d62b00eSchristos      ctf_id_t with number of elements specified at creation time, that should
297d62b00eSchristos      support addition of items but need not support removal.
307d62b00eSchristos 
317d62b00eSchristos    - ctf_dynhash_* is an interface to a dynamically-expanding hash with
327d62b00eSchristos      unknown size that should support addition of large numbers of items, and
337d62b00eSchristos      removal as well, and is used only at type-insertion time and during
347d62b00eSchristos      linking.
357d62b00eSchristos 
367d62b00eSchristos    - ctf_dynset_* is an interface to a dynamically-expanding hash that contains
377d62b00eSchristos      only keys: no values.
387d62b00eSchristos 
397d62b00eSchristos    These can be implemented by the same underlying hashmap if you wish.  */
407d62b00eSchristos 
417d62b00eSchristos /* The helem is used for general key/value mappings in both the ctf_hash and
427d62b00eSchristos    ctf_dynhash: the owner may not have space allocated for it, and will be
437d62b00eSchristos    garbage (not NULL!) in that case.  */
447d62b00eSchristos 
457d62b00eSchristos typedef struct ctf_helem
467d62b00eSchristos {
477d62b00eSchristos   void *key;			 /* Either a pointer, or a coerced ctf_id_t.  */
487d62b00eSchristos   void *value;			 /* The value (possibly a coerced int).  */
497d62b00eSchristos   ctf_dynhash_t *owner;          /* The hash that owns us.  */
507d62b00eSchristos } ctf_helem_t;
517d62b00eSchristos 
527d62b00eSchristos /* Equally, the key_free and value_free may not exist.  */
537d62b00eSchristos 
547d62b00eSchristos struct ctf_dynhash
557d62b00eSchristos {
567d62b00eSchristos   struct htab *htab;
577d62b00eSchristos   ctf_hash_free_fun key_free;
587d62b00eSchristos   ctf_hash_free_fun value_free;
597d62b00eSchristos };
607d62b00eSchristos 
617d62b00eSchristos /* Hash and eq functions for the dynhash and hash. */
627d62b00eSchristos 
637d62b00eSchristos unsigned int
647d62b00eSchristos ctf_hash_integer (const void *ptr)
657d62b00eSchristos {
667d62b00eSchristos   ctf_helem_t *hep = (ctf_helem_t *) ptr;
677d62b00eSchristos 
687d62b00eSchristos   return htab_hash_pointer (hep->key);
697d62b00eSchristos }
707d62b00eSchristos 
717d62b00eSchristos int
727d62b00eSchristos ctf_hash_eq_integer (const void *a, const void *b)
737d62b00eSchristos {
747d62b00eSchristos   ctf_helem_t *hep_a = (ctf_helem_t *) a;
757d62b00eSchristos   ctf_helem_t *hep_b = (ctf_helem_t *) b;
767d62b00eSchristos 
777d62b00eSchristos   return htab_eq_pointer (hep_a->key, hep_b->key);
787d62b00eSchristos }
797d62b00eSchristos 
807d62b00eSchristos unsigned int
817d62b00eSchristos ctf_hash_string (const void *ptr)
827d62b00eSchristos {
837d62b00eSchristos   ctf_helem_t *hep = (ctf_helem_t *) ptr;
847d62b00eSchristos 
857d62b00eSchristos   return htab_hash_string (hep->key);
867d62b00eSchristos }
877d62b00eSchristos 
887d62b00eSchristos int
897d62b00eSchristos ctf_hash_eq_string (const void *a, const void *b)
907d62b00eSchristos {
917d62b00eSchristos   ctf_helem_t *hep_a = (ctf_helem_t *) a;
927d62b00eSchristos   ctf_helem_t *hep_b = (ctf_helem_t *) b;
937d62b00eSchristos 
947d62b00eSchristos   return !strcmp((const char *) hep_a->key, (const char *) hep_b->key);
957d62b00eSchristos }
967d62b00eSchristos 
977d62b00eSchristos /* Hash a type_key.  */
987d62b00eSchristos unsigned int
997d62b00eSchristos ctf_hash_type_key (const void *ptr)
1007d62b00eSchristos {
1017d62b00eSchristos   ctf_helem_t *hep = (ctf_helem_t *) ptr;
1027d62b00eSchristos   ctf_link_type_key_t *k = (ctf_link_type_key_t *) hep->key;
1037d62b00eSchristos 
1047d62b00eSchristos   return htab_hash_pointer (k->cltk_fp) + 59
1057d62b00eSchristos     * htab_hash_pointer ((void *) (uintptr_t) k->cltk_idx);
1067d62b00eSchristos }
1077d62b00eSchristos 
1087d62b00eSchristos int
1097d62b00eSchristos ctf_hash_eq_type_key (const void *a, const void *b)
1107d62b00eSchristos {
1117d62b00eSchristos   ctf_helem_t *hep_a = (ctf_helem_t *) a;
1127d62b00eSchristos   ctf_helem_t *hep_b = (ctf_helem_t *) b;
1137d62b00eSchristos   ctf_link_type_key_t *key_a = (ctf_link_type_key_t *) hep_a->key;
1147d62b00eSchristos   ctf_link_type_key_t *key_b = (ctf_link_type_key_t *) hep_b->key;
1157d62b00eSchristos 
1167d62b00eSchristos   return (key_a->cltk_fp == key_b->cltk_fp)
1177d62b00eSchristos     && (key_a->cltk_idx == key_b->cltk_idx);
1187d62b00eSchristos }
1197d62b00eSchristos 
1207d62b00eSchristos /* Hash a type_id_key.  */
1217d62b00eSchristos unsigned int
1227d62b00eSchristos ctf_hash_type_id_key (const void *ptr)
1237d62b00eSchristos {
1247d62b00eSchristos   ctf_helem_t *hep = (ctf_helem_t *) ptr;
1257d62b00eSchristos   ctf_type_id_key_t *k = (ctf_type_id_key_t *) hep->key;
1267d62b00eSchristos 
1277d62b00eSchristos   return htab_hash_pointer ((void *) (uintptr_t) k->ctii_input_num)
1287d62b00eSchristos     + 59 * htab_hash_pointer ((void *) (uintptr_t) k->ctii_type);
1297d62b00eSchristos }
1307d62b00eSchristos 
1317d62b00eSchristos int
1327d62b00eSchristos ctf_hash_eq_type_id_key (const void *a, const void *b)
1337d62b00eSchristos {
1347d62b00eSchristos   ctf_helem_t *hep_a = (ctf_helem_t *) a;
1357d62b00eSchristos   ctf_helem_t *hep_b = (ctf_helem_t *) b;
1367d62b00eSchristos   ctf_type_id_key_t *key_a = (ctf_type_id_key_t *) hep_a->key;
1377d62b00eSchristos   ctf_type_id_key_t *key_b = (ctf_type_id_key_t *) hep_b->key;
1387d62b00eSchristos 
1397d62b00eSchristos   return (key_a->ctii_input_num == key_b->ctii_input_num)
1407d62b00eSchristos     && (key_a->ctii_type == key_b->ctii_type);
1417d62b00eSchristos }
1427d62b00eSchristos 
1437d62b00eSchristos /* The dynhash, used for hashes whose size is not known at creation time. */
1447d62b00eSchristos 
1457d62b00eSchristos /* Free a single ctf_helem with arbitrary key/value functions.  */
1467d62b00eSchristos 
1477d62b00eSchristos static void
1487d62b00eSchristos ctf_dynhash_item_free (void *item)
1497d62b00eSchristos {
1507d62b00eSchristos   ctf_helem_t *helem = item;
1517d62b00eSchristos 
1527d62b00eSchristos   if (helem->owner->key_free && helem->key)
1537d62b00eSchristos     helem->owner->key_free (helem->key);
1547d62b00eSchristos   if (helem->owner->value_free && helem->value)
1557d62b00eSchristos     helem->owner->value_free (helem->value);
1567d62b00eSchristos   free (helem);
1577d62b00eSchristos }
1587d62b00eSchristos 
1597d62b00eSchristos ctf_dynhash_t *
1607d62b00eSchristos ctf_dynhash_create (ctf_hash_fun hash_fun, ctf_hash_eq_fun eq_fun,
1617d62b00eSchristos                     ctf_hash_free_fun key_free, ctf_hash_free_fun value_free)
1627d62b00eSchristos {
1637d62b00eSchristos   ctf_dynhash_t *dynhash;
1647d62b00eSchristos   htab_del del = ctf_dynhash_item_free;
1657d62b00eSchristos 
1667d62b00eSchristos   if (key_free || value_free)
1677d62b00eSchristos     dynhash = malloc (sizeof (ctf_dynhash_t));
1687d62b00eSchristos   else
1697d62b00eSchristos     dynhash = malloc (offsetof (ctf_dynhash_t, key_free));
1707d62b00eSchristos   if (!dynhash)
1717d62b00eSchristos     return NULL;
1727d62b00eSchristos 
1737d62b00eSchristos   if (key_free == NULL && value_free == NULL)
1747d62b00eSchristos     del = free;
1757d62b00eSchristos 
1767d62b00eSchristos   /* 7 is arbitrary and untested for now.  */
1777d62b00eSchristos   if ((dynhash->htab = htab_create_alloc (7, (htab_hash) hash_fun, eq_fun,
1787d62b00eSchristos 					  del, xcalloc, free)) == NULL)
1797d62b00eSchristos     {
1807d62b00eSchristos       free (dynhash);
1817d62b00eSchristos       return NULL;
1827d62b00eSchristos     }
1837d62b00eSchristos 
1847d62b00eSchristos   if (key_free || value_free)
1857d62b00eSchristos     {
1867d62b00eSchristos       dynhash->key_free = key_free;
1877d62b00eSchristos       dynhash->value_free = value_free;
1887d62b00eSchristos     }
1897d62b00eSchristos 
1907d62b00eSchristos   return dynhash;
1917d62b00eSchristos }
1927d62b00eSchristos 
1937d62b00eSchristos static ctf_helem_t **
1947d62b00eSchristos ctf_hashtab_lookup (struct htab *htab, const void *key, enum insert_option insert)
1957d62b00eSchristos {
1967d62b00eSchristos   ctf_helem_t tmp = { .key = (void *) key };
1977d62b00eSchristos   return (ctf_helem_t **) htab_find_slot (htab, &tmp, insert);
1987d62b00eSchristos }
1997d62b00eSchristos 
2007d62b00eSchristos static ctf_helem_t *
2017d62b00eSchristos ctf_hashtab_insert (struct htab *htab, void *key, void *value,
2027d62b00eSchristos 		    ctf_hash_free_fun key_free,
2037d62b00eSchristos 		    ctf_hash_free_fun value_free)
2047d62b00eSchristos {
2057d62b00eSchristos   ctf_helem_t **slot;
2067d62b00eSchristos 
2077d62b00eSchristos   slot = ctf_hashtab_lookup (htab, key, INSERT);
2087d62b00eSchristos 
2097d62b00eSchristos   if (!slot)
2107d62b00eSchristos     {
2117d62b00eSchristos       errno = ENOMEM;
2127d62b00eSchristos       return NULL;
2137d62b00eSchristos     }
2147d62b00eSchristos 
2157d62b00eSchristos   if (!*slot)
2167d62b00eSchristos     {
2177d62b00eSchristos       /* Only spend space on the owner if we're going to use it: if there is a
2187d62b00eSchristos 	 key or value freeing function.  */
2197d62b00eSchristos       if (key_free || value_free)
2207d62b00eSchristos 	*slot = malloc (sizeof (ctf_helem_t));
2217d62b00eSchristos       else
2227d62b00eSchristos 	*slot = malloc (offsetof (ctf_helem_t, owner));
2237d62b00eSchristos       if (!*slot)
2247d62b00eSchristos 	return NULL;
2257d62b00eSchristos       (*slot)->key = key;
2267d62b00eSchristos     }
2277d62b00eSchristos   else
2287d62b00eSchristos     {
2297d62b00eSchristos       if (key_free)
2307d62b00eSchristos 	  key_free (key);
2317d62b00eSchristos       if (value_free)
2327d62b00eSchristos 	  value_free ((*slot)->value);
2337d62b00eSchristos     }
2347d62b00eSchristos   (*slot)->value = value;
2357d62b00eSchristos   return *slot;
2367d62b00eSchristos }
2377d62b00eSchristos 
2387d62b00eSchristos int
2397d62b00eSchristos ctf_dynhash_insert (ctf_dynhash_t *hp, void *key, void *value)
2407d62b00eSchristos {
2417d62b00eSchristos   ctf_helem_t *slot;
2427d62b00eSchristos   ctf_hash_free_fun key_free = NULL, value_free = NULL;
2437d62b00eSchristos 
2447d62b00eSchristos   if (hp->htab->del_f == ctf_dynhash_item_free)
2457d62b00eSchristos     {
2467d62b00eSchristos       key_free = hp->key_free;
2477d62b00eSchristos       value_free = hp->value_free;
2487d62b00eSchristos     }
2497d62b00eSchristos   slot = ctf_hashtab_insert (hp->htab, key, value,
2507d62b00eSchristos 			     key_free, value_free);
2517d62b00eSchristos 
2527d62b00eSchristos   if (!slot)
2537d62b00eSchristos     return errno;
2547d62b00eSchristos 
2557d62b00eSchristos   /* Keep track of the owner, so that the del function can get at the key_free
2567d62b00eSchristos      and value_free functions.  Only do this if one of those functions is set:
2577d62b00eSchristos      if not, the owner is not even present in the helem.  */
2587d62b00eSchristos 
2597d62b00eSchristos   if (key_free || value_free)
2607d62b00eSchristos     slot->owner = hp;
2617d62b00eSchristos 
2627d62b00eSchristos   return 0;
2637d62b00eSchristos }
2647d62b00eSchristos 
2657d62b00eSchristos void
2667d62b00eSchristos ctf_dynhash_remove (ctf_dynhash_t *hp, const void *key)
2677d62b00eSchristos {
2687d62b00eSchristos   ctf_helem_t hep = { (void *) key, NULL, NULL };
2697d62b00eSchristos   htab_remove_elt (hp->htab, &hep);
2707d62b00eSchristos }
2717d62b00eSchristos 
2727d62b00eSchristos void
2737d62b00eSchristos ctf_dynhash_empty (ctf_dynhash_t *hp)
2747d62b00eSchristos {
2757d62b00eSchristos   htab_empty (hp->htab);
2767d62b00eSchristos }
2777d62b00eSchristos 
2787d62b00eSchristos size_t
2797d62b00eSchristos ctf_dynhash_elements (ctf_dynhash_t *hp)
2807d62b00eSchristos {
2817d62b00eSchristos   return htab_elements (hp->htab);
2827d62b00eSchristos }
2837d62b00eSchristos 
2847d62b00eSchristos void *
2857d62b00eSchristos ctf_dynhash_lookup (ctf_dynhash_t *hp, const void *key)
2867d62b00eSchristos {
2877d62b00eSchristos   ctf_helem_t **slot;
2887d62b00eSchristos 
2897d62b00eSchristos   slot = ctf_hashtab_lookup (hp->htab, key, NO_INSERT);
2907d62b00eSchristos 
2917d62b00eSchristos   if (slot)
2927d62b00eSchristos     return (*slot)->value;
2937d62b00eSchristos 
2947d62b00eSchristos   return NULL;
2957d62b00eSchristos }
2967d62b00eSchristos 
2977d62b00eSchristos /* TRUE/FALSE return.  */
2987d62b00eSchristos int
2997d62b00eSchristos ctf_dynhash_lookup_kv (ctf_dynhash_t *hp, const void *key,
3007d62b00eSchristos 		       const void **orig_key, void **value)
3017d62b00eSchristos {
3027d62b00eSchristos   ctf_helem_t **slot;
3037d62b00eSchristos 
3047d62b00eSchristos   slot = ctf_hashtab_lookup (hp->htab, key, NO_INSERT);
3057d62b00eSchristos 
3067d62b00eSchristos   if (slot)
3077d62b00eSchristos     {
3087d62b00eSchristos       if (orig_key)
3097d62b00eSchristos 	*orig_key = (*slot)->key;
3107d62b00eSchristos       if (value)
3117d62b00eSchristos 	*value = (*slot)->value;
3127d62b00eSchristos       return 1;
3137d62b00eSchristos     }
3147d62b00eSchristos   return 0;
3157d62b00eSchristos }
3167d62b00eSchristos 
3177d62b00eSchristos typedef struct ctf_traverse_cb_arg
3187d62b00eSchristos {
3197d62b00eSchristos   ctf_hash_iter_f fun;
3207d62b00eSchristos   void *arg;
3217d62b00eSchristos } ctf_traverse_cb_arg_t;
3227d62b00eSchristos 
3237d62b00eSchristos static int
3247d62b00eSchristos ctf_hashtab_traverse (void **slot, void *arg_)
3257d62b00eSchristos {
3267d62b00eSchristos   ctf_helem_t *helem = *((ctf_helem_t **) slot);
3277d62b00eSchristos   ctf_traverse_cb_arg_t *arg = (ctf_traverse_cb_arg_t *) arg_;
3287d62b00eSchristos 
3297d62b00eSchristos   arg->fun (helem->key, helem->value, arg->arg);
3307d62b00eSchristos   return 1;
3317d62b00eSchristos }
3327d62b00eSchristos 
3337d62b00eSchristos void
3347d62b00eSchristos ctf_dynhash_iter (ctf_dynhash_t *hp, ctf_hash_iter_f fun, void *arg_)
3357d62b00eSchristos {
3367d62b00eSchristos   ctf_traverse_cb_arg_t arg = { fun, arg_ };
3377d62b00eSchristos   htab_traverse (hp->htab, ctf_hashtab_traverse, &arg);
3387d62b00eSchristos }
3397d62b00eSchristos 
3407d62b00eSchristos typedef struct ctf_traverse_find_cb_arg
3417d62b00eSchristos {
3427d62b00eSchristos   ctf_hash_iter_find_f fun;
3437d62b00eSchristos   void *arg;
3447d62b00eSchristos   void *found_key;
3457d62b00eSchristos } ctf_traverse_find_cb_arg_t;
3467d62b00eSchristos 
3477d62b00eSchristos static int
3487d62b00eSchristos ctf_hashtab_traverse_find (void **slot, void *arg_)
3497d62b00eSchristos {
3507d62b00eSchristos   ctf_helem_t *helem = *((ctf_helem_t **) slot);
3517d62b00eSchristos   ctf_traverse_find_cb_arg_t *arg = (ctf_traverse_find_cb_arg_t *) arg_;
3527d62b00eSchristos 
3537d62b00eSchristos   if (arg->fun (helem->key, helem->value, arg->arg))
3547d62b00eSchristos     {
3557d62b00eSchristos       arg->found_key = helem->key;
3567d62b00eSchristos       return 0;
3577d62b00eSchristos     }
3587d62b00eSchristos   return 1;
3597d62b00eSchristos }
3607d62b00eSchristos 
3617d62b00eSchristos void *
3627d62b00eSchristos ctf_dynhash_iter_find (ctf_dynhash_t *hp, ctf_hash_iter_find_f fun, void *arg_)
3637d62b00eSchristos {
3647d62b00eSchristos   ctf_traverse_find_cb_arg_t arg = { fun, arg_, NULL };
3657d62b00eSchristos   htab_traverse (hp->htab, ctf_hashtab_traverse_find, &arg);
3667d62b00eSchristos   return arg.found_key;
3677d62b00eSchristos }
3687d62b00eSchristos 
3697d62b00eSchristos typedef struct ctf_traverse_remove_cb_arg
3707d62b00eSchristos {
3717d62b00eSchristos   struct htab *htab;
3727d62b00eSchristos   ctf_hash_iter_remove_f fun;
3737d62b00eSchristos   void *arg;
3747d62b00eSchristos } ctf_traverse_remove_cb_arg_t;
3757d62b00eSchristos 
3767d62b00eSchristos static int
3777d62b00eSchristos ctf_hashtab_traverse_remove (void **slot, void *arg_)
3787d62b00eSchristos {
3797d62b00eSchristos   ctf_helem_t *helem = *((ctf_helem_t **) slot);
3807d62b00eSchristos   ctf_traverse_remove_cb_arg_t *arg = (ctf_traverse_remove_cb_arg_t *) arg_;
3817d62b00eSchristos 
3827d62b00eSchristos   if (arg->fun (helem->key, helem->value, arg->arg))
3837d62b00eSchristos     htab_clear_slot (arg->htab, slot);
3847d62b00eSchristos   return 1;
3857d62b00eSchristos }
3867d62b00eSchristos 
3877d62b00eSchristos void
3887d62b00eSchristos ctf_dynhash_iter_remove (ctf_dynhash_t *hp, ctf_hash_iter_remove_f fun,
3897d62b00eSchristos                          void *arg_)
3907d62b00eSchristos {
3917d62b00eSchristos   ctf_traverse_remove_cb_arg_t arg = { hp->htab, fun, arg_ };
3927d62b00eSchristos   htab_traverse (hp->htab, ctf_hashtab_traverse_remove, &arg);
3937d62b00eSchristos }
3947d62b00eSchristos 
3957d62b00eSchristos /* Traverse a dynhash in arbitrary order, in _next iterator form.
3967d62b00eSchristos 
3977d62b00eSchristos    Mutating the dynhash while iterating is not supported (just as it isn't for
3987d62b00eSchristos    htab_traverse).
3997d62b00eSchristos 
4007d62b00eSchristos    Note: unusually, this returns zero on success and a *positive* value on
4017d62b00eSchristos    error, because it does not take an fp, taking an error pointer would be
4027d62b00eSchristos    incredibly clunky, and nearly all error-handling ends up stuffing the result
4037d62b00eSchristos    of this into some sort of errno or ctf_errno, which is invariably
4047d62b00eSchristos    positive.  So doing this simplifies essentially all callers.  */
4057d62b00eSchristos int
4067d62b00eSchristos ctf_dynhash_next (ctf_dynhash_t *h, ctf_next_t **it, void **key, void **value)
4077d62b00eSchristos {
4087d62b00eSchristos   ctf_next_t *i = *it;
4097d62b00eSchristos   ctf_helem_t *slot;
4107d62b00eSchristos 
4117d62b00eSchristos   if (!i)
4127d62b00eSchristos     {
4137d62b00eSchristos       size_t size = htab_size (h->htab);
4147d62b00eSchristos 
4157d62b00eSchristos       /* If the table has too many entries to fit in an ssize_t, just give up.
4167d62b00eSchristos 	 This might be spurious, but if any type-related hashtable has ever been
4177d62b00eSchristos 	 nearly as large as that then something very odd is going on.  */
4187d62b00eSchristos       if (((ssize_t) size) < 0)
4197d62b00eSchristos 	return EDOM;
4207d62b00eSchristos 
4217d62b00eSchristos       if ((i = ctf_next_create ()) == NULL)
4227d62b00eSchristos 	return ENOMEM;
4237d62b00eSchristos 
4247d62b00eSchristos       i->u.ctn_hash_slot = h->htab->entries;
4257d62b00eSchristos       i->cu.ctn_h = h;
4267d62b00eSchristos       i->ctn_n = 0;
4277d62b00eSchristos       i->ctn_size = (ssize_t) size;
4287d62b00eSchristos       i->ctn_iter_fun = (void (*) (void)) ctf_dynhash_next;
4297d62b00eSchristos       *it = i;
4307d62b00eSchristos     }
4317d62b00eSchristos 
4327d62b00eSchristos   if ((void (*) (void)) ctf_dynhash_next != i->ctn_iter_fun)
4337d62b00eSchristos     return ECTF_NEXT_WRONGFUN;
4347d62b00eSchristos 
4357d62b00eSchristos   if (h != i->cu.ctn_h)
4367d62b00eSchristos     return ECTF_NEXT_WRONGFP;
4377d62b00eSchristos 
4387d62b00eSchristos   if ((ssize_t) i->ctn_n == i->ctn_size)
4397d62b00eSchristos     goto hash_end;
4407d62b00eSchristos 
4417d62b00eSchristos   while ((ssize_t) i->ctn_n < i->ctn_size
4427d62b00eSchristos 	 && (*i->u.ctn_hash_slot == HTAB_EMPTY_ENTRY
4437d62b00eSchristos 	     || *i->u.ctn_hash_slot == HTAB_DELETED_ENTRY))
4447d62b00eSchristos     {
4457d62b00eSchristos       i->u.ctn_hash_slot++;
4467d62b00eSchristos       i->ctn_n++;
4477d62b00eSchristos     }
4487d62b00eSchristos 
4497d62b00eSchristos   if ((ssize_t) i->ctn_n == i->ctn_size)
4507d62b00eSchristos     goto hash_end;
4517d62b00eSchristos 
4527d62b00eSchristos   slot = *i->u.ctn_hash_slot;
4537d62b00eSchristos 
4547d62b00eSchristos   if (key)
4557d62b00eSchristos     *key = slot->key;
4567d62b00eSchristos   if (value)
4577d62b00eSchristos     *value = slot->value;
4587d62b00eSchristos 
4597d62b00eSchristos   i->u.ctn_hash_slot++;
4607d62b00eSchristos   i->ctn_n++;
4617d62b00eSchristos 
4627d62b00eSchristos   return 0;
4637d62b00eSchristos 
4647d62b00eSchristos  hash_end:
4657d62b00eSchristos   ctf_next_destroy (i);
4667d62b00eSchristos   *it = NULL;
4677d62b00eSchristos   return ECTF_NEXT_END;
4687d62b00eSchristos }
4697d62b00eSchristos 
470*6881a400Schristos int
471*6881a400Schristos ctf_dynhash_sort_by_name (const ctf_next_hkv_t *one, const ctf_next_hkv_t *two,
472*6881a400Schristos 			  void *unused _libctf_unused_)
473*6881a400Schristos {
474*6881a400Schristos   return strcmp ((char *) one->hkv_key, (char *) two->hkv_key);
475*6881a400Schristos }
476*6881a400Schristos 
4777d62b00eSchristos /* Traverse a sorted dynhash, in _next iterator form.
4787d62b00eSchristos 
4797d62b00eSchristos    See ctf_dynhash_next for notes on error returns, etc.
4807d62b00eSchristos 
4817d62b00eSchristos    Sort keys before iterating over them using the SORT_FUN and SORT_ARG.
4827d62b00eSchristos 
4837d62b00eSchristos    If SORT_FUN is null, thunks to ctf_dynhash_next.  */
4847d62b00eSchristos int
4857d62b00eSchristos ctf_dynhash_next_sorted (ctf_dynhash_t *h, ctf_next_t **it, void **key,
4867d62b00eSchristos 			 void **value, ctf_hash_sort_f sort_fun, void *sort_arg)
4877d62b00eSchristos {
4887d62b00eSchristos   ctf_next_t *i = *it;
4897d62b00eSchristos 
4907d62b00eSchristos   if (sort_fun == NULL)
4917d62b00eSchristos     return ctf_dynhash_next (h, it, key, value);
4927d62b00eSchristos 
4937d62b00eSchristos   if (!i)
4947d62b00eSchristos     {
4957d62b00eSchristos       size_t els = ctf_dynhash_elements (h);
4967d62b00eSchristos       ctf_next_t *accum_i = NULL;
4977d62b00eSchristos       void *key, *value;
4987d62b00eSchristos       int err;
4997d62b00eSchristos       ctf_next_hkv_t *walk;
5007d62b00eSchristos 
5017d62b00eSchristos       if (((ssize_t) els) < 0)
5027d62b00eSchristos 	return EDOM;
5037d62b00eSchristos 
5047d62b00eSchristos       if ((i = ctf_next_create ()) == NULL)
5057d62b00eSchristos 	return ENOMEM;
5067d62b00eSchristos 
5077d62b00eSchristos       if ((i->u.ctn_sorted_hkv = calloc (els, sizeof (ctf_next_hkv_t))) == NULL)
5087d62b00eSchristos 	{
5097d62b00eSchristos 	  ctf_next_destroy (i);
5107d62b00eSchristos 	  return ENOMEM;
5117d62b00eSchristos 	}
5127d62b00eSchristos       walk = i->u.ctn_sorted_hkv;
5137d62b00eSchristos 
5147d62b00eSchristos       i->cu.ctn_h = h;
5157d62b00eSchristos 
5167d62b00eSchristos       while ((err = ctf_dynhash_next (h, &accum_i, &key, &value)) == 0)
5177d62b00eSchristos 	{
5187d62b00eSchristos 	  walk->hkv_key = key;
5197d62b00eSchristos 	  walk->hkv_value = value;
5207d62b00eSchristos 	  walk++;
5217d62b00eSchristos 	}
5227d62b00eSchristos       if (err != ECTF_NEXT_END)
5237d62b00eSchristos 	{
5247d62b00eSchristos 	  ctf_next_destroy (i);
5257d62b00eSchristos 	  return err;
5267d62b00eSchristos 	}
5277d62b00eSchristos 
5287d62b00eSchristos       if (sort_fun)
5297d62b00eSchristos 	  ctf_qsort_r (i->u.ctn_sorted_hkv, els, sizeof (ctf_next_hkv_t),
5307d62b00eSchristos 		       (int (*) (const void *, const void *, void *)) sort_fun,
5317d62b00eSchristos 		       sort_arg);
5327d62b00eSchristos       i->ctn_n = 0;
5337d62b00eSchristos       i->ctn_size = (ssize_t) els;
5347d62b00eSchristos       i->ctn_iter_fun = (void (*) (void)) ctf_dynhash_next_sorted;
5357d62b00eSchristos       *it = i;
5367d62b00eSchristos     }
5377d62b00eSchristos 
5387d62b00eSchristos   if ((void (*) (void)) ctf_dynhash_next_sorted != i->ctn_iter_fun)
5397d62b00eSchristos     return ECTF_NEXT_WRONGFUN;
5407d62b00eSchristos 
5417d62b00eSchristos   if (h != i->cu.ctn_h)
5427d62b00eSchristos     return ECTF_NEXT_WRONGFP;
5437d62b00eSchristos 
5447d62b00eSchristos   if ((ssize_t) i->ctn_n == i->ctn_size)
5457d62b00eSchristos     {
5467d62b00eSchristos       ctf_next_destroy (i);
5477d62b00eSchristos       *it = NULL;
5487d62b00eSchristos       return ECTF_NEXT_END;
5497d62b00eSchristos     }
5507d62b00eSchristos 
5517d62b00eSchristos   if (key)
5527d62b00eSchristos     *key = i->u.ctn_sorted_hkv[i->ctn_n].hkv_key;
5537d62b00eSchristos   if (value)
5547d62b00eSchristos     *value = i->u.ctn_sorted_hkv[i->ctn_n].hkv_value;
5557d62b00eSchristos   i->ctn_n++;
5567d62b00eSchristos   return 0;
5577d62b00eSchristos }
5587d62b00eSchristos 
5597d62b00eSchristos void
5607d62b00eSchristos ctf_dynhash_destroy (ctf_dynhash_t *hp)
5617d62b00eSchristos {
5627d62b00eSchristos   if (hp != NULL)
5637d62b00eSchristos     htab_delete (hp->htab);
5647d62b00eSchristos   free (hp);
5657d62b00eSchristos }
5667d62b00eSchristos 
5677d62b00eSchristos /* The dynset, used for sets of keys with no value.  The implementation of this
5687d62b00eSchristos    can be much simpler, because without a value the slot can simply be the
5697d62b00eSchristos    stored key, which means we don't need to store the freeing functions and the
5707d62b00eSchristos    dynset itself is just a htab.  */
5717d62b00eSchristos 
5727d62b00eSchristos ctf_dynset_t *
5737d62b00eSchristos ctf_dynset_create (htab_hash hash_fun, htab_eq eq_fun,
5747d62b00eSchristos 		   ctf_hash_free_fun key_free)
5757d62b00eSchristos {
5767d62b00eSchristos   /* 7 is arbitrary and untested for now.  */
5777d62b00eSchristos   return (ctf_dynset_t *) htab_create_alloc (7, (htab_hash) hash_fun, eq_fun,
5787d62b00eSchristos 					     key_free, xcalloc, free);
5797d62b00eSchristos }
5807d62b00eSchristos 
5817d62b00eSchristos /* The dynset has one complexity: the underlying implementation reserves two
5827d62b00eSchristos    values for internal hash table implementation details (empty versus deleted
5837d62b00eSchristos    entries).  These values are otherwise very useful for pointers cast to ints,
5847d62b00eSchristos    so transform the ctf_dynset_inserted value to allow for it.  (This
5857d62b00eSchristos    introduces an ambiguity in that one can no longer store these two values in
5867d62b00eSchristos    the dynset, but if we pick high enough values this is very unlikely to be a
5877d62b00eSchristos    problem.)
5887d62b00eSchristos 
5897d62b00eSchristos    We leak this implementation detail to the freeing functions on the grounds
5907d62b00eSchristos    that any use of these functions is overwhelmingly likely to be in sets using
5917d62b00eSchristos    real pointers, which will be unaffected.  */
5927d62b00eSchristos 
5937d62b00eSchristos #define DYNSET_EMPTY_ENTRY_REPLACEMENT ((void *) (uintptr_t) -64)
5947d62b00eSchristos #define DYNSET_DELETED_ENTRY_REPLACEMENT ((void *) (uintptr_t) -63)
5957d62b00eSchristos 
5967d62b00eSchristos static void *
5977d62b00eSchristos key_to_internal (const void *key)
5987d62b00eSchristos {
5997d62b00eSchristos   if (key == HTAB_EMPTY_ENTRY)
6007d62b00eSchristos     return DYNSET_EMPTY_ENTRY_REPLACEMENT;
6017d62b00eSchristos   else if (key == HTAB_DELETED_ENTRY)
6027d62b00eSchristos     return DYNSET_DELETED_ENTRY_REPLACEMENT;
6037d62b00eSchristos 
6047d62b00eSchristos   return (void *) key;
6057d62b00eSchristos }
6067d62b00eSchristos 
6077d62b00eSchristos static void *
6087d62b00eSchristos internal_to_key (const void *internal)
6097d62b00eSchristos {
6107d62b00eSchristos   if (internal == DYNSET_EMPTY_ENTRY_REPLACEMENT)
6117d62b00eSchristos     return HTAB_EMPTY_ENTRY;
6127d62b00eSchristos   else if (internal == DYNSET_DELETED_ENTRY_REPLACEMENT)
6137d62b00eSchristos     return HTAB_DELETED_ENTRY;
6147d62b00eSchristos   return (void *) internal;
6157d62b00eSchristos }
6167d62b00eSchristos 
6177d62b00eSchristos int
6187d62b00eSchristos ctf_dynset_insert (ctf_dynset_t *hp, void *key)
6197d62b00eSchristos {
6207d62b00eSchristos   struct htab *htab = (struct htab *) hp;
6217d62b00eSchristos   void **slot;
6227d62b00eSchristos 
6237d62b00eSchristos   slot = htab_find_slot (htab, key, INSERT);
6247d62b00eSchristos 
6257d62b00eSchristos   if (!slot)
6267d62b00eSchristos     {
6277d62b00eSchristos       errno = ENOMEM;
6287d62b00eSchristos       return -errno;
6297d62b00eSchristos     }
6307d62b00eSchristos 
6317d62b00eSchristos   if (*slot)
6327d62b00eSchristos     {
6337d62b00eSchristos       if (htab->del_f)
6347d62b00eSchristos 	(*htab->del_f) (*slot);
6357d62b00eSchristos     }
6367d62b00eSchristos 
6377d62b00eSchristos   *slot = key_to_internal (key);
6387d62b00eSchristos 
6397d62b00eSchristos   return 0;
6407d62b00eSchristos }
6417d62b00eSchristos 
6427d62b00eSchristos void
6437d62b00eSchristos ctf_dynset_remove (ctf_dynset_t *hp, const void *key)
6447d62b00eSchristos {
6457d62b00eSchristos   htab_remove_elt ((struct htab *) hp, key_to_internal (key));
6467d62b00eSchristos }
6477d62b00eSchristos 
6487d62b00eSchristos void
6497d62b00eSchristos ctf_dynset_destroy (ctf_dynset_t *hp)
6507d62b00eSchristos {
6517d62b00eSchristos   if (hp != NULL)
6527d62b00eSchristos     htab_delete ((struct htab *) hp);
6537d62b00eSchristos }
6547d62b00eSchristos 
6557d62b00eSchristos void *
6567d62b00eSchristos ctf_dynset_lookup (ctf_dynset_t *hp, const void *key)
6577d62b00eSchristos {
6587d62b00eSchristos   void **slot = htab_find_slot ((struct htab *) hp,
6597d62b00eSchristos 				key_to_internal (key), NO_INSERT);
6607d62b00eSchristos 
6617d62b00eSchristos   if (slot)
6627d62b00eSchristos     return internal_to_key (*slot);
6637d62b00eSchristos   return NULL;
6647d62b00eSchristos }
6657d62b00eSchristos 
666*6881a400Schristos size_t
667*6881a400Schristos ctf_dynset_elements (ctf_dynset_t *hp)
668*6881a400Schristos {
669*6881a400Schristos   return htab_elements ((struct htab *) hp);
670*6881a400Schristos }
671*6881a400Schristos 
6727d62b00eSchristos /* TRUE/FALSE return.  */
6737d62b00eSchristos int
6747d62b00eSchristos ctf_dynset_exists (ctf_dynset_t *hp, const void *key, const void **orig_key)
6757d62b00eSchristos {
6767d62b00eSchristos   void **slot = htab_find_slot ((struct htab *) hp,
6777d62b00eSchristos 				key_to_internal (key), NO_INSERT);
6787d62b00eSchristos 
6797d62b00eSchristos   if (orig_key && slot)
6807d62b00eSchristos     *orig_key = internal_to_key (*slot);
6817d62b00eSchristos   return (slot != NULL);
6827d62b00eSchristos }
6837d62b00eSchristos 
6847d62b00eSchristos /* Look up a completely random value from the set, if any exist.
6857d62b00eSchristos    Keys with value zero cannot be distinguished from a nonexistent key.  */
6867d62b00eSchristos void *
6877d62b00eSchristos ctf_dynset_lookup_any (ctf_dynset_t *hp)
6887d62b00eSchristos {
6897d62b00eSchristos   struct htab *htab = (struct htab *) hp;
6907d62b00eSchristos   void **slot = htab->entries;
6917d62b00eSchristos   void **limit = slot + htab_size (htab);
6927d62b00eSchristos 
6937d62b00eSchristos   while (slot < limit
6947d62b00eSchristos 	 && (*slot == HTAB_EMPTY_ENTRY || *slot == HTAB_DELETED_ENTRY))
6957d62b00eSchristos       slot++;
6967d62b00eSchristos 
6977d62b00eSchristos   if (slot < limit)
6987d62b00eSchristos     return internal_to_key (*slot);
6997d62b00eSchristos   return NULL;
7007d62b00eSchristos }
7017d62b00eSchristos 
7027d62b00eSchristos /* Traverse a dynset in arbitrary order, in _next iterator form.
7037d62b00eSchristos 
7047d62b00eSchristos    Otherwise, just like ctf_dynhash_next.  */
7057d62b00eSchristos int
7067d62b00eSchristos ctf_dynset_next (ctf_dynset_t *hp, ctf_next_t **it, void **key)
7077d62b00eSchristos {
7087d62b00eSchristos   struct htab *htab = (struct htab *) hp;
7097d62b00eSchristos   ctf_next_t *i = *it;
7107d62b00eSchristos   void *slot;
7117d62b00eSchristos 
7127d62b00eSchristos   if (!i)
7137d62b00eSchristos     {
7147d62b00eSchristos       size_t size = htab_size (htab);
7157d62b00eSchristos 
7167d62b00eSchristos       /* If the table has too many entries to fit in an ssize_t, just give up.
7177d62b00eSchristos 	 This might be spurious, but if any type-related hashtable has ever been
7187d62b00eSchristos 	 nearly as large as that then somthing very odd is going on.  */
7197d62b00eSchristos 
7207d62b00eSchristos       if (((ssize_t) size) < 0)
7217d62b00eSchristos 	return EDOM;
7227d62b00eSchristos 
7237d62b00eSchristos       if ((i = ctf_next_create ()) == NULL)
7247d62b00eSchristos 	return ENOMEM;
7257d62b00eSchristos 
7267d62b00eSchristos       i->u.ctn_hash_slot = htab->entries;
7277d62b00eSchristos       i->cu.ctn_s = hp;
7287d62b00eSchristos       i->ctn_n = 0;
7297d62b00eSchristos       i->ctn_size = (ssize_t) size;
7307d62b00eSchristos       i->ctn_iter_fun = (void (*) (void)) ctf_dynset_next;
7317d62b00eSchristos       *it = i;
7327d62b00eSchristos     }
7337d62b00eSchristos 
7347d62b00eSchristos   if ((void (*) (void)) ctf_dynset_next != i->ctn_iter_fun)
7357d62b00eSchristos     return ECTF_NEXT_WRONGFUN;
7367d62b00eSchristos 
7377d62b00eSchristos   if (hp != i->cu.ctn_s)
7387d62b00eSchristos     return ECTF_NEXT_WRONGFP;
7397d62b00eSchristos 
7407d62b00eSchristos   if ((ssize_t) i->ctn_n == i->ctn_size)
7417d62b00eSchristos     goto set_end;
7427d62b00eSchristos 
7437d62b00eSchristos   while ((ssize_t) i->ctn_n < i->ctn_size
7447d62b00eSchristos 	 && (*i->u.ctn_hash_slot == HTAB_EMPTY_ENTRY
7457d62b00eSchristos 	     || *i->u.ctn_hash_slot == HTAB_DELETED_ENTRY))
7467d62b00eSchristos     {
7477d62b00eSchristos       i->u.ctn_hash_slot++;
7487d62b00eSchristos       i->ctn_n++;
7497d62b00eSchristos     }
7507d62b00eSchristos 
7517d62b00eSchristos   if ((ssize_t) i->ctn_n == i->ctn_size)
7527d62b00eSchristos     goto set_end;
7537d62b00eSchristos 
7547d62b00eSchristos   slot = *i->u.ctn_hash_slot;
7557d62b00eSchristos 
7567d62b00eSchristos   if (key)
7577d62b00eSchristos     *key = internal_to_key (slot);
7587d62b00eSchristos 
7597d62b00eSchristos   i->u.ctn_hash_slot++;
7607d62b00eSchristos   i->ctn_n++;
7617d62b00eSchristos 
7627d62b00eSchristos   return 0;
7637d62b00eSchristos 
7647d62b00eSchristos  set_end:
7657d62b00eSchristos   ctf_next_destroy (i);
7667d62b00eSchristos   *it = NULL;
7677d62b00eSchristos   return ECTF_NEXT_END;
7687d62b00eSchristos }
7697d62b00eSchristos 
7707d62b00eSchristos /* ctf_hash, used for fixed-size maps from const char * -> ctf_id_t without
7717d62b00eSchristos    removal.  This is a straight cast of a hashtab.  */
7727d62b00eSchristos 
7737d62b00eSchristos ctf_hash_t *
7747d62b00eSchristos ctf_hash_create (unsigned long nelems, ctf_hash_fun hash_fun,
7757d62b00eSchristos 		 ctf_hash_eq_fun eq_fun)
7767d62b00eSchristos {
7777d62b00eSchristos   return (ctf_hash_t *) htab_create_alloc (nelems, (htab_hash) hash_fun,
7787d62b00eSchristos 					   eq_fun, free, xcalloc, free);
7797d62b00eSchristos }
7807d62b00eSchristos 
7817d62b00eSchristos uint32_t
7827d62b00eSchristos ctf_hash_size (const ctf_hash_t *hp)
7837d62b00eSchristos {
7847d62b00eSchristos   return htab_elements ((struct htab *) hp);
7857d62b00eSchristos }
7867d62b00eSchristos 
7877d62b00eSchristos int
788*6881a400Schristos ctf_hash_insert_type (ctf_hash_t *hp, ctf_dict_t *fp, uint32_t type,
7897d62b00eSchristos 		      uint32_t name)
7907d62b00eSchristos {
7917d62b00eSchristos   const char *str = ctf_strraw (fp, name);
7927d62b00eSchristos 
7937d62b00eSchristos   if (type == 0)
7947d62b00eSchristos     return EINVAL;
7957d62b00eSchristos 
7967d62b00eSchristos   if (str == NULL
7977d62b00eSchristos       && CTF_NAME_STID (name) == CTF_STRTAB_1
7987d62b00eSchristos       && fp->ctf_syn_ext_strtab == NULL
7997d62b00eSchristos       && fp->ctf_str[CTF_NAME_STID (name)].cts_strs == NULL)
8007d62b00eSchristos     return ECTF_STRTAB;
8017d62b00eSchristos 
8027d62b00eSchristos   if (str == NULL)
8037d62b00eSchristos     return ECTF_BADNAME;
8047d62b00eSchristos 
8057d62b00eSchristos   if (str[0] == '\0')
8067d62b00eSchristos     return 0;		   /* Just ignore empty strings on behalf of caller.  */
8077d62b00eSchristos 
8087d62b00eSchristos   if (ctf_hashtab_insert ((struct htab *) hp, (char *) str,
8097d62b00eSchristos 			  (void *) (ptrdiff_t) type, NULL, NULL) != NULL)
8107d62b00eSchristos     return 0;
8117d62b00eSchristos   return errno;
8127d62b00eSchristos }
8137d62b00eSchristos 
8147d62b00eSchristos /* if the key is already in the hash, override the previous definition with
8157d62b00eSchristos    this new official definition. If the key is not present, then call
8167d62b00eSchristos    ctf_hash_insert_type and hash it in.  */
8177d62b00eSchristos int
818*6881a400Schristos ctf_hash_define_type (ctf_hash_t *hp, ctf_dict_t *fp, uint32_t type,
8197d62b00eSchristos                       uint32_t name)
8207d62b00eSchristos {
8217d62b00eSchristos   /* This matches the semantics of ctf_hash_insert_type in this
8227d62b00eSchristos      implementation anyway.  */
8237d62b00eSchristos 
8247d62b00eSchristos   return ctf_hash_insert_type (hp, fp, type, name);
8257d62b00eSchristos }
8267d62b00eSchristos 
8277d62b00eSchristos ctf_id_t
828*6881a400Schristos ctf_hash_lookup_type (ctf_hash_t *hp, ctf_dict_t *fp __attribute__ ((__unused__)),
8297d62b00eSchristos 		      const char *key)
8307d62b00eSchristos {
8317d62b00eSchristos   ctf_helem_t **slot;
8327d62b00eSchristos 
8337d62b00eSchristos   slot = ctf_hashtab_lookup ((struct htab *) hp, key, NO_INSERT);
8347d62b00eSchristos 
8357d62b00eSchristos   if (slot)
8367d62b00eSchristos     return (ctf_id_t) (uintptr_t) ((*slot)->value);
8377d62b00eSchristos 
8387d62b00eSchristos   return 0;
8397d62b00eSchristos }
8407d62b00eSchristos 
8417d62b00eSchristos void
8427d62b00eSchristos ctf_hash_destroy (ctf_hash_t *hp)
8437d62b00eSchristos {
8447d62b00eSchristos   if (hp != NULL)
8457d62b00eSchristos     htab_delete ((struct htab *) hp);
8467d62b00eSchristos }
847