blob: 15a849c2d41498989c82ff9d86740eb5cfe4d7cb [file] [log] [blame]
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001/* Copyright (C) 2006-2012 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
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000018 */
19
20#include "main.h"
21#include "hash.h"
22
23/* clears the hash */
Sven Eckelmann5bf74e92012-06-05 22:31:28 +020024static void batadv_hash_init(struct batadv_hashtable *hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000025{
Antonio Quartullic90681b2011-10-05 17:05:25 +020026 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000027
Sven Eckelmanncb4cca72012-06-17 16:27:22 +020028 for (i = 0; i < hash->size; i++) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000029 INIT_HLIST_HEAD(&hash->table[i]);
Marek Lindnerfb778ea2011-01-19 20:01:40 +000030 spin_lock_init(&hash->list_locks[i]);
31 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000032}
33
34/* free only the hashtable and the hash itself. */
Sven Eckelmann5bf74e92012-06-05 22:31:28 +020035void batadv_hash_destroy(struct batadv_hashtable *hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000036{
Marek Lindnerfb778ea2011-01-19 20:01:40 +000037 kfree(hash->list_locks);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000038 kfree(hash->table);
39 kfree(hash);
40}
41
42/* allocates and clears the hash */
Sven Eckelmann5bf74e92012-06-05 22:31:28 +020043struct batadv_hashtable *batadv_hash_new(uint32_t size)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000044{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +020045 struct batadv_hashtable *hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000046
Sven Eckelmann704509b2011-05-14 23:14:54 +020047 hash = kmalloc(sizeof(*hash), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000048 if (!hash)
49 return NULL;
50
Sven Eckelmann704509b2011-05-14 23:14:54 +020051 hash->table = kmalloc(sizeof(*hash->table) * size, GFP_ATOMIC);
Marek Lindnerfb778ea2011-01-19 20:01:40 +000052 if (!hash->table)
53 goto free_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000054
Sven Eckelmann704509b2011-05-14 23:14:54 +020055 hash->list_locks = kmalloc(sizeof(*hash->list_locks) * size,
56 GFP_ATOMIC);
Marek Lindnerfb778ea2011-01-19 20:01:40 +000057 if (!hash->list_locks)
58 goto free_table;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000059
Marek Lindnerfb778ea2011-01-19 20:01:40 +000060 hash->size = size;
Sven Eckelmann7f9f02c2012-05-12 18:33:58 +020061 batadv_hash_init(hash);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000062 return hash;
Marek Lindnerfb778ea2011-01-19 20:01:40 +000063
64free_table:
65 kfree(hash->table);
66free_hash:
67 kfree(hash);
68 return NULL;
69}
Sven Eckelmann5d52dad2012-03-29 12:38:20 +020070
Sven Eckelmann5bf74e92012-06-05 22:31:28 +020071void batadv_hash_set_lock_class(struct batadv_hashtable *hash,
Sven Eckelmann5d52dad2012-03-29 12:38:20 +020072 struct lock_class_key *key)
73{
74 uint32_t i;
75
76 for (i = 0; i < hash->size; i++)
77 lockdep_set_class(&hash->list_locks[i], key);
78}