drm_hashtab.c 4.7 KB

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