文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>struct list_head 做hashlist

struct list_head 做hashlist

时间:2009-05-21  来源:@sky

#include <linux/module.h>
#include <linux/list.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/string.h>
#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");
相关阅读 更多 +
排行榜 更多 +
王牌食神

王牌食神

音乐节奏 下载
生理期记录

生理期记录

健康医疗 下载
露娜大冒险

露娜大冒险

动作格斗 下载