STM32应用linux内核链表

[复制链接]
2167|8
 楼主| keer_zu 发表于 2021-11-23 10:36 | 显示全部楼层 |阅读模式
STM32应用linux内核链表
链表是非常重要的数据结构,针对不同的情况使用不同数据结构,去解决不同的问题。它的优点:动态删除数据单元,不需要移动元素,存储空间可以不连续(与数组相比)。


1、开发环境

使用的开发板:正点原子开发板战舰V3
单片机型号:STM32F103ZET6

开发软件平台:Keil 5

下载linux内核版本:4.19.144 大小:98.64MB

内核代码查看使用软件:Source Insight 4.0



 楼主| keer_zu 发表于 2021-11-23 10:37 | 显示全部楼层
2、移植

由于Keil 5编译从/linux-4.19.144/include/list.h直接复制过来代码有些问题需要修改,要从这些定义头文件里面复制相关代码出来,并加以修改。。

list.h文件里面定义了几个头文件,清除这几个文件的依赖,如下文件:

#include <linux/types.h>
#include <linux/stddef.h>
#include <linux/poison.h>
#include <linux/const.h>
#include <linux/kernel.h>

【1】在这个工程文件里面,按照红色框里面添加 --gnu 在这里提前写这个,大量减少了报错

20200408214312645.png

 楼主| keer_zu 发表于 2021-11-23 10:40 | 显示全部楼层
【2】先在types.h,存放了结构体拷贝下来,放到移植的头文件。
  1. struct list_head {
  2.     struct list_head* next, * prev;
  3. };


【3】在Keil5平台上,new会成为关键字,new修改成newStruct,保留typeof()代码不改。

【4】删除了预编译定义 #ifdef CONFIG_DEBUG_LIST与 #else 里面相关代码,只保留了 #else 与 #endif 里面代码。
【5】bool 类型使用可能会出现报错,我查找了uVision Help相关文件,使用这个类型要定义头文件

  1. //bool类型会报错,定义这个头文件才行,我的移植添加了这个
  2. #include <stdbool.h>


 楼主| keer_zu 发表于 2021-11-23 10:40 | 显示全部楼层
【6】未定义NULL、LIST_POISON1、LIST_POISON2 ,找了相关的代码发现这样定义比较好一点

  1. #define NULL 0

  2. #define LIST_POISON1  NULL
  3. #define LIST_POISON2  NULL
 楼主| keer_zu 发表于 2021-11-23 10:40 | 显示全部楼层
【7】inline属于C++关键字,这里注释掉这个关键字,一般Ctrl+F换出窗口选择Replace进行替换

inline 替换成 /* inline */

【8】在linux内核文件找到相关定义并修改如下,

  1. #define offsetof(TYPE, MEMBER)        ((size_t)&((TYPE *)0)->MEMBER)

  2. #define container_of(ptr, type, member) \
  3.         (type *)( (char *)(ptr) - offsetof(type,member))
 楼主| keer_zu 发表于 2021-11-23 10:41 | 显示全部楼层
其中**container_of()**是通过 成员变量地址 经过计算偏移后 找到 父地址,然后就可以通过指针访问,找到结构体里面其他的变量成员,这个比较美妙了。换句话说,将这个list_head添加到自己的代码里面就能用了。
 楼主| keer_zu 发表于 2021-11-23 10:42 | 显示全部楼层
3、移植后的代码
  1. //代码移植日期:2020/4/8
  2. //代码移植者:须须草

  3. #ifndef _LINUX_LIST_H
  4. #define _LINUX_LIST_H

  5. //bool类型定义,引用的头文件
  6. #include <stdbool.h>

  7. //结构体文件路径/linux/types.h定义
  8. struct list_head {
  9.     struct list_head* next, * prev;
  10. };

  11. struct hlist_head {
  12.     struct hlist_node* first;
  13. };

  14. struct hlist_node {
  15.     struct hlist_node* next, ** pprev;
  16. };

  17. // 文件路径/tool/virtio/linux/compiler.h
  18. //这里报错添加--gnu -O1 -g -W
  19. #define WRITE_ONCE(var, val)  (*((volatile typeof(var)*)(&(var))) = (val))

  20. #define READ_ONCE(var) (*((volatile typeof(var) *)(&(var))))

  21. //手动定义NULL
  22. #define NULL 0

  23. //去除C++编译器预指令
  24. /*#ifdef __cplusplus*/

  25. #define LIST_POISON1  NULL
  26. #define LIST_POISON2  NULL

  27. /*#endif */

  28. /*
  29. * Simple doubly linked list implementation.
  30. *
  31. * Some of the internal functions ("__xxx") are useful when
  32. * manipulating whole lists rather than single entries, as
  33. * sometimes we already know the next/prev entries and we can
  34. * generate better code by using them directly rather than
  35. * using the generic single-entry routines.
  36. */

  37. #define LIST_HEAD_INIT(name) { &(name), &(name) }

  38. #define LIST_HEAD(name) \
  39.         struct list_head name = LIST_HEAD_INIT(name)

  40. static /*  inline */void INIT_LIST_HEAD(struct list_head* list)
  41. {
  42.     WRITE_ONCE(list->next, list);
  43.     list->prev = list;
  44. }


  45. static /*  inline */bool __list_add_valid(struct list_head* newStruct,
  46.     struct list_head* prev,
  47.     struct list_head* next)
  48. {
  49.     return true;
  50. }
  51. static /*  inline */bool __list_del_entry_valid(struct list_head* entry)
  52. {
  53.     return true;
  54. }

  55. /*
  56. * Insert a new entry between two known consecutive entries.
  57. *
  58. * This is only for internal list manipulation where we know
  59. * the prev/next entries already!
  60. */
  61. static /*  inline */void __list_add(struct list_head* newStruct,
  62.     struct list_head* prev,
  63.     struct list_head* next)
  64. {
  65.     if (!__list_add_valid(newStruct, prev, next))
  66.         return;

  67.     next->prev = newStruct;
  68.     newStruct->next = next;
  69.     newStruct->prev = prev;
  70.     WRITE_ONCE(prev->next, newStruct);
  71. }

  72. /**
  73. * list_add - add a newStruct entry
  74. * @newStruct: new entry to be added
  75. * @head: list head to add it after
  76. *
  77. * Insert a new entry after the specified head.
  78. * This is good for implementing stacks.
  79. */
  80. static /*  inline */void list_add(struct list_head* newStruct, struct list_head* head)
  81. {
  82.     __list_add(newStruct, head, head->next);
  83. }


  84. /**
  85. * list_add_tail - add a newStruct entry
  86. * @newStruct: new entry to be added
  87. * @head: list head to add it before
  88. *
  89. * Insert a new entry before the specified head.
  90. * This is useful for implementing queues.
  91. */
  92. static /*  inline */void list_add_tail(struct list_head* newStruct, struct list_head* head)
  93. {
  94.     __list_add(newStruct, head->prev, head);
  95. }

  96. /*
  97. * Delete a list entry by making the prev/next entries
  98. * point to each other.
  99. *
  100. * This is only for internal list manipulation where we know
  101. * the prev/next entries already!
  102. */
  103. static /*  inline */void __list_del(struct list_head* prev, struct list_head* next)
  104. {
  105.     next->prev = prev;
  106.     WRITE_ONCE(prev->next, next);
  107. }

  108. /**
  109. * list_del - deletes entry from list.
  110. * @entry: the element to delete from the list.
  111. * Note: list_empty() on entry does not return true after this, the entry is
  112. * in an undefined state.
  113. */
  114. static /*  inline */void __list_del_entry(struct list_head* entry)
  115. {
  116.     if (!__list_del_entry_valid(entry))
  117.         return;

  118.     __list_del(entry->prev, entry->next);
  119. }

  120. static /*  inline */void list_del(struct list_head* entry)
  121. {
  122.     __list_del_entry(entry);
  123.     entry->next = LIST_POISON1;
  124.     entry->prev = LIST_POISON2;
  125. }

  126. /**
  127. * list_replace - replace old entry by new one
  128. * [url=home.php?mod=space&uid=1396567]@old[/url] : the element to be replaced
  129. * @newStruct : the new element to insert
  130. *
  131. * If @old was empty, it will be overwritten.
  132. */
  133. static /*  inline */void list_replace(struct list_head* old,
  134.     struct list_head* newStruct)
  135. {
  136.     newStruct->next = old->next;
  137.     newStruct->next->prev = newStruct;
  138.     newStruct->prev = old->prev;
  139.     newStruct->prev->next = newStruct;
  140. }

  141. static /*  inline */void list_replace_init(struct list_head* old,
  142.     struct list_head* newStruct)
  143. {
  144.     list_replace(old, newStruct);
  145.     INIT_LIST_HEAD(old);
  146. }

  147. /**
  148. * list_del_init - deletes entry from list and reinitialize it.
  149. * @entry: the element to delete from the list.
  150. */
  151. static /*  inline */void list_del_init(struct list_head* entry)
  152. {
  153.     __list_del_entry(entry);
  154.     INIT_LIST_HEAD(entry);
  155. }

  156. /**
  157. * list_move - delete from one list and add as another's head
  158. * @list: the entry to move
  159. * @head: the head that will precede our entry
  160. */
  161. static /*  inline */void list_move(struct list_head* list, struct list_head* head)
  162. {
  163.     __list_del_entry(list);
  164.     list_add(list, head);
  165. }

  166. /**
  167. * list_move_tail - delete from one list and add as another's tail
  168. * @list: the entry to move
  169. * @head: the head that will follow our entry
  170. */
  171. static /*  inline */void list_move_tail(struct list_head* list,
  172.     struct list_head* head)
  173. {
  174.     __list_del_entry(list);
  175.     list_add_tail(list, head);
  176. }

  177. /**
  178. * list_is_last - tests whether [url=home.php?mod=space&uid=151083]@list[/url] is the last entry in list @head
  179. * @list: the entry to test
  180. * @head: the head of the list
  181. */
  182. static /*  inline */int list_is_last(const struct list_head* list,
  183.     const struct list_head* head)
  184. {
  185.     return list->next == head;
  186. }

  187. /**
  188. * list_empty - tests whether a list is empty
  189. * @head: the list to test.
  190. */
  191. static /*  inline */int list_empty(const struct list_head* head)
  192. {
  193.     return READ_ONCE(head->next) == head;
  194. }

  195. /**
  196. * list_empty_careful - tests whether a list is empty and not being modified
  197. * @head: the list to test
  198. *
  199. * Description:
  200. * tests whether a list is empty _and_ checks that no other CPU might be
  201. * in the process of modifying either member (next or prev)
  202. *
  203. * NOTE: using list_empty_careful() without synchronization
  204. * can only be safe if the only activity that can happen
  205. * to the list entry is list_del_init(). Eg. it cannot be used
  206. * if another CPU could re-list_add() it.
  207. */
  208. static /*  inline */int list_empty_careful(const struct list_head* head)
  209. {
  210.     struct list_head* next = head->next;
  211.     return (next == head) && (next == head->prev);
  212. }

  213. /**
  214. * list_rotate_left - rotate the list to the left
  215. * @head: the head of the list
  216. */
  217. static /*  inline */void list_rotate_left(struct list_head* head)
  218. {
  219.     struct list_head* first;

  220.     if (!list_empty(head)) {
  221.         first = head->next;
  222.         list_move_tail(first, head);
  223.     }
  224. }

  225. /**
  226. * list_is_singular - tests whether a list has just one entry.
  227. * @head: the list to test.
  228. */
  229. static /*  inline */int list_is_singular(const struct list_head* head)
  230. {
  231.     return !list_empty(head) && (head->next == head->prev);
  232. }

  233. static /*  inline */void __list_cut_position(struct list_head* list,
  234.     struct list_head* head, struct list_head* entry)
  235. {
  236.     struct list_head* new_first = entry->next;
  237.     list->next = head->next;
  238.     list->next->prev = list;
  239.     list->prev = entry;
  240.     entry->next = list;
  241.     head->next = new_first;
  242.     new_first->prev = head;
  243. }

  244. /**
  245. * list_cut_position - cut a list into two
  246. * @list: a new list to add all removed entries
  247. * @head: a list with entries
  248. * @entry: an entry within head, could be the head itself
  249. *        and if so we won't cut the list
  250. *
  251. * This helper moves the initial part of @head, up to and
  252. * including @entry, from [url=home.php?mod=space&uid=179908]@head[/url] to @list. You should
  253. * pass on [url=home.php?mod=space&uid=445349]@entry[/url] an element you know is on @head. @list
  254. * should be an empty list or a list you do not care about
  255. * losing its data.
  256. *
  257. */
  258. static /*  inline */void list_cut_position(struct list_head* list,
  259.     struct list_head* head, struct list_head* entry)
  260. {
  261.     if (list_empty(head))
  262.         return;
  263.     if (list_is_singular(head) &&
  264.         (head->next != entry && head != entry))
  265.         return;
  266.     if (entry == head)
  267.         INIT_LIST_HEAD(list);
  268.     else
  269.         __list_cut_position(list, head, entry);
  270. }

  271. static /*  inline */void __list_splice(const struct list_head* list,
  272.     struct list_head* prev,
  273.     struct list_head* next)
  274. {
  275.     struct list_head* first = list->next;
  276.     struct list_head* last = list->prev;

  277.     first->prev = prev;
  278.     prev->next = first;

  279.     last->next = next;
  280.     next->prev = last;
  281. }

  282. /**
  283. * list_splice - join two lists, this is designed for stacks
  284. * @list: the new list to add.
  285. * @head: the place to add it in the first list.
  286. */
  287. static /*  inline */void list_splice(const struct list_head* list,
  288.     struct list_head* head)
  289. {
  290.     if (!list_empty(list))
  291.         __list_splice(list, head, head->next);
  292. }

  293. /**
  294. * list_splice_tail - join two lists, each list being a queue
  295. * @list: the new list to add.
  296. * @head: the place to add it in the first list.
  297. */
  298. static /*  inline */void list_splice_tail(struct list_head* list,
  299.     struct list_head* head)
  300. {
  301.     if (!list_empty(list))
  302.         __list_splice(list, head->prev, head);
  303. }

  304. /**
  305. * list_splice_init - join two lists and reinitialise the emptied list.
  306. * @list: the new list to add.
  307. * @head: the place to add it in the first list.
  308. *
  309. * The list at @list is reinitialised
  310. */
  311. static /*  inline */void list_splice_init(struct list_head* list,
  312.     struct list_head* head)
  313. {
  314.     if (!list_empty(list)) {
  315.         __list_splice(list, head, head->next);
  316.         INIT_LIST_HEAD(list);
  317.     }
  318. }

  319. /**
  320. * list_splice_tail_init - join two lists and reinitialise the emptied list
  321. * @list: the new list to add.
  322. * @head: the place to add it in the first list.
  323. *
  324. * Each of the lists is a queue.
  325. * The list at @list is reinitialised
  326. */
  327. static /*  inline */void list_splice_tail_init(struct list_head* list,
  328.     struct list_head* head)
  329. {
  330.     if (!list_empty(list)) {
  331.         __list_splice(list, head->prev, head);
  332.         INIT_LIST_HEAD(list);
  333.     }
  334. }

  335. //ÓÉÓÚûÓÐoffsetofºê¶¨Ò壬Õâ¸öÎÒÐèÒªÖØÐÂÌí¼Ó¡£¡£¡£
  336. #define offsetof(TYPE, MEMBER)        ((size_t)&((TYPE *)0)->MEMBER)

  337. #define container_of(ptr, type, member) (type *)((char *) (ptr) - offsetof(type,member))

  338. /**
  339. * list_entry - get the struct for this entry
  340. * @ptr:        the &struct list_head pointer.
  341. * @type:        the type of the struct this is embedded in.
  342. * @member:        the name of the list_head within the struct.
  343. */
  344. #define list_entry(ptr, type, member) \
  345.         container_of(ptr, type, member)

  346. /**
  347.   * list_first_entry - get the first element from a list
  348.   * @ptr:        the list head to take the element from.
  349.   * @type:        the type of the struct this is embedded in.
  350.   * @member:        the name of the list_head within the struct.
  351.   *
  352.   * Note, that list is expected to be not empty.
  353.   */
  354. #define list_first_entry(ptr, type, member) \
  355.         list_entry((ptr)->next, type, member)

  356.   /**
  357.    * list_last_entry - get the last element from a list
  358.    * @ptr:        the list head to take the element from.
  359.    * @type:        the type of the struct this is embedded in.
  360.    * @member:        the name of the list_head within the struct.
  361.    *
  362.    * Note, that list is expected to be not empty.
  363.    */
  364. #define list_last_entry(ptr, type, member) \
  365.         list_entry((ptr)->prev, type, member)

  366.    /**
  367.     * list_first_entry_or_null - get the first element from a list
  368.     * @ptr:        the list head to take the element from.
  369.     * @type:        the type of the struct this is embedded in.
  370.     * @member:        the name of the list_head within the struct.
  371.     *
  372.     * Note that if the list is empty, it returns NULL.
  373.     */
  374. #define list_first_entry_or_null(ptr, type, member) ({ \
  375.         struct list_head *head__ = (ptr); \
  376.         struct list_head *pos__ = READ_ONCE(head__->next); \
  377.         pos__ != head__ ? list_entry(pos__, type, member) : NULL; \
  378. })

  379.     /**
  380.      * list_next_entry - get the next element in list
  381.      * @pos:        the type * to cursor
  382.      * @member:        the name of the list_head within the struct.
  383.      */
  384. #define list_next_entry(pos, member) \
  385.         list_entry((pos)->member.next, typeof(*(pos)), member)

  386.      /**
  387.       * list_prev_entry - get the prev element in list
  388.       * @pos:        the type * to cursor
  389.       * @member:        the name of the list_head within the struct.
  390.       */
  391. #define list_prev_entry(pos, member) \
  392.         list_entry((pos)->member.prev, typeof(*(pos)), member)

  393.       /**
  394.        * list_for_each        -        iterate over a list
  395.        * @pos:        the &struct list_head to use as a loop cursor.
  396.        * @head:        the head for your list.
  397.        */
  398. #define list_for_each(pos, head) \
  399.         for (pos = (head)->next; pos != (head); pos = pos->next)

  400.        /**
  401.         * list_for_each_prev        -        iterate over a list backwards
  402.         * @pos:        the &struct list_head to use as a loop cursor.
  403.         * @head:        the head for your list.
  404.         */
  405. #define list_for_each_prev(pos, head) \
  406.         for (pos = (head)->prev; pos != (head); pos = pos->prev)

  407.         /**
  408.          * list_for_each_safe - iterate over a list safe against removal of list entry
  409.          * @pos:        the &struct list_head to use as a loop cursor.
  410.          * @n:                another &struct list_head to use as temporary storage
  411.          * @head:        the head for your list.
  412.          */
  413. #define list_for_each_safe(pos, n, head) \
  414.         for (pos = (head)->next, n = pos->next; pos != (head); \
  415.                 pos = n, n = pos->next)

  416.          /**
  417.           * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
  418.           * @pos:        the &struct list_head to use as a loop cursor.
  419.           * @n:                another &struct list_head to use as temporary storage
  420.           * @head:        the head for your list.
  421.           */
  422. #define list_for_each_prev_safe(pos, n, head) \
  423.         for (pos = (head)->prev, n = pos->prev; \
  424.              pos != (head); \
  425.              pos = n, n = pos->prev)

  426.           /**
  427.            * list_for_each_entry        -        iterate over list of given type
  428.            * @pos:        the type * to use as a loop cursor.
  429.            * @head:        the head for your list.
  430.            * @member:        the name of the list_head within the struct.
  431.            */
  432. #define list_for_each_entry(pos, head, member)                                \
  433.         for (pos = list_first_entry(head, typeof(*pos), member);        \
  434.              &pos->member != (head);                                        \
  435.              pos = list_next_entry(pos, member))

  436.            /**
  437.             * list_for_each_entry_reverse - iterate backwards over list of given type.
  438.             * @pos:        the type * to use as a loop cursor.
  439.             * @head:        the head for your list.
  440.             * @member:        the name of the list_head within the struct.
  441.             */
  442. #define list_for_each_entry_reverse(pos, head, member)                        \
  443.         for (pos = list_last_entry(head, typeof(*pos), member);                \
  444.              &pos->member != (head);                                         \
  445.              pos = list_prev_entry(pos, member))

  446.             /**
  447.              * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
  448.              * @pos:        the type * to use as a start point
  449.              * @head:        the head of the list
  450.              * @member:        the name of the list_head within the struct.
  451.              *
  452.              * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
  453.              */
  454. #define list_prepare_entry(pos, head, member) \
  455.         ((pos) ? : list_entry(head, typeof(*pos), member))

  456.              /**
  457.               * list_for_each_entry_continue - continue iteration over list of given type
  458.               * @pos:        the type * to use as a loop cursor.
  459.               * @head:        the head for your list.
  460.               * @member:        the name of the list_head within the struct.
  461.               *
  462.               * Continue to iterate over list of given type, continuing after
  463.               * the current position.
  464.               */
  465. #define list_for_each_entry_continue(pos, head, member)                 \
  466.         for (pos = list_next_entry(pos, member);                        \
  467.              &pos->member != (head);                                        \
  468.              pos = list_next_entry(pos, member))

  469.               /**
  470.                * list_for_each_entry_continue_reverse - iterate backwards from the given point
  471.                * @pos:        the type * to use as a loop cursor.
  472.                * @head:        the head for your list.
  473.                * @member:        the name of the list_head within the struct.
  474.                *
  475.                * Start to iterate over list of given type backwards, continuing after
  476.                * the current position.
  477.                */
  478. #define list_for_each_entry_continue_reverse(pos, head, member)                \
  479.         for (pos = list_prev_entry(pos, member);                        \
  480.              &pos->member != (head);                                        \
  481.              pos = list_prev_entry(pos, member))

  482.                /**
  483.                 * list_for_each_entry_from - iterate over list of given type from the current point
  484.                 * @pos:        the type * to use as a loop cursor.
  485.                 * @head:        the head for your list.
  486.                 * @member:        the name of the list_head within the struct.
  487.                 *
  488.                 * Iterate over list of given type, continuing from current position.
  489.                 */
  490. #define list_for_each_entry_from(pos, head, member)                         \
  491.         for (; &pos->member != (head);                                        \
  492.              pos = list_next_entry(pos, member))

  493.                 /**
  494.                  * list_for_each_entry_from_reverse - iterate backwards over list of given type
  495.                  *                                    from the current point
  496.                  * @pos:        the type * to use as a loop cursor.
  497.                  * @head:        the head for your list.
  498.                  * @member:        the name of the list_head within the struct.
  499.                  *
  500.                  * Iterate backwards over list of given type, continuing from current position.
  501.                  */
  502. #define list_for_each_entry_from_reverse(pos, head, member)                \
  503.         for (; &pos->member != (head);                                        \
  504.              pos = list_prev_entry(pos, member))

  505.                  /**
  506.                   * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  507.                   * @pos:        the type * to use as a loop cursor.
  508.                   * @n:                another type * to use as temporary storage
  509.                   * @head:        the head for your list.
  510.                   * @member:        the name of the list_head within the struct.
  511.                   */
  512. #define list_for_each_entry_safe(pos, n, head, member)                        \
  513.         for (pos = list_first_entry(head, typeof(*pos), member),        \
  514.                 n = list_next_entry(pos, member);                        \
  515.              &pos->member != (head);                                         \
  516.              pos = n, n = list_next_entry(n, member))

  517.                   /**
  518.                    * list_for_each_entry_safe_continue - continue list iteration safe against removal
  519.                    * @pos:        the type * to use as a loop cursor.
  520.                    * @n:                another type * to use as temporary storage
  521.                    * @head:        the head for your list.
  522.                    * @member:        the name of the list_head within the struct.
  523.                    *
  524.                    * Iterate over list of given type, continuing after current point,
  525.                    * safe against removal of list entry.
  526.                    */
  527. #define list_for_each_entry_safe_continue(pos, n, head, member)                 \
  528.         for (pos = list_next_entry(pos, member),                                 \
  529.                 n = list_next_entry(pos, member);                                \
  530.              &pos->member != (head);                                                \
  531.              pos = n, n = list_next_entry(n, member))

  532.                    /**
  533.                     * list_for_each_entry_safe_from - iterate over list from current point safe against removal
  534.                     * @pos:        the type * to use as a loop cursor.
  535.                     * @n:                another type * to use as temporary storage
  536.                     * @head:        the head for your list.
  537.                     * @member:        the name of the list_head within the struct.
  538.                     *
  539.                     * Iterate over list of given type from current point, safe against
  540.                     * removal of list entry.
  541.                     */
  542. #define list_for_each_entry_safe_from(pos, n, head, member)                         \
  543.         for (n = list_next_entry(pos, member);                                        \
  544.              &pos->member != (head);                                                \
  545.              pos = n, n = list_next_entry(n, member))

  546.                     /**
  547.                      * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
  548.                      * @pos:        the type * to use as a loop cursor.
  549.                      * @n:                another type * to use as temporary storage
  550.                      * @head:        the head for your list.
  551.                      * @member:        the name of the list_head within the struct.
  552.                      *
  553.                      * Iterate backwards over list of given type, safe against removal
  554.                      * of list entry.
  555.                      */
  556. #define list_for_each_entry_safe_reverse(pos, n, head, member)                \
  557.         for (pos = list_last_entry(head, typeof(*pos), member),                \
  558.                 n = list_prev_entry(pos, member);                        \
  559.              &pos->member != (head);                                         \
  560.              pos = n, n = list_prev_entry(n, member))

  561.                      /**
  562.                       * list_safe_reset_next - reset a stale list_for_each_entry_safe loop
  563.                       * @pos:        the loop cursor used in the list_for_each_entry_safe loop
  564.                       * @n:                temporary storage used in list_for_each_entry_safe
  565.                       * @member:        the name of the list_head within the struct.
  566.                       *
  567.                       * list_safe_reset_next is not safe to use in general if the list may be
  568.                       * modified concurrently (eg. the lock is dropped in the loop body). An
  569.                       * exception to this is if the cursor element (pos) is pinned in the list,
  570.                       * and list_safe_reset_next is called after re-taking the lock and before
  571.                       * completing the current iteration of the loop body.
  572.                       */
  573. #define list_safe_reset_next(pos, n, member)                                \
  574.         n = list_next_entry(pos, member)

  575.                       /*
  576.                        * Double linked lists with a single pointer list head.
  577.                        * Mostly useful for hash tables where the two pointer list head is
  578.                        * too wasteful.
  579.                        * You lose the ability to access the tail in O(1).
  580.                        */

  581. #define HLIST_HEAD_INIT { .first = NULL }
  582. #define HLIST_HEAD(name) struct hlist_head name = {  .first = NULL }
  583. #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
  584. static /*  inline */void INIT_HLIST_NODE(struct hlist_node* h)
  585. {
  586.     h->next = NULL;
  587.     h->pprev = NULL;
  588. }

  589. static /*  inline */int hlist_unhashed(const struct hlist_node* h)
  590. {
  591.     return !h->pprev;
  592. }

  593. static /*  inline */int hlist_empty(const struct hlist_head* h)
  594. {
  595.     return !READ_ONCE(h->first);
  596. }

  597. static /*  inline */void __hlist_del(struct hlist_node* n)
  598. {
  599.     struct hlist_node* next = n->next;
  600.     struct hlist_node** pprev = n->pprev;

  601.     WRITE_ONCE(*pprev, next);
  602.     if (next)
  603.         next->pprev = pprev;
  604. }

  605. static /*  inline */void hlist_del(struct hlist_node* n)
  606. {
  607.     __hlist_del(n);
  608.     n->next = LIST_POISON1;
  609.     n->pprev = LIST_POISON2;
  610. }

  611. static /*  inline */void hlist_del_init(struct hlist_node* n)
  612. {
  613.     if (!hlist_unhashed(n)) {
  614.         __hlist_del(n);
  615.         INIT_HLIST_NODE(n);
  616.     }
  617. }

  618. static /*  inline */void hlist_add_head(struct hlist_node* n, struct hlist_head* h)
  619. {
  620.     struct hlist_node* first = h->first;
  621.     n->next = first;
  622.     if (first)
  623.         first->pprev = &n->next;
  624.     WRITE_ONCE(h->first, n);
  625.     n->pprev = &h->first;
  626. }

  627. /* next must be != NULL */
  628. static /*  inline */void hlist_add_before(struct hlist_node* n,
  629.     struct hlist_node* next)
  630. {
  631.     n->pprev = next->pprev;
  632.     n->next = next;
  633.     next->pprev = &n->next;
  634.     WRITE_ONCE(*(n->pprev), n);
  635. }

  636. static /*  inline */void hlist_add_behind(struct hlist_node* n,
  637.     struct hlist_node* prev)
  638. {
  639.     n->next = prev->next;
  640.     WRITE_ONCE(prev->next, n);
  641.     n->pprev = &prev->next;

  642.     if (n->next)
  643.         n->next->pprev = &n->next;
  644. }

  645. /* after that we'll appear to be on some hlist and hlist_del will work */
  646. static /*  inline */void hlist_add_fake(struct hlist_node* n)
  647. {
  648.     n->pprev = &n->next;
  649. }

  650. static /*  inline */bool hlist_fake(struct hlist_node* h)
  651. {
  652.     return h->pprev == &h->next;
  653. }

  654. /*
  655. * Check whether the node is the only node of the head without
  656. * accessing head:
  657. */
  658. static /*  inline */bool hlist_is_singular_node(struct hlist_node* n, struct hlist_head* h)
  659. {
  660.     return !n->next && n->pprev == &h->first;
  661. }

  662. /*
  663. * Move a list from one list head to another. Fixup the pprev
  664. * reference of the first entry if it exists.
  665. */
  666. static /*  inline */void hlist_move_list(struct hlist_head* old,
  667.     struct hlist_head* newStruct)
  668. {
  669.     newStruct->first = old->first;
  670.     if (newStruct->first)
  671.         newStruct->first->pprev = &newStruct->first;
  672.     old->first = NULL;
  673. }

  674. #define hlist_entry(ptr, type, member) container_of(ptr,type,member)

  675. #define hlist_for_each(pos, head) \
  676.         for (pos = (head)->first; pos ; pos = pos->next)

  677. #define hlist_for_each_safe(pos, n, head) \
  678.         for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
  679.              pos = n)

  680. #define hlist_entry_safe(ptr, type, member) \
  681.         ({ typeof(ptr) ____ptr = (ptr); \
  682.            ____ptr ? hlist_entry(____ptr, type, member) : NULL; \
  683.         })

  684. /**
  685. * hlist_for_each_entry        - iterate over list of given type
  686. * @pos:        the type * to use as a loop cursor.
  687. * @head:        the head for your list.
  688. * @member:        the name of the hlist_node within the struct.
  689. */
  690. #define hlist_for_each_entry(pos, head, member)                                \
  691.         for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
  692.              pos;                                                        \
  693.              pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))

  694. /**
  695.   * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
  696.   * @pos:        the type * to use as a loop cursor.
  697.   * @member:        the name of the hlist_node within the struct.
  698.   */
  699. #define hlist_for_each_entry_continue(pos, member)                        \
  700.         for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member);\
  701.              pos;                                                        \
  702.              pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))

  703.   /**
  704.    * hlist_for_each_entry_from - iterate over a hlist continuing from current point
  705.    * @pos:        the type * to use as a loop cursor.
  706.    * @member:        the name of the hlist_node within the struct.
  707.    */
  708. #define hlist_for_each_entry_from(pos, member)                                \
  709.         for (; pos;                                                        \
  710.              pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))

  711.    /**
  712.     * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  713.     * @pos:        the type * to use as a loop cursor.
  714.     * @n:                another &struct hlist_node to use as temporary storage
  715.     * @head:        the head for your list.
  716.     * @member:        the name of the hlist_node within the struct.
  717.     */
  718. #define hlist_for_each_entry_safe(pos, n, head, member)                 \
  719.         for (pos = hlist_entry_safe((head)->first, typeof(*pos), member);\
  720.              pos && ({ n = pos->member.next; 1; });                        \
  721.              pos = hlist_entry_safe(n, typeof(*pos), member))

  722. #endif

 楼主| keer_zu 发表于 2021-11-23 10:43 | 显示全部楼层
5、主函数应用代码

  1. #include "sys.h"
  2. #include "usart.h"               
  3. #include "delay.h"                  
  4. #include "list.h"

  5. struct student{

  6.         char name[60];
  7.         int id;
  8.         struct list_head list;
  9. };

  10. int main(void)
  11. {                                                       

  12.         struct student *q;
  13.         struct student *p;

  14.         struct student A = { "张三" ,13, LIST_HEAD_INIT(A.list) };
  15.         struct student B = { "小红" ,22, LIST_HEAD_INIT(B.list) };
  16.         struct student C = { "李四" ,34, LIST_HEAD_INIT(C.list) };
  17.        
  18.         list_add_tail(&B.list, &A.list);//B成员添加到A列队尾部
  19.         list_add_tail(&C.list, &A.list);//C成员添加到A列队尾部
  20.        
  21.         Stm32_Clock_Init(9);        //8MHz晶振,8*9=72Mhz系统时钟
  22.         uart_init(72,115200);         //72MHz系统时钟,波特率115200
  23.         delay_init(72);                            //72MHz系统时钟

  24.         while(1)
  25.         {
  26.                
  27.                 q = container_of(&A.list, struct student , list);
  28.                
  29.                 p = container_of(q->list.next, struct student , list);
  30.                 printf("---------------------------------");
  31.                 printf("name:  %d ",p->id);
  32.                 printf("name:  %s ",p->name);
  33.                 printf("---------------------------------");
  34.                
  35.                 p = container_of(q->list.next->next, struct student, list);
  36.                 printf("---------------------------------");
  37.                 printf("name:  %d ",p->id);
  38.                 printf("name:  %s ",p->name);
  39.                 printf("---------------------------------");
  40.                
  41.                 p = container_of(q->list.next->next->next, struct student, list);
  42.                 printf("---------------------------------");
  43.                 printf("id:  %d ",p->id);
  44.                 printf("name:  %s ",p->name);
  45.                 printf("---------------------------------");
  46.                 delay_ms(2000); //ÑÓʱ2Ãë
  47.         }                 
  48. }
 楼主| keer_zu 发表于 2021-11-23 10:43 | 显示全部楼层
程序下载烧录,通过串口助手观察;


27333619c555118615.png

这个结果说明代码移植成功,而且编译后占用空间不大。

总结:利用linux内核链表移植到STM32非常便利,而且链表在数据结构中栈重要地位。


您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:qq群:49734243 Email:zukeqiang@gmail.com

1488

主题

12949

帖子

55

粉丝
快速回复 在线客服 返回列表 返回顶部