[ltt-dev] [PATCH] lfhash: constify arguments to compare and hash function

Mathieu Desnoyers mathieu.desnoyers at efficios.com
Wed Nov 16 07:28:16 EST 2011


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




More information about the lttng-dev mailing list