From patchwork Wed Aug 24 13:00:31 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Lindner X-Patchwork-Id: 1256 Return-Path: Received: from nm18.bullet.mail.ukl.yahoo.com (nm18.bullet.mail.ukl.yahoo.com [217.146.183.192]) by open-mesh.org (Postfix) with SMTP id 508DA600839 for ; Wed, 24 Aug 2011 15:00:49 +0200 (CEST) Authentication-Results: open-mesh.org; dkim=pass (1024-bit key) header.i=@yahoo.de; dkim-adsp=none Received: from [217.146.183.215] by nm18.bullet.mail.ukl.yahoo.com with NNFMP; 24 Aug 2011 13:00:48 -0000 Received: from [77.238.184.62] by tm8.bullet.mail.ukl.yahoo.com with NNFMP; 24 Aug 2011 13:00:48 -0000 Received: from [127.0.0.1] by smtp131.mail.ukl.yahoo.com with NNFMP; 24 Aug 2011 13:00:48 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.de; s=s1024; t=1314190848; bh=qXI8CI143n87++CZ07lLw382ehgF7/sEwKIzMKFaIBQ=; h=X-Yahoo-Newman-Id:X-Yahoo-Newman-Property:X-YMail-OSG:X-Yahoo-SMTP:Received:From:To:Cc:Subject:Date:Message-Id:X-Mailer:In-Reply-To:References; b=oi+HRrBNLm/nmNZZ++MLzpJddFAU46yqVMrx3qVcLxJM5MXwA/SQWTElIBcScT9Dslr11cQEmIRDjjI4aG9cqLyJL9t44b9018IIZUFgWbrB50IqfDjrOeIZKKsdNFvWkSUfrvc01RPPlksqw50dRCM4mYebfwWMiDaI6BgrzSc= X-Yahoo-Newman-Id: 934684.16309.bm@smtp131.mail.ukl.yahoo.com X-Yahoo-Newman-Property: ymail-3 X-YMail-OSG: KF7nBHcVM1nST0VAhbm44Me72JV_ezGFv0vKmxhfBiE2HlZ 9u7vlkZ45WzHowPqiVUnhBNfHq0kbVl0g84cRG3RqS0NqneUp.GwOfNilxPL JkZ5K9espQ4YJ5_BjTK3wcRPrGxV2i6SwuI6jxEs_G2Enhh2aenu5izYxO_8 MiBTq2OXTxgGkaLPeNle_RMr9Xqq3MTKCIyO19wRl4mBNliSvxMYBTfReuVH 7ofQ2rWqm3Cz9D63qTxpEc6clT99NvPI1X0Lt_tbcELa4NVQxtCUMNpK1tQj rh.FOrFN6aDqdnmS7vWPAEyvo4uXp_Qbn2CRq07u7a.5XiTlL5HYwm1eqozL R4GU0.y1YqgELqDyCSqDh.u0w5OsO9NKWGWTBGAsd0jxm15nmJtuznlA- X-Yahoo-SMTP: tW.h3tiswBBMXO2coYcbPigGD5Lt6zY_.Zc- Received: from localhost (lindner_marek@90.61.151.134 with plain) by smtp131.mail.ukl.yahoo.com with SMTP; 24 Aug 2011 13:00:48 +0000 GMT From: Marek Lindner To: davem@davemloft.net Date: Wed, 24 Aug 2011 15:00:31 +0200 Message-Id: <1314190838-2273-2-git-send-email-lindner_marek@yahoo.de> X-Mailer: git-send-email 1.7.5.3 In-Reply-To: <1314190838-2273-1-git-send-email-lindner_marek@yahoo.de> References: <1314190838-2273-1-git-send-email-lindner_marek@yahoo.de> Cc: netdev@vger.kernel.org, b.a.t.m.a.n@lists.open-mesh.org, Marek Lindner Subject: [B.A.T.M.A.N.] [PATCH 1/8] batman-adv: hash_add() has to discriminate on the return value X-BeenThere: b.a.t.m.a.n@lists.open-mesh.org X-Mailman-Version: 2.1.13 Precedence: list Reply-To: The list for a Better Approach To Mobile Ad-hoc Networking List-Id: The list for a Better Approach To Mobile Ad-hoc Networking List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 24 Aug 2011 13:00:49 -0000 From: Antonio Quartulli 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 Signed-off-by: Marek Lindner --- net/batman-adv/hash.h | 25 +++++++++++++++++++------ net/batman-adv/originator.c | 2 +- net/batman-adv/vis.c | 4 ++-- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/net/batman-adv/hash.h b/net/batman-adv/hash.h index dd5c9fd..d20aa71 100644 --- a/net/batman-adv/hash.h +++ b/net/batman-adv/hash.h @@ -76,19 +76,30 @@ static inline void hash_delete(struct hashtable_t *hash, hash_destroy(hash); } -/* adds data to the hashtable. returns 0 on success, -1 on error */ +/** + * hash_add - adds data to the hashtable + * @hash: storage hash table + * @compare: callback to determine if 2 hash elements are identical + * @choose: callback calculating the hash index + * @data: data passed to the aforementioned callbacks as argument + * @data_node: to be added element + * + * Returns 0 on success, 1 if the element already is in the hash + * and -1 on error. + */ + static inline int hash_add(struct hashtable_t *hash, hashdata_compare_cb compare, 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 */ if (!hash) - goto err; + goto out; index = choose(data, hash->size); head = &hash->table[index]; @@ -99,6 +110,7 @@ static inline int hash_add(struct hashtable_t *hash, if (!compare(node, data)) continue; + ret = 1; goto err_unlock; } rcu_read_unlock(); @@ -108,12 +120,13 @@ 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; + goto out; err_unlock: rcu_read_unlock(); -err: - return -1; +out: + return ret; } /* removes data from hash, if found. returns pointer do data on success, so you diff --git a/net/batman-adv/originator.c b/net/batman-adv/originator.c index f3c3f62..d448018 100644 --- a/net/batman-adv/originator.c +++ b/net/batman-adv/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/net/batman-adv/vis.c b/net/batman-adv/vis.c index 8a1b985..8b75cc5 100644 --- a/net/batman-adv/vis.c +++ b/net/batman-adv/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);