base_hash.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. #include "fitz-internal.h"
  2. /*
  3. Simple hashtable with open addressing linear probe.
  4. Unlike text book examples, removing entries works
  5. correctly in this implementation, so it wont start
  6. exhibiting bad behaviour if entries are inserted
  7. and removed frequently.
  8. */
  9. enum { MAX_KEY_LEN = 48 };
  10. typedef struct fz_hash_entry_s fz_hash_entry;
  11. struct fz_hash_entry_s
  12. {
  13. unsigned char key[MAX_KEY_LEN];
  14. void *val;
  15. };
  16. struct fz_hash_table_s
  17. {
  18. int keylen;
  19. int size;
  20. int load;
  21. int lock; /* -1 or the lock used to protect this hash table */
  22. fz_hash_entry *ents;
  23. };
  24. static unsigned hash(unsigned char *s, int len)
  25. {
  26. unsigned val = 0;
  27. int i;
  28. for (i = 0; i < len; i++)
  29. {
  30. val += s[i];
  31. val += (val << 10);
  32. val ^= (val >> 6);
  33. }
  34. val += (val << 3);
  35. val ^= (val >> 11);
  36. val += (val << 15);
  37. return val;
  38. }
  39. fz_hash_table *
  40. fz_new_hash_table(fz_context *ctx, int initialsize, int keylen, int lock)
  41. {
  42. fz_hash_table *table;
  43. assert(keylen <= MAX_KEY_LEN);
  44. table = fz_malloc_struct(ctx, fz_hash_table);
  45. table->keylen = keylen;
  46. table->size = initialsize;
  47. table->load = 0;
  48. table->lock = lock;
  49. fz_try(ctx)
  50. {
  51. table->ents = fz_malloc_array(ctx, table->size, sizeof(fz_hash_entry));
  52. memset(table->ents, 0, sizeof(fz_hash_entry) * table->size);
  53. }
  54. fz_catch(ctx)
  55. {
  56. fz_free(ctx, table);
  57. fz_rethrow(ctx);
  58. }
  59. return table;
  60. }
  61. void
  62. fz_empty_hash(fz_context *ctx, fz_hash_table *table)
  63. {
  64. table->load = 0;
  65. memset(table->ents, 0, sizeof(fz_hash_entry) * table->size);
  66. }
  67. int
  68. fz_hash_len(fz_context *ctx, fz_hash_table *table)
  69. {
  70. return table->size;
  71. }
  72. void *
  73. fz_hash_get_key(fz_context *ctx, fz_hash_table *table, int idx)
  74. {
  75. return table->ents[idx].key;
  76. }
  77. void *
  78. fz_hash_get_val(fz_context *ctx, fz_hash_table *table, int idx)
  79. {
  80. return table->ents[idx].val;
  81. }
  82. void
  83. fz_free_hash(fz_context *ctx, fz_hash_table *table)
  84. {
  85. fz_free(ctx, table->ents);
  86. fz_free(ctx, table);
  87. }
  88. static void *
  89. do_hash_insert(fz_context *ctx, fz_hash_table *table, void *key, void *val)
  90. {
  91. fz_hash_entry *ents;
  92. unsigned size;
  93. unsigned pos;
  94. ents = table->ents;
  95. size = table->size;
  96. pos = hash(key, table->keylen) % size;
  97. if (table->lock >= 0)
  98. fz_assert_lock_held(ctx, table->lock);
  99. while (1)
  100. {
  101. if (!ents[pos].val)
  102. {
  103. memcpy(ents[pos].key, key, table->keylen);
  104. ents[pos].val = val;
  105. table->load ++;
  106. return NULL;
  107. }
  108. if (memcmp(key, ents[pos].key, table->keylen) == 0)
  109. {
  110. fz_warn(ctx, "assert: overwrite hash slot");
  111. return ents[pos].val;
  112. }
  113. pos = (pos + 1) % size;
  114. }
  115. }
  116. static void
  117. fz_resize_hash(fz_context *ctx, fz_hash_table *table, int newsize)
  118. {
  119. fz_hash_entry *oldents = table->ents;
  120. fz_hash_entry *newents;
  121. int oldsize = table->size;
  122. int oldload = table->load;
  123. int i;
  124. if (newsize < oldload * 8 / 10)
  125. {
  126. fz_warn(ctx, "assert: resize hash too small");
  127. return;
  128. }
  129. if (table->lock == FZ_LOCK_ALLOC)
  130. fz_unlock(ctx, FZ_LOCK_ALLOC);
  131. newents = fz_malloc_array(ctx, newsize, sizeof(fz_hash_entry));
  132. if (table->lock == FZ_LOCK_ALLOC)
  133. fz_lock(ctx, FZ_LOCK_ALLOC);
  134. if (table->lock >= 0)
  135. {
  136. if (table->size >= newsize)
  137. {
  138. /* Someone else fixed it before we could lock! */
  139. fz_unlock(ctx, table->lock);
  140. fz_free(ctx, newents);
  141. return;
  142. }
  143. }
  144. table->ents = newents;
  145. memset(table->ents, 0, sizeof(fz_hash_entry) * newsize);
  146. table->size = newsize;
  147. table->load = 0;
  148. for (i = 0; i < oldsize; i++)
  149. {
  150. if (oldents[i].val)
  151. {
  152. do_hash_insert(ctx, table, oldents[i].key, oldents[i].val);
  153. }
  154. }
  155. if (table->lock == FZ_LOCK_ALLOC)
  156. fz_unlock(ctx, FZ_LOCK_ALLOC);
  157. fz_free(ctx, oldents);
  158. if (table->lock == FZ_LOCK_ALLOC)
  159. fz_lock(ctx, FZ_LOCK_ALLOC);
  160. }
  161. void *
  162. fz_hash_find(fz_context *ctx, fz_hash_table *table, void *key)
  163. {
  164. fz_hash_entry *ents = table->ents;
  165. unsigned size = table->size;
  166. unsigned pos = hash(key, table->keylen) % size;
  167. if (table->lock >= 0)
  168. fz_assert_lock_held(ctx, table->lock);
  169. while (1)
  170. {
  171. if (!ents[pos].val)
  172. return NULL;
  173. if (memcmp(key, ents[pos].key, table->keylen) == 0)
  174. return ents[pos].val;
  175. pos = (pos + 1) % size;
  176. }
  177. }
  178. void *
  179. fz_hash_insert(fz_context *ctx, fz_hash_table *table, void *key, void *val)
  180. {
  181. if (table->load > table->size * 8 / 10)
  182. {
  183. fz_resize_hash(ctx, table, table->size * 2);
  184. }
  185. return do_hash_insert(ctx, table, key, val);
  186. }
  187. void
  188. fz_hash_remove(fz_context *ctx, fz_hash_table *table, void *key)
  189. {
  190. fz_hash_entry *ents = table->ents;
  191. unsigned size = table->size;
  192. unsigned pos = hash(key, table->keylen) % size;
  193. unsigned hole, look, code;
  194. if (table->lock >= 0)
  195. fz_assert_lock_held(ctx, table->lock);
  196. while (1)
  197. {
  198. if (!ents[pos].val)
  199. {
  200. fz_warn(ctx, "assert: remove non-existent hash entry");
  201. return;
  202. }
  203. if (memcmp(key, ents[pos].key, table->keylen) == 0)
  204. {
  205. ents[pos].val = NULL;
  206. hole = pos;
  207. look = (hole + 1) % size;
  208. while (ents[look].val)
  209. {
  210. code = hash(ents[look].key, table->keylen) % size;
  211. if ((code <= hole && hole < look) ||
  212. (look < code && code <= hole) ||
  213. (hole < look && look < code))
  214. {
  215. ents[hole] = ents[look];
  216. ents[look].val = NULL;
  217. hole = look;
  218. }
  219. look = (look + 1) % size;
  220. }
  221. table->load --;
  222. return;
  223. }
  224. pos = (pos + 1) % size;
  225. }
  226. }
  227. #ifndef NDEBUG
  228. void
  229. fz_print_hash(fz_context *ctx, FILE *out, fz_hash_table *table)
  230. {
  231. int i, k;
  232. fprintf(out, "cache load %d / %d\n", table->load, table->size);
  233. for (i = 0; i < table->size; i++)
  234. {
  235. if (!table->ents[i].val)
  236. fprintf(out, "table % 4d: empty\n", i);
  237. else
  238. {
  239. fprintf(out, "table % 4d: key=", i);
  240. for (k = 0; k < MAX_KEY_LEN; k++)
  241. fprintf(out, "%02x", ((char*)table->ents[i].key)[k]);
  242. fprintf(out, " val=$%p\n", table->ents[i].val);
  243. }
  244. }
  245. }
  246. #endif