| 1 | /* |
| 2 | * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca> |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0-only |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #ifndef _LTT_HT_H |
| 9 | #define _LTT_HT_H |
| 10 | |
| 11 | #include <urcu.h> |
| 12 | #include <stdint.h> |
| 13 | |
| 14 | #include <common/macros.h> |
| 15 | #include <lttng/lttng-export.h> |
| 16 | #include <urcu/rculfhash.h> |
| 17 | |
| 18 | #ifdef __cplusplus |
| 19 | extern "C" { |
| 20 | #endif |
| 21 | |
| 22 | LTTNG_EXPORT extern unsigned long lttng_ht_seed; |
| 23 | |
| 24 | typedef unsigned long (*hash_fct_type)(const void *_key, unsigned long seed); |
| 25 | typedef cds_lfht_match_fct hash_match_fct; |
| 26 | |
| 27 | enum lttng_ht_type { |
| 28 | LTTNG_HT_TYPE_STRING, |
| 29 | LTTNG_HT_TYPE_ULONG, |
| 30 | LTTNG_HT_TYPE_U64, |
| 31 | LTTNG_HT_TYPE_TWO_U64, |
| 32 | }; |
| 33 | |
| 34 | struct lttng_ht { |
| 35 | struct cds_lfht *ht; |
| 36 | cds_lfht_match_fct match_fct; |
| 37 | hash_fct_type hash_fct; |
| 38 | }; |
| 39 | |
| 40 | struct lttng_ht_iter { |
| 41 | struct cds_lfht_iter iter; |
| 42 | }; |
| 43 | |
| 44 | struct lttng_ht_node_str { |
| 45 | char *key; |
| 46 | struct cds_lfht_node node; |
| 47 | struct rcu_head head; |
| 48 | }; |
| 49 | |
| 50 | struct lttng_ht_node_ulong { |
| 51 | unsigned long key; |
| 52 | struct cds_lfht_node node; |
| 53 | struct rcu_head head; |
| 54 | }; |
| 55 | |
| 56 | struct lttng_ht_node_u64 { |
| 57 | uint64_t key; |
| 58 | struct cds_lfht_node node; |
| 59 | struct rcu_head head; |
| 60 | }; |
| 61 | |
| 62 | struct lttng_ht_two_u64 { |
| 63 | uint64_t key1; |
| 64 | uint64_t key2; |
| 65 | }; |
| 66 | |
| 67 | struct lttng_ht_node_two_u64 { |
| 68 | struct lttng_ht_two_u64 key; |
| 69 | struct cds_lfht_node node; |
| 70 | struct rcu_head head; |
| 71 | }; |
| 72 | |
| 73 | /* Hashtable new and destroy */ |
| 74 | struct lttng_ht *lttng_ht_new(unsigned long size, int type); |
| 75 | void lttng_ht_destroy(struct lttng_ht *ht); |
| 76 | |
| 77 | /* Specialized node init and free functions */ |
| 78 | void lttng_ht_node_init_str(struct lttng_ht_node_str *node, char *key); |
| 79 | void lttng_ht_node_init_ulong(struct lttng_ht_node_ulong *node, |
| 80 | unsigned long key); |
| 81 | void lttng_ht_node_init_u64(struct lttng_ht_node_u64 *node, |
| 82 | uint64_t key); |
| 83 | void lttng_ht_node_init_two_u64(struct lttng_ht_node_two_u64 *node, |
| 84 | uint64_t key1, uint64_t key2); |
| 85 | void lttng_ht_node_free_str(struct lttng_ht_node_str *node); |
| 86 | void lttng_ht_node_free_ulong(struct lttng_ht_node_ulong *node); |
| 87 | void lttng_ht_node_free_u64(struct lttng_ht_node_u64 *node); |
| 88 | void lttng_ht_node_free_two_u64(struct lttng_ht_node_two_u64 *node); |
| 89 | |
| 90 | void lttng_ht_lookup(struct lttng_ht *ht, const void *key, |
| 91 | struct lttng_ht_iter *iter); |
| 92 | |
| 93 | /* Specialized add unique functions */ |
| 94 | void lttng_ht_add_unique_str(struct lttng_ht *ht, |
| 95 | struct lttng_ht_node_str *node); |
| 96 | void lttng_ht_add_unique_ulong(struct lttng_ht *ht, |
| 97 | struct lttng_ht_node_ulong *node); |
| 98 | void lttng_ht_add_unique_u64(struct lttng_ht *ht, |
| 99 | struct lttng_ht_node_u64 *node); |
| 100 | void lttng_ht_add_unique_two_u64(struct lttng_ht *ht, |
| 101 | struct lttng_ht_node_two_u64 *node); |
| 102 | struct lttng_ht_node_ulong *lttng_ht_add_replace_ulong( |
| 103 | struct lttng_ht *ht, struct lttng_ht_node_ulong *node); |
| 104 | struct lttng_ht_node_u64 *lttng_ht_add_replace_u64( |
| 105 | struct lttng_ht *ht, struct lttng_ht_node_u64 *node); |
| 106 | void lttng_ht_add_str(struct lttng_ht *ht, |
| 107 | struct lttng_ht_node_str *node); |
| 108 | void lttng_ht_add_ulong(struct lttng_ht *ht, |
| 109 | struct lttng_ht_node_ulong *node); |
| 110 | void lttng_ht_add_u64(struct lttng_ht *ht, |
| 111 | struct lttng_ht_node_u64 *node); |
| 112 | |
| 113 | int lttng_ht_del(struct lttng_ht *ht, struct lttng_ht_iter *iter); |
| 114 | |
| 115 | void lttng_ht_get_first(struct lttng_ht *ht, |
| 116 | struct lttng_ht_iter *iter); |
| 117 | void lttng_ht_get_next(struct lttng_ht *ht, struct lttng_ht_iter *iter); |
| 118 | |
| 119 | unsigned long lttng_ht_get_count(struct lttng_ht *ht); |
| 120 | |
| 121 | struct lttng_ht_node_str *lttng_ht_iter_get_node_str( |
| 122 | struct lttng_ht_iter *iter); |
| 123 | struct lttng_ht_node_ulong *lttng_ht_iter_get_node_ulong( |
| 124 | struct lttng_ht_iter *iter); |
| 125 | struct lttng_ht_node_u64 *lttng_ht_iter_get_node_u64( |
| 126 | struct lttng_ht_iter *iter); |
| 127 | struct lttng_ht_node_two_u64 *lttng_ht_iter_get_node_two_u64( |
| 128 | struct lttng_ht_iter *iter); |
| 129 | |
| 130 | #ifdef __cplusplus |
| 131 | } |
| 132 | #endif |
| 133 | |
| 134 | #endif /* _LTT_HT_H */ |