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

518 lines
15 KiB

  1. /* The implementation of the hash table (_Py_hashtable_t) is based on the cfuhash
  2. project:
  3. http://sourceforge.net/projects/libcfu/
  4. Copyright of cfuhash:
  5. ----------------------------------
  6. Creation date: 2005-06-24 21:22:40
  7. Authors: Don
  8. Change log:
  9. Copyright (c) 2005 Don Owens
  10. All rights reserved.
  11. This code is released under the BSD license:
  12. Redistribution and use in source and binary forms, with or without
  13. modification, are permitted provided that the following conditions
  14. are met:
  15. * Redistributions of source code must retain the above copyright
  16. notice, this list of conditions and the following disclaimer.
  17. * Redistributions in binary form must reproduce the above
  18. copyright notice, this list of conditions and the following
  19. disclaimer in the documentation and/or other materials provided
  20. with the distribution.
  21. * Neither the name of the author nor the names of its
  22. contributors may be used to endorse or promote products derived
  23. from this software without specific prior written permission.
  24. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  27. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  28. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  29. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  30. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  31. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  33. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  34. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  35. OF THE POSSIBILITY OF SUCH DAMAGE.
  36. ----------------------------------
  37. */
  38. #include "Python.h"
  39. #include "hashtable.h"
  40. #define HASHTABLE_MIN_SIZE 16
  41. #define HASHTABLE_HIGH 0.50
  42. #define HASHTABLE_LOW 0.10
  43. #define HASHTABLE_REHASH_FACTOR 2.0 / (HASHTABLE_LOW + HASHTABLE_HIGH)
  44. #define BUCKETS_HEAD(SLIST) \
  45. ((_Py_hashtable_entry_t *)_Py_SLIST_HEAD(&(SLIST)))
  46. #define TABLE_HEAD(HT, BUCKET) \
  47. ((_Py_hashtable_entry_t *)_Py_SLIST_HEAD(&(HT)->buckets[BUCKET]))
  48. #define ENTRY_NEXT(ENTRY) \
  49. ((_Py_hashtable_entry_t *)_Py_SLIST_ITEM_NEXT(ENTRY))
  50. #define HASHTABLE_ITEM_SIZE(HT) \
  51. (sizeof(_Py_hashtable_entry_t) + (HT)->data_size)
  52. /* Forward declaration */
  53. static void hashtable_rehash(_Py_hashtable_t *ht);
  54. static void
  55. _Py_slist_init(_Py_slist_t *list)
  56. {
  57. list->head = NULL;
  58. }
  59. static void
  60. _Py_slist_prepend(_Py_slist_t *list, _Py_slist_item_t *item)
  61. {
  62. item->next = list->head;
  63. list->head = item;
  64. }
  65. static void
  66. _Py_slist_remove(_Py_slist_t *list, _Py_slist_item_t *previous,
  67. _Py_slist_item_t *item)
  68. {
  69. if (previous != NULL)
  70. previous->next = item->next;
  71. else
  72. list->head = item->next;
  73. }
  74. Py_uhash_t
  75. _Py_hashtable_hash_int(const void *key)
  76. {
  77. return (Py_uhash_t)key;
  78. }
  79. Py_uhash_t
  80. _Py_hashtable_hash_ptr(const void *key)
  81. {
  82. return (Py_uhash_t)_Py_HashPointer((void *)key);
  83. }
  84. int
  85. _Py_hashtable_compare_direct(const void *key, const _Py_hashtable_entry_t *entry)
  86. {
  87. return entry->key == key;
  88. }
  89. /* makes sure the real size of the buckets array is a power of 2 */
  90. static size_t
  91. round_size(size_t s)
  92. {
  93. size_t i;
  94. if (s < HASHTABLE_MIN_SIZE)
  95. return HASHTABLE_MIN_SIZE;
  96. i = 1;
  97. while (i < s)
  98. i <<= 1;
  99. return i;
  100. }
  101. _Py_hashtable_t *
  102. _Py_hashtable_new_full(size_t data_size, size_t init_size,
  103. _Py_hashtable_hash_func hash_func,
  104. _Py_hashtable_compare_func compare_func,
  105. _Py_hashtable_copy_data_func copy_data_func,
  106. _Py_hashtable_free_data_func free_data_func,
  107. _Py_hashtable_get_data_size_func get_data_size_func,
  108. _Py_hashtable_allocator_t *allocator)
  109. {
  110. _Py_hashtable_t *ht;
  111. size_t buckets_size;
  112. _Py_hashtable_allocator_t alloc;
  113. if (allocator == NULL) {
  114. alloc.malloc = PyMem_RawMalloc;
  115. alloc.free = PyMem_RawFree;
  116. }
  117. else
  118. alloc = *allocator;
  119. ht = (_Py_hashtable_t *)alloc.malloc(sizeof(_Py_hashtable_t));
  120. if (ht == NULL)
  121. return ht;
  122. ht->num_buckets = round_size(init_size);
  123. ht->entries = 0;
  124. ht->data_size = data_size;
  125. buckets_size = ht->num_buckets * sizeof(ht->buckets[0]);
  126. ht->buckets = alloc.malloc(buckets_size);
  127. if (ht->buckets == NULL) {
  128. alloc.free(ht);
  129. return NULL;
  130. }
  131. memset(ht->buckets, 0, buckets_size);
  132. ht->hash_func = hash_func;
  133. ht->compare_func = compare_func;
  134. ht->copy_data_func = copy_data_func;
  135. ht->free_data_func = free_data_func;
  136. ht->get_data_size_func = get_data_size_func;
  137. ht->alloc = alloc;
  138. return ht;
  139. }
  140. _Py_hashtable_t *
  141. _Py_hashtable_new(size_t data_size,
  142. _Py_hashtable_hash_func hash_func,
  143. _Py_hashtable_compare_func compare_func)
  144. {
  145. return _Py_hashtable_new_full(data_size, HASHTABLE_MIN_SIZE,
  146. hash_func, compare_func,
  147. NULL, NULL, NULL, NULL);
  148. }
  149. size_t
  150. _Py_hashtable_size(_Py_hashtable_t *ht)
  151. {
  152. size_t size;
  153. size_t hv;
  154. size = sizeof(_Py_hashtable_t);
  155. /* buckets */
  156. size += ht->num_buckets * sizeof(_Py_hashtable_entry_t *);
  157. /* entries */
  158. size += ht->entries * HASHTABLE_ITEM_SIZE(ht);
  159. /* data linked from entries */
  160. if (ht->get_data_size_func) {
  161. for (hv = 0; hv < ht->num_buckets; hv++) {
  162. _Py_hashtable_entry_t *entry;
  163. for (entry = TABLE_HEAD(ht, hv); entry; entry = ENTRY_NEXT(entry)) {
  164. void *data;
  165. data = _Py_HASHTABLE_ENTRY_DATA_AS_VOID_P(entry);
  166. size += ht->get_data_size_func(data);
  167. }
  168. }
  169. }
  170. return size;
  171. }
  172. #ifdef Py_DEBUG
  173. void
  174. _Py_hashtable_print_stats(_Py_hashtable_t *ht)
  175. {
  176. size_t size;
  177. size_t chain_len, max_chain_len, total_chain_len, nchains;
  178. _Py_hashtable_entry_t *entry;
  179. size_t hv;
  180. double load;
  181. size = _Py_hashtable_size(ht);
  182. load = (double)ht->entries / ht->num_buckets;
  183. max_chain_len = 0;
  184. total_chain_len = 0;
  185. nchains = 0;
  186. for (hv = 0; hv < ht->num_buckets; hv++) {
  187. entry = TABLE_HEAD(ht, hv);
  188. if (entry != NULL) {
  189. chain_len = 0;
  190. for (; entry; entry = ENTRY_NEXT(entry)) {
  191. chain_len++;
  192. }
  193. if (chain_len > max_chain_len)
  194. max_chain_len = chain_len;
  195. total_chain_len += chain_len;
  196. nchains++;
  197. }
  198. }
  199. printf("hash table %p: entries=%zu/%zu (%.0f%%), ",
  200. ht, ht->entries, ht->num_buckets, load * 100.0);
  201. if (nchains)
  202. printf("avg_chain_len=%.1f, ", (double)total_chain_len / nchains);
  203. printf("max_chain_len=%zu, %zu kB\n",
  204. max_chain_len, size / 1024);
  205. }
  206. #endif
  207. /* Get an entry. Return NULL if the key does not exist. */
  208. _Py_hashtable_entry_t *
  209. _Py_hashtable_get_entry(_Py_hashtable_t *ht, const void *key)
  210. {
  211. Py_uhash_t key_hash;
  212. size_t index;
  213. _Py_hashtable_entry_t *entry;
  214. key_hash = ht->hash_func(key);
  215. index = key_hash & (ht->num_buckets - 1);
  216. for (entry = TABLE_HEAD(ht, index); entry != NULL; entry = ENTRY_NEXT(entry)) {
  217. if (entry->key_hash == key_hash && ht->compare_func(key, entry))
  218. break;
  219. }
  220. return entry;
  221. }
  222. static int
  223. _hashtable_pop_entry(_Py_hashtable_t *ht, const void *key, void *data, size_t data_size)
  224. {
  225. Py_uhash_t key_hash;
  226. size_t index;
  227. _Py_hashtable_entry_t *entry, *previous;
  228. key_hash = ht->hash_func(key);
  229. index = key_hash & (ht->num_buckets - 1);
  230. previous = NULL;
  231. for (entry = TABLE_HEAD(ht, index); entry != NULL; entry = ENTRY_NEXT(entry)) {
  232. if (entry->key_hash == key_hash && ht->compare_func(key, entry))
  233. break;
  234. previous = entry;
  235. }
  236. if (entry == NULL)
  237. return 0;
  238. _Py_slist_remove(&ht->buckets[index], (_Py_slist_item_t *)previous,
  239. (_Py_slist_item_t *)entry);
  240. ht->entries--;
  241. if (data != NULL)
  242. _Py_HASHTABLE_ENTRY_READ_DATA(ht, data, data_size, entry);
  243. ht->alloc.free(entry);
  244. if ((float)ht->entries / (float)ht->num_buckets < HASHTABLE_LOW)
  245. hashtable_rehash(ht);
  246. return 1;
  247. }
  248. /* Add a new entry to the hash. The key must not be present in the hash table.
  249. Return 0 on success, -1 on memory error. */
  250. int
  251. _Py_hashtable_set(_Py_hashtable_t *ht, const void *key,
  252. void *data, size_t data_size)
  253. {
  254. Py_uhash_t key_hash;
  255. size_t index;
  256. _Py_hashtable_entry_t *entry;
  257. assert(data != NULL || data_size == 0);
  258. #ifndef NDEBUG
  259. /* Don't write the assertion on a single line because it is interesting
  260. to know the duplicated entry if the assertion failed. The entry can
  261. be read using a debugger. */
  262. entry = _Py_hashtable_get_entry(ht, key);
  263. assert(entry == NULL);
  264. #endif
  265. key_hash = ht->hash_func(key);
  266. index = key_hash & (ht->num_buckets - 1);
  267. entry = ht->alloc.malloc(HASHTABLE_ITEM_SIZE(ht));
  268. if (entry == NULL) {
  269. /* memory allocation failed */
  270. return -1;
  271. }
  272. entry->key = (void *)key;
  273. entry->key_hash = key_hash;
  274. assert(data_size == ht->data_size);
  275. memcpy(_PY_HASHTABLE_ENTRY_DATA(entry), data, data_size);
  276. _Py_slist_prepend(&ht->buckets[index], (_Py_slist_item_t*)entry);
  277. ht->entries++;
  278. if ((float)ht->entries / (float)ht->num_buckets > HASHTABLE_HIGH)
  279. hashtable_rehash(ht);
  280. return 0;
  281. }
  282. /* Get data from an entry. Copy entry data into data and return 1 if the entry
  283. exists, return 0 if the entry does not exist. */
  284. int
  285. _Py_hashtable_get(_Py_hashtable_t *ht, const void *key, void *data, size_t data_size)
  286. {
  287. _Py_hashtable_entry_t *entry;
  288. assert(data != NULL);
  289. entry = _Py_hashtable_get_entry(ht, key);
  290. if (entry == NULL)
  291. return 0;
  292. _Py_HASHTABLE_ENTRY_READ_DATA(ht, data, data_size, entry);
  293. return 1;
  294. }
  295. int
  296. _Py_hashtable_pop(_Py_hashtable_t *ht, const void *key, void *data, size_t data_size)
  297. {
  298. assert(data != NULL);
  299. assert(ht->free_data_func == NULL);
  300. return _hashtable_pop_entry(ht, key, data, data_size);
  301. }
  302. /* Delete an entry. The entry must exist. */
  303. void
  304. _Py_hashtable_delete(_Py_hashtable_t *ht, const void *key)
  305. {
  306. #ifndef NDEBUG
  307. int found = _hashtable_pop_entry(ht, key, NULL, 0);
  308. assert(found);
  309. #else
  310. (void)_hashtable_pop_entry(ht, key, NULL, 0);
  311. #endif
  312. }
  313. /* Prototype for a pointer to a function to be called foreach
  314. key/value pair in the hash by hashtable_foreach(). Iteration
  315. stops if a non-zero value is returned. */
  316. int
  317. _Py_hashtable_foreach(_Py_hashtable_t *ht,
  318. int (*func) (_Py_hashtable_entry_t *entry, void *arg),
  319. void *arg)
  320. {
  321. _Py_hashtable_entry_t *entry;
  322. size_t hv;
  323. for (hv = 0; hv < ht->num_buckets; hv++) {
  324. for (entry = TABLE_HEAD(ht, hv); entry; entry = ENTRY_NEXT(entry)) {
  325. int res = func(entry, arg);
  326. if (res)
  327. return res;
  328. }
  329. }
  330. return 0;
  331. }
  332. static void
  333. hashtable_rehash(_Py_hashtable_t *ht)
  334. {
  335. size_t buckets_size, new_size, bucket;
  336. _Py_slist_t *old_buckets = NULL;
  337. size_t old_num_buckets;
  338. new_size = round_size((size_t)(ht->entries * HASHTABLE_REHASH_FACTOR));
  339. if (new_size == ht->num_buckets)
  340. return;
  341. old_num_buckets = ht->num_buckets;
  342. buckets_size = new_size * sizeof(ht->buckets[0]);
  343. old_buckets = ht->buckets;
  344. ht->buckets = ht->alloc.malloc(buckets_size);
  345. if (ht->buckets == NULL) {
  346. /* cancel rehash on memory allocation failure */
  347. ht->buckets = old_buckets ;
  348. /* memory allocation failed */
  349. return;
  350. }
  351. memset(ht->buckets, 0, buckets_size);
  352. ht->num_buckets = new_size;
  353. for (bucket = 0; bucket < old_num_buckets; bucket++) {
  354. _Py_hashtable_entry_t *entry, *next;
  355. for (entry = BUCKETS_HEAD(old_buckets[bucket]); entry != NULL; entry = next) {
  356. size_t entry_index;
  357. assert(ht->hash_func(entry->key) == entry->key_hash);
  358. next = ENTRY_NEXT(entry);
  359. entry_index = entry->key_hash & (new_size - 1);
  360. _Py_slist_prepend(&ht->buckets[entry_index], (_Py_slist_item_t*)entry);
  361. }
  362. }
  363. ht->alloc.free(old_buckets);
  364. }
  365. void
  366. _Py_hashtable_clear(_Py_hashtable_t *ht)
  367. {
  368. _Py_hashtable_entry_t *entry, *next;
  369. size_t i;
  370. for (i=0; i < ht->num_buckets; i++) {
  371. for (entry = TABLE_HEAD(ht, i); entry != NULL; entry = next) {
  372. next = ENTRY_NEXT(entry);
  373. if (ht->free_data_func)
  374. ht->free_data_func(_Py_HASHTABLE_ENTRY_DATA_AS_VOID_P(entry));
  375. ht->alloc.free(entry);
  376. }
  377. _Py_slist_init(&ht->buckets[i]);
  378. }
  379. ht->entries = 0;
  380. hashtable_rehash(ht);
  381. }
  382. void
  383. _Py_hashtable_destroy(_Py_hashtable_t *ht)
  384. {
  385. size_t i;
  386. for (i = 0; i < ht->num_buckets; i++) {
  387. _Py_slist_item_t *entry = ht->buckets[i].head;
  388. while (entry) {
  389. _Py_slist_item_t *entry_next = entry->next;
  390. if (ht->free_data_func)
  391. ht->free_data_func(_Py_HASHTABLE_ENTRY_DATA_AS_VOID_P(entry));
  392. ht->alloc.free(entry);
  393. entry = entry_next;
  394. }
  395. }
  396. ht->alloc.free(ht->buckets);
  397. ht->alloc.free(ht);
  398. }
  399. /* Return a copy of the hash table */
  400. _Py_hashtable_t *
  401. _Py_hashtable_copy(_Py_hashtable_t *src)
  402. {
  403. _Py_hashtable_t *dst;
  404. _Py_hashtable_entry_t *entry;
  405. size_t bucket;
  406. int err;
  407. void *data, *new_data;
  408. dst = _Py_hashtable_new_full(src->data_size, src->num_buckets,
  409. src->hash_func, src->compare_func,
  410. src->copy_data_func, src->free_data_func,
  411. src->get_data_size_func, &src->alloc);
  412. if (dst == NULL)
  413. return NULL;
  414. for (bucket=0; bucket < src->num_buckets; bucket++) {
  415. entry = TABLE_HEAD(src, bucket);
  416. for (; entry; entry = ENTRY_NEXT(entry)) {
  417. if (src->copy_data_func) {
  418. data = _Py_HASHTABLE_ENTRY_DATA_AS_VOID_P(entry);
  419. new_data = src->copy_data_func(data);
  420. if (new_data != NULL)
  421. err = _Py_hashtable_set(dst, entry->key,
  422. &new_data, src->data_size);
  423. else
  424. err = 1;
  425. }
  426. else {
  427. data = _PY_HASHTABLE_ENTRY_DATA(entry);
  428. err = _Py_hashtable_set(dst, entry->key, data, src->data_size);
  429. }
  430. if (err) {
  431. _Py_hashtable_destroy(dst);
  432. return NULL;
  433. }
  434. }
  435. }
  436. return dst;
  437. }