2 * Basic Linux list API usage.
4 * Luiz Fernando N. Capitulino
5 * <lcapitulino@gmail.com>
8 #include <linux/init.h>
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/list.h>
14 struct list_head list
;
18 static int __init
list_test_init(void)
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
);
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
);
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
);
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>");