hashtab.c 3.2 KB

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