2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; only version 2 of the License.
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 with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include <urcu/compiler.h>
24 #include <common/common.h>
25 #include <common/defaults.h>
27 #include "hashtable.h"
30 #define HASH_SEED 0x42UL /* The answer to life */
32 static unsigned long min_hash_alloc_size
= 1;
33 static unsigned long max_hash_buckets_size
= (1UL << 20);
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 * Return an allocated lttng hashtable.
60 struct lttng_ht
*lttng_ht_new(unsigned long size
, int type
)
66 size
= DEFAULT_HT_SIZE
;
68 ht
= zmalloc(sizeof(*ht
));
70 PERROR("zmalloc lttng_ht");
74 ht
->ht
= cds_lfht_new(size
, min_hash_alloc_size
, max_hash_buckets_size
,
75 CDS_LFHT_AUTO_RESIZE
, NULL
);
77 * There is already an assert in the RCU hashtable code so if the ht is
78 * NULL here there is a *huge* problem.
83 case LTTNG_HT_TYPE_STRING
:
84 ht
->match_fct
= match_str
;
85 ht
->hash_fct
= hash_key_str
;
87 case LTTNG_HT_TYPE_ULONG
:
88 ht
->match_fct
= match_ulong
;
89 ht
->hash_fct
= hash_key_ulong
;
92 ERR("Unknown lttng hashtable type %d", type
);
96 DBG3("Created hashtable size %lu at %p of type %d", size
, ht
->ht
, type
);
105 * Free a lttng hashtable.
107 void lttng_ht_destroy(struct lttng_ht
*ht
)
111 ret
= cds_lfht_destroy(ht
->ht
, NULL
);
116 * Init lttng ht node string.
118 void lttng_ht_node_init_str(struct lttng_ht_node_str
*node
, char *key
)
123 cds_lfht_node_init(&node
->node
);
127 * Init lttng ht node unsigned long.
129 void lttng_ht_node_init_ulong(struct lttng_ht_node_ulong
*node
,
135 cds_lfht_node_init(&node
->node
);
139 * Free lttng ht node string.
141 void lttng_ht_node_free_str(struct lttng_ht_node_str
*node
)
148 * Free lttng ht node unsigned long.
150 void lttng_ht_node_free_ulong(struct lttng_ht_node_ulong
*node
)
157 * Lookup function in hashtable.
159 void lttng_ht_lookup(struct lttng_ht
*ht
, void *key
,
160 struct lttng_ht_iter
*iter
)
165 cds_lfht_lookup(ht
->ht
, ht
->hash_fct(key
, HASH_SEED
),
166 ht
->match_fct
, key
, &iter
->iter
);
170 * Add unique string node to hashtable.
172 void lttng_ht_add_unique_str(struct lttng_ht
*ht
,
173 struct lttng_ht_node_str
*node
)
175 struct cds_lfht_node
*node_ptr
;
180 node_ptr
= cds_lfht_add_unique(ht
->ht
, ht
->hash_fct(node
->key
, HASH_SEED
),
181 ht
->match_fct
, node
->key
, &node
->node
);
182 assert(node_ptr
== &node
->node
);
186 * Add unique unsigned long node to hashtable.
188 void lttng_ht_add_unique_ulong(struct lttng_ht
*ht
,
189 struct lttng_ht_node_ulong
*node
)
191 struct cds_lfht_node
*node_ptr
;
196 node_ptr
= cds_lfht_add_unique(ht
->ht
,
197 ht
->hash_fct((void *) node
->key
, HASH_SEED
), ht
->match_fct
,
198 (void *) node
->key
, &node
->node
);
199 assert(node_ptr
== &node
->node
);
203 * Add replace unsigned long node to hashtable.
205 struct lttng_ht_node_ulong
*lttng_ht_add_replace_ulong(struct lttng_ht
*ht
,
206 struct lttng_ht_node_ulong
*node
)
208 struct cds_lfht_node
*node_ptr
;
213 node_ptr
= cds_lfht_add_replace(ht
->ht
,
214 ht
->hash_fct((void *) node
->key
, HASH_SEED
), ht
->match_fct
,
215 (void *) node
->key
, &node
->node
);
219 return caa_container_of(node_ptr
, struct lttng_ht_node_ulong
, node
);
221 assert(node_ptr
== &node
->node
);
225 * Delete node from hashtable.
227 int lttng_ht_del(struct lttng_ht
*ht
, struct lttng_ht_iter
*iter
)
233 return cds_lfht_del(ht
->ht
, iter
->iter
.node
);
237 * Get first node in the hashtable.
239 void lttng_ht_get_first(struct lttng_ht
*ht
, struct lttng_ht_iter
*iter
)
245 cds_lfht_first(ht
->ht
, &iter
->iter
);
249 * Get next node in the hashtable.
251 void lttng_ht_get_next(struct lttng_ht
*ht
, struct lttng_ht_iter
*iter
)
257 cds_lfht_next(ht
->ht
, &iter
->iter
);
261 * Return the number of nodes in the hashtable.
263 unsigned long lttng_ht_get_count(struct lttng_ht
*ht
)
271 cds_lfht_count_nodes(ht
->ht
, &scb
, &count
, &sca
);
277 * Return lttng ht string node from iterator.
279 struct lttng_ht_node_str
*lttng_ht_iter_get_node_str(
280 struct lttng_ht_iter
*iter
)
282 struct cds_lfht_node
*node
;
285 node
= cds_lfht_iter_get_node(&iter
->iter
);
289 return caa_container_of(node
, struct lttng_ht_node_str
, node
);
293 * Return lttng ht unsigned long node from iterator.
295 struct lttng_ht_node_ulong
*lttng_ht_iter_get_node_ulong(
296 struct lttng_ht_iter
*iter
)
298 struct cds_lfht_node
*node
;
301 node
= cds_lfht_iter_get_node(&iter
->iter
);
305 return caa_container_of(node
, struct lttng_ht_node_ulong
, node
);
This page took 0.035237 seconds and 4 git commands to generate.