hash.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /* Hash tables for Objective C internal structures
  2. Copyright (C) 1993-2022 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. GCC is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. #include "objc-private/common.h"
  20. #include <assert.h> /* For assert. */
  21. #include "objc/runtime.h" /* For objc_calloc. */
  22. #include "objc-private/hash.h"
  23. /* These two macros determine when a hash table is full and
  24. by how much it should be expanded respectively.
  25. These equations are percentages. */
  26. #define FULLNESS(cache) \
  27. ((((cache)->size * 75) / 100) <= (cache)->used)
  28. #define EXPANSION(cache) \
  29. ((cache)->size * 2)
  30. cache_ptr
  31. objc_hash_new (unsigned int size, hash_func_type hash_func,
  32. compare_func_type compare_func)
  33. {
  34. cache_ptr cache;
  35. /* Pass me a value greater than 0 and a power of 2. */
  36. assert (size);
  37. assert (! (size & (size - 1)));
  38. /* Allocate the cache structure. calloc insures its initialization
  39. for default values. */
  40. cache = (cache_ptr) objc_calloc (1, sizeof (struct cache));
  41. assert (cache);
  42. /* Allocate the array of buckets for the cache. calloc initializes
  43. all of the pointers to NULL. */
  44. cache->node_table
  45. = (node_ptr *) objc_calloc (size, sizeof (node_ptr));
  46. assert (cache->node_table);
  47. cache->size = size;
  48. /* This should work for all processor architectures (?). */
  49. cache->mask = (size - 1);
  50. /* Store the hashing function so that codes can be computed. */
  51. cache->hash_func = hash_func;
  52. /* Store the function that compares hash keys to determine if they
  53. are equal. */
  54. cache->compare_func = compare_func;
  55. return cache;
  56. }
  57. void
  58. objc_hash_delete (cache_ptr cache)
  59. {
  60. node_ptr node;
  61. node_ptr next_node;
  62. unsigned int i;
  63. /* Purge all key/value pairs from the table. */
  64. /* Step through the nodes one by one and remove every node WITHOUT
  65. using objc_hash_next. this makes objc_hash_delete much more
  66. efficient. */
  67. for (i = 0; i < cache->size; i++)
  68. {
  69. if ((node = cache->node_table[i]))
  70. {
  71. /* An entry in the hash table has been found. Now step
  72. through the nodes next in the list and free them. */
  73. while ((next_node = node->next))
  74. {
  75. objc_hash_remove (cache,node->key);
  76. node = next_node;
  77. }
  78. objc_hash_remove (cache,node->key);
  79. }
  80. }
  81. /* Release the array of nodes and the cache itself. */
  82. objc_free(cache->node_table);
  83. objc_free(cache);
  84. }
  85. void
  86. objc_hash_add (cache_ptr *cachep, const void *key, void *value)
  87. {
  88. size_t indx = (*(*cachep)->hash_func) (*cachep, key);
  89. node_ptr node = (node_ptr) objc_calloc (1, sizeof (struct cache_node));
  90. assert (node);
  91. /* Initialize the new node. */
  92. node->key = key;
  93. node->value = value;
  94. node->next = (*cachep)->node_table[indx];
  95. /* Debugging. Check the list for another key. */
  96. #ifdef DEBUG
  97. {
  98. node_ptr node1 = (*cachep)->node_table[indx];
  99. while (node1)
  100. {
  101. assert (node1->key != key);
  102. node1 = node1->next;
  103. }
  104. }
  105. #endif
  106. /* Install the node as the first element on the list. */
  107. (*cachep)->node_table[indx] = node;
  108. /* Bump the number of entries in the cache. */
  109. ++(*cachep)->used;
  110. /* Check the hash table's fullness. We're going to expand if it is
  111. above the fullness level. */
  112. if (FULLNESS (*cachep))
  113. {
  114. /* The hash table has reached its fullness level. Time to
  115. expand it.
  116. I'm using a slow method here but is built on other primitive
  117. functions thereby increasing its correctness. */
  118. node_ptr node1 = NULL;
  119. cache_ptr new = objc_hash_new (EXPANSION (*cachep),
  120. (*cachep)->hash_func,
  121. (*cachep)->compare_func);
  122. DEBUG_PRINTF ("Expanding cache %#x from %d to %d\n",
  123. (int) *cachep, (*cachep)->size, new->size);
  124. /* Copy the nodes from the first hash table to the new one. */
  125. while ((node1 = objc_hash_next (*cachep, node1)))
  126. objc_hash_add (&new, node1->key, node1->value);
  127. /* Trash the old cache. */
  128. objc_hash_delete (*cachep);
  129. /* Return a pointer to the new hash table. */
  130. *cachep = new;
  131. }
  132. }
  133. void
  134. objc_hash_remove (cache_ptr cache, const void *key)
  135. {
  136. size_t indx = (*cache->hash_func) (cache, key);
  137. node_ptr node = cache->node_table[indx];
  138. /* We assume there is an entry in the table. Error if it is
  139. not. */
  140. assert (node);
  141. /* Special case. First element is the key/value pair to be
  142. removed. */
  143. if ((*cache->compare_func) (node->key, key))
  144. {
  145. cache->node_table[indx] = node->next;
  146. objc_free(node);
  147. }
  148. else
  149. {
  150. /* Otherwise, find the hash entry. */
  151. node_ptr prev = node;
  152. BOOL removed = NO;
  153. do
  154. {
  155. if ((*cache->compare_func) (node->key, key))
  156. {
  157. prev->next = node->next, removed = YES;
  158. objc_free(node);
  159. }
  160. else
  161. prev = node, node = node->next;
  162. }
  163. while (!removed && node);
  164. assert (removed);
  165. }
  166. /* Decrement the number of entries in the hash table. */
  167. --cache->used;
  168. }
  169. node_ptr
  170. objc_hash_next (cache_ptr cache, node_ptr node)
  171. {
  172. /* If the scan is being started then reset the last node visitied
  173. pointer and bucket index. */
  174. if (!node)
  175. cache->last_bucket = 0;
  176. /* If there is a node visited last then check for another entry in
  177. the same bucket. Otherwise step to the next bucket. */
  178. if (node)
  179. {
  180. if (node->next)
  181. {
  182. /* There is a node which follows the last node returned.
  183. Step to that node and retun it. */
  184. return node->next;
  185. }
  186. else
  187. ++cache->last_bucket;
  188. }
  189. /* If the list isn't exhausted then search the buckets for other
  190. nodes. */
  191. if (cache->last_bucket < cache->size)
  192. {
  193. /* Scan the remainder of the buckets looking for an entry at
  194. the head of the list. Return the first item found. */
  195. while (cache->last_bucket < cache->size)
  196. if (cache->node_table[cache->last_bucket])
  197. return cache->node_table[cache->last_bucket];
  198. else
  199. ++cache->last_bucket;
  200. /* No further nodes were found in the hash table. */
  201. return NULL;
  202. }
  203. else
  204. return NULL;
  205. }
  206. /* Given KEY, return corresponding value for it in CACHE. Return NULL
  207. if the KEY is not recorded. */
  208. void *
  209. objc_hash_value_for_key (cache_ptr cache, const void *key)
  210. {
  211. node_ptr node = cache->node_table[(*cache->hash_func) (cache, key)];
  212. void *retval = NULL;
  213. if (node)
  214. do
  215. {
  216. if ((*cache->compare_func) (node->key, key))
  217. {
  218. retval = node->value;
  219. break;
  220. }
  221. else
  222. node = node->next;
  223. }
  224. while (! retval && node);
  225. return retval;
  226. }
  227. /* Given KEY, return YES if it exists in the CACHE. Return NO if it
  228. does not */
  229. BOOL
  230. objc_hash_is_key_in_hash (cache_ptr cache, const void *key)
  231. {
  232. node_ptr node = cache->node_table[(*cache->hash_func) (cache, key)];
  233. if (node)
  234. do
  235. {
  236. if ((*cache->compare_func)(node->key, key))
  237. return YES;
  238. else
  239. node = node->next;
  240. }
  241. while (node);
  242. return NO;
  243. }