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");
#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");
相关阅读 更多 +