Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. #include<linux/module.h>
  2. #include<linux/slab.h>
  3.  
  4.  
  5. static struct list_node
  6. {
  7. int data;
  8. struct list_node *next;
  9. }*list;
  10.  
  11.  
  12. static struct kmem_cache *list_cache;
  13.  
  14. static struct list_node *create_list(int data)
  15. {
  16. struct list_node *front = (struct list_node *)kmem_cache_alloc(list_cache,GFP_KERNEL);
  17.  
  18. if(front!=NULL)
  19. {
  20. front->data = data;
  21. front->next = front;
  22. }
  23. return front;
  24. }
  25.  
  26. static void add_back(struct list_node *list,int number)
  27. {
  28. if(list)
  29. {
  30. struct list_node *first = list;
  31.  
  32. struct list_node *new_element = (struct list_node *)kmem_cache_alloc(list_cache,GFP_KERNEL);
  33. do
  34. {
  35. list = list->next;
  36. } while(list->next != first);
  37. list->next = new_element;
  38. new_element->next = first;
  39. new_element->data = number;
  40. }
  41. }
  42.  
  43. static int __init list_init(void)
  44. {
  45. int i;
  46. list_cache = kmem_cache_create("cycle list",sizeof(struct list_node),0,SLAB_HWCACHE_ALIGN|SLAB_POISON|SLAB_RED_ZONE,0);
  47.  
  48. if(IS_ERR(list_cache))
  49. {
  50. pr_alert("Error creating cache: %ld\n",PTR_ERR(list_cache));
  51. return -ENOMEM;
  52. }
  53. list = create_list(100);
  54. for(i=101;i<=110;i++)
  55. add_back(list,i);
  56. printk(KERN_ALERT"wypelnilem liste od 100 do 110\n");
  57. return 0;
  58. }
  59.  
  60. static void print_list(struct list_node *list)
  61. {
  62.  
  63. struct list_node *start = list;
  64.  
  65. if(list!=NULL)
  66. { printk(KERN_ALERT"LIST:\n");
  67. do
  68. {
  69. printk(KERN_ALERT"%d\n",list->data);
  70. list=list->next;
  71. }while(list!=start);
  72. }
  73. }
  74.  
  75. static void remove_list(struct list_node **list)
  76. {
  77. if(*list)
  78. {
  79. struct list_node *tmp;
  80. struct list_node *start =*list;
  81.  
  82. do
  83. {
  84. tmp=(*list)->next;
  85. kmem_cache_free(list_cache,*list);
  86. *list=tmp;
  87. }while(*list!=start);
  88. *list=NULL;
  89. }
  90. }
  91.  
  92. static void __exit list_exit(void)
  93. {
  94. print_list(list);
  95. remove_list(&list);
  96. kmem_cache_destroy(list_cache);
  97. }
  98.  
  99. module_init(list_init);
  100. module_exit(list_exit);
  101.  
  102. MODULE_LICENSE("GPL");
  103. MODULE_AUTHOR("ja");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement