sessiond: main.cpp: iterate on list using list_iteration_adapter
[lttng-tools.git] / src / common / hashtable / hashtable.hpp
... / ...
CommitLineData
1/*
2 * Copyright (C) 2011 EfficiOS Inc.
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 <common/macros.hpp>
12
13#include <lttng/lttng-export.h>
14
15#include <memory>
16#include <stdint.h>
17#include <urcu.h>
18#include <urcu/rculfhash.h>
19
20LTTNG_EXPORT extern unsigned long lttng_ht_seed;
21
22using hash_fct_type = unsigned long (*)(const void *, unsigned long);
23using hash_match_fct = cds_lfht_match_fct;
24
25enum lttng_ht_type {
26 LTTNG_HT_TYPE_STRING,
27 LTTNG_HT_TYPE_ULONG,
28 LTTNG_HT_TYPE_U64,
29 LTTNG_HT_TYPE_TWO_U64,
30};
31
32struct lttng_ht_deleter;
33
34struct lttng_ht {
35 using uptr = std::unique_ptr<lttng_ht, lttng_ht_deleter>;
36 struct cds_lfht *ht;
37 cds_lfht_match_fct match_fct;
38 hash_fct_type hash_fct;
39};
40
41struct lttng_ht_iter {
42 struct cds_lfht_iter iter;
43};
44
45struct lttng_ht_node {
46 struct cds_lfht_node node;
47};
48
49struct lttng_ht_node_str : public lttng_ht_node {
50 char *key;
51 struct rcu_head head;
52};
53
54struct lttng_ht_node_ulong : public lttng_ht_node {
55 unsigned long key;
56 struct rcu_head head;
57};
58
59struct lttng_ht_node_u64 : public lttng_ht_node {
60 uint64_t key;
61 struct rcu_head head;
62};
63
64struct lttng_ht_two_u64 {
65 uint64_t key1;
66 uint64_t key2;
67};
68
69struct lttng_ht_node_two_u64 : public lttng_ht_node {
70 struct lttng_ht_two_u64 key;
71 struct rcu_head head;
72};
73
74/* Hashtable new and destroy */
75struct lttng_ht *lttng_ht_new(unsigned long size, enum lttng_ht_type type);
76void lttng_ht_destroy(struct lttng_ht *ht);
77struct lttng_ht_deleter {
78 void operator()(lttng_ht *ht)
79 {
80 lttng_ht_destroy(ht);
81 };
82};
83
84/* Specialized node init and free functions */
85void lttng_ht_node_init_str(struct lttng_ht_node_str *node, char *key);
86void lttng_ht_node_init_ulong(struct lttng_ht_node_ulong *node, unsigned long key);
87void lttng_ht_node_init_u64(struct lttng_ht_node_u64 *node, uint64_t key);
88void lttng_ht_node_init_two_u64(struct lttng_ht_node_two_u64 *node, uint64_t key1, uint64_t key2);
89void lttng_ht_node_free_str(struct lttng_ht_node_str *node);
90void lttng_ht_node_free_ulong(struct lttng_ht_node_ulong *node);
91void lttng_ht_node_free_u64(struct lttng_ht_node_u64 *node);
92void lttng_ht_node_free_two_u64(struct lttng_ht_node_two_u64 *node);
93
94void lttng_ht_lookup(struct lttng_ht *ht, const void *key, struct lttng_ht_iter *iter);
95
96/* Specialized add unique functions */
97void lttng_ht_add_unique_str(struct lttng_ht *ht, struct lttng_ht_node_str *node);
98void lttng_ht_add_unique_ulong(struct lttng_ht *ht, struct lttng_ht_node_ulong *node);
99void lttng_ht_add_unique_u64(struct lttng_ht *ht, struct lttng_ht_node_u64 *node);
100void lttng_ht_add_unique_two_u64(struct lttng_ht *ht, struct lttng_ht_node_two_u64 *node);
101struct lttng_ht_node_ulong *lttng_ht_add_replace_ulong(struct lttng_ht *ht,
102 struct lttng_ht_node_ulong *node);
103struct lttng_ht_node_u64 *lttng_ht_add_replace_u64(struct lttng_ht *ht,
104 struct lttng_ht_node_u64 *node);
105void lttng_ht_add_str(struct lttng_ht *ht, struct lttng_ht_node_str *node);
106void lttng_ht_add_ulong(struct lttng_ht *ht, struct lttng_ht_node_ulong *node);
107void lttng_ht_add_u64(struct lttng_ht *ht, struct lttng_ht_node_u64 *node);
108
109int lttng_ht_del(struct lttng_ht *ht, struct lttng_ht_iter *iter);
110
111void lttng_ht_get_first(struct lttng_ht *ht, struct lttng_ht_iter *iter);
112void lttng_ht_get_next(struct lttng_ht *ht, struct lttng_ht_iter *iter);
113
114unsigned long lttng_ht_get_count(struct lttng_ht *ht);
115
116template <class NodeType>
117NodeType *lttng_ht_iter_get_node(const lttng_ht_iter *iter)
118{
119 LTTNG_ASSERT(iter);
120
121 auto node = iter->iter.node;
122 if (!node) {
123 return nullptr;
124 }
125
126 return reinterpret_cast<NodeType *>(lttng::utils::container_of(node, &NodeType::node));
127}
128
129template <class ParentType, class NodeType>
130ParentType *lttng_ht_node_container_of(cds_lfht_node *node, const NodeType ParentType::*Member)
131{
132 /*
133 * The node member is within lttng_ht_node, the parent class of all ht wrapper nodes. We
134 * compute the address of the ht wrapper node from the native lfht node.
135 */
136 auto *wrapper_node = lttng::utils::container_of(node, &NodeType::node);
137
138 /* Knowing the address of the wrapper node, we can get that of the contained type. */
139 return lttng::utils::container_of(static_cast<NodeType *>(wrapper_node), Member);
140}
141
142#endif /* _LTT_HT_H */
This page took 0.023439 seconds and 5 git commands to generate.