.
[cascardo/kernel/samples/list/.git] / test_list.c
1 #include <linux/module.h>
2 #include <linux/list.h>
3
4 MODULE_LICENSE("GPL");
5
6 struct test
7 {
8         int val;
9         struct list_head l;
10 };
11
12 static struct test *mylist;
13
14 LIST_HEAD(test_head);
15
16 static int test_list_init(void)
17 {
18         struct list_head *l;
19         int i;
20         mylist = kmalloc(sizeof(struct test) * 16, GFP_KERNEL);
21         for (i = 0; i < 16; i++)
22                 mylist[i].val = i;
23         if (mylist == NULL)
24                 return ENOMEM;
25         list_add_tail(&mylist[0].l, &test_head);
26         list_add_tail(&mylist[1].l, &test_head);
27         list_add_tail(&mylist[2].l, &test_head);
28         list_add_tail(&mylist[3].l, &test_head);
29         list_del(&mylist[1].l);
30         list_for_each(l, &test_head) {
31                 struct test *h = list_entry(l, struct test, l);
32                 printk(KERN_DEBUG "%d\n", h->val);
33         }
34         return 0;
35 }
36
37 static void test_list_exit(void)
38 {
39         kfree(mylist);
40 }
41
42 module_init(test_list_init);
43 module_exit(test_list_exit);