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