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.

519 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=%"
  200. PY_FORMAT_SIZE_T "u/%" PY_FORMAT_SIZE_T "u (%.0f%%), ",
  201. ht, ht->entries, ht->num_buckets, load * 100.0);
  202. if (nchains)
  203. printf("avg_chain_len=%.1f, ", (double)total_chain_len / nchains);
  204. printf("max_chain_len=%" PY_FORMAT_SIZE_T "u, %" PY_FORMAT_SIZE_T "u kB\n",
  205. max_chain_len, size / 1024);
  206. }
  207. #endif
  208. /* Get an entry. Return NULL if the key does not exist. */
  209. _Py_hashtable_entry_t *
  210. _Py_hashtable_get_entry(_Py_hashtable_t *ht, const void *key)
  211. {
  212. Py_uhash_t key_hash;
  213. size_t index;
  214. _Py_hashtable_entry_t *entry;
  215. key_hash = ht->hash_func(key);
  216. index = key_hash & (ht->num_buckets - 1);
  217. for (entry = TABLE_HEAD(ht, index); entry != NULL; entry = ENTRY_NEXT(entry)) {
  218. if (entry->key_hash == key_hash && ht->compare_func(key, entry))
  219. break;
  220. }
  221. return entry;
  222. }
  223. static int
  224. _hashtable_pop_entry(_Py_hashtable_t *ht, const void *key, void *data, size_t data_size)
  225. {
  226. Py_uhash_t key_hash;
  227. size_t index;
  228. _Py_hashtable_entry_t *entry, *previous;
  229. key_hash = ht->hash_func(key);
  230. index = key_hash & (ht->num_buckets - 1);
  231. previous = NULL;
  232. for (entry = TABLE_HEAD(ht, index); entry != NULL; entry = ENTRY_NEXT(entry)) {
  233. if (entry->key_hash == key_hash && ht->compare_func(key, entry))
  234. break;
  235. previous = entry;
  236. }
  237. if (entry == NULL)
  238. return 0;
  239. _Py_slist_remove(&ht->buckets[index], (_Py_slist_item_t *)previous,
  240. (_Py_slist_item_t *)entry);
  241. ht->entries--;
  242. if (data != NULL)
  243. _Py_HASHTABLE_ENTRY_READ_DATA(ht, data, data_size, entry);
  244. ht->alloc.free(entry);
  245. if ((float)ht->entries / (float)ht->num_buckets < HASHTABLE_LOW)
  246. hashtable_rehash(ht);
  247. return 1;
  248. }
  249. /* Add a new entry to the hash. The key must not be present in the hash table.
  250. Return 0 on success, -1 on memory error. */
  251. int
  252. _Py_hashtable_set(_Py_hashtable_t *ht, const void *key,
  253. void *data, size_t data_size)
  254. {
  255. Py_uhash_t key_hash;
  256. size_t index;
  257. _Py_hashtable_entry_t *entry;
  258. assert(data != NULL || data_size == 0);
  259. #ifndef NDEBUG
  260. /* Don't write the assertion on a single line because it is interesting
  261. to know the duplicated entry if the assertion failed. The entry can
  262. be read using a debugger. */
  263. entry = _Py_hashtable_get_entry(ht, key);
  264. assert(entry == NULL);
  265. #endif
  266. key_hash = ht->hash_func(key);
  267. index = key_hash & (ht->num_buckets - 1);
  268. entry = ht->alloc.malloc(HASHTABLE_ITEM_SIZE(ht));
  269. if (entry == NULL) {
  270. /* memory allocation failed */
  271. return -1;
  272. }
  273. entry->key = (void *)key;
  274. entry->key_hash = key_hash;
  275. assert(data_size == ht->data_size);
  276. memcpy(_Py_HASHTABLE_ENTRY_DATA(entry), data, data_size);
  277. _Py_slist_prepend(&ht->buckets[index], (_Py_slist_item_t*)entry);
  278. ht->entries++;
  279. if ((float)ht->entries / (float)ht->num_buckets > HASHTABLE_HIGH)
  280. hashtable_rehash(ht);
  281. return 0;
  282. }
  283. /* Get data from an entry. Copy entry data into data and return 1 if the entry
  284. exists, return 0 if the entry does not exist. */
  285. int
  286. _Py_hashtable_get(_Py_hashtable_t *ht, const void *key, void *data, size_t data_size)
  287. {
  288. _Py_hashtable_entry_t *entry;
  289. assert(data != NULL);
  290. entry = _Py_hashtable_get_entry(ht, key);
  291. if (entry == NULL)
  292. return 0;
  293. _Py_HASHTABLE_ENTRY_READ_DATA(ht, data, data_size, entry);
  294. return 1;
  295. }
  296. int
  297. _Py_hashtable_pop(_Py_hashtable_t *ht, const void *key, void *data, size_t data_size)
  298. {
  299. assert(data != NULL);
  300. assert(ht->free_data_func == NULL);
  301. return _hashtable_pop_entry(ht, key, data, data_size);
  302. }
  303. /* Delete an entry. The entry must exist. */
  304. void
  305. _Py_hashtable_delete(_Py_hashtable_t *ht, const void *key)
  306. {
  307. #ifndef NDEBUG
  308. int found = _hashtable_pop_entry(ht, key, NULL, 0);
  309. assert(found);
  310. #else
  311. (void)_hashtable_pop_entry(ht, key, NULL, 0);
  312. #endif
  313. }
  314. /* Prototype for a pointer to a function to be called foreach
  315. key/value pair in the hash by hashtable_foreach(). Iteration
  316. stops if a non-zero value is returned. */
  317. int
  318. _Py_hashtable_foreach(_Py_hashtable_t *ht,
  319. int (*func) (_Py_hashtable_entry_t *entry, void *arg),
  320. void *arg)
  321. {
  322. _Py_hashtable_entry_t *entry;
  323. size_t hv;
  324. for (hv = 0; hv < ht->num_buckets; hv++) {
  325. for (entry = TABLE_HEAD(ht, hv); entry; entry = ENTRY_NEXT(entry)) {
  326. int res = func(entry, arg);
  327. if (res)
  328. return res;
  329. }
  330. }
  331. return 0;
  332. }
  333. static void
  334. hashtable_rehash(_Py_hashtable_t *ht)
  335. {
  336. size_t buckets_size, new_size, bucket;
  337. _Py_slist_t *old_buckets = NULL;
  338. size_t old_num_buckets;
  339. new_size = round_size((size_t)(ht->entries * HASHTABLE_REHASH_FACTOR));
  340. if (new_size == ht->num_buckets)
  341. return;
  342. old_num_buckets = ht->num_buckets;
  343. buckets_size = new_size * sizeof(ht->buckets[0]);
  344. old_buckets = ht->buckets;
  345. ht->buckets = ht->alloc.malloc(buckets_size);
  346. if (ht->buckets == NULL) {
  347. /* cancel rehash on memory allocation failure */
  348. ht->buckets = old_buckets ;
  349. /* memory allocation failed */
  350. return;
  351. }
  352. memset(ht->buckets, 0, buckets_size);
  353. ht->num_buckets = new_size;
  354. for (bucket = 0; bucket < old_num_buckets; bucket++) {
  355. _Py_hashtable_entry_t *entry, *next;
  356. for (entry = BUCKETS_HEAD(old_buckets[bucket]); entry != NULL; entry = next) {
  357. size_t entry_index;
  358. assert(ht->hash_func(entry->key) == entry->key_hash);
  359. next = ENTRY_NEXT(entry);
  360. entry_index = entry->key_hash & (new_size - 1);
  361. _Py_slist_prepend(&ht->buckets[entry_index], (_Py_slist_item_t*)entry);
  362. }
  363. }
  364. ht->alloc.free(old_buckets);
  365. }
  366. void
  367. _Py_hashtable_clear(_Py_hashtable_t *ht)
  368. {
  369. _Py_hashtable_entry_t *entry, *next;
  370. size_t i;
  371. for (i=0; i < ht->num_buckets; i++) {
  372. for (entry = TABLE_HEAD(ht, i); entry != NULL; entry = next) {
  373. next = ENTRY_NEXT(entry);
  374. if (ht->free_data_func)
  375. ht->free_data_func(_Py_HASHTABLE_ENTRY_DATA_AS_VOID_P(entry));
  376. ht->alloc.free(entry);
  377. }
  378. _Py_slist_init(&ht->buckets[i]);
  379. }
  380. ht->entries = 0;
  381. hashtable_rehash(ht);
  382. }
  383. void
  384. _Py_hashtable_destroy(_Py_hashtable_t *ht)
  385. {
  386. size_t i;
  387. for (i = 0; i < ht->num_buckets; i++) {
  388. _Py_slist_item_t *entry = ht->buckets[i].head;
  389. while (entry) {
  390. _Py_slist_item_t *entry_next = entry->next;
  391. if (ht->free_data_func)
  392. ht->free_data_func(_Py_HASHTABLE_ENTRY_DATA_AS_VOID_P(entry));
  393. ht->alloc.free(entry);
  394. entry = entry_next;
  395. }
  396. }
  397. ht->alloc.free(ht->buckets);
  398. ht->alloc.free(ht);
  399. }
  400. /* Return a copy of the hash table */
  401. _Py_hashtable_t *
  402. _Py_hashtable_copy(_Py_hashtable_t *src)
  403. {
  404. _Py_hashtable_t *dst;
  405. _Py_hashtable_entry_t *entry;
  406. size_t bucket;
  407. int err;
  408. void *data, *new_data;
  409. dst = _Py_hashtable_new_full(src->data_size, src->num_buckets,
  410. src->hash_func, src->compare_func,
  411. src->copy_data_func, src->free_data_func,
  412. src->get_data_size_func, &src->alloc);
  413. if (dst == NULL)
  414. return NULL;
  415. for (bucket=0; bucket < src->num_buckets; bucket++) {
  416. entry = TABLE_HEAD(src, bucket);
  417. for (; entry; entry = ENTRY_NEXT(entry)) {
  418. if (src->copy_data_func) {
  419. data = _Py_HASHTABLE_ENTRY_DATA_AS_VOID_P(entry);
  420. new_data = src->copy_data_func(data);
  421. if (new_data != NULL)
  422. err = _Py_hashtable_set(dst, entry->key,
  423. &new_data, src->data_size);
  424. else
  425. err = 1;
  426. }
  427. else {
  428. data = _Py_HASHTABLE_ENTRY_DATA(entry);
  429. err = _Py_hashtable_set(dst, entry->key, data, src->data_size);
  430. }
  431. if (err) {
  432. _Py_hashtable_destroy(dst);
  433. return NULL;
  434. }
  435. }
  436. }
  437. return dst;
  438. }