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.
22 #include <urcu/compiler.h>
24 #include <common/common.h>
25 #include <common/defaults.h>
27 #include "hashtable.h"
30 unsigned long lttng_ht_seed
;
32 static unsigned long min_hash_alloc_size
= 1;
33 static unsigned long max_hash_buckets_size
= 0;
36 * Match function for string node.
38 static int match_str(struct cds_lfht_node
*node
, const void *key
)
40 struct lttng_ht_node_str
*match_node
=
41 caa_container_of(node
, struct lttng_ht_node_str
, node
);
43 return hash_match_key_str(match_node
->key
, (void *) key
);
47 * Match function for ulong node.
49 static int match_ulong(struct cds_lfht_node
*node
, const void *key
)
51 struct lttng_ht_node_ulong
*match_node
=
52 caa_container_of(node
, struct lttng_ht_node_ulong
, node
);
54 return hash_match_key_ulong((void *) match_node
->key
, (void *) key
);
58 * Match function for u64 node.
60 static int match_u64(struct cds_lfht_node
*node
, const void *key
)
62 struct lttng_ht_node_u64
*match_node
=
63 caa_container_of(node
, struct lttng_ht_node_u64
, node
);
65 return hash_match_key_u64(&match_node
->key
, (void *) key
);
69 * Return an allocated lttng hashtable.
71 struct lttng_ht
*lttng_ht_new(unsigned long size
, int type
)
77 size
= DEFAULT_HT_SIZE
;
79 ht
= zmalloc(sizeof(*ht
));
81 PERROR("zmalloc lttng_ht");
85 ht
->ht
= cds_lfht_new(size
, min_hash_alloc_size
, max_hash_buckets_size
,
86 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
88 * There is already an assert in the RCU hashtable code so if the ht is
89 * NULL here there is a *huge* problem.
94 case LTTNG_HT_TYPE_STRING
:
95 ht
->match_fct
= match_str
;
96 ht
->hash_fct
= hash_key_str
;
98 case LTTNG_HT_TYPE_ULONG
:
99 ht
->match_fct
= match_ulong
;
100 ht
->hash_fct
= hash_key_ulong
;
102 case LTTNG_HT_TYPE_U64
:
103 ht
->match_fct
= match_u64
;
104 ht
->hash_fct
= hash_key_u64
;
107 ERR("Unknown lttng hashtable type %d", type
);
111 DBG3("Created hashtable size %lu at %p of type %d", size
, ht
->ht
, type
);
120 * Free a lttng hashtable.
122 void lttng_ht_destroy(struct lttng_ht
*ht
)
126 ret
= cds_lfht_destroy(ht
->ht
, NULL
);
132 * Init lttng ht node string.
134 void lttng_ht_node_init_str(struct lttng_ht_node_str
*node
, char *key
)
139 cds_lfht_node_init(&node
->node
);
143 * Init lttng ht node unsigned long.
145 void lttng_ht_node_init_ulong(struct lttng_ht_node_ulong
*node
,
151 cds_lfht_node_init(&node
->node
);
155 * Init lttng ht node uint64_t.
157 void lttng_ht_node_init_u64(struct lttng_ht_node_u64
*node
,
163 cds_lfht_node_init(&node
->node
);
167 * Free lttng ht node string.
169 void lttng_ht_node_free_str(struct lttng_ht_node_str
*node
)
176 * Free lttng ht node unsigned long.
178 void lttng_ht_node_free_ulong(struct lttng_ht_node_ulong
*node
)
185 * Free lttng ht node uint64_t.
187 void lttng_ht_node_free_u64(struct lttng_ht_node_u64
*node
)
194 * Lookup function in hashtable.
196 void lttng_ht_lookup(struct lttng_ht
*ht
, void *key
,
197 struct lttng_ht_iter
*iter
)
202 cds_lfht_lookup(ht
->ht
, ht
->hash_fct(key
, lttng_ht_seed
),
203 ht
->match_fct
, key
, &iter
->iter
);
207 * Add unique string node to hashtable.
209 void lttng_ht_add_unique_str(struct lttng_ht
*ht
,
210 struct lttng_ht_node_str
*node
)
212 struct cds_lfht_node
*node_ptr
;
217 node_ptr
= cds_lfht_add_unique(ht
->ht
, ht
->hash_fct(node
->key
, lttng_ht_seed
),
218 ht
->match_fct
, node
->key
, &node
->node
);
219 assert(node_ptr
== &node
->node
);
223 * Add unsigned long node to hashtable.
225 void lttng_ht_add_ulong(struct lttng_ht
*ht
, struct lttng_ht_node_ulong
*node
)
231 cds_lfht_add(ht
->ht
, ht
->hash_fct((void *) node
->key
, lttng_ht_seed
),
236 * Add uint64_t node to hashtable.
239 void lttng_ht_add_u64(struct lttng_ht
*ht
, struct lttng_ht_node_u64
*node
)
245 cds_lfht_add(ht
->ht
, ht
->hash_fct(&node
->key
, lttng_ht_seed
),
250 * Add unique unsigned long node to hashtable.
252 void lttng_ht_add_unique_ulong(struct lttng_ht
*ht
,
253 struct lttng_ht_node_ulong
*node
)
255 struct cds_lfht_node
*node_ptr
;
260 node_ptr
= cds_lfht_add_unique(ht
->ht
,
261 ht
->hash_fct((void *) node
->key
, lttng_ht_seed
), ht
->match_fct
,
262 (void *) node
->key
, &node
->node
);
263 assert(node_ptr
== &node
->node
);
267 * Add unique uint64_t node to hashtable.
269 void lttng_ht_add_unique_u64(struct lttng_ht
*ht
,
270 struct lttng_ht_node_u64
*node
)
272 struct cds_lfht_node
*node_ptr
;
277 node_ptr
= cds_lfht_add_unique(ht
->ht
,
278 ht
->hash_fct(&node
->key
, lttng_ht_seed
), ht
->match_fct
,
279 &node
->key
, &node
->node
);
280 assert(node_ptr
== &node
->node
);
284 * Add replace unsigned long node to hashtable.
286 struct lttng_ht_node_ulong
*lttng_ht_add_replace_ulong(struct lttng_ht
*ht
,
287 struct lttng_ht_node_ulong
*node
)
289 struct cds_lfht_node
*node_ptr
;
294 node_ptr
= cds_lfht_add_replace(ht
->ht
,
295 ht
->hash_fct((void *) node
->key
, lttng_ht_seed
), ht
->match_fct
,
296 (void *) node
->key
, &node
->node
);
300 return caa_container_of(node_ptr
, struct lttng_ht_node_ulong
, node
);
302 assert(node_ptr
== &node
->node
);
306 * Add replace unsigned long node to hashtable.
308 struct lttng_ht_node_u64
*lttng_ht_add_replace_u64(struct lttng_ht
*ht
,
309 struct lttng_ht_node_u64
*node
)
311 struct cds_lfht_node
*node_ptr
;
316 node_ptr
= cds_lfht_add_replace(ht
->ht
,
317 ht
->hash_fct(&node
->key
, lttng_ht_seed
), ht
->match_fct
,
318 &node
->key
, &node
->node
);
322 return caa_container_of(node_ptr
, struct lttng_ht_node_u64
, node
);
324 assert(node_ptr
== &node
->node
);
328 * Delete node from hashtable.
330 int lttng_ht_del(struct lttng_ht
*ht
, struct lttng_ht_iter
*iter
)
336 return cds_lfht_del(ht
->ht
, iter
->iter
.node
);
340 * Get first node in the hashtable.
342 void lttng_ht_get_first(struct lttng_ht
*ht
, struct lttng_ht_iter
*iter
)
348 cds_lfht_first(ht
->ht
, &iter
->iter
);
352 * Get next node in the hashtable.
354 void lttng_ht_get_next(struct lttng_ht
*ht
, struct lttng_ht_iter
*iter
)
360 cds_lfht_next(ht
->ht
, &iter
->iter
);
364 * Return the number of nodes in the hashtable.
366 unsigned long lttng_ht_get_count(struct lttng_ht
*ht
)
374 cds_lfht_count_nodes(ht
->ht
, &scb
, &count
, &sca
);
380 * Return lttng ht string node from iterator.
382 struct lttng_ht_node_str
*lttng_ht_iter_get_node_str(
383 struct lttng_ht_iter
*iter
)
385 struct cds_lfht_node
*node
;
388 node
= cds_lfht_iter_get_node(&iter
->iter
);
392 return caa_container_of(node
, struct lttng_ht_node_str
, node
);
396 * Return lttng ht unsigned long node from iterator.
398 struct lttng_ht_node_ulong
*lttng_ht_iter_get_node_ulong(
399 struct lttng_ht_iter
*iter
)
401 struct cds_lfht_node
*node
;
404 node
= cds_lfht_iter_get_node(&iter
->iter
);
408 return caa_container_of(node
, struct lttng_ht_node_ulong
, node
);
412 * Return lttng ht unsigned long node from iterator.
414 struct lttng_ht_node_u64
*lttng_ht_iter_get_node_u64(
415 struct lttng_ht_iter
*iter
)
417 struct cds_lfht_node
*node
;
420 node
= cds_lfht_iter_get_node(&iter
->iter
);
424 return caa_container_of(node
, struct lttng_ht_node_u64
, node
);
430 static void __attribute__((constructor
)) _init(void)
432 /* Init hash table seed */
433 lttng_ht_seed
= (unsigned long) time(NULL
);
This page took 0.075841 seconds and 4 git commands to generate.