Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. #include<linux/module.h>
  2. #include<linux/rbtree.h>
  3. #include<linux/slab.h>
  4.  
  5. struct my_struct
  6. {
  7. int data;
  8. struct rb_node node;
  9. };
  10.  
  11. static struct rb_root root = RB_ROOT;
  12.  
  13. static struct my_struct *search_node(struct rb_root *root, int number)
  14. {
  15. struct rb_node *node = root->rb_node;
  16.  
  17. while(node) {
  18. struct my_struct *current_data = rb_entry(node, struct my_struct, node);
  19.  
  20. if(number<current_data->data)
  21. node = node->rb_left;
  22. else if(number>current_data->data)
  23. node = node->rb_right;
  24. else
  25. return current_data;
  26. }
  27. return NULL;
  28. }
  29.  
  30. static bool insert_node(struct rb_root *root, struct my_struct *node)
  31. {
  32. struct rb_node **new_node = &(root->rb_node), *parent = NULL;
  33.  
  34. while(*new_node) {
  35. struct my_struct *this = rb_entry(*new_node, struct my_struct, node);
  36.  
  37. parent = *new_node;
  38. if(node->data<this->data)
  39. new_node = &((*new_node)->rb_left);
  40. else if(node->data>this->data)
  41. new_node = &((*new_node)->rb_right);
  42. else return false;
  43. }
  44.  
  45. rb_link_node(&node->node,parent,new_node);
  46. rb_insert_color(&node->node,root);
  47. return true;
  48. }
  49.  
  50. static int __init rbtreemod_init(void)
  51. {
  52. int i;
  53. struct my_struct *node = NULL;
  54.  
  55. for(i=10;i<25;i++) {
  56. node = (struct my_struct *)kmalloc(sizeof(struct my_struct), GFP_KERNEL);
  57. if(!IS_ERR(node)) {
  58. node->data = i;
  59. if(!insert_node(&root,node))
  60. pr_alert("Blad wstawienia wartosci do wezla!\n");
  61.  
  62. } else
  63. pr_alert("Blad alokacji pamieci dla wezla: %ld\n",PTR_ERR(node));
  64. }
  65. struct rb_node *node2;
  66. struct rb_node *copyNode;
  67. pr_alert("Niemalejaco:");
  68. for(node2 = rb_first(&root); node2;node2 = rb_next(node2)){
  69. if (rb_entry(node2,struct my_struct, node)->data == 10){
  70. copyNode = node2;
  71. pr_alert("Zapisano wezel z wartoscia: 10!");
  72. }
  73.  
  74. pr_notice("Wartosc wezla: %d\n",rb_entry(node2, struct my_struct, node)->data);
  75. }
  76. pr_alert("Nierosnaco: ");
  77. for(node2 = rb_last(&root); node2;node2 = rb_prev(node2)){
  78. if (rb_entry(node2,struct my_struct,node)->data == 10){
  79. rb_replace_node(node2,copyNode,&root);
  80. pr_alert("Zamiana wezla z wartoscia: 10 z zapisanym wezlem!");
  81. }
  82. pr_notice("Wartosc wezla: %d\n",rb_entry(node2, struct my_struct, node)->data);
  83. }
  84. pr_alert("Normalny: ");
  85. for(i=10;i<25;i++) {
  86. node = search_node(&root,i);
  87. if(node)
  88. pr_notice("Wartosc wezla: %d\n",node->data);
  89. else
  90. pr_alert("Blad odczytu wezla!\n");
  91. }
  92.  
  93. return 0;
  94. }
  95.  
  96. static void __exit rbtreemod_exit(void)
  97. {
  98. int i;
  99. struct my_struct *node = NULL;
  100.  
  101. for(i=10;i<25;i++) {
  102. node = search_node(&root,i);
  103. if(node) {
  104. rb_erase(&node->node, &root);
  105. kfree(node);
  106. } else
  107. pr_alert("Blad pozyskania wezla z drzewa czerwono-czarnego!\n");
  108. }
  109. }
  110.  
  111. module_init(rbtreemod_init);
  112. module_exit(rbtreemod_exit);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement