You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

hash.h 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * @file hash.h
  3. * Hash table implementation that allows using memory pools for storage as well as using
  4. * shared memory for this purpose
  5. */
  6. #ifndef RSPAMD_HASH_H
  7. #define RSPAMD_HASH_H
  8. #include "config.h"
  9. #ifdef __cplusplus
  10. extern "C" {
  11. #endif
  12. struct rspamd_lru_hash_s;
  13. typedef struct rspamd_lru_hash_s rspamd_lru_hash_t;
  14. struct rspamd_lru_element_s;
  15. typedef struct rspamd_lru_element_s rspamd_lru_element_t;
  16. /**
  17. * Create new lru hash
  18. * @param maxsize maximum elements in a hash
  19. * @param maxage maximum age of element
  20. * @param hash_func pointer to hash function
  21. * @param key_equal_func pointer to function for comparing keys
  22. * @return new rspamd_hash object
  23. */
  24. rspamd_lru_hash_t *rspamd_lru_hash_new(int maxsize,
  25. GDestroyNotify key_destroy,
  26. GDestroyNotify value_destroy);
  27. /**
  28. * Create new lru hash
  29. * @param maxsize maximum elements in a hash
  30. * @param maxage maximum age of element
  31. * @param hash_func pointer to hash function
  32. * @param key_equal_func pointer to function for comparing keys
  33. * @return new rspamd_hash object
  34. */
  35. rspamd_lru_hash_t *rspamd_lru_hash_new_full(int maxsize,
  36. GDestroyNotify key_destroy,
  37. GDestroyNotify value_destroy,
  38. GHashFunc hfunc,
  39. GEqualFunc eqfunc);
  40. /**
  41. * Lookup item from hash
  42. * @param hash hash object
  43. * @param key key to find
  44. * @return value of key or NULL if key is not found
  45. */
  46. gpointer rspamd_lru_hash_lookup(rspamd_lru_hash_t *hash,
  47. gconstpointer key,
  48. time_t now);
  49. /**
  50. * Removes key from LRU cache
  51. * @param hash
  52. * @param key
  53. * @return TRUE if key has been found and removed
  54. */
  55. gboolean rspamd_lru_hash_remove(rspamd_lru_hash_t *hash,
  56. gconstpointer key);
  57. /**
  58. * Insert item in hash
  59. * @param hash hash object
  60. * @param key key to insert
  61. * @param value value of key
  62. */
  63. void rspamd_lru_hash_insert(rspamd_lru_hash_t *hash,
  64. gpointer key,
  65. gpointer value,
  66. time_t now,
  67. unsigned int ttl);
  68. /**
  69. * Remove lru hash
  70. * @param hash hash object
  71. */
  72. void rspamd_lru_hash_destroy(rspamd_lru_hash_t *hash);
  73. /**
  74. * Iterate over lru hash. Iterations must start from it=0 and are done when it==-1
  75. * @param hash
  76. * @param it
  77. * @param k
  78. * @param v
  79. * @return new it or -1 if iteration has been reached over
  80. */
  81. int rspamd_lru_hash_foreach(rspamd_lru_hash_t *hash, int it, gpointer *k,
  82. gpointer *v);
  83. /**
  84. * Returns number of elements in a hash
  85. * @param hash hash object
  86. */
  87. unsigned int rspamd_lru_hash_size(rspamd_lru_hash_t *hash);
  88. /**
  89. * Returns hash capacity
  90. * @param hash hash object
  91. */
  92. unsigned int rspamd_lru_hash_capacity(rspamd_lru_hash_t *hash);
  93. #ifdef __cplusplus
  94. }
  95. #endif
  96. #endif