1 /* Interface to hashtable implementations.
2 Copyright (C) 2006-2023 Free Software Foundation, Inc.
4 This file is part of libctf.
6 libctf is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 See the GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; see the file COPYING. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "libiberty.h"
25 /* We have three hashtable implementations:
27 - ctf_hash_* is an interface to a fixed-size hash from const char * ->
28 ctf_id_t with number of elements specified at creation time, that should
29 support addition of items but need not support removal.
31 - ctf_dynhash_* is an interface to a dynamically-expanding hash with
32 unknown size that should support addition of large numbers of items, and
33 removal as well, and is used only at type-insertion time and during
36 - ctf_dynset_* is an interface to a dynamically-expanding hash that contains
39 These can be implemented by the same underlying hashmap if you wish. */
41 /* The helem is used for general key/value mappings in both the ctf_hash and
42 ctf_dynhash: the owner may not have space allocated for it, and will be
43 garbage (not NULL!) in that case. */
45 typedef struct ctf_helem
47 void *key
; /* Either a pointer, or a coerced ctf_id_t. */
48 void *value
; /* The value (possibly a coerced int). */
49 ctf_dynhash_t
*owner
; /* The hash that owns us. */
52 /* Equally, the key_free and value_free may not exist. */
57 ctf_hash_free_fun key_free
;
58 ctf_hash_free_fun value_free
;
61 /* Hash and eq functions for the dynhash and hash. */
64 ctf_hash_integer (const void *ptr
)
66 ctf_helem_t
*hep
= (ctf_helem_t
*) ptr
;
68 return htab_hash_pointer (hep
->key
);
72 ctf_hash_eq_integer (const void *a
, const void *b
)
74 ctf_helem_t
*hep_a
= (ctf_helem_t
*) a
;
75 ctf_helem_t
*hep_b
= (ctf_helem_t
*) b
;
77 return htab_eq_pointer (hep_a
->key
, hep_b
->key
);
81 ctf_hash_string (const void *ptr
)
83 ctf_helem_t
*hep
= (ctf_helem_t
*) ptr
;
85 return htab_hash_string (hep
->key
);
89 ctf_hash_eq_string (const void *a
, const void *b
)
91 ctf_helem_t
*hep_a
= (ctf_helem_t
*) a
;
92 ctf_helem_t
*hep_b
= (ctf_helem_t
*) b
;
94 return !strcmp((const char *) hep_a
->key
, (const char *) hep_b
->key
);
97 /* Hash a type_key. */
99 ctf_hash_type_key (const void *ptr
)
101 ctf_helem_t
*hep
= (ctf_helem_t
*) ptr
;
102 ctf_link_type_key_t
*k
= (ctf_link_type_key_t
*) hep
->key
;
104 return htab_hash_pointer (k
->cltk_fp
) + 59
105 * htab_hash_pointer ((void *) (uintptr_t) k
->cltk_idx
);
109 ctf_hash_eq_type_key (const void *a
, const void *b
)
111 ctf_helem_t
*hep_a
= (ctf_helem_t
*) a
;
112 ctf_helem_t
*hep_b
= (ctf_helem_t
*) b
;
113 ctf_link_type_key_t
*key_a
= (ctf_link_type_key_t
*) hep_a
->key
;
114 ctf_link_type_key_t
*key_b
= (ctf_link_type_key_t
*) hep_b
->key
;
116 return (key_a
->cltk_fp
== key_b
->cltk_fp
)
117 && (key_a
->cltk_idx
== key_b
->cltk_idx
);
120 /* Hash a type_id_key. */
122 ctf_hash_type_id_key (const void *ptr
)
124 ctf_helem_t
*hep
= (ctf_helem_t
*) ptr
;
125 ctf_type_id_key_t
*k
= (ctf_type_id_key_t
*) hep
->key
;
127 return htab_hash_pointer ((void *) (uintptr_t) k
->ctii_input_num
)
128 + 59 * htab_hash_pointer ((void *) (uintptr_t) k
->ctii_type
);
132 ctf_hash_eq_type_id_key (const void *a
, const void *b
)
134 ctf_helem_t
*hep_a
= (ctf_helem_t
*) a
;
135 ctf_helem_t
*hep_b
= (ctf_helem_t
*) b
;
136 ctf_type_id_key_t
*key_a
= (ctf_type_id_key_t
*) hep_a
->key
;
137 ctf_type_id_key_t
*key_b
= (ctf_type_id_key_t
*) hep_b
->key
;
139 return (key_a
->ctii_input_num
== key_b
->ctii_input_num
)
140 && (key_a
->ctii_type
== key_b
->ctii_type
);
143 /* The dynhash, used for hashes whose size is not known at creation time. */
145 /* Free a single ctf_helem with arbitrary key/value functions. */
148 ctf_dynhash_item_free (void *item
)
150 ctf_helem_t
*helem
= item
;
152 if (helem
->owner
->key_free
&& helem
->key
)
153 helem
->owner
->key_free (helem
->key
);
154 if (helem
->owner
->value_free
&& helem
->value
)
155 helem
->owner
->value_free (helem
->value
);
160 ctf_dynhash_create (ctf_hash_fun hash_fun
, ctf_hash_eq_fun eq_fun
,
161 ctf_hash_free_fun key_free
, ctf_hash_free_fun value_free
)
163 ctf_dynhash_t
*dynhash
;
164 htab_del del
= ctf_dynhash_item_free
;
166 if (key_free
|| value_free
)
167 dynhash
= malloc (sizeof (ctf_dynhash_t
));
169 dynhash
= malloc (offsetof (ctf_dynhash_t
, key_free
));
173 if (key_free
== NULL
&& value_free
== NULL
)
176 /* 7 is arbitrary and untested for now. */
177 if ((dynhash
->htab
= htab_create_alloc (7, (htab_hash
) hash_fun
, eq_fun
,
178 del
, xcalloc
, free
)) == NULL
)
184 if (key_free
|| value_free
)
186 dynhash
->key_free
= key_free
;
187 dynhash
->value_free
= value_free
;
193 static ctf_helem_t
**
194 ctf_hashtab_lookup (struct htab
*htab
, const void *key
, enum insert_option insert
)
196 ctf_helem_t tmp
= { .key
= (void *) key
};
197 return (ctf_helem_t
**) htab_find_slot (htab
, &tmp
, insert
);
201 ctf_hashtab_insert (struct htab
*htab
, void *key
, void *value
,
202 ctf_hash_free_fun key_free
,
203 ctf_hash_free_fun value_free
)
207 slot
= ctf_hashtab_lookup (htab
, key
, INSERT
);
217 /* Only spend space on the owner if we're going to use it: if there is a
218 key or value freeing function. */
219 if (key_free
|| value_free
)
220 *slot
= malloc (sizeof (ctf_helem_t
));
222 *slot
= malloc (offsetof (ctf_helem_t
, owner
));
232 value_free ((*slot
)->value
);
234 (*slot
)->value
= value
;
239 ctf_dynhash_insert (ctf_dynhash_t
*hp
, void *key
, void *value
)
242 ctf_hash_free_fun key_free
= NULL
, value_free
= NULL
;
244 if (hp
->htab
->del_f
== ctf_dynhash_item_free
)
246 key_free
= hp
->key_free
;
247 value_free
= hp
->value_free
;
249 slot
= ctf_hashtab_insert (hp
->htab
, key
, value
,
250 key_free
, value_free
);
255 /* Keep track of the owner, so that the del function can get at the key_free
256 and value_free functions. Only do this if one of those functions is set:
257 if not, the owner is not even present in the helem. */
259 if (key_free
|| value_free
)
266 ctf_dynhash_remove (ctf_dynhash_t
*hp
, const void *key
)
268 ctf_helem_t hep
= { (void *) key
, NULL
, NULL
};
269 htab_remove_elt (hp
->htab
, &hep
);
273 ctf_dynhash_empty (ctf_dynhash_t
*hp
)
275 htab_empty (hp
->htab
);
279 ctf_dynhash_elements (ctf_dynhash_t
*hp
)
281 return htab_elements (hp
->htab
);
285 ctf_dynhash_lookup (ctf_dynhash_t
*hp
, const void *key
)
289 slot
= ctf_hashtab_lookup (hp
->htab
, key
, NO_INSERT
);
292 return (*slot
)->value
;
297 /* TRUE/FALSE return. */
299 ctf_dynhash_lookup_kv (ctf_dynhash_t
*hp
, const void *key
,
300 const void **orig_key
, void **value
)
304 slot
= ctf_hashtab_lookup (hp
->htab
, key
, NO_INSERT
);
309 *orig_key
= (*slot
)->key
;
311 *value
= (*slot
)->value
;
317 typedef struct ctf_traverse_cb_arg
321 } ctf_traverse_cb_arg_t
;
324 ctf_hashtab_traverse (void **slot
, void *arg_
)
326 ctf_helem_t
*helem
= *((ctf_helem_t
**) slot
);
327 ctf_traverse_cb_arg_t
*arg
= (ctf_traverse_cb_arg_t
*) arg_
;
329 arg
->fun (helem
->key
, helem
->value
, arg
->arg
);
334 ctf_dynhash_iter (ctf_dynhash_t
*hp
, ctf_hash_iter_f fun
, void *arg_
)
336 ctf_traverse_cb_arg_t arg
= { fun
, arg_
};
337 htab_traverse (hp
->htab
, ctf_hashtab_traverse
, &arg
);
340 typedef struct ctf_traverse_find_cb_arg
342 ctf_hash_iter_find_f fun
;
345 } ctf_traverse_find_cb_arg_t
;
348 ctf_hashtab_traverse_find (void **slot
, void *arg_
)
350 ctf_helem_t
*helem
= *((ctf_helem_t
**) slot
);
351 ctf_traverse_find_cb_arg_t
*arg
= (ctf_traverse_find_cb_arg_t
*) arg_
;
353 if (arg
->fun (helem
->key
, helem
->value
, arg
->arg
))
355 arg
->found_key
= helem
->key
;
362 ctf_dynhash_iter_find (ctf_dynhash_t
*hp
, ctf_hash_iter_find_f fun
, void *arg_
)
364 ctf_traverse_find_cb_arg_t arg
= { fun
, arg_
, NULL
};
365 htab_traverse (hp
->htab
, ctf_hashtab_traverse_find
, &arg
);
366 return arg
.found_key
;
369 typedef struct ctf_traverse_remove_cb_arg
372 ctf_hash_iter_remove_f fun
;
374 } ctf_traverse_remove_cb_arg_t
;
377 ctf_hashtab_traverse_remove (void **slot
, void *arg_
)
379 ctf_helem_t
*helem
= *((ctf_helem_t
**) slot
);
380 ctf_traverse_remove_cb_arg_t
*arg
= (ctf_traverse_remove_cb_arg_t
*) arg_
;
382 if (arg
->fun (helem
->key
, helem
->value
, arg
->arg
))
383 htab_clear_slot (arg
->htab
, slot
);
388 ctf_dynhash_iter_remove (ctf_dynhash_t
*hp
, ctf_hash_iter_remove_f fun
,
391 ctf_traverse_remove_cb_arg_t arg
= { hp
->htab
, fun
, arg_
};
392 htab_traverse (hp
->htab
, ctf_hashtab_traverse_remove
, &arg
);
395 /* Traverse a dynhash in arbitrary order, in _next iterator form.
397 Mutating the dynhash while iterating is not supported (just as it isn't for
400 Note: unusually, this returns zero on success and a *positive* value on
401 error, because it does not take an fp, taking an error pointer would be
402 incredibly clunky, and nearly all error-handling ends up stuffing the result
403 of this into some sort of errno or ctf_errno, which is invariably
404 positive. So doing this simplifies essentially all callers. */
406 ctf_dynhash_next (ctf_dynhash_t
*h
, ctf_next_t
**it
, void **key
, void **value
)
413 size_t size
= htab_size (h
->htab
);
415 /* If the table has too many entries to fit in an ssize_t, just give up.
416 This might be spurious, but if any type-related hashtable has ever been
417 nearly as large as that then something very odd is going on. */
418 if (((ssize_t
) size
) < 0)
421 if ((i
= ctf_next_create ()) == NULL
)
424 i
->u
.ctn_hash_slot
= h
->htab
->entries
;
427 i
->ctn_size
= (ssize_t
) size
;
428 i
->ctn_iter_fun
= (void (*) (void)) ctf_dynhash_next
;
432 if ((void (*) (void)) ctf_dynhash_next
!= i
->ctn_iter_fun
)
433 return ECTF_NEXT_WRONGFUN
;
435 if (h
!= i
->cu
.ctn_h
)
436 return ECTF_NEXT_WRONGFP
;
438 if ((ssize_t
) i
->ctn_n
== i
->ctn_size
)
441 while ((ssize_t
) i
->ctn_n
< i
->ctn_size
442 && (*i
->u
.ctn_hash_slot
== HTAB_EMPTY_ENTRY
443 || *i
->u
.ctn_hash_slot
== HTAB_DELETED_ENTRY
))
445 i
->u
.ctn_hash_slot
++;
449 if ((ssize_t
) i
->ctn_n
== i
->ctn_size
)
452 slot
= *i
->u
.ctn_hash_slot
;
457 *value
= slot
->value
;
459 i
->u
.ctn_hash_slot
++;
465 ctf_next_destroy (i
);
467 return ECTF_NEXT_END
;
471 ctf_dynhash_sort_by_name (const ctf_next_hkv_t
*one
, const ctf_next_hkv_t
*two
,
472 void *unused _libctf_unused_
)
474 return strcmp ((char *) one
->hkv_key
, (char *) two
->hkv_key
);
477 /* Traverse a sorted dynhash, in _next iterator form.
479 See ctf_dynhash_next for notes on error returns, etc.
481 Sort keys before iterating over them using the SORT_FUN and SORT_ARG.
483 If SORT_FUN is null, thunks to ctf_dynhash_next. */
485 ctf_dynhash_next_sorted (ctf_dynhash_t
*h
, ctf_next_t
**it
, void **key
,
486 void **value
, ctf_hash_sort_f sort_fun
, void *sort_arg
)
490 if (sort_fun
== NULL
)
491 return ctf_dynhash_next (h
, it
, key
, value
);
495 size_t els
= ctf_dynhash_elements (h
);
496 ctf_next_t
*accum_i
= NULL
;
499 ctf_next_hkv_t
*walk
;
501 if (((ssize_t
) els
) < 0)
504 if ((i
= ctf_next_create ()) == NULL
)
507 if ((i
->u
.ctn_sorted_hkv
= calloc (els
, sizeof (ctf_next_hkv_t
))) == NULL
)
509 ctf_next_destroy (i
);
512 walk
= i
->u
.ctn_sorted_hkv
;
516 while ((err
= ctf_dynhash_next (h
, &accum_i
, &key
, &value
)) == 0)
519 walk
->hkv_value
= value
;
522 if (err
!= ECTF_NEXT_END
)
524 ctf_next_destroy (i
);
529 ctf_qsort_r (i
->u
.ctn_sorted_hkv
, els
, sizeof (ctf_next_hkv_t
),
530 (int (*) (const void *, const void *, void *)) sort_fun
,
533 i
->ctn_size
= (ssize_t
) els
;
534 i
->ctn_iter_fun
= (void (*) (void)) ctf_dynhash_next_sorted
;
538 if ((void (*) (void)) ctf_dynhash_next_sorted
!= i
->ctn_iter_fun
)
539 return ECTF_NEXT_WRONGFUN
;
541 if (h
!= i
->cu
.ctn_h
)
542 return ECTF_NEXT_WRONGFP
;
544 if ((ssize_t
) i
->ctn_n
== i
->ctn_size
)
546 ctf_next_destroy (i
);
548 return ECTF_NEXT_END
;
552 *key
= i
->u
.ctn_sorted_hkv
[i
->ctn_n
].hkv_key
;
554 *value
= i
->u
.ctn_sorted_hkv
[i
->ctn_n
].hkv_value
;
560 ctf_dynhash_destroy (ctf_dynhash_t
*hp
)
563 htab_delete (hp
->htab
);
567 /* The dynset, used for sets of keys with no value. The implementation of this
568 can be much simpler, because without a value the slot can simply be the
569 stored key, which means we don't need to store the freeing functions and the
570 dynset itself is just a htab. */
573 ctf_dynset_create (htab_hash hash_fun
, htab_eq eq_fun
,
574 ctf_hash_free_fun key_free
)
576 /* 7 is arbitrary and untested for now. */
577 return (ctf_dynset_t
*) htab_create_alloc (7, (htab_hash
) hash_fun
, eq_fun
,
578 key_free
, xcalloc
, free
);
581 /* The dynset has one complexity: the underlying implementation reserves two
582 values for internal hash table implementation details (empty versus deleted
583 entries). These values are otherwise very useful for pointers cast to ints,
584 so transform the ctf_dynset_inserted value to allow for it. (This
585 introduces an ambiguity in that one can no longer store these two values in
586 the dynset, but if we pick high enough values this is very unlikely to be a
589 We leak this implementation detail to the freeing functions on the grounds
590 that any use of these functions is overwhelmingly likely to be in sets using
591 real pointers, which will be unaffected. */
593 #define DYNSET_EMPTY_ENTRY_REPLACEMENT ((void *) (uintptr_t) -64)
594 #define DYNSET_DELETED_ENTRY_REPLACEMENT ((void *) (uintptr_t) -63)
597 key_to_internal (const void *key
)
599 if (key
== HTAB_EMPTY_ENTRY
)
600 return DYNSET_EMPTY_ENTRY_REPLACEMENT
;
601 else if (key
== HTAB_DELETED_ENTRY
)
602 return DYNSET_DELETED_ENTRY_REPLACEMENT
;
608 internal_to_key (const void *internal
)
610 if (internal
== DYNSET_EMPTY_ENTRY_REPLACEMENT
)
611 return HTAB_EMPTY_ENTRY
;
612 else if (internal
== DYNSET_DELETED_ENTRY_REPLACEMENT
)
613 return HTAB_DELETED_ENTRY
;
614 return (void *) internal
;
618 ctf_dynset_insert (ctf_dynset_t
*hp
, void *key
)
620 struct htab
*htab
= (struct htab
*) hp
;
623 slot
= htab_find_slot (htab
, key
, INSERT
);
634 (*htab
->del_f
) (*slot
);
637 *slot
= key_to_internal (key
);
643 ctf_dynset_remove (ctf_dynset_t
*hp
, const void *key
)
645 htab_remove_elt ((struct htab
*) hp
, key_to_internal (key
));
649 ctf_dynset_destroy (ctf_dynset_t
*hp
)
652 htab_delete ((struct htab
*) hp
);
656 ctf_dynset_lookup (ctf_dynset_t
*hp
, const void *key
)
658 void **slot
= htab_find_slot ((struct htab
*) hp
,
659 key_to_internal (key
), NO_INSERT
);
662 return internal_to_key (*slot
);
667 ctf_dynset_elements (ctf_dynset_t
*hp
)
669 return htab_elements ((struct htab
*) hp
);
672 /* TRUE/FALSE return. */
674 ctf_dynset_exists (ctf_dynset_t
*hp
, const void *key
, const void **orig_key
)
676 void **slot
= htab_find_slot ((struct htab
*) hp
,
677 key_to_internal (key
), NO_INSERT
);
679 if (orig_key
&& slot
)
680 *orig_key
= internal_to_key (*slot
);
681 return (slot
!= NULL
);
684 /* Look up a completely random value from the set, if any exist.
685 Keys with value zero cannot be distinguished from a nonexistent key. */
687 ctf_dynset_lookup_any (ctf_dynset_t
*hp
)
689 struct htab
*htab
= (struct htab
*) hp
;
690 void **slot
= htab
->entries
;
691 void **limit
= slot
+ htab_size (htab
);
694 && (*slot
== HTAB_EMPTY_ENTRY
|| *slot
== HTAB_DELETED_ENTRY
))
698 return internal_to_key (*slot
);
702 /* Traverse a dynset in arbitrary order, in _next iterator form.
704 Otherwise, just like ctf_dynhash_next. */
706 ctf_dynset_next (ctf_dynset_t
*hp
, ctf_next_t
**it
, void **key
)
708 struct htab
*htab
= (struct htab
*) hp
;
714 size_t size
= htab_size (htab
);
716 /* If the table has too many entries to fit in an ssize_t, just give up.
717 This might be spurious, but if any type-related hashtable has ever been
718 nearly as large as that then somthing very odd is going on. */
720 if (((ssize_t
) size
) < 0)
723 if ((i
= ctf_next_create ()) == NULL
)
726 i
->u
.ctn_hash_slot
= htab
->entries
;
729 i
->ctn_size
= (ssize_t
) size
;
730 i
->ctn_iter_fun
= (void (*) (void)) ctf_dynset_next
;
734 if ((void (*) (void)) ctf_dynset_next
!= i
->ctn_iter_fun
)
735 return ECTF_NEXT_WRONGFUN
;
737 if (hp
!= i
->cu
.ctn_s
)
738 return ECTF_NEXT_WRONGFP
;
740 if ((ssize_t
) i
->ctn_n
== i
->ctn_size
)
743 while ((ssize_t
) i
->ctn_n
< i
->ctn_size
744 && (*i
->u
.ctn_hash_slot
== HTAB_EMPTY_ENTRY
745 || *i
->u
.ctn_hash_slot
== HTAB_DELETED_ENTRY
))
747 i
->u
.ctn_hash_slot
++;
751 if ((ssize_t
) i
->ctn_n
== i
->ctn_size
)
754 slot
= *i
->u
.ctn_hash_slot
;
757 *key
= internal_to_key (slot
);
759 i
->u
.ctn_hash_slot
++;
765 ctf_next_destroy (i
);
767 return ECTF_NEXT_END
;
770 /* ctf_hash, used for fixed-size maps from const char * -> ctf_id_t without
771 removal. This is a straight cast of a hashtab. */
774 ctf_hash_create (unsigned long nelems
, ctf_hash_fun hash_fun
,
775 ctf_hash_eq_fun eq_fun
)
777 return (ctf_hash_t
*) htab_create_alloc (nelems
, (htab_hash
) hash_fun
,
778 eq_fun
, free
, xcalloc
, free
);
782 ctf_hash_size (const ctf_hash_t
*hp
)
784 return htab_elements ((struct htab
*) hp
);
788 ctf_hash_insert_type (ctf_hash_t
*hp
, ctf_dict_t
*fp
, uint32_t type
,
791 const char *str
= ctf_strraw (fp
, name
);
797 && CTF_NAME_STID (name
) == CTF_STRTAB_1
798 && fp
->ctf_syn_ext_strtab
== NULL
799 && fp
->ctf_str
[CTF_NAME_STID (name
)].cts_strs
== NULL
)
806 return 0; /* Just ignore empty strings on behalf of caller. */
808 if (ctf_hashtab_insert ((struct htab
*) hp
, (char *) str
,
809 (void *) (ptrdiff_t) type
, NULL
, NULL
) != NULL
)
814 /* if the key is already in the hash, override the previous definition with
815 this new official definition. If the key is not present, then call
816 ctf_hash_insert_type and hash it in. */
818 ctf_hash_define_type (ctf_hash_t
*hp
, ctf_dict_t
*fp
, uint32_t type
,
821 /* This matches the semantics of ctf_hash_insert_type in this
822 implementation anyway. */
824 return ctf_hash_insert_type (hp
, fp
, type
, name
);
828 ctf_hash_lookup_type (ctf_hash_t
*hp
, ctf_dict_t
*fp
__attribute__ ((__unused__
)),
833 slot
= ctf_hashtab_lookup ((struct htab
*) hp
, key
, NO_INSERT
);
836 return (ctf_id_t
) (uintptr_t) ((*slot
)->value
);
842 ctf_hash_destroy (ctf_hash_t
*hp
)
845 htab_delete ((struct htab
*) hp
);