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/hash.h | |
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/hash.h')
-rw-r--r-- | net/batman-adv/hash.h | 25 |
1 files changed, 19 insertions, 6 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 |