All of lore.kernel.org
 help / color / mirror / Atom feed
From: Julien Grall <julien@xen.org>
To: xen-devel@lists.xenproject.org
Cc: julien@xen.org, Julien Grall <jgrall@amazon.com>,
	Wei Liu <wl@xen.org>, Juergen Gross <jgross@suse.com>,
	Anthony PERARD <anthony.perard@citrix.com>
Subject: [PATCH] tools/xenstored: hashtable: Constify the parameters of hashfn/eqfn
Date: Fri, 27 Jan 2023 18:55:46 +0000	[thread overview]
Message-ID: <20230127185546.65760-1-julien@xen.org> (raw)

From: Julien Grall <jgrall@amazon.com>

The parameters of hashfn/eqfn should never be modified. So constify
them and propagate the const to the users.

Take the opportunity to solve some coding style issues around the
code modified.

Signed-off-by: Julien Grall <jgrall@amazon.com>
---
 tools/xenstore/hashtable.c        | 16 ++++++++--------
 tools/xenstore/hashtable.h        | 10 +++++-----
 tools/xenstore/xenstored_core.c   |  8 ++++----
 tools/xenstore/xenstored_domain.c |  8 ++++----
 4 files changed, 21 insertions(+), 21 deletions(-)

diff --git a/tools/xenstore/hashtable.c b/tools/xenstore/hashtable.c
index 30eb9f21d250..3d4466b59756 100644
--- a/tools/xenstore/hashtable.c
+++ b/tools/xenstore/hashtable.c
@@ -23,8 +23,8 @@ struct hashtable {
     unsigned int entrycount;
     unsigned int loadlimit;
     unsigned int primeindex;
-    unsigned int (*hashfn) (void *k);
-    int (*eqfn) (void *k1, void *k2);
+    unsigned int (*hashfn) (const void *k);
+    int (*eqfn) (const void *k1, const void *k2);
 };
 
 /*
@@ -53,8 +53,8 @@ indexFor(unsigned int tablelength, unsigned int hashvalue) {
 /*****************************************************************************/
 struct hashtable *
 create_hashtable(const void *ctx, unsigned int minsize,
-                 unsigned int (*hashf) (void*),
-                 int (*eqf) (void*,void*),
+                 unsigned int (*hashf) (const void *),
+                 int (*eqf) (const void *, const void *),
                  unsigned int flags)
 {
     struct hashtable *h;
@@ -92,7 +92,7 @@ err0:
 
 /*****************************************************************************/
 unsigned int
-hash(struct hashtable *h, void *k)
+hash(const struct hashtable *h, const void *k)
 {
     /* Aim to protect against poor hash functions by adding logic here
      * - logic taken from java 1.4 hashtable source */
@@ -151,7 +151,7 @@ hashtable_expand(struct hashtable *h)
 
 /*****************************************************************************/
 unsigned int
-hashtable_count(struct hashtable *h)
+hashtable_count(const struct hashtable *h)
 {
     return h->entrycount;
 }
@@ -188,7 +188,7 @@ hashtable_insert(struct hashtable *h, void *k, void *v)
 
 /*****************************************************************************/
 void * /* returns value associated with key */
-hashtable_search(struct hashtable *h, void *k)
+hashtable_search(const struct hashtable *h, const void *k)
 {
     struct entry *e;
     unsigned int hashvalue, index;
@@ -206,7 +206,7 @@ hashtable_search(struct hashtable *h, void *k)
 
 /*****************************************************************************/
 void
-hashtable_remove(struct hashtable *h, void *k)
+hashtable_remove(struct hashtable *h, const void *k)
 {
     /* TODO: consider compacting the table when the load factor drops enough,
      *       or provide a 'compact' method. */
diff --git a/tools/xenstore/hashtable.h b/tools/xenstore/hashtable.h
index 4e2823134eb3..cc0090f13378 100644
--- a/tools/xenstore/hashtable.h
+++ b/tools/xenstore/hashtable.h
@@ -24,8 +24,8 @@ struct hashtable;
 
 struct hashtable *
 create_hashtable(const void *ctx, unsigned int minsize,
-                 unsigned int (*hashfunction) (void*),
-                 int (*key_eq_fn) (void*,void*),
+                 unsigned int (*hashfunction) (const void *),
+                 int (*key_eq_fn) (const void *, const void *),
                  unsigned int flags
 );
 
@@ -61,7 +61,7 @@ hashtable_insert(struct hashtable *h, void *k, void *v);
  */
 
 void *
-hashtable_search(struct hashtable *h, void *k);
+hashtable_search(const struct hashtable *h, const void *k);
 
 /*****************************************************************************
  * hashtable_remove
@@ -72,7 +72,7 @@ hashtable_search(struct hashtable *h, void *k);
  */
 
 void
-hashtable_remove(struct hashtable *h, void *k);
+hashtable_remove(struct hashtable *h, const void *k);
 
 /*****************************************************************************
  * hashtable_count
@@ -82,7 +82,7 @@ hashtable_remove(struct hashtable *h, void *k);
  * @return      the number of items stored in the hashtable
  */
 unsigned int
-hashtable_count(struct hashtable *h);
+hashtable_count(const struct hashtable *h);
 
 /*****************************************************************************
  * hashtable_iterate
diff --git a/tools/xenstore/xenstored_core.c b/tools/xenstore/xenstored_core.c
index 4f00e0cdc0cf..7348f935bc26 100644
--- a/tools/xenstore/xenstored_core.c
+++ b/tools/xenstore/xenstored_core.c
@@ -2386,9 +2386,9 @@ void setup_structure(bool live_update)
 	}
 }
 
-static unsigned int hash_from_key_fn(void *k)
+static unsigned int hash_from_key_fn(const void *k)
 {
-	char *str = k;
+	const char *str = k;
 	unsigned int hash = 5381;
 	char c;
 
@@ -2399,9 +2399,9 @@ static unsigned int hash_from_key_fn(void *k)
 }
 
 
-static int keys_equal_fn(void *key1, void *key2)
+static int keys_equal_fn(const void *key1, const void *key2)
 {
-	return 0 == strcmp((char *)key1, (char *)key2);
+	return 0 == strcmp(key1, key2);
 }
 
 int remember_string(struct hashtable *hash, const char *str)
diff --git a/tools/xenstore/xenstored_domain.c b/tools/xenstore/xenstored_domain.c
index 9ef41ede03ae..d7fc2fafc729 100644
--- a/tools/xenstore/xenstored_domain.c
+++ b/tools/xenstore/xenstored_domain.c
@@ -916,14 +916,14 @@ void dom0_init(void)
 	xenevtchn_notify(xce_handle, dom0->port);
 }
 
-static unsigned int domhash_fn(void *k)
+static unsigned int domhash_fn(const void *k)
 {
-	return *(unsigned int *)k;
+	return *(const unsigned int *)k;
 }
 
-static int domeq_fn(void *key1, void *key2)
+static int domeq_fn(const void *key1, const void *key2)
 {
-	return *(unsigned int *)key1 == *(unsigned int *)key2;
+	return *(const unsigned int *)key1 == *(const unsigned int *)key2;
 }
 
 void domain_init(int evtfd)
-- 
2.38.1



             reply	other threads:[~2023-01-27 18:56 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-27 18:55 Julien Grall [this message]
2023-01-28  3:41 ` [PATCH] tools/xenstored: hashtable: Constify the parameters of hashfn/eqfn Henry Wang
2023-01-30  6:15 ` Juergen Gross

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230127185546.65760-1-julien@xen.org \
    --to=julien@xen.org \
    --cc=anthony.perard@citrix.com \
    --cc=jgrall@amazon.com \
    --cc=jgross@suse.com \
    --cc=wl@xen.org \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.