batman-adv: hash_add() has to discriminate on the return value

Message ID 1310250996-4456-1-git-send-email-ordex@autistici.org (mailing list archive)
State Accepted, archived
Headers

Commit Message

Antonio Quartulli July 9, 2011, 10:36 p.m. UTC
  hash_add() returns 0 on success while returns -1 either on error and on
entry already present. The caller could use such information to select
its behaviour. For this reason it is useful that hash_add() returns -1
in case on error and returns 1 in case of entry already present.

Signed-off-by: Antonio Quartulli <ordex@autistici.org>
---
 hash.h       |    9 ++++++---
 originator.c |    2 +-
 vis.c        |    4 ++--
 3 files changed, 9 insertions(+), 6 deletions(-)
  

Comments

Marek Lindner July 23, 2011, 10:15 a.m. UTC | #1
On Sunday, July 10, 2011 00:36:36 Antonio Quartulli wrote:
> hash_add() returns 0 on success while returns -1 either on error and on
> entry already present. The caller could use such information to select
> its behaviour. For this reason it is useful that hash_add() returns -1
> in case on error and returns 1 in case of entry already present.

Applied in revision fae2d08.

Thanks,
Marek
  

Patch

diff --git a/hash.h b/hash.h
index dd5c9fd..864b9b0 100644
--- a/hash.h
+++ b/hash.h
@@ -82,7 +82,7 @@  static inline int hash_add(struct hashtable_t *hash,
 			   hashdata_choose_cb choose,
 			   const void *data, struct hlist_node *data_node)
 {
-	int index;
+	int index, ret = -1;
 	struct hlist_head *head;
 	struct hlist_node *node;
 	spinlock_t *list_lock; /* spinlock to protect write access */
@@ -98,6 +98,7 @@  static inline int hash_add(struct hashtable_t *hash,
 	__hlist_for_each_rcu(node, head) {
 		if (!compare(node, data))
 			continue;
+		ret = 1;
 
 		goto err_unlock;
 	}
@@ -108,12 +109,14 @@  static inline int hash_add(struct hashtable_t *hash,
 	hlist_add_head_rcu(data_node, head);
 	spin_unlock_bh(list_lock);
 
-	return 0;
+	ret = 0;
+
+	return ret;
 
 err_unlock:
 	rcu_read_unlock();
 err:
-	return -1;
+	return ret;
 }
 
 /* removes data from hash, if found. returns pointer do data on success, so you
diff --git a/originator.c b/originator.c
index f3c3f62..d448018 100644
--- a/originator.c
+++ b/originator.c
@@ -252,7 +252,7 @@  struct orig_node *get_orig_node(struct bat_priv *bat_priv, const uint8_t *addr)
 
 	hash_added = hash_add(bat_priv->orig_hash, compare_orig,
 			      choose_orig, orig_node, &orig_node->hash_entry);
-	if (hash_added < 0)
+	if (hash_added != 0)
 		goto free_bcast_own_sum;
 
 	return orig_node;
diff --git a/vis.c b/vis.c
index 8a1b985..8b75cc5 100644
--- a/vis.c
+++ b/vis.c
@@ -465,7 +465,7 @@  static struct vis_info *add_packet(struct bat_priv *bat_priv,
 	/* try to add it */
 	hash_added = hash_add(bat_priv->vis_hash, vis_info_cmp, vis_info_choose,
 			      info, &info->hash_entry);
-	if (hash_added < 0) {
+	if (hash_added != 0) {
 		/* did not work (for some reason) */
 		kref_put(&info->refcount, free_info);
 		info = NULL;
@@ -920,7 +920,7 @@  int vis_init(struct bat_priv *bat_priv)
 	hash_added = hash_add(bat_priv->vis_hash, vis_info_cmp, vis_info_choose,
 			      bat_priv->my_vis_info,
 			      &bat_priv->my_vis_info->hash_entry);
-	if (hash_added < 0) {
+	if (hash_added != 0) {
 		pr_err("Can't add own vis packet into hash\n");
 		/* not in hash, need to remove it manually. */
 		kref_put(&bat_priv->my_vis_info->refcount, free_info);