Chinaunix首页 | 论坛 | 博客
  • 博客访问: 511662
  • 博文数量: 398
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 14
  • 用 户 组: 普通用户
  • 注册时间: 2013-08-21 16:02
个人简介

嵌入式屌丝

文章分类

全部博文(398)

文章存档

2013年(398)

我的朋友

分类: LINUX

2013-08-21 17:18:40

原文地址: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");
阅读(599) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~