inline-hashtab.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /* Fully-inline hash table, used mainly for managing TLS descriptors.
  2. Copyright (C) 1999-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. This file is derived from a 2003's version of libiberty's
  5. hashtab.c, contributed by Vladimir Makarov (vmakarov@cygnus.com),
  6. but with most adaptation points and support for deleting elements
  7. removed.
  8. The GNU C Library is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU Lesser General Public
  10. License as published by the Free Software Foundation; either
  11. version 2.1 of the License, or (at your option) any later version.
  12. The GNU C Library is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. Lesser General Public License for more details.
  16. You should have received a copy of the GNU Lesser General Public
  17. License along with the GNU C Library; if not, see
  18. <https://www.gnu.org/licenses/>. */
  19. #ifndef INLINE_HASHTAB_H
  20. # define INLINE_HASHTAB_H 1
  21. struct hashtab
  22. {
  23. /* Table itself. */
  24. void **entries;
  25. /* Current size (in entries) of the hash table */
  26. size_t size;
  27. /* Current number of elements. */
  28. size_t n_elements;
  29. /* Free function for the entries array. This may vary depending on
  30. how early the array was allocated. If it is NULL, then the array
  31. can't be freed. */
  32. void (*free) (void *ptr);
  33. };
  34. inline static struct hashtab *
  35. htab_create (void)
  36. {
  37. struct hashtab *ht = malloc (sizeof (struct hashtab));
  38. if (! ht)
  39. return NULL;
  40. ht->size = 3;
  41. ht->entries = malloc (sizeof (void *) * ht->size);
  42. ht->free = __rtld_free;
  43. if (! ht->entries)
  44. {
  45. free (ht);
  46. return NULL;
  47. }
  48. ht->n_elements = 0;
  49. memset (ht->entries, 0, sizeof (void *) * ht->size);
  50. return ht;
  51. }
  52. /* This is only called from _dl_unmap, so it's safe to call
  53. free(). */
  54. inline static void
  55. htab_delete (struct hashtab *htab)
  56. {
  57. int i;
  58. for (i = htab->size - 1; i >= 0; i--)
  59. free (htab->entries[i]);
  60. htab->free (htab->entries);
  61. free (htab);
  62. }
  63. /* Similar to htab_find_slot, but without several unwanted side effects:
  64. - Does not call htab->eq_f when it finds an existing entry.
  65. - Does not change the count of elements/searches/collisions in the
  66. hash table.
  67. This function also assumes there are no deleted entries in the table.
  68. HASH is the hash value for the element to be inserted. */
  69. inline static void **
  70. find_empty_slot_for_expand (struct hashtab *htab, int hash)
  71. {
  72. size_t size = htab->size;
  73. unsigned int index = hash % size;
  74. void **slot = htab->entries + index;
  75. int hash2;
  76. if (! *slot)
  77. return slot;
  78. hash2 = 1 + hash % (size - 2);
  79. for (;;)
  80. {
  81. index += hash2;
  82. if (index >= size)
  83. index -= size;
  84. slot = htab->entries + index;
  85. if (! *slot)
  86. return slot;
  87. }
  88. }
  89. /* The following function changes size of memory allocated for the
  90. entries and repeatedly inserts the table elements. The occupancy
  91. of the table after the call will be about 50%. Naturally the hash
  92. table must already exist. Remember also that the place of the
  93. table entries is changed. If memory allocation failures are allowed,
  94. this function will return zero, indicating that the table could not be
  95. expanded. If all goes well, it will return a non-zero value. */
  96. inline static int
  97. htab_expand (struct hashtab *htab, int (*hash_fn) (void *))
  98. {
  99. void **oentries;
  100. void **olimit;
  101. void **p;
  102. void **nentries;
  103. size_t nsize;
  104. oentries = htab->entries;
  105. olimit = oentries + htab->size;
  106. /* Resize only when table after removal of unused elements is either
  107. too full or too empty. */
  108. if (htab->n_elements * 2 > htab->size)
  109. nsize = _dl_higher_prime_number (htab->n_elements * 2);
  110. else
  111. nsize = htab->size;
  112. nentries = calloc (sizeof (void *), nsize);
  113. if (nentries == NULL)
  114. return 0;
  115. htab->entries = nentries;
  116. htab->size = nsize;
  117. p = oentries;
  118. do
  119. {
  120. if (*p)
  121. *find_empty_slot_for_expand (htab, hash_fn (*p))
  122. = *p;
  123. p++;
  124. }
  125. while (p < olimit);
  126. /* Without recording the free corresponding to the malloc used to
  127. allocate the table, we couldn't tell whether this was allocated
  128. by the malloc() built into ld.so or the one in the main
  129. executable or libc. Calling free() for something that was
  130. allocated by the early malloc(), rather than the final run-time
  131. malloc() could do Very Bad Things (TM). We will waste memory
  132. allocated early as long as there's no corresponding free(), but
  133. this isn't so much memory as to be significant. */
  134. htab->free (oentries);
  135. /* Use the free() corresponding to the malloc() above to free this
  136. up. */
  137. htab->free = __rtld_free;
  138. return 1;
  139. }
  140. /* This function searches for a hash table slot containing an entry
  141. equal to the given element. To delete an entry, call this with
  142. INSERT = 0, then call htab_clear_slot on the slot returned (possibly
  143. after doing some checks). To insert an entry, call this with
  144. INSERT = 1, then write the value you want into the returned slot.
  145. When inserting an entry, NULL may be returned if memory allocation
  146. fails. */
  147. inline static void **
  148. htab_find_slot (struct hashtab *htab, void *ptr, int insert,
  149. int (*hash_fn)(void *), int (*eq_fn)(void *, void *))
  150. {
  151. unsigned int index;
  152. int hash, hash2;
  153. size_t size;
  154. void **entry;
  155. if (htab->size * 3 <= htab->n_elements * 4
  156. && htab_expand (htab, hash_fn) == 0)
  157. return NULL;
  158. hash = hash_fn (ptr);
  159. size = htab->size;
  160. index = hash % size;
  161. entry = &htab->entries[index];
  162. if (!*entry)
  163. goto empty_entry;
  164. else if (eq_fn (*entry, ptr))
  165. return entry;
  166. hash2 = 1 + hash % (size - 2);
  167. for (;;)
  168. {
  169. index += hash2;
  170. if (index >= size)
  171. index -= size;
  172. entry = &htab->entries[index];
  173. if (!*entry)
  174. goto empty_entry;
  175. else if (eq_fn (*entry, ptr))
  176. return entry;
  177. }
  178. empty_entry:
  179. if (!insert)
  180. return NULL;
  181. htab->n_elements++;
  182. return entry;
  183. }
  184. #endif /* INLINE_HASHTAB_H */