hashtable.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Routines to provide a memory-efficient hashtable.
  3. *
  4. * Copyright (C) 2007-2009 Wayne Davison
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, visit the http://fsf.org website.
  18. */
  19. #include "rsync.h"
  20. #define HASH_LOAD_LIMIT(size) ((size)*3/4)
  21. struct hashtable *hashtable_create(int size, int key64)
  22. {
  23. struct hashtable *tbl;
  24. int node_size = key64 ? sizeof (struct ht_int64_node)
  25. : sizeof (struct ht_int32_node);
  26. /* Pick a power of 2 that can hold the requested size. */
  27. if (size & (size-1) || size < 16) {
  28. int req = size;
  29. size = 16;
  30. while (size < req)
  31. size *= 2;
  32. }
  33. if (!(tbl = new(struct hashtable))
  34. || !(tbl->nodes = new_array0(char, size * node_size)))
  35. out_of_memory("hashtable_create");
  36. tbl->size = size;
  37. tbl->entries = 0;
  38. tbl->node_size = node_size;
  39. tbl->key64 = key64 ? 1 : 0;
  40. return tbl;
  41. }
  42. void hashtable_destroy(struct hashtable *tbl)
  43. {
  44. free(tbl->nodes);
  45. free(tbl);
  46. }
  47. /* This returns the node for the indicated key, either newly created or
  48. * already existing. Returns NULL if not allocating and not found. */
  49. void *hashtable_find(struct hashtable *tbl, int64 key, int allocate_if_missing)
  50. {
  51. int key64 = tbl->key64;
  52. struct ht_int32_node *node;
  53. uint32 ndx;
  54. if (key64 ? key == 0 : (int32)key == 0) {
  55. rprintf(FERROR, "Internal hashtable error: illegal key supplied!\n");
  56. exit_cleanup(RERR_MESSAGEIO);
  57. }
  58. if (allocate_if_missing && tbl->entries > HASH_LOAD_LIMIT(tbl->size)) {
  59. void *old_nodes = tbl->nodes;
  60. int size = tbl->size * 2;
  61. int i;
  62. if (!(tbl->nodes = new_array0(char, size * tbl->node_size)))
  63. out_of_memory("hashtable_node");
  64. tbl->size = size;
  65. tbl->entries = 0;
  66. for (i = size / 2; i-- > 0; ) {
  67. struct ht_int32_node *move_node = HT_NODE(tbl, old_nodes, i);
  68. int64 move_key = HT_KEY(move_node, key64);
  69. if (move_key == 0)
  70. continue;
  71. node = hashtable_find(tbl, move_key, 1);
  72. node->data = move_node->data;
  73. }
  74. free(old_nodes);
  75. }
  76. if (!key64) {
  77. /* Based on Jenkins One-at-a-time hash. */
  78. uchar buf[4], *keyp = buf;
  79. int i;
  80. SIVALu(buf, 0, key);
  81. for (ndx = 0, i = 0; i < 4; i++) {
  82. ndx += keyp[i];
  83. ndx += (ndx << 10);
  84. ndx ^= (ndx >> 6);
  85. }
  86. ndx += (ndx << 3);
  87. ndx ^= (ndx >> 11);
  88. ndx += (ndx << 15);
  89. } else {
  90. /* Based on Jenkins hashword() from lookup3.c. */
  91. uint32 a, b, c;
  92. /* Set up the internal state */
  93. a = b = c = 0xdeadbeef + (8 << 2);
  94. #define rot(x,k) (((x)<<(k)) ^ ((x)>>(32-(k))))
  95. #if SIZEOF_INT64 >= 8
  96. b += (uint32)(key >> 32);
  97. #endif
  98. a += (uint32)key;
  99. c ^= b; c -= rot(b, 14);
  100. a ^= c; a -= rot(c, 11);
  101. b ^= a; b -= rot(a, 25);
  102. c ^= b; c -= rot(b, 16);
  103. a ^= c; a -= rot(c, 4);
  104. b ^= a; b -= rot(a, 14);
  105. c ^= b; c -= rot(b, 24);
  106. #undef rot
  107. ndx = c;
  108. }
  109. /* If it already exists, return the node. If we're not
  110. * allocating, return NULL if the key is not found. */
  111. while (1) {
  112. int64 nkey;
  113. ndx &= tbl->size - 1;
  114. node = HT_NODE(tbl, tbl->nodes, ndx);
  115. nkey = HT_KEY(node, key64);
  116. if (nkey == key)
  117. return node;
  118. if (nkey == 0) {
  119. if (!allocate_if_missing)
  120. return NULL;
  121. break;
  122. }
  123. ndx++;
  124. }
  125. /* Take over this empty spot and then return the node. */
  126. if (key64)
  127. ((struct ht_int64_node*)node)->key = key;
  128. else
  129. node->key = (int32)key;
  130. tbl->entries++;
  131. return node;
  132. }