drm_hashtab.c 5.2 KB

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