Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* Copyright (C) B.A.T.M.A.N. contributors:
0003  *
0004  * Simon Wunderlich, Marek Lindner
0005  */
0006 
0007 #include "hash.h"
0008 #include "main.h"
0009 
0010 #include <linux/gfp.h>
0011 #include <linux/lockdep.h>
0012 #include <linux/slab.h>
0013 
0014 /* clears the hash */
0015 static void batadv_hash_init(struct batadv_hashtable *hash)
0016 {
0017     u32 i;
0018 
0019     for (i = 0; i < hash->size; i++) {
0020         INIT_HLIST_HEAD(&hash->table[i]);
0021         spin_lock_init(&hash->list_locks[i]);
0022     }
0023 
0024     atomic_set(&hash->generation, 0);
0025 }
0026 
0027 /**
0028  * batadv_hash_destroy() - Free only the hashtable and the hash itself
0029  * @hash: hash object to destroy
0030  */
0031 void batadv_hash_destroy(struct batadv_hashtable *hash)
0032 {
0033     kfree(hash->list_locks);
0034     kfree(hash->table);
0035     kfree(hash);
0036 }
0037 
0038 /**
0039  * batadv_hash_new() - Allocates and clears the hashtable
0040  * @size: number of hash buckets to allocate
0041  *
0042  * Return: newly allocated hashtable, NULL on errors
0043  */
0044 struct batadv_hashtable *batadv_hash_new(u32 size)
0045 {
0046     struct batadv_hashtable *hash;
0047 
0048     hash = kmalloc(sizeof(*hash), GFP_ATOMIC);
0049     if (!hash)
0050         return NULL;
0051 
0052     hash->table = kmalloc_array(size, sizeof(*hash->table), GFP_ATOMIC);
0053     if (!hash->table)
0054         goto free_hash;
0055 
0056     hash->list_locks = kmalloc_array(size, sizeof(*hash->list_locks),
0057                      GFP_ATOMIC);
0058     if (!hash->list_locks)
0059         goto free_table;
0060 
0061     hash->size = size;
0062     batadv_hash_init(hash);
0063     return hash;
0064 
0065 free_table:
0066     kfree(hash->table);
0067 free_hash:
0068     kfree(hash);
0069     return NULL;
0070 }
0071 
0072 /**
0073  * batadv_hash_set_lock_class() - Set specific lockdep class for hash spinlocks
0074  * @hash: hash object to modify
0075  * @key: lockdep class key address
0076  */
0077 void batadv_hash_set_lock_class(struct batadv_hashtable *hash,
0078                 struct lock_class_key *key)
0079 {
0080     u32 i;
0081 
0082     for (i = 0; i < hash->size; i++)
0083         lockdep_set_class(&hash->list_locks[i], key);
0084 }