ctf-hash.c 21 KB

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