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