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 * Getter/lookup functions need to be called with RCU read-side lock
38 * held. However, modification functions (add, add_unique, replace, del)
39 * take the RCU lock internally, so it does not matter whether the
40 * caller hold the RCU lock or not.
44 * Match function for string node.
46 static int match_str(struct cds_lfht_node
*node
, const void *key
)
48 struct lttng_ht_node_str
*match_node
=
49 caa_container_of(node
, struct lttng_ht_node_str
, node
);
51 return hash_match_key_str(match_node
->key
, (void *) key
);
55 * Match function for ulong node.
57 static int match_ulong(struct cds_lfht_node
*node
, const void *key
)
59 struct lttng_ht_node_ulong
*match_node
=
60 caa_container_of(node
, struct lttng_ht_node_ulong
, node
);
62 return hash_match_key_ulong((void *) match_node
->key
, (void *) key
);
66 * Match function for u64 node.
68 static int match_u64(struct cds_lfht_node
*node
, const void *key
)
70 struct lttng_ht_node_u64
*match_node
=
71 caa_container_of(node
, struct lttng_ht_node_u64
, node
);
73 return hash_match_key_u64(&match_node
->key
, (void *) key
);
77 * Match function for two uint64_t node.
79 static int match_two_u64(struct cds_lfht_node
*node
, const void *key
)
81 struct lttng_ht_node_two_u64
*match_node
=
82 caa_container_of(node
, struct lttng_ht_node_two_u64
, node
);
84 return hash_match_key_two_u64((void *) &match_node
->key
, (void *) key
);
88 * Return an allocated lttng hashtable.
91 struct lttng_ht
*lttng_ht_new(unsigned long size
, int type
)
97 size
= DEFAULT_HT_SIZE
;
99 ht
= zmalloc(sizeof(*ht
));
101 PERROR("zmalloc lttng_ht");
105 ht
->ht
= cds_lfht_new(size
, min_hash_alloc_size
, max_hash_buckets_size
,
106 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
108 * There is already an assert in the RCU hashtable code so if the ht is
109 * NULL here there is a *huge* problem.
114 case LTTNG_HT_TYPE_STRING
:
115 ht
->match_fct
= match_str
;
116 ht
->hash_fct
= hash_key_str
;
118 case LTTNG_HT_TYPE_ULONG
:
119 ht
->match_fct
= match_ulong
;
120 ht
->hash_fct
= hash_key_ulong
;
122 case LTTNG_HT_TYPE_U64
:
123 ht
->match_fct
= match_u64
;
124 ht
->hash_fct
= hash_key_u64
;
126 case LTTNG_HT_TYPE_TWO_U64
:
127 ht
->match_fct
= match_two_u64
;
128 ht
->hash_fct
= hash_key_two_u64
;
131 ERR("Unknown lttng hashtable type %d", type
);
132 lttng_ht_destroy(ht
);
136 DBG3("Created hashtable size %lu at %p of type %d", size
, ht
->ht
, type
);
145 * Free a lttng hashtable.
148 void lttng_ht_destroy(struct lttng_ht
*ht
)
152 ret
= cds_lfht_destroy(ht
->ht
, NULL
);
158 * Init lttng ht node string.
161 void lttng_ht_node_init_str(struct lttng_ht_node_str
*node
, char *key
)
166 cds_lfht_node_init(&node
->node
);
170 * Init lttng ht node unsigned long.
173 void lttng_ht_node_init_ulong(struct lttng_ht_node_ulong
*node
,
179 cds_lfht_node_init(&node
->node
);
183 * Init lttng ht node uint64_t.
186 void lttng_ht_node_init_u64(struct lttng_ht_node_u64
*node
,
192 cds_lfht_node_init(&node
->node
);
196 * Init lttng ht node with two uint64_t.
199 void lttng_ht_node_init_two_u64(struct lttng_ht_node_two_u64
*node
,
200 uint64_t key1
, uint64_t key2
)
204 node
->key
.key1
= key1
;
205 node
->key
.key2
= key2
;
206 cds_lfht_node_init(&node
->node
);
210 * Free lttng ht node string.
213 void lttng_ht_node_free_str(struct lttng_ht_node_str
*node
)
220 * Free lttng ht node unsigned long.
223 void lttng_ht_node_free_ulong(struct lttng_ht_node_ulong
*node
)
230 * Free lttng ht node uint64_t.
233 void lttng_ht_node_free_u64(struct lttng_ht_node_u64
*node
)
240 * Free lttng ht node two uint64_t.
243 void lttng_ht_node_free_two_u64(struct lttng_ht_node_two_u64
*node
)
250 * Lookup function in hashtable.
253 void lttng_ht_lookup(struct lttng_ht
*ht
, void *key
,
254 struct lttng_ht_iter
*iter
)
259 cds_lfht_lookup(ht
->ht
, ht
->hash_fct(key
, lttng_ht_seed
),
260 ht
->match_fct
, key
, &iter
->iter
);
264 * Add unique string node to hashtable.
267 void lttng_ht_add_unique_str(struct lttng_ht
*ht
,
268 struct lttng_ht_node_str
*node
)
270 struct cds_lfht_node
*node_ptr
;
275 /* RCU read lock protects from ABA. */
277 node_ptr
= cds_lfht_add_unique(ht
->ht
, ht
->hash_fct(node
->key
, lttng_ht_seed
),
278 ht
->match_fct
, node
->key
, &node
->node
);
280 assert(node_ptr
== &node
->node
);
284 * Add string node to hashtable.
287 void lttng_ht_add_str(struct lttng_ht
*ht
,
288 struct lttng_ht_node_str
*node
)
294 /* RCU read lock protects from ABA. */
296 cds_lfht_add(ht
->ht
, ht
->hash_fct(node
->key
, lttng_ht_seed
),
302 * Add unsigned long node to hashtable.
305 void lttng_ht_add_ulong(struct lttng_ht
*ht
, struct lttng_ht_node_ulong
*node
)
311 /* RCU read lock protects from ABA. */
313 cds_lfht_add(ht
->ht
, ht
->hash_fct((void *) node
->key
, lttng_ht_seed
),
319 * Add uint64_t node to hashtable.
322 void lttng_ht_add_u64(struct lttng_ht
*ht
, struct lttng_ht_node_u64
*node
)
328 /* RCU read lock protects from ABA. */
330 cds_lfht_add(ht
->ht
, ht
->hash_fct(&node
->key
, lttng_ht_seed
),
336 * Add unique unsigned long node to hashtable.
339 void lttng_ht_add_unique_ulong(struct lttng_ht
*ht
,
340 struct lttng_ht_node_ulong
*node
)
342 struct cds_lfht_node
*node_ptr
;
347 /* RCU read lock protects from ABA. */
349 node_ptr
= cds_lfht_add_unique(ht
->ht
,
350 ht
->hash_fct((void *) node
->key
, lttng_ht_seed
), ht
->match_fct
,
351 (void *) node
->key
, &node
->node
);
353 assert(node_ptr
== &node
->node
);
357 * Add unique uint64_t node to hashtable.
360 void lttng_ht_add_unique_u64(struct lttng_ht
*ht
,
361 struct lttng_ht_node_u64
*node
)
363 struct cds_lfht_node
*node_ptr
;
368 /* RCU read lock protects from ABA. */
370 node_ptr
= cds_lfht_add_unique(ht
->ht
,
371 ht
->hash_fct(&node
->key
, lttng_ht_seed
), ht
->match_fct
,
372 &node
->key
, &node
->node
);
374 assert(node_ptr
== &node
->node
);
378 * Add unique two uint64_t node to hashtable.
381 void lttng_ht_add_unique_two_u64(struct lttng_ht
*ht
,
382 struct lttng_ht_node_two_u64
*node
)
384 struct cds_lfht_node
*node_ptr
;
389 /* RCU read lock protects from ABA. */
391 node_ptr
= cds_lfht_add_unique(ht
->ht
,
392 ht
->hash_fct((void *) &node
->key
, lttng_ht_seed
), ht
->match_fct
,
393 (void *) &node
->key
, &node
->node
);
395 assert(node_ptr
== &node
->node
);
399 * Add replace unsigned long node to hashtable.
402 struct lttng_ht_node_ulong
*lttng_ht_add_replace_ulong(struct lttng_ht
*ht
,
403 struct lttng_ht_node_ulong
*node
)
405 struct cds_lfht_node
*node_ptr
;
410 /* RCU read lock protects from ABA. */
412 node_ptr
= cds_lfht_add_replace(ht
->ht
,
413 ht
->hash_fct((void *) node
->key
, lttng_ht_seed
), ht
->match_fct
,
414 (void *) node
->key
, &node
->node
);
419 return caa_container_of(node_ptr
, struct lttng_ht_node_ulong
, node
);
421 assert(node_ptr
== &node
->node
);
425 * Add replace unsigned long node to hashtable.
428 struct lttng_ht_node_u64
*lttng_ht_add_replace_u64(struct lttng_ht
*ht
,
429 struct lttng_ht_node_u64
*node
)
431 struct cds_lfht_node
*node_ptr
;
436 /* RCU read lock protects from ABA. */
438 node_ptr
= cds_lfht_add_replace(ht
->ht
,
439 ht
->hash_fct(&node
->key
, lttng_ht_seed
), ht
->match_fct
,
440 &node
->key
, &node
->node
);
445 return caa_container_of(node_ptr
, struct lttng_ht_node_u64
, node
);
447 assert(node_ptr
== &node
->node
);
451 * Delete node from hashtable.
454 int lttng_ht_del(struct lttng_ht
*ht
, struct lttng_ht_iter
*iter
)
462 /* RCU read lock protects from ABA. */
464 ret
= cds_lfht_del(ht
->ht
, iter
->iter
.node
);
470 * Get first node in the hashtable.
473 void lttng_ht_get_first(struct lttng_ht
*ht
, struct lttng_ht_iter
*iter
)
479 cds_lfht_first(ht
->ht
, &iter
->iter
);
483 * Get next node in the hashtable.
486 void lttng_ht_get_next(struct lttng_ht
*ht
, struct lttng_ht_iter
*iter
)
492 cds_lfht_next(ht
->ht
, &iter
->iter
);
496 * Return the number of nodes in the hashtable.
499 unsigned long lttng_ht_get_count(struct lttng_ht
*ht
)
507 /* RCU read lock protects from ABA and allows RCU traversal. */
509 cds_lfht_count_nodes(ht
->ht
, &scb
, &count
, &sca
);
516 * Return lttng ht string node from iterator.
519 struct lttng_ht_node_str
*lttng_ht_iter_get_node_str(
520 struct lttng_ht_iter
*iter
)
522 struct cds_lfht_node
*node
;
525 node
= cds_lfht_iter_get_node(&iter
->iter
);
529 return caa_container_of(node
, struct lttng_ht_node_str
, node
);
533 * Return lttng ht unsigned long node from iterator.
536 struct lttng_ht_node_ulong
*lttng_ht_iter_get_node_ulong(
537 struct lttng_ht_iter
*iter
)
539 struct cds_lfht_node
*node
;
542 node
= cds_lfht_iter_get_node(&iter
->iter
);
546 return caa_container_of(node
, struct lttng_ht_node_ulong
, node
);
550 * Return lttng ht unsigned long node from iterator.
553 struct lttng_ht_node_u64
*lttng_ht_iter_get_node_u64(
554 struct lttng_ht_iter
*iter
)
556 struct cds_lfht_node
*node
;
559 node
= cds_lfht_iter_get_node(&iter
->iter
);
563 return caa_container_of(node
, struct lttng_ht_node_u64
, node
);
567 * Return lttng ht stream and index id node from iterator.
570 struct lttng_ht_node_two_u64
*lttng_ht_iter_get_node_two_u64(
571 struct lttng_ht_iter
*iter
)
573 struct cds_lfht_node
*node
;
576 node
= cds_lfht_iter_get_node(&iter
->iter
);
580 return caa_container_of(node
, struct lttng_ht_node_two_u64
, node
);
586 static void __attribute__((constructor
)) _init(void)
588 /* Init hash table seed */
589 lttng_ht_seed
= (unsigned long) time(NULL
);
This page took 0.043689 seconds and 4 git commands to generate.