drm_hashtab.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /**************************************************************************
  2. *
  3. * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND. USA.
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sub license, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial portions
  16. * of the 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 NON-INFRINGEMENT. IN NO EVENT SHALL
  21. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  22. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  23. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  24. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. *
  27. **************************************************************************/
  28. /*
  29. * Simple open hash tab implementation.
  30. *
  31. * Authors:
  32. * Thomas Hellström <thomas-at-tungstengraphics-dot-com>
  33. */
  34. #include "drmP.h"
  35. #include "drm_hashtab.h"
  36. #include <linux/hash.h>
  37. int drm_ht_create(drm_open_hash_t *ht, unsigned int order)
  38. {
  39. unsigned int i;
  40. ht->size = 1 << order;
  41. ht->order = order;
  42. ht->fill = 0;
  43. ht->use_vmalloc = ((ht->size * sizeof(*ht->table)) > PAGE_SIZE);
  44. if (!ht->use_vmalloc) {
  45. ht->table = drm_calloc(ht->size, sizeof(*ht->table),
  46. DRM_MEM_HASHTAB);
  47. }
  48. if (!ht->table) {
  49. ht->use_vmalloc = 1;
  50. ht->table = vmalloc(ht->size*sizeof(*ht->table));
  51. }
  52. if (!ht->table) {
  53. DRM_ERROR("Out of memory for hash table\n");
  54. return -ENOMEM;
  55. }
  56. for (i=0; i< ht->size; ++i) {
  57. INIT_HLIST_HEAD(&ht->table[i]);
  58. }
  59. return 0;
  60. }
  61. void drm_ht_verbose_list(drm_open_hash_t *ht, unsigned long key)
  62. {
  63. drm_hash_item_t *entry;
  64. struct hlist_head *h_list;
  65. struct hlist_node *list;
  66. unsigned int hashed_key;
  67. int count = 0;
  68. hashed_key = hash_long(key, ht->order);
  69. DRM_DEBUG("Key is 0x%08lx, Hashed key is 0x%08x\n", key, hashed_key);
  70. h_list = &ht->table[hashed_key];
  71. hlist_for_each(list, h_list) {
  72. entry = hlist_entry(list, drm_hash_item_t, head);
  73. DRM_DEBUG("count %d, key: 0x%08lx\n", count++, entry->key);
  74. }
  75. }
  76. static struct hlist_node *drm_ht_find_key(drm_open_hash_t *ht,
  77. unsigned long key)
  78. {
  79. drm_hash_item_t *entry;
  80. struct hlist_head *h_list;
  81. struct hlist_node *list;
  82. unsigned int hashed_key;
  83. hashed_key = hash_long(key, ht->order);
  84. h_list = &ht->table[hashed_key];
  85. hlist_for_each(list, h_list) {
  86. entry = hlist_entry(list, drm_hash_item_t, head);
  87. if (entry->key == key)
  88. return list;
  89. if (entry->key > key)
  90. break;
  91. }
  92. return NULL;
  93. }
  94. int drm_ht_insert_item(drm_open_hash_t *ht, drm_hash_item_t *item)
  95. {
  96. drm_hash_item_t *entry;
  97. struct hlist_head *h_list;
  98. struct hlist_node *list, *parent;
  99. unsigned int hashed_key;
  100. unsigned long key = item->key;
  101. hashed_key = hash_long(key, ht->order);
  102. h_list = &ht->table[hashed_key];
  103. parent = NULL;
  104. hlist_for_each(list, h_list) {
  105. entry = hlist_entry(list, drm_hash_item_t, head);
  106. if (entry->key == key)
  107. return -EINVAL;
  108. if (entry->key > key)
  109. break;
  110. parent = list;
  111. }
  112. if (parent) {
  113. hlist_add_after(parent, &item->head);
  114. } else {
  115. hlist_add_head(&item->head, h_list);
  116. }
  117. return 0;
  118. }
  119. /*
  120. * Just insert an item and return any "bits" bit key that hasn't been
  121. * used before.
  122. */
  123. int drm_ht_just_insert_please(drm_open_hash_t *ht, drm_hash_item_t *item,
  124. unsigned long seed, int bits, int shift,
  125. unsigned long add)
  126. {
  127. int ret;
  128. unsigned long mask = (1 << bits) - 1;
  129. unsigned long first, unshifted_key;
  130. unshifted_key = hash_long(seed, bits);
  131. first = unshifted_key;
  132. do {
  133. item->key = (unshifted_key << shift) + add;
  134. ret = drm_ht_insert_item(ht, item);
  135. if (ret)
  136. unshifted_key = (unshifted_key + 1) & mask;
  137. } while(ret && (unshifted_key != first));
  138. if (ret) {
  139. DRM_ERROR("Available key bit space exhausted\n");
  140. return -EINVAL;
  141. }
  142. return 0;
  143. }
  144. int drm_ht_find_item(drm_open_hash_t *ht, unsigned long key,
  145. drm_hash_item_t **item)
  146. {
  147. struct hlist_node *list;
  148. list = drm_ht_find_key(ht, key);
  149. if (!list)
  150. return -EINVAL;
  151. *item = hlist_entry(list, drm_hash_item_t, head);
  152. return 0;
  153. }
  154. int drm_ht_remove_key(drm_open_hash_t *ht, unsigned long key)
  155. {
  156. struct hlist_node *list;
  157. list = drm_ht_find_key(ht, key);
  158. if (list) {
  159. hlist_del_init(list);
  160. ht->fill--;
  161. return 0;
  162. }
  163. return -EINVAL;
  164. }
  165. int drm_ht_remove_item(drm_open_hash_t *ht, drm_hash_item_t *item)
  166. {
  167. hlist_del_init(&item->head);
  168. ht->fill--;
  169. return 0;
  170. }
  171. void drm_ht_remove(drm_open_hash_t *ht)
  172. {
  173. if (ht->table) {
  174. if (ht->use_vmalloc)
  175. vfree(ht->table);
  176. else
  177. drm_free(ht->table, ht->size * sizeof(*ht->table),
  178. DRM_MEM_HASHTAB);
  179. ht->table = NULL;
  180. }
  181. }