c_rhash.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright: SPDX-License-Identifier: GPL-3.0-only
  2. #include <sys/types.h>
  3. #include <stdint.h>
  4. #include <stddef.h>
  5. #ifndef DEFAULT_BIN_COUNT
  6. #define DEFAULT_BIN_COUNT 1000
  7. #endif
  8. #define ITEMTYPE_UNSET (0x0)
  9. #define ITEMTYPE_STRING (0x1)
  10. #define ITEMTYPE_UINT8 (0x2)
  11. #define ITEMTYPE_UINT64 (0x3)
  12. #define ITEMTYPE_OPAQUE_PTR (0x4)
  13. typedef struct c_rhash_s *c_rhash;
  14. c_rhash c_rhash_new(size_t bin_count);
  15. void c_rhash_destroy(c_rhash hash);
  16. // # Insert
  17. // ## Insert where key is string
  18. int c_rhash_insert_str_ptr(c_rhash hash, const char *key, void *value);
  19. int c_rhash_insert_str_uint8(c_rhash hash, const char *key, uint8_t value);
  20. // ## Insert where key is uint64
  21. int c_rhash_insert_uint64_ptr(c_rhash hash, uint64_t key, void *value);
  22. // # Get
  23. // ## Get where key is string
  24. int c_rhash_get_ptr_by_str(c_rhash hash, const char *key, void **ret_val);
  25. int c_rhash_get_uint8_by_str(c_rhash hash, const char *key, uint8_t *ret_val);
  26. // ## Get where key is uint64
  27. int c_rhash_get_ptr_by_uint64(c_rhash hash, uint64_t key, void **ret_val);
  28. typedef struct {
  29. size_t bin;
  30. struct bin_item *item;
  31. int initialized;
  32. } c_rhash_iter_t;
  33. #define C_RHASH_ITER_T_INITIALIZER { .bin = 0, .item = NULL, .initialized = 0 }
  34. #define c_rhash_iter_t_initialize(p_iter) memset(p_iter, 0, sizeof(c_rhash_iter_t))
  35. /*
  36. * goes trough whole hash map and returns every
  37. * type uint64 key present/stored
  38. *
  39. * it is not necessary to finish iterating and iterator can be reinitialized
  40. * there are no guarantees on the order in which the keys will come
  41. * behavior here is implementation dependent and can change any time
  42. *
  43. * returns:
  44. * 0 for every key and stores the key in *key
  45. * 1 on error or when all keys of this type has been already iterated over
  46. */
  47. int c_rhash_iter_uint64_keys(c_rhash hash, c_rhash_iter_t *iter, uint64_t *key);
  48. int c_rhash_iter_str_keys(c_rhash hash, c_rhash_iter_t *iter, const char **key);