blob: 7c1c63080e20362cd628251b37f44da446cdbdba [file] [log] [blame]
Simon Wunderliche19f9752014-01-04 18:04:25 +01001/* Copyright (C) 2006-2014 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002 *
3 * Simon Wunderlich, Marek Lindner
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
Antonio Quartulliebf38fb2013-11-03 20:40:48 +010015 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000016 */
17
18#include "main.h"
19#include "hash.h"
20
21/* clears the hash */
Sven Eckelmann5bf74e92012-06-05 22:31:28 +020022static void batadv_hash_init(struct batadv_hashtable *hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000023{
Antonio Quartullic90681b2011-10-05 17:05:25 +020024 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000025
Sven Eckelmanncb4cca72012-06-17 16:27:22 +020026 for (i = 0; i < hash->size; i++) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000027 INIT_HLIST_HEAD(&hash->table[i]);
Marek Lindnerfb778ea2011-01-19 20:01:40 +000028 spin_lock_init(&hash->list_locks[i]);
29 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000030}
31
32/* free only the hashtable and the hash itself. */
Sven Eckelmann5bf74e92012-06-05 22:31:28 +020033void batadv_hash_destroy(struct batadv_hashtable *hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000034{
Marek Lindnerfb778ea2011-01-19 20:01:40 +000035 kfree(hash->list_locks);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000036 kfree(hash->table);
37 kfree(hash);
38}
39
40/* allocates and clears the hash */
Sven Eckelmann5bf74e92012-06-05 22:31:28 +020041struct batadv_hashtable *batadv_hash_new(uint32_t size)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000042{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +020043 struct batadv_hashtable *hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000044
Sven Eckelmann704509b2011-05-14 23:14:54 +020045 hash = kmalloc(sizeof(*hash), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000046 if (!hash)
47 return NULL;
48
Antonio Quartulli0185dda2014-05-24 06:43:47 +020049 hash->table = kmalloc_array(size, sizeof(*hash->table), GFP_ATOMIC);
Marek Lindnerfb778ea2011-01-19 20:01:40 +000050 if (!hash->table)
51 goto free_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000052
Antonio Quartulli0185dda2014-05-24 06:43:47 +020053 hash->list_locks = kmalloc_array(size, sizeof(*hash->list_locks),
54 GFP_ATOMIC);
Marek Lindnerfb778ea2011-01-19 20:01:40 +000055 if (!hash->list_locks)
56 goto free_table;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000057
Marek Lindnerfb778ea2011-01-19 20:01:40 +000058 hash->size = size;
Sven Eckelmann7f9f02c2012-05-12 18:33:58 +020059 batadv_hash_init(hash);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000060 return hash;
Marek Lindnerfb778ea2011-01-19 20:01:40 +000061
62free_table:
63 kfree(hash->table);
64free_hash:
65 kfree(hash);
66 return NULL;
67}
Sven Eckelmann5d52dad2012-03-29 12:38:20 +020068
Sven Eckelmann5bf74e92012-06-05 22:31:28 +020069void batadv_hash_set_lock_class(struct batadv_hashtable *hash,
Sven Eckelmann5d52dad2012-03-29 12:38:20 +020070 struct lock_class_key *key)
71{
72 uint32_t i;
73
74 for (i = 0; i < hash->size; i++)
75 lockdep_set_class(&hash->list_locks[i], key);
76}