Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1673736
  • 博文数量: 782
  • 博客积分: 2455
  • 博客等级: 大尉
  • 技术积分: 4140
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-06 21:37
个人简介

Linux ,c/c++, web,前端,php,js

文章分类

全部博文(782)

文章存档

2015年(8)

2014年(28)

2013年(110)

2012年(307)

2011年(329)

分类:

2012-06-11 17:45:08

原文地址:struct list_head 做hashlist 作者:@sky

#include
#include
#include
#include
#include
#define err(msg) printk(KERN_ALERT "%s\n", msg)
#define HASHSIZE 512
#define DALIGN (sizeof(unsigned long) - 1)
static struct list_head hashlist[HASHSIZE];
struct buffinfo
{
        struct list_head list;
        char data[0];
};
static int hashlist_init(void)
{
        char data[] = "hello linux";
        struct buffinfo *t;
        int len = (sizeof(struct buffinfo) + strlen(data) + DALIGN) & ~DALIGN;
        int i;
        struct list_head *p;
        for (i = 0; i < HASHSIZE; i++)
                INIT_LIST_HEAD(&hashlist[i]);
        for (i = 0; i < HASHSIZE; i++)
        {
                t = kzalloc(len, GFP_KERNEL);
                if (!t)
                {
                        err("kmalloc");
                        continue;
                }
                memcpy(t->data, data, strlen(data));
                list_add_tail(&t->list, &hashlist[i]);
        }
        for (i = 0; i < HASHSIZE; i++)
        {
                while (!list_empty(&hashlist[i]))
                {
                        p = (&hashlist[i])->next;
                        list_del(p);
                        t = list_entry(p, struct buffinfo, list);
                        printk(KERN_ALERT "%s\n", t->data);
                        kfree(t);
                }
        }
        return 0;
}
static void hashlist_exit(void)
{
        printk(KERN_ALERT "hashlist_exit\n");
}
module_init(hashlist_init);
module_exit(hashlist_exit);
MODULE_LICENSE("GPL");
阅读(534) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~