hashtab.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * A hash table (hashtab) maintains associations between
  4. * key values and datum values. The type of the key values
  5. * and the type of the datum values is arbitrary. The
  6. * functions for hash computation and key comparison are
  7. * provided by the creator of the table.
  8. *
  9. * Author : Stephen Smalley, <sds@tycho.nsa.gov>
  10. */
  11. #ifndef _SS_HASHTAB_H_
  12. #define _SS_HASHTAB_H_
  13. #define HASHTAB_MAX_NODES 0xffffffff
  14. struct hashtab_node {
  15. void *key;
  16. void *datum;
  17. struct hashtab_node *next;
  18. };
  19. struct hashtab {
  20. struct hashtab_node **htable; /* hash table */
  21. u32 size; /* number of slots in hash table */
  22. u32 nel; /* number of elements in hash table */
  23. u32 (*hash_value)(struct hashtab *h, const void *key);
  24. /* hash function */
  25. int (*keycmp)(struct hashtab *h, const void *key1, const void *key2);
  26. /* key comparison function */
  27. };
  28. struct hashtab_info {
  29. u32 slots_used;
  30. u32 max_chain_len;
  31. };
  32. /*
  33. * Creates a new hash table with the specified characteristics.
  34. *
  35. * Returns NULL if insufficent space is available or
  36. * the new hash table otherwise.
  37. */
  38. struct hashtab *hashtab_create(u32 (*hash_value)(struct hashtab *h, const void *key),
  39. int (*keycmp)(struct hashtab *h, const void *key1, const void *key2),
  40. u32 size);
  41. /*
  42. * Inserts the specified (key, datum) pair into the specified hash table.
  43. *
  44. * Returns -ENOMEM on memory allocation error,
  45. * -EEXIST if there is already an entry with the same key,
  46. * -EINVAL for general errors or
  47. 0 otherwise.
  48. */
  49. int hashtab_insert(struct hashtab *h, void *k, void *d);
  50. /*
  51. * Searches for the entry with the specified key in the hash table.
  52. *
  53. * Returns NULL if no entry has the specified key or
  54. * the datum of the entry otherwise.
  55. */
  56. void *hashtab_search(struct hashtab *h, const void *k);
  57. /*
  58. * Destroys the specified hash table.
  59. */
  60. void hashtab_destroy(struct hashtab *h);
  61. /*
  62. * Applies the specified apply function to (key,datum,args)
  63. * for each entry in the specified hash table.
  64. *
  65. * The order in which the function is applied to the entries
  66. * is dependent upon the internal structure of the hash table.
  67. *
  68. * If apply returns a non-zero status, then hashtab_map will cease
  69. * iterating through the hash table and will propagate the error
  70. * return to its caller.
  71. */
  72. int hashtab_map(struct hashtab *h,
  73. int (*apply)(void *k, void *d, void *args),
  74. void *args);
  75. /* Fill info with some hash table statistics */
  76. void hashtab_stat(struct hashtab *h, struct hashtab_info *info);
  77. #endif /* _SS_HASHTAB_H */