You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

linkhash.c 5.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * $Id: linkhash.c,v 1.4 2006/01/26 02:16:28 mclark Exp $
  3. *
  4. * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
  5. * Michael Clark <michael@metaparadigm.com>
  6. * Copyright (c) 2009 Hewlett-Packard Development Company, L.P.
  7. *
  8. * This library is free software; you can redistribute it and/or modify
  9. * it under the terms of the MIT license. See COPYING for details.
  10. *
  11. */
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include <stdarg.h>
  16. #include <stddef.h>
  17. #include <limits.h>
  18. #include "linkhash.h"
  19. void lh_abort(const char *msg, ...)
  20. {
  21. va_list ap;
  22. va_start(ap, msg);
  23. vprintf(msg, ap);
  24. va_end(ap);
  25. exit(1);
  26. }
  27. unsigned long lh_ptr_hash(const void *k)
  28. {
  29. /* CAW: refactored to be 64bit nice */
  30. return (unsigned long)((((ptrdiff_t)k * LH_PRIME) >> 4) & ULONG_MAX);
  31. }
  32. int lh_ptr_equal(const void *k1, const void *k2)
  33. {
  34. return (k1 == k2);
  35. }
  36. unsigned long lh_char_hash(const void *k)
  37. {
  38. unsigned int h = 0;
  39. const char* data = (const char*)k;
  40. while( *data!=0 ) h = h*129 + (unsigned int)(*data++) + LH_PRIME;
  41. return h;
  42. }
  43. int lh_char_equal(const void *k1, const void *k2)
  44. {
  45. return (strcmp((const char*)k1, (const char*)k2) == 0);
  46. }
  47. struct lh_table* lh_table_new(int size, const char *name,
  48. lh_entry_free_fn *free_fn,
  49. lh_hash_fn *hash_fn,
  50. lh_equal_fn *equal_fn)
  51. {
  52. int i;
  53. struct lh_table *t;
  54. t = (struct lh_table*)calloc(1, sizeof(struct lh_table));
  55. if(!t) {
  56. goto nomem;
  57. }
  58. t->count = 0;
  59. t->size = size;
  60. t->name = name;
  61. t->table = (struct lh_entry*)calloc(size, sizeof(struct lh_entry));
  62. if(!t->table) {
  63. goto notablemem;
  64. }
  65. t->free_fn = free_fn;
  66. t->hash_fn = hash_fn;
  67. t->equal_fn = equal_fn;
  68. for(i = 0; i < size; i++) t->table[i].k = LH_EMPTY;
  69. return t;
  70. notablemem:
  71. free( t );
  72. nomem:
  73. return NULL;
  74. }
  75. struct lh_table* lh_kchar_table_new(int size, const char *name,
  76. lh_entry_free_fn *free_fn)
  77. {
  78. return lh_table_new(size, name, free_fn, lh_char_hash, lh_char_equal);
  79. }
  80. struct lh_table* lh_kptr_table_new(int size, const char *name,
  81. lh_entry_free_fn *free_fn)
  82. {
  83. return lh_table_new(size, name, free_fn, lh_ptr_hash, lh_ptr_equal);
  84. }
  85. int lh_table_resize(struct lh_table *t, int new_size)
  86. {
  87. struct lh_table *new_t;
  88. new_t = lh_table_new(new_size, t->name, NULL, t->hash_fn, t->equal_fn);
  89. if ( new_t == NULL ) {
  90. goto nonewtable;
  91. }
  92. for ( struct lh_entry * ent = t->head; ent != NULL; ent = ent->next ) {
  93. int rc = lh_table_insert(new_t, ent->k, ent->v);
  94. if ( rc != 0 ) {
  95. goto noinsert;
  96. }
  97. }
  98. free(t->table);
  99. t->table = new_t->table;
  100. t->size = new_size;
  101. t->head = new_t->head;
  102. t->tail = new_t->tail;
  103. t->resizes++;
  104. free(new_t);
  105. return 0;
  106. noinsert:
  107. free( new_t->table );
  108. free( new_t );
  109. nonewtable:
  110. return -1;
  111. }
  112. void lh_table_free(struct lh_table *t)
  113. {
  114. struct lh_entry *c;
  115. for(c = t->head; c != NULL; c = c->next) {
  116. if(t->free_fn) {
  117. t->free_fn(c);
  118. }
  119. }
  120. free(t->table);
  121. free(t);
  122. }
  123. int lh_table_insert(struct lh_table *t, void *k, const void *v)
  124. {
  125. unsigned long h, n;
  126. t->inserts++;
  127. if(t->count >= t->size * LH_LOAD_FACTOR) {
  128. int rc = lh_table_resize(t, t->size * 2);
  129. if ( rc != 0 ) {
  130. return -1;
  131. }
  132. }
  133. h = t->hash_fn(k);
  134. n = h % t->size;
  135. while( 1 ) {
  136. if(t->table[n].k == LH_EMPTY || t->table[n].k == LH_FREED) break;
  137. t->collisions++;
  138. if(++n == t->size) n = 0;
  139. }
  140. t->table[n].k = k;
  141. t->table[n].v = v;
  142. t->count++;
  143. if(t->head == NULL) {
  144. t->head = t->tail = &t->table[n];
  145. t->table[n].next = t->table[n].prev = NULL;
  146. } else {
  147. t->tail->next = &t->table[n];
  148. t->table[n].prev = t->tail;
  149. t->table[n].next = NULL;
  150. t->tail = &t->table[n];
  151. }
  152. return 0;
  153. }
  154. struct lh_entry* lh_table_lookup_entry(struct lh_table *t, const void *k)
  155. {
  156. unsigned long h = t->hash_fn(k);
  157. unsigned long n = h % t->size;
  158. int count = 0;
  159. t->lookups++;
  160. while( count < t->size ) {
  161. if(t->table[n].k == LH_EMPTY) return NULL;
  162. if(t->table[n].k != LH_FREED &&
  163. t->equal_fn(t->table[n].k, k)) return &t->table[n];
  164. if(++n == t->size) n = 0;
  165. count++;
  166. }
  167. return NULL;
  168. }
  169. const void* lh_table_lookup(struct lh_table *t, const void *k)
  170. {
  171. void *result;
  172. lh_table_lookup_ex(t, k, &result);
  173. return result;
  174. }
  175. json_bool lh_table_lookup_ex(struct lh_table* t, const void* k, void **v)
  176. {
  177. struct lh_entry *e = lh_table_lookup_entry(t, k);
  178. if (e != NULL) {
  179. if (v != NULL) *v = (void *)e->v;
  180. return TRUE; /* key found */
  181. }
  182. if (v != NULL) *v = NULL;
  183. return FALSE; /* key not found */
  184. }
  185. int lh_table_delete_entry(struct lh_table *t, struct lh_entry *e)
  186. {
  187. ptrdiff_t n = (ptrdiff_t)(e - t->table); /* CAW: fixed to be 64bit nice, still need the crazy negative case... */
  188. /* CAW: this is bad, really bad, maybe stack goes other direction on this machine... */
  189. if(n < 0) { return -2; }
  190. if(t->table[n].k == LH_EMPTY || t->table[n].k == LH_FREED) return -1;
  191. t->count--;
  192. if(t->free_fn) t->free_fn(e);
  193. t->table[n].v = NULL;
  194. t->table[n].k = LH_FREED;
  195. if(t->tail == &t->table[n] && t->head == &t->table[n]) {
  196. t->head = t->tail = NULL;
  197. } else if (t->head == &t->table[n]) {
  198. t->head->next->prev = NULL;
  199. t->head = t->head->next;
  200. } else if (t->tail == &t->table[n]) {
  201. t->tail->prev->next = NULL;
  202. t->tail = t->tail->prev;
  203. } else {
  204. t->table[n].prev->next = t->table[n].next;
  205. t->table[n].next->prev = t->table[n].prev;
  206. }
  207. t->table[n].next = t->table[n].prev = NULL;
  208. return 0;
  209. }
  210. int lh_table_delete(struct lh_table *t, const void *k)
  211. {
  212. struct lh_entry *e = lh_table_lookup_entry(t, k);
  213. if(!e) return -1;
  214. return lh_table_delete_entry(t, e);
  215. }