hashtab.h 2.5 KB

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