drm_hashtab.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. #include <linux/slab.h>
  38. int drm_ht_create(struct drm_open_hash *ht, unsigned int order)
  39. {
  40. unsigned int i;
  41. ht->size = 1 << order;
  42. ht->order = order;
  43. ht->fill = 0;
  44. ht->table = NULL;
  45. ht->use_vmalloc = ((ht->size * sizeof(*ht->table)) > PAGE_SIZE);
  46. if (!ht->use_vmalloc) {
  47. ht->table = kcalloc(ht->size, sizeof(*ht->table), GFP_KERNEL);
  48. }
  49. if (!ht->table) {
  50. ht->use_vmalloc = 1;
  51. ht->table = vmalloc(ht->size*sizeof(*ht->table));
  52. }
  53. if (!ht->table) {
  54. DRM_ERROR("Out of memory for hash table\n");
  55. return -ENOMEM;
  56. }
  57. for (i=0; i< ht->size; ++i) {
  58. INIT_HLIST_HEAD(&ht->table[i]);
  59. }
  60. return 0;
  61. }
  62. EXPORT_SYMBOL(drm_ht_create);
  63. void drm_ht_verbose_list(struct drm_open_hash *ht, unsigned long key)
  64. {
  65. struct drm_hash_item *entry;
  66. struct hlist_head *h_list;
  67. struct hlist_node *list;
  68. unsigned int hashed_key;
  69. int count = 0;
  70. hashed_key = hash_long(key, ht->order);
  71. DRM_DEBUG("Key is 0x%08lx, Hashed key is 0x%08x\n", key, hashed_key);
  72. h_list = &ht->table[hashed_key];
  73. hlist_for_each(list, h_list) {
  74. entry = hlist_entry(list, struct drm_hash_item, head);
  75. DRM_DEBUG("count %d, key: 0x%08lx\n", count++, entry->key);
  76. }
  77. }
  78. static struct hlist_node *drm_ht_find_key(struct drm_open_hash *ht,
  79. unsigned long key)
  80. {
  81. struct drm_hash_item *entry;
  82. struct hlist_head *h_list;
  83. struct hlist_node *list;
  84. unsigned int hashed_key;
  85. hashed_key = hash_long(key, ht->order);
  86. h_list = &ht->table[hashed_key];
  87. hlist_for_each(list, h_list) {
  88. entry = hlist_entry(list, struct drm_hash_item, head);
  89. if (entry->key == key)
  90. return list;
  91. if (entry->key > key)
  92. break;
  93. }
  94. return NULL;
  95. }
  96. int drm_ht_insert_item(struct drm_open_hash *ht, struct drm_hash_item *item)
  97. {
  98. struct drm_hash_item *entry;
  99. struct hlist_head *h_list;
  100. struct hlist_node *list, *parent;
  101. unsigned int hashed_key;
  102. unsigned long key = item->key;
  103. hashed_key = hash_long(key, ht->order);
  104. h_list = &ht->table[hashed_key];
  105. parent = NULL;
  106. hlist_for_each(list, h_list) {
  107. entry = hlist_entry(list, struct drm_hash_item, head);
  108. if (entry->key == key)
  109. return -EINVAL;
  110. if (entry->key > key)
  111. break;
  112. parent = list;
  113. }
  114. if (parent) {
  115. hlist_add_after(parent, &item->head);
  116. } else {
  117. hlist_add_head(&item->head, h_list);
  118. }
  119. return 0;
  120. }
  121. EXPORT_SYMBOL(drm_ht_insert_item);
  122. /*
  123. * Just insert an item and return any "bits" bit key that hasn't been
  124. * used before.
  125. */
  126. int drm_ht_just_insert_please(struct drm_open_hash *ht, struct drm_hash_item *item,
  127. unsigned long seed, int bits, int shift,
  128. unsigned long add)
  129. {
  130. int ret;
  131. unsigned long mask = (1 << bits) - 1;
  132. unsigned long first, unshifted_key;
  133. unshifted_key = hash_long(seed, bits);
  134. first = unshifted_key;
  135. do {
  136. item->key = (unshifted_key << shift) + add;
  137. ret = drm_ht_insert_item(ht, item);
  138. if (ret)
  139. unshifted_key = (unshifted_key + 1) & mask;
  140. } while(ret && (unshifted_key != first));
  141. if (ret) {
  142. DRM_ERROR("Available key bit space exhausted\n");
  143. return -EINVAL;
  144. }
  145. return 0;
  146. }
  147. EXPORT_SYMBOL(drm_ht_just_insert_please);
  148. int drm_ht_find_item(struct drm_open_hash *ht, unsigned long key,
  149. struct drm_hash_item **item)
  150. {
  151. struct hlist_node *list;
  152. list = drm_ht_find_key(ht, key);
  153. if (!list)
  154. return -EINVAL;
  155. *item = hlist_entry(list, struct drm_hash_item, head);
  156. return 0;
  157. }
  158. EXPORT_SYMBOL(drm_ht_find_item);
  159. int drm_ht_remove_key(struct drm_open_hash *ht, unsigned long key)
  160. {
  161. struct hlist_node *list;
  162. list = drm_ht_find_key(ht, key);
  163. if (list) {
  164. hlist_del_init(list);
  165. ht->fill--;
  166. return 0;
  167. }
  168. return -EINVAL;
  169. }
  170. int drm_ht_remove_item(struct drm_open_hash *ht, struct drm_hash_item *item)
  171. {
  172. hlist_del_init(&item->head);
  173. ht->fill--;
  174. return 0;
  175. }
  176. EXPORT_SYMBOL(drm_ht_remove_item);
  177. void drm_ht_remove(struct drm_open_hash *ht)
  178. {
  179. if (ht->table) {
  180. if (ht->use_vmalloc)
  181. vfree(ht->table);
  182. else
  183. kfree(ht->table);
  184. ht->table = NULL;
  185. }
  186. }
  187. EXPORT_SYMBOL(drm_ht_remove);