A clone of btpd with my configuration changes.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

82 linhas
2.4 KiB

  1. #ifndef BTPD_HASHTABLE_H
  2. #define BTPD_HASHTABLE_H
  3. struct htbl_iter {
  4. struct _htbl *tbl;
  5. size_t bi;
  6. size_t cnt;
  7. struct _any *obj;
  8. };
  9. struct _htbl *_htbl_create(int (*equal)(const void *, const void *),
  10. uint32_t (*hash)(const void *), size_t keyoff, size_t chainoff);
  11. void _htbl_free(struct _htbl *tbl);
  12. void _htbl_insert(struct _htbl *tbl, struct _any *o);
  13. struct _any *_htbl_remove(struct _htbl *tbl, const void *key);
  14. struct _any *_htbl_find(struct _htbl *tbl, const void *key);
  15. void _htbl_tov(struct _htbl *tb, struct _any **v);
  16. size_t _htbl_size(struct _htbl *tbl);
  17. void _htbl_iter_init(struct _htbl *tbl, struct htbl_iter *it);
  18. struct _any *_htbl_iter_next(struct htbl_iter *it);
  19. #define HTBL_ENTRY(name) struct _any *name
  20. #define HTBL_TYPE(name, type, ktype, kname, cname) \
  21. __attribute__((always_inline)) static inline struct name * \
  22. name##_create(int (*equal)(const void *, const void *), \
  23. uint32_t (*hash)(const void *)) \
  24. { \
  25. return (struct name *) \
  26. _htbl_create(equal, hash, offsetof(struct type, kname), \
  27. offsetof(struct type, cname)); \
  28. } \
  29. \
  30. __attribute__((always_inline)) static inline struct type * \
  31. name##_find(struct name *tbl, const ktype *key) \
  32. { \
  33. return (struct type *)_htbl_find((struct _htbl *)tbl, key); \
  34. } \
  35. \
  36. __attribute__((always_inline)) static inline struct type * \
  37. name##_remove(struct name *tbl, const ktype *key) \
  38. { \
  39. return (struct type *)_htbl_remove((struct _htbl *)tbl, key); \
  40. } \
  41. \
  42. __attribute__((always_inline)) static inline void \
  43. name##_free(struct name *tbl) \
  44. { \
  45. _htbl_free((struct _htbl *)tbl); \
  46. } \
  47. \
  48. __attribute__((always_inline)) static inline void \
  49. name##_insert(struct name *tbl, struct type *o) \
  50. { \
  51. _htbl_insert((struct _htbl *)tbl, (struct _any *)o); \
  52. } \
  53. __attribute__((always_inline)) static inline void \
  54. name##_tov(struct name *tbl, struct type **v) \
  55. { \
  56. _htbl_tov((struct _htbl *)tbl, (struct _any **)v); \
  57. } \
  58. \
  59. __attribute__((always_inline)) static inline size_t \
  60. name##_size(struct name *tbl) \
  61. { \
  62. return _htbl_size((struct _htbl *)tbl); \
  63. } \
  64. \
  65. __attribute__((always_inline)) static inline void \
  66. name##_iter_init(struct name *tbl, struct htbl_iter *it) \
  67. { \
  68. _htbl_iter_init((struct _htbl *)tbl, it); \
  69. } \
  70. \
  71. __attribute__((always_inline)) static inline struct type * \
  72. name##_iter_next(struct htbl_iter *it) \
  73. { \
  74. return (struct type *)_htbl_iter_next(it); \
  75. }
  76. #endif