hashtab.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Implementation of the hash table type.
  3. *
  4. * Author : Stephen Smalley, <sds@epoch.ncsc.mil>
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/slab.h>
  8. #include <linux/errno.h>
  9. #include "hashtab.h"
  10. struct hashtab *hashtab_create(u32 (*hash_value)(struct hashtab *h, const void *key),
  11. int (*keycmp)(struct hashtab *h, const void *key1, const void *key2),
  12. u32 size)
  13. {
  14. struct hashtab *p;
  15. u32 i;
  16. p = kzalloc(sizeof(*p), GFP_KERNEL);
  17. if (p == NULL)
  18. return p;
  19. p->size = size;
  20. p->nel = 0;
  21. p->hash_value = hash_value;
  22. p->keycmp = keycmp;
  23. p->htable = kmalloc(sizeof(*(p->htable)) * size, GFP_KERNEL);
  24. if (p->htable == NULL) {
  25. kfree(p);
  26. return NULL;
  27. }
  28. for (i = 0; i < size; i++)
  29. p->htable[i] = NULL;
  30. return p;
  31. }
  32. int hashtab_insert(struct hashtab *h, void *key, void *datum)
  33. {
  34. u32 hvalue;
  35. struct hashtab_node *prev, *cur, *newnode;
  36. if (!h || h->nel == HASHTAB_MAX_NODES)
  37. return -EINVAL;
  38. hvalue = h->hash_value(h, key);
  39. prev = NULL;
  40. cur = h->htable[hvalue];
  41. while (cur && h->keycmp(h, key, cur->key) > 0) {
  42. prev = cur;
  43. cur = cur->next;
  44. }
  45. if (cur && (h->keycmp(h, key, cur->key) == 0))
  46. return -EEXIST;
  47. newnode = kzalloc(sizeof(*newnode), GFP_KERNEL);
  48. if (newnode == NULL)
  49. return -ENOMEM;
  50. newnode->key = key;
  51. newnode->datum = datum;
  52. if (prev) {
  53. newnode->next = prev->next;
  54. prev->next = newnode;
  55. } else {
  56. newnode->next = h->htable[hvalue];
  57. h->htable[hvalue] = newnode;
  58. }
  59. h->nel++;
  60. return 0;
  61. }
  62. void *hashtab_search(struct hashtab *h, const void *key)
  63. {
  64. u32 hvalue;
  65. struct hashtab_node *cur;
  66. if (!h)
  67. return NULL;
  68. hvalue = h->hash_value(h, key);
  69. cur = h->htable[hvalue];
  70. while (cur && h->keycmp(h, key, cur->key) > 0)
  71. cur = cur->next;
  72. if (cur == NULL || (h->keycmp(h, key, cur->key) != 0))
  73. return NULL;
  74. return cur->datum;
  75. }
  76. void hashtab_destroy(struct hashtab *h)
  77. {
  78. u32 i;
  79. struct hashtab_node *cur, *temp;
  80. if (!h)
  81. return;
  82. for (i = 0; i < h->size; i++) {
  83. cur = h->htable[i];
  84. while (cur) {
  85. temp = cur;
  86. cur = cur->next;
  87. kfree(temp);
  88. }
  89. h->htable[i] = NULL;
  90. }
  91. kfree(h->htable);
  92. h->htable = NULL;
  93. kfree(h);
  94. }
  95. int hashtab_map(struct hashtab *h,
  96. int (*apply)(void *k, void *d, void *args),
  97. void *args)
  98. {
  99. u32 i;
  100. int ret;
  101. struct hashtab_node *cur;
  102. if (!h)
  103. return 0;
  104. for (i = 0; i < h->size; i++) {
  105. cur = h->htable[i];
  106. while (cur) {
  107. ret = apply(cur->key, cur->datum, args);
  108. if (ret)
  109. return ret;
  110. cur = cur->next;
  111. }
  112. }
  113. return 0;
  114. }
  115. void hashtab_stat(struct hashtab *h, struct hashtab_info *info)
  116. {
  117. u32 i, chain_len, slots_used, max_chain_len;
  118. struct hashtab_node *cur;
  119. slots_used = 0;
  120. max_chain_len = 0;
  121. for (slots_used = max_chain_len = i = 0; i < h->size; i++) {
  122. cur = h->htable[i];
  123. if (cur) {
  124. slots_used++;
  125. chain_len = 0;
  126. while (cur) {
  127. chain_len++;
  128. cur = cur->next;
  129. }
  130. if (chain_len > max_chain_len)
  131. max_chain_len = chain_len;
  132. }
  133. }
  134. info->slots_used = slots_used;
  135. info->max_chain_len = max_chain_len;
  136. }