2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <urcu/compiler.h>
25 #include <common/common.h>
26 #include <common/defaults.h>
28 #include "hashtable.h"
31 unsigned long lttng_ht_seed
;
33 static unsigned long min_hash_alloc_size
= 1;
34 static unsigned long max_hash_buckets_size
= 0;
37 * Match function for string node.
39 static int match_str(struct cds_lfht_node
*node
, const void *key
)
41 struct lttng_ht_node_str
*match_node
=
42 caa_container_of(node
, struct lttng_ht_node_str
, node
);
44 return hash_match_key_str(match_node
->key
, (void *) key
);
48 * Match function for ulong node.
50 static int match_ulong(struct cds_lfht_node
*node
, const void *key
)
52 struct lttng_ht_node_ulong
*match_node
=
53 caa_container_of(node
, struct lttng_ht_node_ulong
, node
);
55 return hash_match_key_ulong((void *) match_node
->key
, (void *) key
);
59 * Match function for u64 node.
61 static int match_u64(struct cds_lfht_node
*node
, const void *key
)
63 struct lttng_ht_node_u64
*match_node
=
64 caa_container_of(node
, struct lttng_ht_node_u64
, node
);
66 return hash_match_key_u64(&match_node
->key
, (void *) key
);
70 * Match function for two uint64_t node.
72 static int match_two_u64(struct cds_lfht_node
*node
, const void *key
)
74 struct lttng_ht_node_two_u64
*match_node
=
75 caa_container_of(node
, struct lttng_ht_node_two_u64
, node
);
77 return hash_match_key_two_u64((void *) &match_node
->key
, (void *) key
);
81 * Return an allocated lttng hashtable.
83 struct lttng_ht
*lttng_ht_new(unsigned long size
, int type
)
89 size
= DEFAULT_HT_SIZE
;
91 ht
= zmalloc(sizeof(*ht
));
93 PERROR("zmalloc lttng_ht");
97 ht
->ht
= cds_lfht_new(size
, min_hash_alloc_size
, max_hash_buckets_size
,
98 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
100 * There is already an assert in the RCU hashtable code so if the ht is
101 * NULL here there is a *huge* problem.
106 case LTTNG_HT_TYPE_STRING
:
107 ht
->match_fct
= match_str
;
108 ht
->hash_fct
= hash_key_str
;
110 case LTTNG_HT_TYPE_ULONG
:
111 ht
->match_fct
= match_ulong
;
112 ht
->hash_fct
= hash_key_ulong
;
114 case LTTNG_HT_TYPE_U64
:
115 ht
->match_fct
= match_u64
;
116 ht
->hash_fct
= hash_key_u64
;
118 case LTTNG_HT_TYPE_TWO_U64
:
119 ht
->match_fct
= match_two_u64
;
120 ht
->hash_fct
= hash_key_two_u64
;
123 ERR("Unknown lttng hashtable type %d", type
);
124 lttng_ht_destroy(ht
);
128 DBG3("Created hashtable size %lu at %p of type %d", size
, ht
->ht
, type
);
137 * Free a lttng hashtable.
139 void lttng_ht_destroy(struct lttng_ht
*ht
)
143 ret
= cds_lfht_destroy(ht
->ht
, NULL
);
149 * Init lttng ht node string.
151 void lttng_ht_node_init_str(struct lttng_ht_node_str
*node
, char *key
)
156 cds_lfht_node_init(&node
->node
);
160 * Init lttng ht node unsigned long.
162 void lttng_ht_node_init_ulong(struct lttng_ht_node_ulong
*node
,
168 cds_lfht_node_init(&node
->node
);
172 * Init lttng ht node uint64_t.
174 void lttng_ht_node_init_u64(struct lttng_ht_node_u64
*node
,
180 cds_lfht_node_init(&node
->node
);
184 * Init lttng ht node with two uint64_t.
186 void lttng_ht_node_init_two_u64(struct lttng_ht_node_two_u64
*node
,
187 uint64_t key1
, uint64_t key2
)
191 node
->key
.key1
= key1
;
192 node
->key
.key2
= key2
;
193 cds_lfht_node_init(&node
->node
);
197 * Free lttng ht node string.
199 void lttng_ht_node_free_str(struct lttng_ht_node_str
*node
)
206 * Free lttng ht node unsigned long.
208 void lttng_ht_node_free_ulong(struct lttng_ht_node_ulong
*node
)
215 * Free lttng ht node uint64_t.
217 void lttng_ht_node_free_u64(struct lttng_ht_node_u64
*node
)
224 * Free lttng ht node two uint64_t.
226 void lttng_ht_node_free_two_u64(struct lttng_ht_node_two_u64
*node
)
233 * Lookup function in hashtable.
235 void lttng_ht_lookup(struct lttng_ht
*ht
, void *key
,
236 struct lttng_ht_iter
*iter
)
241 cds_lfht_lookup(ht
->ht
, ht
->hash_fct(key
, lttng_ht_seed
),
242 ht
->match_fct
, key
, &iter
->iter
);
246 * Add unique string node to hashtable.
248 void lttng_ht_add_unique_str(struct lttng_ht
*ht
,
249 struct lttng_ht_node_str
*node
)
251 struct cds_lfht_node
*node_ptr
;
256 node_ptr
= cds_lfht_add_unique(ht
->ht
, ht
->hash_fct(node
->key
, lttng_ht_seed
),
257 ht
->match_fct
, node
->key
, &node
->node
);
258 assert(node_ptr
== &node
->node
);
262 * Add string node to hashtable.
264 void lttng_ht_add_str(struct lttng_ht
*ht
,
265 struct lttng_ht_node_str
*node
)
271 cds_lfht_add(ht
->ht
, ht
->hash_fct(node
->key
, lttng_ht_seed
),
276 * Add unsigned long node to hashtable.
278 void lttng_ht_add_ulong(struct lttng_ht
*ht
, struct lttng_ht_node_ulong
*node
)
284 cds_lfht_add(ht
->ht
, ht
->hash_fct((void *) node
->key
, lttng_ht_seed
),
289 * Add uint64_t node to hashtable.
292 void lttng_ht_add_u64(struct lttng_ht
*ht
, struct lttng_ht_node_u64
*node
)
298 cds_lfht_add(ht
->ht
, ht
->hash_fct(&node
->key
, lttng_ht_seed
),
303 * Add unique unsigned long node to hashtable.
305 void lttng_ht_add_unique_ulong(struct lttng_ht
*ht
,
306 struct lttng_ht_node_ulong
*node
)
308 struct cds_lfht_node
*node_ptr
;
313 node_ptr
= cds_lfht_add_unique(ht
->ht
,
314 ht
->hash_fct((void *) node
->key
, lttng_ht_seed
), ht
->match_fct
,
315 (void *) node
->key
, &node
->node
);
316 assert(node_ptr
== &node
->node
);
320 * Add unique uint64_t node to hashtable.
322 void lttng_ht_add_unique_u64(struct lttng_ht
*ht
,
323 struct lttng_ht_node_u64
*node
)
325 struct cds_lfht_node
*node_ptr
;
330 node_ptr
= cds_lfht_add_unique(ht
->ht
,
331 ht
->hash_fct(&node
->key
, lttng_ht_seed
), ht
->match_fct
,
332 &node
->key
, &node
->node
);
333 assert(node_ptr
== &node
->node
);
337 * Add unique two uint64_t node to hashtable.
339 void lttng_ht_add_unique_two_u64(struct lttng_ht
*ht
,
340 struct lttng_ht_node_two_u64
*node
)
342 struct cds_lfht_node
*node_ptr
;
347 node_ptr
= cds_lfht_add_unique(ht
->ht
,
348 ht
->hash_fct((void *) &node
->key
, lttng_ht_seed
), ht
->match_fct
,
349 (void *) &node
->key
, &node
->node
);
350 assert(node_ptr
== &node
->node
);
354 * Add replace unsigned long node to hashtable.
356 struct lttng_ht_node_ulong
*lttng_ht_add_replace_ulong(struct lttng_ht
*ht
,
357 struct lttng_ht_node_ulong
*node
)
359 struct cds_lfht_node
*node_ptr
;
364 node_ptr
= cds_lfht_add_replace(ht
->ht
,
365 ht
->hash_fct((void *) node
->key
, lttng_ht_seed
), ht
->match_fct
,
366 (void *) node
->key
, &node
->node
);
370 return caa_container_of(node_ptr
, struct lttng_ht_node_ulong
, node
);
372 assert(node_ptr
== &node
->node
);
376 * Add replace unsigned long node to hashtable.
378 struct lttng_ht_node_u64
*lttng_ht_add_replace_u64(struct lttng_ht
*ht
,
379 struct lttng_ht_node_u64
*node
)
381 struct cds_lfht_node
*node_ptr
;
386 node_ptr
= cds_lfht_add_replace(ht
->ht
,
387 ht
->hash_fct(&node
->key
, lttng_ht_seed
), ht
->match_fct
,
388 &node
->key
, &node
->node
);
392 return caa_container_of(node_ptr
, struct lttng_ht_node_u64
, node
);
394 assert(node_ptr
== &node
->node
);
398 * Delete node from hashtable.
400 int lttng_ht_del(struct lttng_ht
*ht
, struct lttng_ht_iter
*iter
)
406 return cds_lfht_del(ht
->ht
, iter
->iter
.node
);
410 * Get first node in the hashtable.
412 void lttng_ht_get_first(struct lttng_ht
*ht
, struct lttng_ht_iter
*iter
)
418 cds_lfht_first(ht
->ht
, &iter
->iter
);
422 * Get next node in the hashtable.
424 void lttng_ht_get_next(struct lttng_ht
*ht
, struct lttng_ht_iter
*iter
)
430 cds_lfht_next(ht
->ht
, &iter
->iter
);
434 * Return the number of nodes in the hashtable.
436 unsigned long lttng_ht_get_count(struct lttng_ht
*ht
)
444 cds_lfht_count_nodes(ht
->ht
, &scb
, &count
, &sca
);
450 * Return lttng ht string node from iterator.
452 struct lttng_ht_node_str
*lttng_ht_iter_get_node_str(
453 struct lttng_ht_iter
*iter
)
455 struct cds_lfht_node
*node
;
458 node
= cds_lfht_iter_get_node(&iter
->iter
);
462 return caa_container_of(node
, struct lttng_ht_node_str
, node
);
466 * Return lttng ht unsigned long node from iterator.
468 struct lttng_ht_node_ulong
*lttng_ht_iter_get_node_ulong(
469 struct lttng_ht_iter
*iter
)
471 struct cds_lfht_node
*node
;
474 node
= cds_lfht_iter_get_node(&iter
->iter
);
478 return caa_container_of(node
, struct lttng_ht_node_ulong
, node
);
482 * Return lttng ht unsigned long node from iterator.
484 struct lttng_ht_node_u64
*lttng_ht_iter_get_node_u64(
485 struct lttng_ht_iter
*iter
)
487 struct cds_lfht_node
*node
;
490 node
= cds_lfht_iter_get_node(&iter
->iter
);
494 return caa_container_of(node
, struct lttng_ht_node_u64
, node
);
498 * Return lttng ht stream and index id node from iterator.
500 struct lttng_ht_node_two_u64
*lttng_ht_iter_get_node_two_u64(
501 struct lttng_ht_iter
*iter
)
503 struct cds_lfht_node
*node
;
506 node
= cds_lfht_iter_get_node(&iter
->iter
);
510 return caa_container_of(node
, struct lttng_ht_node_two_u64
, node
);
516 static void __attribute__((constructor
)) _init(void)
518 /* Init hash table seed */
519 lttng_ht_seed
= (unsigned long) time(NULL
);
This page took 0.03894 seconds and 4 git commands to generate.