当前位置:   article > 正文

Linux内核中的通用链表list.h在windows下的移植实现_windows本地list.h

windows本地list.h

在windows的通用开发平台上,有MFC或者STL的支持,很少自己去编写一个链表list程序。现在把Linux下的list.h取出来,在Windows平台上实现:


我这里用的是Linux2.4版本的,2.6版本的其实都一样,下面是修改后的list.h源文件,注意几点:① 注释掉了和Linux相关的字眼,如第四行、第六行等,添加了prefetch(w)两个函数的实现;② 因为是在C语言下实现(不是C++),VC6-VC2005-VC2010编译器均不支持C99,而这些编译器遵循的C89规范里不支持inline关键字,所以关键字inline要去掉,直接查找替换为无即可,这一点和gcc的编译器不同;③ C语言里,函数中所有的变量定义一定要放在函数的开始部分,一次性定义完毕,不要在函数体内再定义变量,这一点高版本的VS2010也是如此。

  1. #ifndef _LINUX_LIST_H
  2. #define _LINUX_LIST_H
  3. //#if defined(__KERNEL__) || defined(_LVM_H_INCLUDE)
  4. //#include <linux/prefetch.h>
  5. void prefetch(const void *x) {;}
  6. void prefetchw(const void *x) {;}
  7. /*
  8. * Simple doubly linked list implementation.
  9. *
  10. * Some of the internal functions ("__xxx") are useful when
  11. * manipulating whole lists rather than single entries, as
  12. * sometimes we already know the next/prev entries and we can
  13. * generate better code by using them directly rather than
  14. * using the generic single-entry routines.
  15. */
  16. struct list_head {
  17. struct list_head *next, *prev;
  18. };
  19. #define LIST_HEAD_INIT(name) { &(name), &(name) }
  20. #define LIST_HEAD(name) \
  21. struct list_head name = LIST_HEAD_INIT(name)
  22. #define INIT_LIST_HEAD(ptr) do { \
  23. (ptr)->next = (ptr); (ptr)->prev = (ptr); \
  24. } while (0)
  25. /*
  26. * Insert a new entry between two known consecutive entries.
  27. *
  28. * This is only for internal list manipulation where we know
  29. * the prev/next entries already!
  30. */
  31. static void __list_add(struct list_head *new,
  32. struct list_head *prev,
  33. struct list_head *next)
  34. {
  35. next->prev = new;
  36. new->next = next;
  37. new->prev = prev;
  38. prev->next = new;
  39. }
  40. /**
  41. * list_add - add a new entry
  42. * @new: new entry to be added
  43. * @head: list head to add it after
  44. *
  45. * Insert a new entry after the specified head.
  46. * This is good for implementing stacks.
  47. */
  48. static void list_add(struct list_head *new, struct list_head *head)
  49. {
  50. __list_add(new, head, head->next);
  51. }
  52. /**
  53. * list_add_tail - add a new entry
  54. * @new: new entry to be added
  55. * @head: list head to add it before
  56. *
  57. * Insert a new entry before the specified head.
  58. * This is useful for implementing queues.
  59. */
  60. static void list_add_tail(struct list_head *new, struct list_head *head)
  61. {
  62. __list_add(new, head->prev, head);
  63. }
  64. /*
  65. * Delete a list entry by making the prev/next entries
  66. * point to each other.
  67. *
  68. * This is only for internal list manipulation where we know
  69. * the prev/next entries already!
  70. */
  71. static void __list_del(struct list_head *prev, struct list_head *next)
  72. {
  73. next->prev = prev;
  74. prev->next = next;
  75. }
  76. /**
  77. * list_del - deletes entry from list.
  78. * @entry: the element to delete from the list.
  79. * Note: list_empty on entry does not return true after this, the entry is in an undefined state.
  80. */
  81. static void list_del(struct list_head *entry)
  82. {
  83. __list_del(entry->prev, entry->next);
  84. entry->next = (void *) 0;
  85. entry->prev = (void *) 0;
  86. }
  87. /**
  88. * list_del_init - deletes entry from list and reinitialize it.
  89. * @entry: the element to delete from the list.
  90. */
  91. static void list_del_init(struct list_head *entry)
  92. {
  93. __list_del(entry->prev, entry->next);
  94. INIT_LIST_HEAD(entry);
  95. }
  96. /**
  97. * list_move - delete from one list and add as another's head
  98. * @list: the entry to move
  99. * @head: the head that will precede our entry
  100. */
  101. static void list_move(struct list_head *list, struct list_head *head)
  102. {
  103. __list_del(list->prev, list->next);
  104. list_add(list, head);
  105. }
  106. /**
  107. * list_move_tail - delete from one list and add as another's tail
  108. * @list: the entry to move
  109. * @head: the head that will follow our entry
  110. */
  111. static void list_move_tail(struct list_head *list,
  112. struct list_head *head)
  113. {
  114. __list_del(list->prev, list->next);
  115. list_add_tail(list, head);
  116. }
  117. /**
  118. * list_empty - tests whether a list is empty
  119. * @head: the list to test.
  120. */
  121. static int list_empty(struct list_head *head)
  122. {
  123. return head->next == head;
  124. }
  125. static void __list_splice(struct list_head *list,
  126. struct list_head *head)
  127. {
  128. struct list_head *first = list->next;
  129. struct list_head *last = list->prev;
  130. struct list_head *at = head->next;
  131. first->prev = head;
  132. head->next = first;
  133. last->next = at;
  134. at->prev = last;
  135. }
  136. /**
  137. * list_splice - join two lists
  138. * @list: the new list to add.
  139. * @head: the place to add it in the first list.
  140. */
  141. static void list_splice(struct list_head *list, struct list_head *head)
  142. {
  143. if (!list_empty(list))
  144. __list_splice(list, head);
  145. }
  146. /**
  147. * list_splice_init - join two lists and reinitialise the emptied list.
  148. * @list: the new list to add.
  149. * @head: the place to add it in the first list.
  150. *
  151. * The list at @list is reinitialised
  152. */
  153. static void list_splice_init(struct list_head *list,
  154. struct list_head *head)
  155. {
  156. if (!list_empty(list)) {
  157. __list_splice(list, head);
  158. INIT_LIST_HEAD(list);
  159. }
  160. }
  161. /**
  162. * list_entry - get the struct for this entry
  163. * @ptr: the &struct list_head pointer.
  164. * @type: the type of the struct this is embedded in.
  165. * @member: the name of the list_struct within the struct.
  166. */
  167. #define list_entry(ptr, type, member) \
  168. ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
  169. /**
  170. * list_for_each - iterate over a list
  171. * @pos: the &struct list_head to use as a loop counter.
  172. * @head: the head for your list.
  173. */
  174. #define list_for_each(pos, head) \
  175. for (pos = (head)->next, prefetch(pos->next); pos != (head); \
  176. pos = pos->next, prefetch(pos->next))
  177. /**
  178. * list_for_each_prev - iterate over a list backwards
  179. * @pos: the &struct list_head to use as a loop counter.
  180. * @head: the head for your list.
  181. */
  182. #define list_for_each_prev(pos, head) \
  183. for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \
  184. pos = pos->prev, prefetch(pos->prev))
  185. /**
  186. * list_for_each_safe - iterate over a list safe against removal of list entry
  187. * @pos: the &struct list_head to use as a loop counter.
  188. * @n: another &struct list_head to use as temporary storage
  189. * @head: the head for your list.
  190. */
  191. #define list_for_each_safe(pos, n, head) \
  192. for (pos = (head)->next, n = pos->next; pos != (head); \
  193. pos = n, n = pos->next)
  194. /**
  195. * list_for_each_entry - iterate over list of given type
  196. * @pos: the type * to use as a loop counter.
  197. * @head: the head for your list.
  198. * @member: the name of the list_struct within the struct.
  199. */
  200. #define list_for_each_entry(pos, head, member) \
  201. for (pos = list_entry((head)->next, typeof(*pos), member), \
  202. prefetch(pos->member.next); \
  203. &pos->member != (head); \
  204. pos = list_entry(pos->member.next, typeof(*pos), member), \
  205. prefetch(pos->member.next))
  206. /**
  207. * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  208. * @pos: the type * to use as a loop counter.
  209. * @n: another type * to use as temporary storage
  210. * @head: the head for your list.
  211. * @member: the name of the list_struct within the struct.
  212. */
  213. #define list_for_each_entry_safe(pos, n, head, member) \
  214. for (pos = list_entry((head)->next, typeof(*pos), member), \
  215. n = list_entry(pos->member.next, typeof(*pos), member); \
  216. &pos->member != (head); \
  217. pos = n, n = list_entry(n->member.next, typeof(*n), member))
  218. /**
  219. * list_for_each_entry_continue - iterate over list of given type
  220. * continuing after existing point
  221. * @pos: the type * to use as a loop counter.
  222. * @head: the head for your list.
  223. * @member: the name of the list_struct within the struct.
  224. */
  225. #define list_for_each_entry_continue(pos, head, member) \
  226. for (pos = list_entry(pos->member.next, typeof(*pos), member), \
  227. prefetch(pos->member.next); \
  228. &pos->member != (head); \
  229. pos = list_entry(pos->member.next, typeof(*pos), member), \
  230. prefetch(pos->member.next))
  231. //#endif /* __KERNEL__ || _LVM_H_INCLUDE */
  232. #endif
下面是测试程序:

  1. #include "stdio.h"
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "list.h"
  5. //自定义的数据结构
  6. struct list_test_struct
  7. {
  8. struct list_head list;
  9. int key;
  10. int data;
  11. };
  12. void main()
  13. {
  14. struct list_head list = {0}; //定义链表(头)
  15. struct list_head *pos = NULL;
  16. struct list_head *n = NULL;
  17. int i=0;
  18. printf("定义链表\n");
  19. printf("初始化链表!\r\n");
  20. INIT_LIST_HEAD(&list); //初始化链表(头尾相接,形成空链表循环)
  21. //判断链表是否为空
  22. printf("判断链表是否为空:");
  23. if(list_empty(&list)){
  24. printf("空\r\n");
  25. }else{
  26. printf("非空\r\n");
  27. }
  28. //批量添加节点
  29. printf("批量添加节点:\r\n");
  30. for(i=0;i<10;i++)
  31. {
  32. int key=i; //key
  33. int data=i*10; //data
  34. struct list_test_struct *st=(struct list_test_struct*)malloc(sizeof(struct list_test_struct));
  35. st->key=key;
  36. st->data=data;
  37. list_add(&st->list, &list);
  38. }
  39. //显示列表所有节点
  40. printf("显示列表所有节点:\r\n");
  41. list_for_each(pos,&list)
  42. {
  43. struct list_test_struct *st=list_entry(pos,struct list_test_struct,list);
  44. printf( "\t node:key(%d),data(%d)\r\n",st->key,st->data);
  45. }
  46. //释放所有节点资源
  47. printf("释放所有节点资源!\r\n");
  48. list_for_each_safe(pos,n,&list)
  49. {
  50. struct list_test_struct *st=list_entry(pos,struct list_test_struct,list);
  51. list_del(pos); //删除节点,删除节点必须在删除节点内存之前
  52. free(st); //释放节点内存
  53. }
  54. }

对于复杂的宏定义,可以使用人工宏展开方式来查看:【Setting】 ->【C/C++】在底部的输入选项中,添加“/P”再次编译可以得到一个扩展名为i的文件,既是宏展开后的文件

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/128096
推荐阅读
相关标签
  

闽ICP备14008679号