* [ltt-dev] [PATCH] lfhash: constify arguments to compare and hash function
@ 2011-11-16 4:24 Stephen Hemminger
2011-11-16 12:28 ` Mathieu Desnoyers
0 siblings, 1 reply; 2+ messages in thread
From: Stephen Hemminger @ 2011-11-16 4:24 UTC (permalink / raw)
The compare and hash functions do not modify key.
---
tests/test_urcu_hash.c | 8 ++++----
urcu/rculfhash.h | 8 ++++----
2 files changed, 8 insertions(+), 8 deletions(-)
diff --git a/tests/test_urcu_hash.c b/tests/test_urcu_hash.c
index 88dd1ae..f45c492 100644
--- a/tests/test_urcu_hash.c
+++ b/tests/test_urcu_hash.c
@@ -295,7 +295,7 @@ void hashword2(
#if (CAA_BITS_PER_LONG == 32)
static
-unsigned long test_hash(void *_key, size_t length, unsigned long seed)
+unsigned long test_hash(const void *_key, size_t length, unsigned long seed)
{
unsigned long key = (unsigned long) _key;
unsigned long v;
@@ -305,7 +305,7 @@ unsigned long test_hash(void *_key, size_t length, unsigned long seed)
}
#else
static
-unsigned long test_hash(void *_key, size_t length, unsigned long seed)
+unsigned long test_hash(const void *_key, size_t length, unsigned long seed)
{
union {
uint64_t v64;
@@ -325,8 +325,8 @@ unsigned long test_hash(void *_key, size_t length, unsigned long seed)
#endif
static
-unsigned long test_compare(void *key1, size_t key1_len,
- void *key2, size_t key2_len)
+unsigned long test_compare(const void *key1, size_t key1_len,
+ const void *key2, size_t key2_len)
{
if (unlikely(key1_len != key2_len))
return -1;
diff --git a/urcu/rculfhash.h b/urcu/rculfhash.h
index 37e36b7..4638d28 100644
--- a/urcu/rculfhash.h
+++ b/urcu/rculfhash.h
@@ -56,10 +56,10 @@ struct cds_lfht;
* Ensure reader and writer threads are registered as urcu readers.
*/
-typedef unsigned long (*cds_lfht_hash_fct)(void *key, size_t length,
- unsigned long seed);
-typedef unsigned long (*cds_lfht_compare_fct)(void *key1, size_t key1_len,
- void *key2, size_t key2_len);
+typedef unsigned long (*cds_lfht_hash_fct)(const void *key, size_t length,
+ unsigned long seed);
+typedef unsigned long (*cds_lfht_compare_fct)(const void *key1, size_t key1_len,
+ const void *key2, size_t key2_len);
/*
* cds_lfht_node_init - initialize a hash table node
--
1.7.7.1
^ permalink raw reply [flat|nested] 2+ messages in thread
* [ltt-dev] [PATCH] lfhash: constify arguments to compare and hash function
2011-11-16 4:24 [ltt-dev] [PATCH] lfhash: constify arguments to compare and hash function Stephen Hemminger
@ 2011-11-16 12:28 ` Mathieu Desnoyers
0 siblings, 0 replies; 2+ messages in thread
From: Mathieu Desnoyers @ 2011-11-16 12:28 UTC (permalink / raw)
Hi Stephen,
* Stephen Hemminger (shemminger at vyatta.com) wrote:
>
> The compare and hash functions do not modify key.
Good point ! I refreshed it for the git tree head, and noticed that we
could turn all instances of the "key" parameter received by the
rculfhash API into a const argument. I therefore pushed the following
commit:
commit 996ff57cc0490bc4ae26de70e9ebe620ff18515f
Author: Mathieu Desnoyers <mathieu.desnoyers at efficios.com>
Date: Wed Nov 16 07:23:19 2011 -0500
rculfhash: constify all key arguments passed to API
The hash table never needs to modify the key, it is only ever used for
"match", so it should always be received as a const argument.
Reported-by: Stephen Hemminger <shemminger at vyatta.com>
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers at efficios.com>
diff --git a/rculfhash.c b/rculfhash.c
index bda3bd6..c08447b 100644
--- a/rculfhash.c
+++ b/rculfhash.c
@@ -318,7 +318,7 @@ struct partition_resize_work {
static
void _cds_lfht_add(struct cds_lfht *ht,
cds_lfht_match_fct match,
- void *key,
+ const void *key,
unsigned long size,
struct cds_lfht_node *node,
struct cds_lfht_iter *unique_ret,
@@ -890,7 +890,7 @@ int _cds_lfht_replace(struct cds_lfht *ht, unsigned long size,
static
void _cds_lfht_add(struct cds_lfht *ht,
cds_lfht_match_fct match,
- void *key,
+ const void *key,
unsigned long size,
struct cds_lfht_node *node,
struct cds_lfht_iter *unique_ret,
@@ -1382,7 +1382,7 @@ struct cds_lfht *_cds_lfht_new(unsigned long init_size,
}
void cds_lfht_lookup(struct cds_lfht *ht, unsigned long hash,
- cds_lfht_match_fct match, void *key,
+ cds_lfht_match_fct match, const void *key,
struct cds_lfht_iter *iter)
{
struct cds_lfht_node *node, *next, *bucket;
@@ -1420,7 +1420,7 @@ void cds_lfht_lookup(struct cds_lfht *ht, unsigned long hash,
}
void cds_lfht_next_duplicate(struct cds_lfht *ht, cds_lfht_match_fct match,
- void *key, struct cds_lfht_iter *iter)
+ const void *key, struct cds_lfht_iter *iter)
{
struct cds_lfht_node *node, *next;
unsigned long reverse_hash;
@@ -1501,7 +1501,7 @@ void cds_lfht_add(struct cds_lfht *ht, unsigned long hash,
struct cds_lfht_node *cds_lfht_add_unique(struct cds_lfht *ht,
unsigned long hash,
cds_lfht_match_fct match,
- void *key,
+ const void *key,
struct cds_lfht_node *node)
{
unsigned long size;
@@ -1518,7 +1518,7 @@ struct cds_lfht_node *cds_lfht_add_unique(struct cds_lfht *ht,
struct cds_lfht_node *cds_lfht_add_replace(struct cds_lfht *ht,
unsigned long hash,
cds_lfht_match_fct match,
- void *key,
+ const void *key,
struct cds_lfht_node *node)
{
unsigned long size;
diff --git a/tests/test_urcu_hash.c b/tests/test_urcu_hash.c
index 509767c..fe650f3 100644
--- a/tests/test_urcu_hash.c
+++ b/tests/test_urcu_hash.c
@@ -384,7 +384,7 @@ void hashword2(
#if (CAA_BITS_PER_LONG == 32)
static
-unsigned long test_hash(void *_key, size_t length, unsigned long seed)
+unsigned long test_hash(const void *_key, size_t length, unsigned long seed)
{
unsigned int key = (unsigned int) _key;
@@ -393,7 +393,7 @@ unsigned long test_hash(void *_key, size_t length, unsigned long seed)
}
#else
static
-unsigned long test_hash(void *_key, size_t length, unsigned long seed)
+unsigned long test_hash(const void *_key, size_t length, unsigned long seed)
{
union {
uint64_t v64;
@@ -413,8 +413,8 @@ unsigned long test_hash(void *_key, size_t length, unsigned long seed)
#endif
static
-unsigned long test_compare(void *key1, size_t key1_len,
- void *key2, size_t key2_len)
+unsigned long test_compare(const void *key1, size_t key1_len,
+ const void *key2, size_t key2_len)
{
if (caa_unlikely(key1_len != key2_len))
return -1;
@@ -426,7 +426,7 @@ unsigned long test_compare(void *key1, size_t key1_len,
}
static
-int test_match(struct cds_lfht_node *node, void *key)
+int test_match(struct cds_lfht_node *node, const void *key)
{
struct lfht_test_node *test_node = to_test_node(node);
diff --git a/urcu/rculfhash.h b/urcu/rculfhash.h
index c13d3df..9208011 100644
--- a/urcu/rculfhash.h
+++ b/urcu/rculfhash.h
@@ -71,7 +71,7 @@ struct cds_lfht;
* Ensure reader and writer threads are registered as urcu readers.
*/
-typedef int (*cds_lfht_match_fct)(struct cds_lfht_node *node, void *key);
+typedef int (*cds_lfht_match_fct)(struct cds_lfht_node *node, const void *key);
/*
* cds_lfht_node_init - initialize a hash table node
@@ -186,7 +186,7 @@ void cds_lfht_count_nodes(struct cds_lfht *ht,
* Threads calling this API need to be registered RCU read-side threads.
*/
void cds_lfht_lookup(struct cds_lfht *ht, unsigned long hash,
- cds_lfht_match_fct match, void *key,
+ cds_lfht_match_fct match, const void *key,
struct cds_lfht_iter *iter);
/*
@@ -206,7 +206,7 @@ void cds_lfht_lookup(struct cds_lfht *ht, unsigned long hash,
* Threads calling this API need to be registered RCU read-side threads.
*/
void cds_lfht_next_duplicate(struct cds_lfht *ht,
- cds_lfht_match_fct match, void *key,
+ cds_lfht_match_fct match, const void *key,
struct cds_lfht_iter *iter);
/*
@@ -268,7 +268,7 @@ void cds_lfht_add(struct cds_lfht *ht, unsigned long hash,
struct cds_lfht_node *cds_lfht_add_unique(struct cds_lfht *ht,
unsigned long hash,
cds_lfht_match_fct match,
- void *key,
+ const void *key,
struct cds_lfht_node *node);
/*
@@ -300,7 +300,7 @@ struct cds_lfht_node *cds_lfht_add_unique(struct cds_lfht *ht,
struct cds_lfht_node *cds_lfht_add_replace(struct cds_lfht *ht,
unsigned long hash,
cds_lfht_match_fct match,
- void *key,
+ const void *key,
struct cds_lfht_node *node);
/*
--
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2011-11-16 12:28 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-11-16 4:24 [ltt-dev] [PATCH] lfhash: constify arguments to compare and hash function Stephen Hemminger
2011-11-16 12:28 ` Mathieu Desnoyers
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox