Blame view

net/batman-adv/hash.c 1.66 KB
c6c8fea29   Sven Eckelmann   net: Add batman-a...
1
  /*
64afe3539   Sven Eckelmann   batman-adv: Updat...
2
   * Copyright (C) 2006-2011 B.A.T.M.A.N. contributors:
c6c8fea29   Sven Eckelmann   net: Add batman-a...
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
   *
   * Simon Wunderlich, Marek Lindner
   *
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of version 2 of the GNU General Public
   * License as published by the Free Software Foundation.
   *
   * This program is distributed in the hope that it will be useful, but
   * WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   * General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
   * 02110-1301, USA
   *
   */
  
  #include "main.h"
  #include "hash.h"
  
  /* clears the hash */
  static void hash_init(struct hashtable_t *hash)
  {
c90681b85   Antonio Quartulli   batman-adv: fixed...
28
  	uint32_t i;
c6c8fea29   Sven Eckelmann   net: Add batman-a...
29

fb778ea17   Marek Lindner   batman-adv: prote...
30
  	for (i = 0 ; i < hash->size; i++) {
c6c8fea29   Sven Eckelmann   net: Add batman-a...
31
  		INIT_HLIST_HEAD(&hash->table[i]);
fb778ea17   Marek Lindner   batman-adv: prote...
32
33
  		spin_lock_init(&hash->list_locks[i]);
  	}
c6c8fea29   Sven Eckelmann   net: Add batman-a...
34
35
36
37
38
  }
  
  /* free only the hashtable and the hash itself. */
  void hash_destroy(struct hashtable_t *hash)
  {
fb778ea17   Marek Lindner   batman-adv: prote...
39
  	kfree(hash->list_locks);
c6c8fea29   Sven Eckelmann   net: Add batman-a...
40
41
42
43
44
  	kfree(hash->table);
  	kfree(hash);
  }
  
  /* allocates and clears the hash */
c90681b85   Antonio Quartulli   batman-adv: fixed...
45
  struct hashtable_t *hash_new(uint32_t size)
c6c8fea29   Sven Eckelmann   net: Add batman-a...
46
47
  {
  	struct hashtable_t *hash;
704509b8d   Sven Eckelmann   batman-adv: Calcu...
48
  	hash = kmalloc(sizeof(*hash), GFP_ATOMIC);
c6c8fea29   Sven Eckelmann   net: Add batman-a...
49
50
  	if (!hash)
  		return NULL;
704509b8d   Sven Eckelmann   batman-adv: Calcu...
51
  	hash->table = kmalloc(sizeof(*hash->table) * size, GFP_ATOMIC);
fb778ea17   Marek Lindner   batman-adv: prote...
52
53
  	if (!hash->table)
  		goto free_hash;
c6c8fea29   Sven Eckelmann   net: Add batman-a...
54

704509b8d   Sven Eckelmann   batman-adv: Calcu...
55
56
  	hash->list_locks = kmalloc(sizeof(*hash->list_locks) * size,
  				   GFP_ATOMIC);
fb778ea17   Marek Lindner   batman-adv: prote...
57
58
  	if (!hash->list_locks)
  		goto free_table;
c6c8fea29   Sven Eckelmann   net: Add batman-a...
59

fb778ea17   Marek Lindner   batman-adv: prote...
60
  	hash->size = size;
c6c8fea29   Sven Eckelmann   net: Add batman-a...
61
  	hash_init(hash);
c6c8fea29   Sven Eckelmann   net: Add batman-a...
62
  	return hash;
fb778ea17   Marek Lindner   batman-adv: prote...
63
64
65
66
67
68
69
  
  free_table:
  	kfree(hash->table);
  free_hash:
  	kfree(hash);
  	return NULL;
  }