Introduce pet-projects dir
[lcapit-junk-code.git] / linux-kernel / list / list-test.c
blobf06c6656f561431e08620242465e8f3496c63ee4
1 /*
2 * Basic Linux list API usage.
3 *
4 * Luiz Fernando N. Capitulino
5 * <lcapitulino@gmail.com>
6 */
8 #include <linux/init.h>
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/list.h>
13 struct foobar {
14 struct list_head list;
15 int num;
18 static int __init list_test_init(void)
20 int i;
21 struct foobar head, *p;
22 struct list_head *pos, *q;
23 const int max_itens = 10;
25 /* initialize the head of the list */
26 INIT_LIST_HEAD(&head.list);
28 /* populate the list */
29 for (i = max_itens; i >= 0; i--) {
30 p = kzalloc(sizeof(*p), GFP_KERNEL);
31 p->num = i;
32 list_add(&p->list, &head.list);
35 /* transverse the list */
36 printk("\n-> Itens in the list:\n");
37 list_for_each(pos, &head.list) {
38 p = list_entry(pos, struct foobar, list);
39 printk("\t-> %d\n", p->num);
42 printk("\n\n");
44 /* delete all list itens */
45 printk("-> Removing itens:\n");
46 list_for_each_safe(pos, q, &head.list) {
47 p = list_entry(pos, struct foobar, list);
48 printk("\t-> %d\n", p->num);
49 list_del(pos);
50 kfree(p);
53 printk("\n");
55 return -EINVAL; /* prevent loading the module */
58 module_init(list_test_init);
60 MODULE_LICENSE("GPL");
61 MODULE_AUTHOR("Luiz Capitulino <lcapitulino@gmail.com>");