bidirect_list.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /****************************************************************************
  2. *
  3. * The MIT License (MIT)
  4. *
  5. * Copyright (c) 2014 - 2021 VERISILICON
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. *
  25. *****************************************************************************
  26. *
  27. * The GPL License (GPL)
  28. *
  29. * Copyright (C) 2014 - 2021 VERISILICON
  30. *
  31. * This program is free software; you can redistribute it and/or
  32. * modify it under the terms of the GNU General Public License
  33. * as published by the Free Software Foundation; either version 2
  34. * of the License, or (at your option) any later version.
  35. *
  36. * This program is distributed in the hope that it will be useful,
  37. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  38. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  39. * GNU General Public License for more details.
  40. *
  41. * You should have received a copy of the GNU General Public License
  42. * along with this program; if not, write to the Free Software Foundation,
  43. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  44. *
  45. *****************************************************************************
  46. *
  47. * Note: This software is released under dual MIT and GPL licenses. A
  48. * recipient may use this file under the terms of either the MIT license or
  49. * GPL License. If you wish to use only one license not the other, you can
  50. * indicate your decision by deleting one of the above license notices in your
  51. * version of this file.
  52. *
  53. *****************************************************************************/
  54. #ifdef __FREERTOS__
  55. #include <string.h>
  56. #include "osal.h"
  57. #elif defined(__linux__)
  58. #include <linux/kernel.h>
  59. #include <linux/module.h>
  60. /* needed for __init,__exit directives */
  61. #include <linux/init.h>
  62. /* needed for remap_page_range
  63. SetPageReserved
  64. ClearPageReserved
  65. */
  66. #include <linux/mm.h>
  67. /* obviously, for kmalloc */
  68. #include <linux/slab.h>
  69. /* for struct file_operations, register_chrdev() */
  70. #include <linux/fs.h>
  71. /* standard error codes */
  72. #include <linux/errno.h>
  73. #include <linux/moduleparam.h>
  74. /* request_irq(), free_irq() */
  75. #include <linux/interrupt.h>
  76. #include <linux/sched.h>
  77. #include <linux/semaphore.h>
  78. #include <linux/spinlock.h>
  79. /* needed for virt_to_phys() */
  80. #include <asm/io.h>
  81. #include <linux/pci.h>
  82. #include <asm/uaccess.h>
  83. #include <linux/ioport.h>
  84. #include <asm/irq.h>
  85. #include <linux/version.h>
  86. #include <linux/vmalloc.h>
  87. #include <linux/timer.h>
  88. #else //For other os
  89. //TODO...
  90. #endif
  91. #include "bidirect_list.h"
  92. void init_bi_list(bi_list* list)
  93. {
  94. list->head = NULL;
  95. list->tail = NULL;
  96. }
  97. bi_list_node* bi_list_create_node(void)
  98. {
  99. bi_list_node* node=NULL;
  100. node=(bi_list_node*)vmalloc(sizeof(bi_list_node));
  101. if(node==NULL)
  102. {
  103. PDEBUG ("%s\n","vmalloc for node fail!");
  104. return node;
  105. }
  106. memset(node,0,sizeof(bi_list_node));
  107. return node;
  108. }
  109. void bi_list_free_node(bi_list_node* node)
  110. {
  111. //free current node
  112. vfree(node);
  113. return;
  114. }
  115. void bi_list_insert_node_tail(bi_list* list,bi_list_node* current_node)
  116. {
  117. if(current_node==NULL)
  118. {
  119. PDEBUG ("%s\n","insert node tail NULL");
  120. return;
  121. }
  122. if(list->tail)
  123. {
  124. current_node->previous=list->tail;
  125. list->tail->next=current_node;
  126. list->tail=current_node;
  127. list->tail->next=NULL;
  128. }
  129. else
  130. {
  131. list->head=current_node;
  132. list->tail=current_node;
  133. current_node->next=NULL;
  134. current_node->previous=NULL;
  135. }
  136. return;
  137. }
  138. void bi_list_insert_node_before(bi_list* list,bi_list_node* base_node,bi_list_node* new_node)
  139. {
  140. bi_list_node* temp_node_previous=NULL;
  141. if(new_node==NULL)
  142. {
  143. PDEBUG ("%s\n","insert node before new node NULL");
  144. return;
  145. }
  146. if(base_node)
  147. {
  148. if(base_node->previous)
  149. {
  150. //at middle position
  151. temp_node_previous = base_node->previous;
  152. temp_node_previous->next=new_node;
  153. new_node->next = base_node;
  154. base_node->previous = new_node;
  155. new_node->previous=temp_node_previous;
  156. }
  157. else
  158. {
  159. //at head
  160. base_node->previous = new_node;
  161. new_node->next = base_node;
  162. list->head=new_node;
  163. new_node->previous = NULL;
  164. }
  165. }
  166. else
  167. {
  168. //at tail
  169. bi_list_insert_node_tail(list,new_node);
  170. }
  171. return;
  172. }
  173. void bi_list_remove_node(bi_list* list,bi_list_node* current_node)
  174. {
  175. bi_list_node* temp_node_previous=NULL;
  176. bi_list_node* temp_node_next=NULL;
  177. if(current_node==NULL)
  178. {
  179. PDEBUG ("%s\n","remove node NULL");
  180. return;
  181. }
  182. temp_node_next=current_node->next;
  183. temp_node_previous=current_node->previous;
  184. if(temp_node_next==NULL && temp_node_previous==NULL )
  185. {
  186. //there is only one node.
  187. list->head=NULL;
  188. list->tail=NULL;
  189. }
  190. else if(temp_node_next==NULL)
  191. {
  192. //at tail
  193. list->tail=temp_node_previous;
  194. temp_node_previous->next=NULL;
  195. }
  196. else if( temp_node_previous==NULL)
  197. {
  198. //at head
  199. list->head=temp_node_next;
  200. temp_node_next->previous=NULL;
  201. }
  202. else
  203. {
  204. //at middle position
  205. temp_node_previous->next=temp_node_next;
  206. temp_node_next->previous=temp_node_previous;
  207. }
  208. return;
  209. }