Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<linux/module.h>
- #include<linux/slab.h>
- static struct list_node
- {
- int data;
- struct list_node *next;
- }*list;
- static struct kmem_cache *list_cache;
- static struct list_node *create_list(int data)
- {
- struct list_node *front = (struct list_node *)kmem_cache_alloc(list_cache,GFP_KERNEL);
- if(front!=NULL)
- {
- front->data = data;
- front->next = front;
- }
- return front;
- }
- static void add_back(struct list_node *list,int number)
- {
- if(list)
- {
- struct list_node *first = list;
- struct list_node *new_element = (struct list_node *)kmem_cache_alloc(list_cache,GFP_KERNEL);
- do
- {
- list = list->next;
- } while(list->next != first);
- list->next = new_element;
- new_element->next = first;
- new_element->data = number;
- }
- }
- static int __init list_init(void)
- {
- int i;
- list_cache = kmem_cache_create("cycle list",sizeof(struct list_node),0,SLAB_HWCACHE_ALIGN|SLAB_POISON|SLAB_RED_ZONE,0);
- if(IS_ERR(list_cache))
- {
- pr_alert("Error creating cache: %ld\n",PTR_ERR(list_cache));
- return -ENOMEM;
- }
- list = create_list(100);
- for(i=101;i<=110;i++)
- add_back(list,i);
- printk(KERN_ALERT"wypelnilem liste od 100 do 110\n");
- return 0;
- }
- static void print_list(struct list_node *list)
- {
- struct list_node *start = list;
- if(list!=NULL)
- { printk(KERN_ALERT"LIST:\n");
- do
- {
- printk(KERN_ALERT"%d\n",list->data);
- list=list->next;
- }while(list!=start);
- }
- }
- static void remove_list(struct list_node **list)
- {
- if(*list)
- {
- struct list_node *tmp;
- struct list_node *start =*list;
- do
- {
- tmp=(*list)->next;
- kmem_cache_free(list_cache,*list);
- *list=tmp;
- }while(*list!=start);
- *list=NULL;
- }
- }
- static void __exit list_exit(void)
- {
- print_list(list);
- remove_list(&list);
- kmem_cache_destroy(list_cache);
- }
- module_init(list_init);
- module_exit(list_exit);
- MODULE_LICENSE("GPL");
- MODULE_AUTHOR("ja");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement