drm_hashtab.c 5.2 KB

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