hash.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /* xf86drmHash.c -- Small hash table support for integer -> integer mapping
  2. * Created: Sun Apr 18 09:35:45 1999 by faith@precisioninsight.com
  3. *
  4. * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
  5. * All Rights Reserved.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the next
  15. * paragraph) shall be included in all copies or substantial portions of the
  16. * Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  21. * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  22. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  23. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  24. * DEALINGS IN THE SOFTWARE.
  25. *
  26. * Authors: Rickard E. (Rik) Faith <faith@valinux.com>
  27. *
  28. * DESCRIPTION
  29. *
  30. * This file contains a straightforward implementation of a fixed-sized
  31. * hash table using self-organizing linked lists [Knuth73, pp. 398-399] for
  32. * collision resolution. There are two potentially interesting things
  33. * about this implementation:
  34. *
  35. * 1) The table is power-of-two sized. Prime sized tables are more
  36. * traditional, but do not have a significant advantage over power-of-two
  37. * sized table, especially when double hashing is not used for collision
  38. * resolution.
  39. *
  40. * 2) The hash computation uses a table of random integers [Hanson97,
  41. * pp. 39-41].
  42. *
  43. * FUTURE ENHANCEMENTS
  44. *
  45. * With a table size of 512, the current implementation is sufficient for a
  46. * few hundred keys. Since this is well above the expected size of the
  47. * tables for which this implementation was designed, the implementation of
  48. * dynamic hash tables was postponed until the need arises. A common (and
  49. * naive) approach to dynamic hash table implementation simply creates a
  50. * new hash table when necessary, rehashes all the data into the new table,
  51. * and destroys the old table. The approach in [Larson88] is superior in
  52. * two ways: 1) only a portion of the table is expanded when needed,
  53. * distributing the expansion cost over several insertions, and 2) portions
  54. * of the table can be locked, enabling a scalable thread-safe
  55. * implementation.
  56. *
  57. * REFERENCES
  58. *
  59. * [Hanson97] David R. Hanson. C Interfaces and Implementations:
  60. * Techniques for Creating Reusable Software. Reading, Massachusetts:
  61. * Addison-Wesley, 1997.
  62. *
  63. * [Knuth73] Donald E. Knuth. The Art of Computer Programming. Volume 3:
  64. * Sorting and Searching. Reading, Massachusetts: Addison-Wesley, 1973.
  65. *
  66. * [Larson88] Per-Ake Larson. "Dynamic Hash Tables". CACM 31(4), April
  67. * 1988, pp. 446-457.
  68. *
  69. */
  70. #include <stdio.h>
  71. #include <stdlib.h>
  72. #include "xf86drm.h"
  73. #include "xf86drmHash.h"
  74. #define DIST_LIMIT 10
  75. static int dist[DIST_LIMIT];
  76. static void clear_dist(void) {
  77. int i;
  78. for (i = 0; i < DIST_LIMIT; i++)
  79. dist[i] = 0;
  80. }
  81. static int count_entries(HashBucketPtr bucket)
  82. {
  83. int count = 0;
  84. for (; bucket; bucket = bucket->next)
  85. ++count;
  86. return count;
  87. }
  88. static void update_dist(int count)
  89. {
  90. if (count >= DIST_LIMIT)
  91. ++dist[DIST_LIMIT-1];
  92. else
  93. ++dist[count];
  94. }
  95. static void compute_dist(HashTablePtr table)
  96. {
  97. int i;
  98. HashBucketPtr bucket;
  99. printf("Entries = %ld, hits = %ld, partials = %ld, misses = %ld\n",
  100. table->entries, table->hits, table->partials, table->misses);
  101. clear_dist();
  102. for (i = 0; i < HASH_SIZE; i++) {
  103. bucket = table->buckets[i];
  104. update_dist(count_entries(bucket));
  105. }
  106. for (i = 0; i < DIST_LIMIT; i++) {
  107. if (i != DIST_LIMIT-1)
  108. printf("%5d %10d\n", i, dist[i]);
  109. else
  110. printf("other %10d\n", dist[i]);
  111. }
  112. }
  113. static int check_table(HashTablePtr table,
  114. unsigned long key, void * value)
  115. {
  116. void *retval;
  117. int retcode = drmHashLookup(table, key, &retval);
  118. switch (retcode) {
  119. case -1:
  120. printf("Bad magic = 0x%08lx:"
  121. " key = %lu, expected = %p, returned = %p\n",
  122. table->magic, key, value, retval);
  123. break;
  124. case 1:
  125. printf("Not found: key = %lu, expected = %p, returned = %p\n",
  126. key, value, retval);
  127. break;
  128. case 0:
  129. if (value != retval) {
  130. printf("Bad value: key = %lu, expected = %p, returned = %p\n",
  131. key, value, retval);
  132. retcode = -1;
  133. }
  134. break;
  135. default:
  136. printf("Bad retcode = %d: key = %lu, expected = %p, returned = %p\n",
  137. retcode, key, value, retval);
  138. break;
  139. }
  140. return retcode;
  141. }
  142. int main(void)
  143. {
  144. HashTablePtr table;
  145. unsigned long i;
  146. int ret = 0;
  147. printf("\n***** 256 consecutive integers ****\n");
  148. table = drmHashCreate();
  149. for (i = 0; i < 256; i++)
  150. drmHashInsert(table, i, (void *)(i << 16 | i));
  151. for (i = 0; i < 256; i++)
  152. ret |= check_table(table, i, (void *)(i << 16 | i));
  153. compute_dist(table);
  154. drmHashDestroy(table);
  155. printf("\n***** 1024 consecutive integers ****\n");
  156. table = drmHashCreate();
  157. for (i = 0; i < 1024; i++)
  158. drmHashInsert(table, i, (void *)(i << 16 | i));
  159. for (i = 0; i < 1024; i++)
  160. ret |= check_table(table, i, (void *)(i << 16 | i));
  161. compute_dist(table);
  162. drmHashDestroy(table);
  163. printf("\n***** 1024 consecutive page addresses (4k pages) ****\n");
  164. table = drmHashCreate();
  165. for (i = 0; i < 1024; i++)
  166. drmHashInsert(table, i*4096, (void *)(i << 16 | i));
  167. for (i = 0; i < 1024; i++)
  168. ret |= check_table(table, i*4096, (void *)(i << 16 | i));
  169. compute_dist(table);
  170. drmHashDestroy(table);
  171. printf("\n***** 1024 random integers ****\n");
  172. table = drmHashCreate();
  173. srandom(0xbeefbeef);
  174. for (i = 0; i < 1024; i++)
  175. drmHashInsert(table, random(), (void *)(i << 16 | i));
  176. srandom(0xbeefbeef);
  177. for (i = 0; i < 1024; i++)
  178. ret |= check_table(table, random(), (void *)(i << 16 | i));
  179. srandom(0xbeefbeef);
  180. for (i = 0; i < 1024; i++)
  181. ret |= check_table(table, random(), (void *)(i << 16 | i));
  182. compute_dist(table);
  183. drmHashDestroy(table);
  184. printf("\n***** 5000 random integers ****\n");
  185. table = drmHashCreate();
  186. srandom(0xbeefbeef);
  187. for (i = 0; i < 5000; i++)
  188. drmHashInsert(table, random(), (void *)(i << 16 | i));
  189. srandom(0xbeefbeef);
  190. for (i = 0; i < 5000; i++)
  191. ret |= check_table(table, random(), (void *)(i << 16 | i));
  192. srandom(0xbeefbeef);
  193. for (i = 0; i < 5000; i++)
  194. ret |= check_table(table, random(), (void *)(i << 16 | i));
  195. compute_dist(table);
  196. drmHashDestroy(table);
  197. return ret;
  198. }