{"id":990,"date":"2020-11-30T17:23:25","date_gmt":"2020-11-30T09:23:25","guid":{"rendered":"http:\/\/www.wangkaixuan.tech\/?p=990"},"modified":"2020-11-30T17:25:45","modified_gmt":"2020-11-30T09:25:45","slug":"linux%e5%86%85%e6%a0%b8%e6%95%b0%e6%8d%ae%e7%bb%93%e6%9e%84-list","status":"publish","type":"post","link":"http:\/\/www.wangkaixuan.tech\/?p=990","title":{"rendered":"Linux\u5185\u6838\u6570\u636e\u7ed3\u6784-list"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">1.\u524d\u8a00<\/h2>\n\n\n\n<p>\u6700\u8fd1\u7684\u9879\u76ee\u4e2d\u9700\u8981\u81ea\u5df1\u5b9e\u73b0\u4e00\u4e2alist\uff0c\u987a\u4fbf\u7814\u7a76\u4e86\u4e0blinux\u5185\u6838\u7684list\u5b9e\u73b0\uff08<a href=\"https:\/\/github.com\/torvalds\/linux\/blob\/master\/include\/linux\/list.h\">\u4ee3\u7801\u5728\u6b64<\/a>\uff09\uff0c\u8bb0\u5f55\u5728\u672c\u7bc7\u6587\u7ae0\u4e2d\uff0c\u4f9b\u60a8\u53c2\u8003\u3002<\/p>\n\n\n\n<p>\u672c\u6587\u4ec5\u5c55\u793a\u90e8\u5206\u4ee3\u7801\u7247\u6bb5\uff0c\u5b8c\u6574\u7684\u4ee3\u7801\u5df2\u7ecf\u4e0a\u4f20\u81f3\u6211\u7684<a href=\"https:\/\/github.com\/pandawkx\/playground\/tree\/master\/linux_kernel\/list\">github\u4ed3\u5e93<\/a>\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u4f1a\u4ece\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u5165\u624b\uff0c\u4e32\u8054\u76f8\u5173\u7684\u77e5\u8bc6\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2.\u521d\u59cb\u5316list<\/h2>\n\n\n\n<p>\u6211\u4eec\u5047\u8bbe\u8981\u5b58\u653e\u4e00\u4e2a\u4efb\u52a1\u5217\u8868\uff0c\u6bcf\u4e2a\u4efb\u52a1\u6709\u4e00\u4e2atask_id\u5c5e\u6027\u6765\u6807\u8bc6\u8fd9\u4e2a\u4efb\u52a1\u3002\u7136\u540e\u6211\u4eec\u9700\u8981\u5148\u5b9a\u4e49\u8fd9\u6837\u4e00\u4e2a\u94fe\u8868\u7684\u8282\u70b9\u5143\u7d20\u7c7b\u578b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>struct task_node_t\n{\n    int task_id;\n    struct list_head list_node;\n};<\/code><\/pre>\n\n\n\n<p>\u6211\u4eec\u6ce8\u610f\u5230\u8fd9\u91cc\u9762\u6709\u4e00\u4e2a<strong>struct list_head<\/strong>\uff0c\u8fd9\u4e2a\u7ed3\u6784\u4f53\u662flinux\u5185\u6838\u94fe\u8868\u81ea\u5e26\u7684\u7ed3\u6784\u4f53\uff0c\u4ee3\u8868linux\u94fe\u8868\u7684\u8282\u70b9\u5143\u7d20\uff0c\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ copy from kernel\/include\/linux\/types.h\nstruct list_head {\n\tstruct list_head *next, *prev;\n};<\/code><\/pre>\n\n\n\n<p>\u5982\u4e0a\u6240\u793a\uff0c\u5b83\u5b9a\u4e49\u4e86\u94fe\u8868\u7684next\u548cprev\u5143\u7d20\uff0c\u7c7b\u578b\u662f\u6307\u9488\uff0c\u6307\u5411\u7684\u5bf9\u8c61\u7c7b\u578b\u662f\u5b83\u81ea\u5df1\u3002<\/p>\n\n\n\n<p>\u8fd9\u5c31\u5f15\u51fa\u4e86linux\u5185\u6838\u94fe\u8868\u7684\u7b2c\u4e00\u4e2a\u5173\u952e\u8bbe\u8ba1\uff1a<strong>\u628a\u94fe\u8868\u8282\u70b9\u5d4c\u5165\u7528\u6237\u81ea\u5b9a\u4e49\u7c7b\u578b\u4e2d<\/strong>(\u800c\u4e0d\u662f\u53cd\u4e4b)\u3002<\/p>\n\n\n\n<p>\u4f7f\u7528\u8fd9\u4e2a\u5173\u952e\u8bbe\u8ba1\u7684\u76ee\u7684\u662f<strong>\u5b9e\u73b0\u6cdb\u578b<\/strong>\uff0c\u8fd9\u4e00\u70b9\u5728\u540e\u9762\u7684\u7bc7\u5e45\u4e2d\u6211\u4eec\u4f1a\u8be6\u7ec6\u8bf4\uff0c\u8fd9\u91cc\u5148\u5356\u4e2a\u5173\u5b50\u3002<\/p>\n\n\n\n<p>\u5b9a\u4e49\u4e86\u94fe\u8868\u8282\u70b9\u4e4b\u540e\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u5176\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u7684\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/init list head\nLIST_HEAD(task_list_head);<\/code><\/pre>\n\n\n\n<p>LIST_HEAD\u662flinux\u5185\u6838\u94fe\u8868\u81ea\u5e26\u7684\u5b8f\uff0c\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#define LIST_HEAD_INIT(name) { &amp;(name), &amp;(name) }\n\n#define LIST_HEAD(name) \\\n\tstruct list_head name = LIST_HEAD_INIT(name)<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u5176\u5b9e\u662f\u5b9a\u4e49\u4e86\u4e00\u4e2atask_list_head\u53d8\u91cf\uff0c\u7c7b\u578b\u662fstruct list_head\uff0c\u7136\u540e\u7ed9\u4ed6\u7684next\u548cprev\u53d8\u91cf\u8d4b\u503c\uff0c\u6307\u5411\u4ed6\u81ea\u5df1\uff0c\u5373\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"760\" height=\"308\" src=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/11\/image-66.png\" alt=\"\" class=\"wp-image-993\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">3.\u6dfb\u52a0\u5143\u7d20<\/h2>\n\n\n\n<p>\u6211\u4eec\u7ed9list\u589e\u52a0100\u4e2a\u5143\u7d20\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/insert 100 elements(from head)\nfor(i=0; i&lt;100; ++i)\n{\n    struct task_node_t *p_task = (struct task_node_t *)malloc(sizeof(p_task));\n    p_task->task_id = i;\n    INIT_LIST_HEAD(&amp;p_task->list_node);\n    list_add(&amp;p_task->list_node, &amp;task_list_head);\n}<\/code><\/pre>\n\n\n\n<p>\u6211\u4eec\u65b0\u5efa\u4e86100\u4e2a\u5143\u7d20\uff0c\u4e3a\u5176id\u8d4b\u503c\uff0c\u7136\u540e\u8c03\u7528INIT_LIST_HEAD\u521d\u59cb\u5316\u7ed3\u6784\u4f53\u7684list\u8282\u70b9\u5b57\u6bb5\u3002<\/p>\n\n\n\n<p>INIT_LIST_HEAD\u5176\u5b9e\u662f\u4e00\u4e2a\u5c0f\u51fd\u6570\uff0c\u800c\u975e\u5b8f\u5b9a\u4e49\u3002\u5b83\u662flinux\u5185\u6838\u94fe\u8868\u81ea\u5e26\u7684\uff0c\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>static inline void INIT_LIST_HEAD(struct list_head *list)\n{\n\tlist->next = list;\n\tlist->prev = list;\n}<\/code><\/pre>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u6211\u4eec\u8c03\u7528\u8be5\u51fd\u6570\u7684\u65f6\u5019\uff0c\u662f\u628a\u8be5list\u8282\u70b9\u7684next\u548cprev\u90fd\u6307\u5411\u4e86\u81ea\u5df1\uff08\u8ddf\u4e0a\u9762\u521d\u59cb\u5316\u5934\u8282\u70b9\u7684\u64cd\u4f5c\u4e00\u6837\uff09\u3002<\/p>\n\n\n\n<p>\u7136\u540e\u6211\u4eec\u8c03\u7528\u4e86list_add\u51fd\u6570\uff0c\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>static inline void __list_add(struct list_head *new,\n\t\t\t      struct list_head *prev,\n\t\t\t      struct list_head *next)\n{\n\tnext->prev = new;\n\tnew->next = next;\n\tnew->prev = prev;\n\tprev->next = new;\n}\n\nstatic inline void list_add(struct list_head *new, struct list_head *head)\n{\n\t__list_add(new, head, head->next);\n}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u5176\u5b9e\u662f\u628a\u65b0\u8282\u70b9\u63d2\u5728\u4e86\u5934\u8282\u70b9\u4e4b\u540e\uff0c\u539f\u6709\u5143\u7d20\u4e4b\u524d(\u5373\u4ece\u5934\u90e8\u63d2\u5165)\u3002\u63d2\u5165\u6548\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1370\" height=\"503\" src=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/11\/image-67.png\" alt=\"\" class=\"wp-image-994\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1354\" height=\"560\" src=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/11\/image-71.png\" alt=\"\" class=\"wp-image-1001\"\/><\/figure>\n\n\n\n<p>\u4e0d\u77e5\u9053\u4f60\u6ce8\u610f\u5230\u4e86\u6ca1\u6709\uff0c\u8fd9\u662f\u4e2a\u53cc\u5411\u94fe\u8868\u3002<\/p>\n\n\n\n<p>\u9664\u4e86list_add\u5916\uff0c\u8fd8\u6709\u4e00\u4e2a<strong>list_add_tail<\/strong>\u51fd\u6570\uff0c\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>static inline void list_add_tail(struct list_head *new, struct list_head *head)\n{\n\t__list_add(new, head->prev, head);\n}<\/code><\/pre>\n\n\n\n<p>\u5b9e\u73b0\u6781\u5176\u7b80\u6d01\uff0c\u5c31\u662f\u628a__list_add\u7684\u53c2\u6570\u6362\u4e86\u4e00\u4e0b\uff0c\u4e0elist_add\u63a5\u53e3\u4e0d\u540c\uff0ctail\u7248\u672c\u5728\u8ffd\u52a0\u5143\u7d20\u65f6\u662f\u4ece\u5c3e\u90e8\u8ffd\u52a0\u7684\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4.\u904d\u5386list<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">4.1 \u6d4b\u8bd5\u4ee3\u7801<\/h3>\n\n\n\n<p>\u5148\u770b\u904d\u5386\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>struct task_node_t *p_task;\n\n\/\/foreach and print\nlist_for_each_entry(p_task, &amp;task_list_head, list_node)\n{\n    printf(\"task_id = %d\\n\", p_task->task_id);\n}<\/code><\/pre>\n\n\n\n<p>p_task\u662f\u904d\u5386\u5217\u8868\u65f6\u7528\u7684\u4e34\u65f6\u53d8\u91cf\uff0ctask_list_head\u662f\u94fe\u8868\u5934\uff0clist_node\u662f\u8981\u904d\u5386\u7684\u5143\u7d20\u7ed3\u6784\u4f53\u4e2d\uff0c\u5145\u5f53\u94fe\u8868\u8282\u70b9\u7684\u90a3\u4e2a\u5b57\u6bb5\u7684\u540d\u79f0\uff0c\u4ee5\u6211\u4eec\u7684struct task_node_t\u4e3a\u4f8b\uff0c\u662flist_node\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4.2 list_for_each_entry<\/h3>\n\n\n\n<p>list_for_each_entry\u662f\u4e2a\u5b8f\u5b9a\u4e49\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#define list_entry(ptr, type, member) \\\n\tcontainer_of(ptr, type, member)\n\n#define list_first_entry(ptr, type, member) \\\n\tlist_entry((ptr)->next, type, member)\n\t\n#define list_entry_is_head(pos, head, member) \\\n\t(&amp;pos->member == (head))\n\n#define list_next_entry(pos, member) \\\n\tlist_entry((pos)->member.next, typeof(*(pos)), member)\n\t\n#define list_for_each_entry(pos, head, member)\t\\\n\tfor (pos = list_first_entry(head, typeof(*pos), member); \\\n\t     !list_entry_is_head(pos, head, member); \\\n\t     pos = list_next_entry(pos, member))<\/code><\/pre>\n\n\n\n<p>\u4fe1\u606f\u91cf\u6bd4\u8f83\u5927\uff0c\u6211\u4eec\u4e00\u70b9\u4e00\u70b9\u5206\u6790\u3002<\/p>\n\n\n\n<p>list_for_each_entry\u5176\u5b9e\u662f\u4e00\u4e2afor\u5faa\u73af\uff0c\u9700\u8981\u7279\u522b\u6ce8\u610f\u7684\u662f\uff0c\u5b83\u7684\u7ec8\u7ed3\u6761\u4ef6\u662f\uff0c\u4fbf\u5229\u5230\u5934\u8282\u70b9\u7684\u65f6\u5019\u505c\u6b62\u3002\u8fd9\u4e00\u70b9\u5176\u5b9e\u4e5f\u6bd4\u8f83\u597d\u7406\u89e3\uff0c\u56e0\u4e3alinux\u5185\u6838\u94fe\u8868\u662f\u4e2a\u53cc\u5411\u94fe\u8868\uff0c\u904d\u5386\u4e86\u4e00\u5708\u56de\u5230\u5934\u90e8\u7684\u65f6\u5019\u5c31\u8bc1\u660e\u904d\u5386\u5b8c\u4e86\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4.3 container_of\u4e0eoffsetof<\/h3>\n\n\n\n<p>\u6211\u4eec\u7ee7\u7eed\u770blist_first_entry\uff0c\u5b83\u8c03\u7528\u4e86list_entry\uff0clist_entry\u53c8\u8c03\u7528\u4e86container_of\uff0c\u800ccontainer_of\u662f\u4e2a\u5b8f\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ copy from kernel\/include\/linux\/kernel.h\n#define container_of(ptr, type, member) ({ \\\n\tvoid *__mptr = (void *)(ptr); \\\n\t((type *)(__mptr - offsetof(type, member))); })<\/code><\/pre>\n\n\n\n<p>\u8fd9\u662f\u4e00\u4e2a\u5f88\u8457\u540d\u7684\u5b8f\uff0c\u4f5c\u7528\u662f\u6839\u636e\u7ed3\u6784\u4f53\u7684\u67d0\u4e2a\u6210\u5458\u53d8\u91cf\u7684\u5730\u5740\uff0c\u6c42\u7ed3\u6784\u4f53\u7684\u5730\u5740\u3002<\/p>\n\n\n\n<p>\u4ee5\u6211\u4eec\u7684struct list_head list_node\u4e3a\u4f8b\uff0c\u6211\u4eec\u5176\u5b9e\u662f\u60f3\u6839\u636elist_node\u7684\u5730\u5740\uff0c\u6c42\u5305\u542b\u5b83\u7684struct task_node_t\u7684\u5730\u5740\u3002<\/p>\n\n\n\n<p>\u5176\u5b9e\u7b97\u8d77\u6765\u6bd4\u8f83\u7b80\u5355\uff0c\u6211\u4eec\u53ea\u9700\u8981\u77e5\u9053list_node\u5728\u5305\u542b\u5b83\u7684task_node_t\u7ed3\u6784\u4f53\u4e2d\u7684\u504f\u79fb\u91cf\uff0c\u7136\u540e\u7528list_node\u7684\u5730\u5740\u51cf\u53bb\u8fd9\u4e2a\u504f\u79fb\u91cf\u5373\u53ef\u3002<\/p>\n\n\n\n<p>\u8fd9\u4e2a\u504f\u79fb\u91cf\u53ef\u4ee5\u901a\u8fc7offsetof\u5b8f\u6c42\u51fa\uff0c\u8fd9\u4e2a\u5b8f\u662f\u6807\u51c6\u5b8f\uff0c\u5305\u542bstddef.h\u5373\u53ef\u4f7f\u7528\uff0c\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#define offsetof(TYPE, MEMBER)\t((size_t)&amp;((TYPE *)0)->MEMBER)<\/code><\/pre>\n\n\n\n<p>\u539f\u7406\u4e5f\u6bd4\u8f83\u7b80\u5355\uff0c\u5f53\u5143\u7d20\u7ed3\u6784\u4f53\u7684\u5730\u5740\u662f0\u65f6\uff0c\u5b83\u7684\u6210\u5458\u7684\u5730\u5740\u503c\u5c31\u662f\u504f\u79fb\u91cf\uff01<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4.4 list_first_entry\u7b49<\/h3>\n\n\n\n<p>\u6211\u4eec\u7ee7\u7eed\u56de\u5934\u770blist_first_entry\uff0c\u5b83\u5148\u8c03\u7528\u4e86list_entry\uff0c\u4f5c\u7528\u662f\u5c06list_node\u7684\u6307\u9488\u7ffb\u8bd1\u6210\u5305\u542b\u5b83\u7684struct task_node_t\u7684\u6307\u9488\u3002<\/p>\n\n\n\n<p>list_first_entry\u7684ptr\u53c2\u6570\u5176\u5b9e\u662f\u6211\u4eec\u5b9a\u4e49\u7684\u5934\u7ed3\u70b9(task_list_head)\u7684\u6307\u9488,ptr\u7684next\u6307\u5411\u7684\u662f\u7b2c\u4e00\u4e2astruct task_node_t\u5143\u7d20\u7684list_node\u7684\u6210\u5458\u53d8\u91cf\uff0c\u6211\u4eec\u6c42\u4ed6\u7684container\uff0c\u5176\u5b9e\u6c42\u5f97\u7684\u5c31\u662f\u7b2c\u4e00\u4e2astruct task_node_t\u5143\u7d20\u7684\u6307\u9488\uff08\u5b58\u653e\u5728p_task\u53d8\u91cf\u4e2d\uff09\u3002<\/p>\n\n\n\n<p>\u7406\u89e3\u4e86list_first_entry\uff0c\u53e6\u5916\u4e24\u4e2a\u4e5f\u5c31\u4e0d\u96be\u7406\u89e3\u4e86\u3002<\/p>\n\n\n\n<p>list_next_entry\u5e95\u5c42\u4e5f\u662f\u8c03\u7528\u7684list_entry\uff0c\u53ea\u4e0d\u8fc7\u4ed6\u4f20\u5165\u7684\u662f\u81ea\u5df1\u4e0b\u4e00\u4e2a\u8282\u70b9\u7684list_node\u53d8\u91cf\u7684\u6307\u9488\uff0c\u6c42\u51fa\u6765\u7684\u81ea\u7136\u662f\u4e0b\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n\n\n\n<p>\u9700\u8981\u7279\u522b\u6ce8\u610f\u7684\u662f\uff0c\u5f53\u904d\u5386\u5b8c\u94fe\u8868\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u65f6\uff0c\u5b83\u4f1a\u6307\u5411\u5934\u7ed3\u70b9\u3002\u6211\u4eec\u7684\u5934\u7ed3\u70b9task_list_head\u662f\u6ca1\u6709\u5916\u56f4\u7684\u7ed3\u6784\u4f53\u5305\u88f9\u7684\uff0c\u56e0\u6b64\u8fd9\u65f6\u8fd4\u56de\u7684\u6307\u9488\u5176\u5b9e\u5e76\u4e0d\u6307\u5411\u4e00\u4e2a\u771f\u6b63\u7684struct task_node_t\u7ed3\u6784\u4f53\uff08\u4e25\u683c\u6765\u8bb2\uff0c\u8fd9\u662f\u4e2a\u975e\u6cd5\u7684\u5730\u5740\uff09\u3002<\/p>\n\n\n\n<p>\u7406\u89e3\u4e86\u4e0a\u9762\u4e00\u70b9\uff0c\u6211\u4eec\u518d\u6765\u770blist_entry_is_head\u7684\u5b9e\u73b0\u3002\u94fe\u8868\u7684\u5934\u7ed3\u70b9\u6ca1\u6709\u5916\u56f4\u5305\u88f9\u5b83\u7684\u7ed3\u6784\u4f53\uff0c\u56e0\u6b64\u4e0a\u8ff0\u201c\u975e\u6cd5\u201d\u7684struct task_node_t\u7ed3\u6784\u4f53\u6307\u9488\u7684list_node\u6210\u5458\u53d8\u91cf\uff0c\u5176\u5b9e\u6307\u5411\u7684\u5c31\u662f\u4ed6\u81ea\u5df1\u3002<\/p>\n\n\n\n<p>\u8bf4\u4e86\u8fd9\u4e48\u591a\uff0c\u6211\u4eec\u770b\u4e0b\u8fd0\u884c\u6548\u679c\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"304\" height=\"356\" src=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/11\/image-69.png\" alt=\"\" class=\"wp-image-998\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">5.\u5220\u9664\u5143\u7d20<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">5.1 list_del<\/h3>\n\n\n\n<p>\u5220\u9664\u5143\u7d20\u7684\u63a5\u53e3\u662flist_del\uff0c\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>static inline void __list_del(struct list_head * prev, struct list_head * next)\n{\n\tnext->prev = prev;\n\tprev->next = next;\n}\n\nstatic inline void __list_del_entry(struct list_head *entry)\n{\n\t__list_del(entry->prev, entry->next);\n}\n\nstatic inline void list_del(struct list_head *entry)\n{\n\t__list_del_entry(entry);\n\tentry->next = NULL;\n\tentry->prev = NULL;\n}<\/code><\/pre>\n\n\n\n<p>\u770b\u8d77\u6765\u5f88\u7b80\u5355\uff0c\u5c31\u662f\u628a\u4ed6\u524d\u9762\u7684\u5143\u7d20\u7684next\u6307\u5411\u540e\u9762\u7684\u5143\u7d20\uff0c\u540e\u9762\u5143\u7d20\u7684prev\u5143\u7d20\u6307\u5411\u524d\u9762\u7684\u5143\u7d20\uff0c\u7136\u540e\u628a\u4ed6\u81ea\u5df1\u7684prev\u548cnext\u6307\u9488\u7f6e\u7a7a\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5.2 \u904d\u5386\u65f6\u5220\u9664<\/h3>\n\n\n\n<p>\u4e0b\u4e00\u4e2a\u95ee\u9898\u662f\u5982\u4f55\u5728\u904d\u5386\u65f6\u5220\u9664\u3002\u7528\u8fc7stl\u7684\u90fd\u77e5\u9053\uff0cstl\u7684list\u5728\u5220\u9664\u5143\u7d20\u7684\u65f6\u5019\u56de\u9047\u5230\u8fed\u4ee3\u5668\u5931\u6548\u7684\u95ee\u9898\uff0clinux\u5185\u6838\u7684list\u540c\u6837\u65e0\u6cd5\u907f\u514d\u3002<\/p>\n\n\n\n<p>\u4e3a\u6b64\uff0clinux\u5185\u6838\u63d0\u4f9b\u4e86\u4e00\u4e2a\u4e13\u7528\u7684safe\u904d\u5386\u63a5\u53e3\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#define list_for_each_entry_safe(pos, n, head, member) \\\n\tfor (pos = list_first_entry(head, typeof(*pos), member), \\\n\t\tn = list_next_entry(pos, member); \\\n\t     !list_entry_is_head(pos, head, member); \\\n\t     pos = n, n = list_next_entry(n, member))<\/code><\/pre>\n\n\n\n<p>\u5176\u539f\u7406\u662f\u5229\u7528\u4e00\u4e2a\u4e34\u65f6\u7684\u53d8\u91cfn\u4fdd\u5b58\u5f53\u524d\u5143\u7d20\u7684\u4e0b\u4e00\u4e2a\u5143\u7d20\uff0c\u4e00\u8f6e\u904d\u5386\u7ed3\u675f\u65f6\uff0c\u628a\u904d\u5386\u53d8\u91cfpos\u8d4b\u503c\u4e3an\uff0c\u7136\u540e\u518d\u6c42\u4e0b\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n\n\n\n<p>\u6d4b\u8bd5\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>struct task_node_t *p_task;\nstruct task_node_t *p_task_tmp;\n\n\/\/delete odd elements\nlist_for_each_entry_safe(p_task, p_task_tmp, &amp;task_list_head, list_node)\n{\n\tif(p_task->task_id % 2 == 1)\n\t{\n\t\tlist_del(&amp;p_task->list_node);\n\t\tfree(p_task);\n\t}\n}<\/code><\/pre>\n\n\n\n<p>\u8fd0\u884c\u6548\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"254\" height=\"287\" src=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/11\/image-70.png\" alt=\"\" class=\"wp-image-999\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">6.\u603b\u7ed3<\/h2>\n\n\n\n<p>\u770b\u5b8clinux\u5185\u6838\u7684list\u5b9e\u73b0\uff0c\u4e0d\u7531\u5f97\u611f\u6168\uff0c\u679c\u7136\u4f60\u5927\u7237\u8fd8\u662f\u4f60\u5927\u7237\u3002<\/p>\n\n\n\n<p>\u77ed\u77ed\u767e\u4f59\u884c\u4ee3\u7801\u5c31\u5b9e\u73b0\u4e86\u4e00\u4e2a\u652f\u6301\u6cdb\u578b\u7684list\uff0c\u51fd\u6570\u7684\u5c01\u88c5\u5f88\u8003\u7a76\uff0c\u53ef\u8bfb\u6027\u5f88\u597d\uff0c\u4f46\u662f\u6ca1\u6709\u4e00\u53e5\u5e9f\u8bdd\uff1b\u7f16\u7a0b\u6280\u5de7\u7684\u8fd0\u7528\u4e5f\u6070\u5230\u597d\u5904\uff0c\u5e73\u8861\u4e86\u6548\u7387\u3001\u7b80\u6d01\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n\n\n\n<p>\u8fd9\u6837\u7684\u4ee3\u7801\uff0c\u6ca1\u4e2a10\u5e74\u7684\u529f\u529b\uff0c\u7edd\u5bf9\u5199\u4e0d\u51fa\u6765\u3002<\/p>\n\n\n\n<p>\u4eba\u5916\u6709\u4eba\u5929\u5916\u6709\u5929\uff0c\u52aa\u529b\u5427\u9a9a\u5e74\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1.\u524d\u8a00 \u6700\u8fd1\u7684\u9879\u76ee\u4e2d\u9700\u8981\u81ea\u5df1\u5b9e\u73b0\u4e00\u4e2alist\uff0c\u987a\u4fbf\u7814\u7a76\u4e86\u4e0blinux\u5185\u6838\u7684list\u5b9e\u73b0\uff08\u4ee3\u7801\u5728\u6b64\uff09\uff0c\u8bb0\u5f55\u5728\u672c\u7bc7\u6587\u7ae0\u4e2d\uff0c\u4f9b\u60a8\u53c2\u8003\u3002 \u672c\u6587\u4ec5\u5c55\u793a\u90e8\u5206\u4ee3\u7801\u7247\u6bb5\uff0c\u5b8c\u6574&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"http:\/\/www.wangkaixuan.tech\/?p=990\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-990","post","type-post","status-publish","format-standard","hentry","category-03-01-"],"_links":{"self":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/posts\/990","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=990"}],"version-history":[{"count":0,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/posts\/990\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=990"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=990"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=990"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}