This commit is contained in:
zhanglianghy 2019-08-02 17:47:54 +08:00
commit 08f496cbc5
1 changed files with 19 additions and 15 deletions

View File

@ -1,5 +1,10 @@
#include "k-userhash.h" #include "k-userhash.h"
#include <linux/slab.h> #include <linux/slab.h>
#include <asm/errno.h>
#include <linux/kthread.h>
/*声明读写锁 */
DEFINE_RWLOCK(g_obj_lock);
/*定义大小为HASH_SIZE的hashtable */ /*定义大小为HASH_SIZE的hashtable */
static struct list_head hash_array[HASH_SIZE]; static struct list_head hash_array[HASH_SIZE];
@ -15,7 +20,7 @@ void init_hashMap(void)
/*获取hashtable位置索引 */ /*获取hashtable位置索引 */
int get_hash_index(uint32_t ip) int get_hash_index(uint32_t ip)
{ {
unsigned int val = ip % HASH_SIZE; int val = ip % HASH_SIZE;
return val; return val;
} }
@ -29,15 +34,8 @@ USERINFO *search_user(uint32_t ip)
index = get_hash_index(ip); index = get_hash_index(ip);
/*判断表中该单元是否为NLL */
/* list_empty - tests whether a list is empty*/
/* @head: the list to test.*/
if( list_empty(&hash_array[index]))
{
return NULL;
}
/*查找用户IP */ /*查找用户IP */
read_lock(&g_obj_lock);
list_for_each(pos, &hash_array[index]) list_for_each(pos, &hash_array[index])
{ {
@ -47,9 +45,10 @@ USERINFO *search_user(uint32_t ip)
* @member: the name of the list_head within the struct. */ * @member: the name of the list_head within the struct. */
pNode = list_entry(pos, USERINFO, hnode); pNode = list_entry(pos, USERINFO, hnode);
if(pNode != NULL) if(pNode != NULL)
printk("user ip:%d user id:%d\n", pNode->user_ip, pNode->user_id); printk("user ip:%ld user id:%d\n", pNode->user_ip, pNode->user_id);
return pNode; return pNode;
} }
read_unlock(&g_obj_lock);
return NULL; return NULL;
} }
@ -71,11 +70,10 @@ int add_user(uint32_t ip, int id)
pNode = (USERINFO *)kmalloc(sizeof(USERINFO), GFP_KERNEL); pNode = (USERINFO *)kmalloc(sizeof(USERINFO), GFP_KERNEL);
if (pNode == NULL) if (pNode == NULL)
{ {
return -1; return ENOMEM;
} }
pNode->user_ip = ip; pNode->user_ip = ip;
list_add_tail(&pNode->hnode,&hash_array[index]);
} }
printk("IP ALEADY EXISTED\n"); printk("IP ALEADY EXISTED\n");
@ -88,7 +86,7 @@ int add_user(uint32_t ip, int id)
void del_user(uint32_t ip) void del_user(uint32_t ip)
{ {
int index; int index;
struct list_head *pos; struct list_head *pos, *n;
USERINFO *pNode; USERINFO *pNode;
index = get_hash_index(ip); index = get_hash_index(ip);
@ -100,13 +98,15 @@ void del_user(uint32_t ip)
} }
/*查找用户ip */ /*查找用户ip */
list_for_each(pos, &hash_array[index]) write_lock(&g_obj_lock);
list_for_each_safe(pos, n, &hash_array[index])
{ {
pNode = list_entry(pos, USERINFO, hnode); pNode = list_entry(pos, USERINFO, hnode);
if(pNode->user_ip == ip) if(pNode->user_ip == ip)
list_del(&pNode->hnode); list_del(&pNode->hnode);
kfree(pNode); kfree(pNode);
} }
write_unlock(&g_obj_lock);
return; return;
} }
@ -123,12 +123,14 @@ void free_all_user(void)
{ {
if(list_empty(&hash_array[i])) if(list_empty(&hash_array[i]))
continue; continue;
write_lock(&g_obj_lock);
list_for_each_safe(pos, n, &hash_array[i]) list_for_each_safe(pos, n, &hash_array[i])
{ {
pNode = list_entry(pos, USERINFO, hnode); pNode = list_entry(pos, USERINFO, hnode);
list_del(&pNode->hnode); list_del(&pNode->hnode);
kfree(pNode); kfree(pNode);
} }
write_unlock(&g_obj_lock);
} }
} }
@ -142,11 +144,13 @@ void printk_all_user(void)
for(i = 0; i < HASH_SIZE; i++) for(i = 0; i < HASH_SIZE; i++)
continue; continue;
read_lock(&g_obj_lock);
list_for_each_safe(pos, n, &hash_array[i]) list_for_each_safe(pos, n, &hash_array[i])
{ {
pNode = list_entry(pos, USERINFO, hnode); pNode = list_entry(pos, USERINFO, hnode);
printk("user ip:%d user id:%d\n", pNode->user_ip, pNode->user_id); printk("user ip:%d user id:%d\n", pNode->user_ip, pNode->user_id);
} }
read_unlock(&g_obj_lock);
} }