diff options
author | Antonio Quartulli <ordex@autistici.org> | 2011-07-09 18:36:36 -0400 |
---|---|---|
committer | Marek Lindner <lindner_marek@yahoo.de> | 2011-08-22 09:16:19 -0400 |
commit | 1a1f37d9257a4792ca17b28b1c2e4ad15fe95b28 (patch) | |
tree | 5bc0aa0d34b87d1f9b70682f1c54298860ce7d13 /net/batman-adv | |
parent | 322a8b034003c0d46d39af85bf24fee27b902f48 (diff) |
batman-adv: hash_add() has to discriminate on the return value
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>
Signed-off-by: Marek Lindner <lindner_marek@yahoo.de>
Diffstat (limited to 'net/batman-adv')
-rw-r--r-- | net/batman-adv/hash.h | 25 | ||||
-rw-r--r-- | net/batman-adv/originator.c | 2 | ||||
-rw-r--r-- | 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 dd5c9fd7a905..d20aa71ba1e8 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, | |||
76 | hash_destroy(hash); | 76 | hash_destroy(hash); |
77 | } | 77 | } |
78 | 78 | ||
79 | /* adds data to the hashtable. returns 0 on success, -1 on error */ | 79 | /** |
80 | * hash_add - adds data to the hashtable | ||
81 | * @hash: storage hash table | ||
82 | * @compare: callback to determine if 2 hash elements are identical | ||
83 | * @choose: callback calculating the hash index | ||
84 | * @data: data passed to the aforementioned callbacks as argument | ||
85 | * @data_node: to be added element | ||
86 | * | ||
87 | * Returns 0 on success, 1 if the element already is in the hash | ||
88 | * and -1 on error. | ||
89 | */ | ||
90 | |||
80 | static inline int hash_add(struct hashtable_t *hash, | 91 | static inline int hash_add(struct hashtable_t *hash, |
81 | hashdata_compare_cb compare, | 92 | hashdata_compare_cb compare, |
82 | hashdata_choose_cb choose, | 93 | hashdata_choose_cb choose, |
83 | const void *data, struct hlist_node *data_node) | 94 | const void *data, struct hlist_node *data_node) |
84 | { | 95 | { |
85 | int index; | 96 | int index, ret = -1; |
86 | struct hlist_head *head; | 97 | struct hlist_head *head; |
87 | struct hlist_node *node; | 98 | struct hlist_node *node; |
88 | spinlock_t *list_lock; /* spinlock to protect write access */ | 99 | spinlock_t *list_lock; /* spinlock to protect write access */ |
89 | 100 | ||
90 | if (!hash) | 101 | if (!hash) |
91 | goto err; | 102 | goto out; |
92 | 103 | ||
93 | index = choose(data, hash->size); | 104 | index = choose(data, hash->size); |
94 | head = &hash->table[index]; | 105 | head = &hash->table[index]; |
@@ -99,6 +110,7 @@ static inline int hash_add(struct hashtable_t *hash, | |||
99 | if (!compare(node, data)) | 110 | if (!compare(node, data)) |
100 | continue; | 111 | continue; |
101 | 112 | ||
113 | ret = 1; | ||
102 | goto err_unlock; | 114 | goto err_unlock; |
103 | } | 115 | } |
104 | rcu_read_unlock(); | 116 | rcu_read_unlock(); |
@@ -108,12 +120,13 @@ static inline int hash_add(struct hashtable_t *hash, | |||
108 | hlist_add_head_rcu(data_node, head); | 120 | hlist_add_head_rcu(data_node, head); |
109 | spin_unlock_bh(list_lock); | 121 | spin_unlock_bh(list_lock); |
110 | 122 | ||
111 | return 0; | 123 | ret = 0; |
124 | goto out; | ||
112 | 125 | ||
113 | err_unlock: | 126 | err_unlock: |
114 | rcu_read_unlock(); | 127 | rcu_read_unlock(); |
115 | err: | 128 | out: |
116 | return -1; | 129 | return ret; |
117 | } | 130 | } |
118 | 131 | ||
119 | /* removes data from hash, if found. returns pointer do data on success, so you | 132 | /* 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 f3c3f620d195..d448018e514f 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) | |||
252 | 252 | ||
253 | hash_added = hash_add(bat_priv->orig_hash, compare_orig, | 253 | hash_added = hash_add(bat_priv->orig_hash, compare_orig, |
254 | choose_orig, orig_node, &orig_node->hash_entry); | 254 | choose_orig, orig_node, &orig_node->hash_entry); |
255 | if (hash_added < 0) | 255 | if (hash_added != 0) |
256 | goto free_bcast_own_sum; | 256 | goto free_bcast_own_sum; |
257 | 257 | ||
258 | return orig_node; | 258 | return orig_node; |
diff --git a/net/batman-adv/vis.c b/net/batman-adv/vis.c index 8a1b98589d76..8b75cc562053 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, | |||
465 | /* try to add it */ | 465 | /* try to add it */ |
466 | hash_added = hash_add(bat_priv->vis_hash, vis_info_cmp, vis_info_choose, | 466 | hash_added = hash_add(bat_priv->vis_hash, vis_info_cmp, vis_info_choose, |
467 | info, &info->hash_entry); | 467 | info, &info->hash_entry); |
468 | if (hash_added < 0) { | 468 | if (hash_added != 0) { |
469 | /* did not work (for some reason) */ | 469 | /* did not work (for some reason) */ |
470 | kref_put(&info->refcount, free_info); | 470 | kref_put(&info->refcount, free_info); |
471 | info = NULL; | 471 | info = NULL; |
@@ -920,7 +920,7 @@ int vis_init(struct bat_priv *bat_priv) | |||
920 | hash_added = hash_add(bat_priv->vis_hash, vis_info_cmp, vis_info_choose, | 920 | hash_added = hash_add(bat_priv->vis_hash, vis_info_cmp, vis_info_choose, |
921 | bat_priv->my_vis_info, | 921 | bat_priv->my_vis_info, |
922 | &bat_priv->my_vis_info->hash_entry); | 922 | &bat_priv->my_vis_info->hash_entry); |
923 | if (hash_added < 0) { | 923 | if (hash_added != 0) { |
924 | pr_err("Can't add own vis packet into hash\n"); | 924 | pr_err("Can't add own vis packet into hash\n"); |
925 | /* not in hash, need to remove it manually. */ | 925 | /* not in hash, need to remove it manually. */ |
926 | kref_put(&bat_priv->my_vis_info->refcount, free_info); | 926 | kref_put(&bat_priv->my_vis_info->refcount, free_info); |