htable.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. #include <ccan/htable/htable.h>
  2. #include <ccan/compiler/compiler.h>
  3. #include <stdint.h>
  4. #include <stdlib.h>
  5. #include <limits.h>
  6. #include <stdbool.h>
  7. #include <assert.h>
  8. /* This means a struct htable takes at least 512 bytes / 1k (32/64 bits). */
  9. #define HTABLE_BASE_BITS 7
  10. /* We use 0x1 as deleted marker. */
  11. #define HTABLE_DELETED (0x1)
  12. struct htable {
  13. size_t (*rehash)(const void *elem, void *priv);
  14. void *priv;
  15. unsigned int bits;
  16. size_t elems, deleted, max, max_with_deleted;
  17. /* These are the bits which are the same in all pointers. */
  18. uintptr_t common_mask, common_bits;
  19. uintptr_t perfect_bit;
  20. uintptr_t *table;
  21. };
  22. /* We clear out the bits which are always the same, and put metadata there. */
  23. static inline uintptr_t get_extra_ptr_bits(const struct htable *ht,
  24. uintptr_t e)
  25. {
  26. return e & ht->common_mask;
  27. }
  28. static inline void *get_raw_ptr(const struct htable *ht, uintptr_t e)
  29. {
  30. return (void *)((e & ~ht->common_mask) | ht->common_bits);
  31. }
  32. static inline uintptr_t make_hval(const struct htable *ht,
  33. const void *p, uintptr_t bits)
  34. {
  35. return ((uintptr_t)p & ~ht->common_mask) | bits;
  36. }
  37. static inline bool entry_is_valid(uintptr_t e)
  38. {
  39. return e > HTABLE_DELETED;
  40. }
  41. static inline uintptr_t get_hash_ptr_bits(const struct htable *ht,
  42. size_t hash)
  43. {
  44. /* Shuffling the extra bits (as specified in mask) down the
  45. * end is quite expensive. But the lower bits are redundant, so
  46. * we fold the value first. */
  47. return (hash ^ (hash >> ht->bits))
  48. & ht->common_mask & ~ht->perfect_bit;
  49. }
  50. struct htable *htable_new(size_t (*rehash)(const void *elem, void *priv),
  51. void *priv)
  52. {
  53. struct htable *ht = malloc(sizeof(struct htable));
  54. if (ht) {
  55. ht->bits = HTABLE_BASE_BITS;
  56. ht->rehash = rehash;
  57. ht->priv = priv;
  58. ht->elems = 0;
  59. ht->deleted = 0;
  60. ht->max = ((size_t)1 << ht->bits) * 3 / 4;
  61. ht->max_with_deleted = ((size_t)1 << ht->bits) * 9 / 10;
  62. /* This guarantees we enter update_common first add. */
  63. ht->common_mask = -1;
  64. ht->common_bits = 0;
  65. ht->perfect_bit = 0;
  66. ht->table = calloc(1 << ht->bits, sizeof(uintptr_t));
  67. if (!ht->table) {
  68. free(ht);
  69. ht = NULL;
  70. }
  71. }
  72. return ht;
  73. }
  74. void htable_free(const struct htable *ht)
  75. {
  76. free((void *)ht->table);
  77. free((void *)ht);
  78. }
  79. static size_t hash_bucket(const struct htable *ht, size_t h)
  80. {
  81. return h & ((1 << ht->bits)-1);
  82. }
  83. static void *htable_val(const struct htable *ht,
  84. struct htable_iter *i, size_t hash, uintptr_t perfect)
  85. {
  86. uintptr_t h2 = get_hash_ptr_bits(ht, hash) | perfect;
  87. while (ht->table[i->off]) {
  88. if (ht->table[i->off] != HTABLE_DELETED) {
  89. if (get_extra_ptr_bits(ht, ht->table[i->off]) == h2)
  90. return get_raw_ptr(ht, ht->table[i->off]);
  91. }
  92. i->off = (i->off + 1) & ((1 << ht->bits)-1);
  93. h2 &= ~perfect;
  94. }
  95. return NULL;
  96. }
  97. void *htable_firstval(const struct htable *ht,
  98. struct htable_iter *i, size_t hash)
  99. {
  100. i->off = hash_bucket(ht, hash);
  101. return htable_val(ht, i, hash, ht->perfect_bit);
  102. }
  103. void *htable_nextval(const struct htable *ht,
  104. struct htable_iter *i, size_t hash)
  105. {
  106. i->off = (i->off + 1) & ((1 << ht->bits)-1);
  107. return htable_val(ht, i, hash, 0);
  108. }
  109. void *htable_first(const struct htable *ht, struct htable_iter *i)
  110. {
  111. for (i->off = 0; i->off < (size_t)1 << ht->bits; i->off++) {
  112. if (entry_is_valid(ht->table[i->off]))
  113. return get_raw_ptr(ht, ht->table[i->off]);
  114. }
  115. return NULL;
  116. }
  117. void *htable_next(const struct htable *ht, struct htable_iter *i)
  118. {
  119. for (i->off++; i->off < (size_t)1 << ht->bits; i->off++) {
  120. if (entry_is_valid(ht->table[i->off]))
  121. return get_raw_ptr(ht, ht->table[i->off]);
  122. }
  123. return NULL;
  124. }
  125. /* This does not expand the hash table, that's up to caller. */
  126. static void ht_add(struct htable *ht, const void *new, size_t h)
  127. {
  128. size_t i;
  129. uintptr_t perfect = ht->perfect_bit;
  130. i = hash_bucket(ht, h);
  131. while (entry_is_valid(ht->table[i])) {
  132. perfect = 0;
  133. i = (i + 1) & ((1 << ht->bits)-1);
  134. }
  135. ht->table[i] = make_hval(ht, new, get_hash_ptr_bits(ht, h)|perfect);
  136. }
  137. static COLD bool double_table(struct htable *ht)
  138. {
  139. unsigned int i;
  140. size_t oldnum = (size_t)1 << ht->bits;
  141. uintptr_t *oldtable, e;
  142. oldtable = ht->table;
  143. ht->table = calloc(1 << (ht->bits+1), sizeof(size_t));
  144. if (!ht->table) {
  145. ht->table = oldtable;
  146. return false;
  147. }
  148. ht->bits++;
  149. ht->max *= 2;
  150. ht->max_with_deleted *= 2;
  151. /* If we lost our "perfect bit", get it back now. */
  152. if (!ht->perfect_bit && ht->common_mask) {
  153. for (i = 0; i < sizeof(ht->common_mask) * CHAR_BIT; i++) {
  154. if (ht->common_mask & ((size_t)1 << i)) {
  155. ht->perfect_bit = (size_t)1 << i;
  156. break;
  157. }
  158. }
  159. }
  160. for (i = 0; i < oldnum; i++) {
  161. if (entry_is_valid(e = oldtable[i])) {
  162. void *p = get_raw_ptr(ht, e);
  163. ht_add(ht, p, ht->rehash(p, ht->priv));
  164. }
  165. }
  166. ht->deleted = 0;
  167. free(oldtable);
  168. return true;
  169. }
  170. static COLD void rehash_table(struct htable *ht)
  171. {
  172. size_t start, i;
  173. uintptr_t e;
  174. /* Beware wrap cases: we need to start from first empty bucket. */
  175. for (start = 0; ht->table[start]; start++);
  176. for (i = 0; i < (size_t)1 << ht->bits; i++) {
  177. size_t h = (i + start) & ((1 << ht->bits)-1);
  178. e = ht->table[h];
  179. if (!e)
  180. continue;
  181. if (e == HTABLE_DELETED)
  182. ht->table[h] = 0;
  183. else if (!(e & ht->perfect_bit)) {
  184. void *p = get_raw_ptr(ht, e);
  185. ht->table[h] = 0;
  186. ht_add(ht, p, ht->rehash(p, ht->priv));
  187. }
  188. }
  189. ht->deleted = 0;
  190. }
  191. /* We stole some bits, now we need to put them back... */
  192. static COLD void update_common(struct htable *ht, const void *p)
  193. {
  194. unsigned int i;
  195. uintptr_t maskdiff, bitsdiff;
  196. if (ht->elems == 0) {
  197. ht->common_mask = -1;
  198. ht->common_bits = (uintptr_t)p;
  199. ht->perfect_bit = 1;
  200. return;
  201. }
  202. /* Find bits which are unequal to old common set. */
  203. maskdiff = ht->common_bits ^ ((uintptr_t)p & ht->common_mask);
  204. /* These are the bits which go there in existing entries. */
  205. bitsdiff = ht->common_bits & maskdiff;
  206. for (i = 0; i < (size_t)1 << ht->bits; i++) {
  207. if (!entry_is_valid(ht->table[i]))
  208. continue;
  209. /* Clear the bits no longer in the mask, set them as
  210. * expected. */
  211. ht->table[i] &= ~maskdiff;
  212. ht->table[i] |= bitsdiff;
  213. }
  214. /* Take away those bits from our mask, bits and perfect bit. */
  215. ht->common_mask &= ~maskdiff;
  216. ht->common_bits &= ~maskdiff;
  217. ht->perfect_bit &= ~maskdiff;
  218. }
  219. bool htable_add(struct htable *ht, size_t hash, const void *p)
  220. {
  221. if (ht->elems+1 > ht->max && !double_table(ht))
  222. return false;
  223. if (ht->elems+1 + ht->deleted > ht->max_with_deleted)
  224. rehash_table(ht);
  225. assert(p);
  226. if (((uintptr_t)p & ht->common_mask) != ht->common_bits)
  227. update_common(ht, p);
  228. ht_add(ht, p, hash);
  229. ht->elems++;
  230. return true;
  231. }
  232. bool htable_del(struct htable *ht, size_t h, const void *p)
  233. {
  234. struct htable_iter i;
  235. void *c;
  236. for (c = htable_firstval(ht,&i,h); c; c = htable_nextval(ht,&i,h)) {
  237. if (c == p) {
  238. htable_delval(ht, &i);
  239. return true;
  240. }
  241. }
  242. return false;
  243. }
  244. void htable_delval(struct htable *ht, struct htable_iter *i)
  245. {
  246. assert(i->off < (size_t)1 << ht->bits);
  247. assert(entry_is_valid(ht->table[i->off]));
  248. ht->elems--;
  249. ht->table[i->off] = HTABLE_DELETED;
  250. ht->deleted++;
  251. }