{"id":5306,"date":"2021-06-09T18:22:14","date_gmt":"2021-06-09T10:22:14","guid":{"rendered":"https:\/\/damogame.cn\/wordpress\/?p=5306"},"modified":"2021-06-09T18:31:21","modified_gmt":"2021-06-09T10:31:21","slug":"%e5%9f%ba%e6%95%b0%e6%a0%91radix-tree","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/5306","title":{"rendered":"\u57fa\u6570\u6811(radix tree)"},"content":{"rendered":"<div class=\"article-header-box\">\n<div class=\"article-header\">\n<div class=\"article-title-box\"><a href=\"https:\/\/blog.csdn.net\/joker0910\/article\/details\/8250085\">\u539f\u6587\u5730\u5740<\/a><\/div>\n<\/div>\n<\/div>\n<article class=\"baidu_pl\">\n<div id=\"article_content\" class=\"article_content clearfix\">\n<div id=\"content_views\" class=\"htmledit_views\">\n<div>\n<h3><span class=\"mw-headline\">\u00a0<\/span><\/h3>\n<\/div>\n<p>Linux\u57fa\u6570\u6811\uff08radix tree\uff09\u662f\u5c06\u6307\u9488\u4e0elong\u6574\u6570\u952e\u503c\u76f8\u5173\u8054\u7684\u673a\u5236\uff0c\u5b83\u5b58\u50a8\u6709\u6548\u7387\uff0c\u5e76\u4e14\u53ef\u5feb\u901f\u67e5\u8be2\uff0c\u7528\u4e8e\u6307\u9488\u4e0e\u6574\u6570\u503c\u7684\u6620\u5c04\uff08\u5982\uff1aIDR\u673a\u5236\uff09\u3001\u5185\u5b58\u7ba1\u7406\u7b49\u3002<br \/>\nIDR\uff08ID Radix\uff09\u673a\u5236\u662f\u5c06\u5bf9\u8c61\u7684\u8eab\u4efd\u9274\u522b\u53f7\u6574\u6570\u503cID\u4e0e\u5bf9\u8c61\u6307\u9488\u5efa\u7acb\u5173\u8054\u8868\uff0c\u5b8c\u6210\u4eceID\u4e0e\u6307\u9488\u4e4b\u95f4\u7684\u76f8\u4e92\u8f6c\u6362\u3002IDR\u673a\u5236\u4f7f\u7528radix\u6811\u72b6\u7ed3\u6784\u4f5c\u4e3a\u7531id\u8fdb\u884c\u7d22\u5f15\u83b7\u53d6\u6307\u9488\u7684\u7a00\u758f\u6570\u7ec4\uff0c\u901a\u8fc7\u4f7f\u7528\u4f4d\u56fe\u53ef\u4ee5\u5feb\u901f\u5206\u914d\u65b0\u7684ID\uff0cIDR\u673a\u5236\u907f\u514d\u4e86\u4f7f\u7528\u56fa\u5b9a\u5c3a\u5bf8\u7684\u6570\u7ec4\u5b58\u653e\u6307\u9488\u3002IDR\u673a\u5236\u7684API\u51fd\u6570\u5728lib\/idr.c\u4e2d\u5b9e\u73b0\uff0c\u8fd9\u91cc\u4e0d\u52a0\u5206\u6790\u3002<br \/>\nLinux radix\u6811\u6700\u5e7f\u6cdb\u7684\u7528\u9014\u662f\u7528\u4e8e\u5185\u5b58\u7ba1\u7406\uff0c\u7ed3\u6784address_space\u901a\u8fc7radix\u6811\u8ddf\u8e2a\u7ed1\u5b9a\u5230\u5730\u5740\u6620\u5c04\u4e0a\u7684\u6838\u5fc3\u9875\uff0c\u8be5radix\u6811\u5141\u8bb8\u5185\u5b58\u7ba1\u7406\u4ee3\u7801\u5feb\u901f\u67e5\u627e\u6807\u8bc6\u4e3adirty\u6216writeback\u7684\u9875\u3002Linux radix\u6811\u7684API\u51fd\u6570\u5728lib\/radix-tree.c\u4e2d\u5b9e\u73b0\u3002<\/p>\n<p>\uff081\uff09radix\u6811\u6982\u8ff0<\/p>\n<p>radix\u6811\u662f\u901a\u7528\u7684\u5b57\u5178\u7c7b\u578b\u6570\u636e\u7ed3\u6784\uff0cradix\u6811\u53c8\u79f0\u4e3aPAT\u4f4d\u6811\uff08Patricia Trie or crit bit tree\uff09\u3002Linux\u5185\u6838\u4f7f\u7528\u4e86\u6570\u636e\u7c7b\u578bunsigned long\u7684\u56fa\u5b9a\u957f\u5ea6\u8f93\u5165\u7684\u7248\u672c\u3002\u6bcf\u7ea7\u4ee3\u8868\u4e86\u8f93\u5165\u7a7a\u95f4\u56fa\u5b9a\u4f4d\u6570\u3002<br \/>\nradix tree\u662f\u4e00\u79cd\u591a\u53c9\u641c\u7d22\u6811\uff0c\u6811\u7684\u53f6\u5b50\u7ed3\u70b9\u662f\u5b9e\u9645\u7684\u6570\u636e\u6761\u76ee\u3002\u6bcf\u4e2a\u7ed3\u70b9\u6709\u4e00\u4e2a\u56fa\u5b9a\u7684\u30012^n\u6307\u9488\u6307\u5411\u5b50\u7ed3\u70b9\uff08\u6bcf\u4e2a\u6307\u9488\u79f0\u4e3a\u69fdslot\uff09\uff0c\u5e76\u6709\u4e00\u4e2a\u6307\u9488\u6307\u5411\u7236\u7ed3\u70b9\u3002<\/p>\n<p>Linux\u5185\u6838\u5229\u7528radix\u6811\u5728\u6587\u4ef6\u5185\u504f\u79fb\u5feb\u901f\u5b9a\u4f4d\u6587\u4ef6\u7f13\u5b58\u9875\uff0c\u56fe4\u662f\u4e00\u4e2aradix\u6811\u6837\u4f8b\uff0c\u8be5radix\u6811\u7684\u5206\u53c9\u4e3a4(22)\uff0c\u6811\u9ad8\u4e3a4\uff0c\u6811\u7684\u6bcf\u4e2a\u53f6\u5b50\u7ed3\u70b9\u7528\u6765\u5feb\u901f\u5b9a\u4f4d8\u4f4d\u6587\u4ef6\u5185\u504f\u79fb\uff0c\u53ef\u4ee5\u5b9a\u4f4d4x4x4x4=256\u9875\uff0c\u5982\uff1a\u56fe\u4e2d\u865a\u7ebf\u5bf9\u5e94\u7684\u4e24\u4e2a\u53f6\u5b50\u7ed3\u70b9\u7684\u8def\u5f84\u7ec4\u6210\u503c0x00000010\u548c0x11111010\uff0c\u6307\u5411\u6587\u4ef6\u5185\u76f8\u5e94\u504f\u79fb\u6240\u5bf9\u5e94\u7684\u7f13\u5b58\u9875\u3002<\/p>\n<p><span class=\"mw-headline\"><img decoding=\"async\" src=\"http:\/\/blog.chinaunix.net\/photo\/96790_101223120455.jpg\" alt=\"\" \/><\/span><br \/>\n\u56fe4 \u4e00\u4e2a\u56db\u53c9radix\u6811<\/p>\n<p>Linux radix\u6811\u6bcf\u4e2a\u7ed3\u70b9\u670964\u4e2aslot\uff0c\u4e0e\u6570\u636e\u7c7b\u578blong\u7684\u4f4d\u6570\u76f8\u540c\uff0c\u56fe1\u663e\u793a\u4e86\u4e00\u4e2a\u67093\u7ea7\u7ed3\u70b9\u7684radix\u6811\uff0c\u6bcf\u4e2a\u6570\u636e\u6761\u76ee\uff08item\uff09\u53ef\u75283\u4e2a6\u4f4d\u7684\u952e\u503c\uff08key\uff09\u8fdb\u884c\u7d22\u5f15\uff0c\u952e\u503c\u4ece\u5de6\u5230\u53f3\u5206\u522b\u4ee3\u8868\u7b2c1~3\u5c42\u7ed3\u70b9\u4f4d\u7f6e\u3002\u6ca1\u6709\u5b69\u5b50\u7684\u7ed3\u70b9\u5728\u56fe\u4e2d\u4e0d\u51fa\u73b0\u3002\u56e0\u6b64\uff0cradix\u6811\u4e3a\u7a00\u758f\u6811\u63d0\u4f9b\u4e86\u6709\u6548\u7684\u5b58\u50a8\uff0c\u4ee3\u66ff\u56fa\u5b9a\u5c3a\u5bf8\u6570\u7ec4\u63d0\u4f9b\u4e86\u952e\u503c\u5230\u6307\u9488\u7684\u5feb\u901f\u67e5\u627e\u3002<\/p>\n<p><span class=\"mw-headline\"><img decoding=\"async\" src=\"http:\/\/blog.chinaunix.net\/photo\/96790_101223120512.jpg\" alt=\"\" \/><\/span><br \/>\n\u56fe1 \u4e00\u4e2a3\u7ea7\u7ed3\u70b9\u7684radix\u6811\u53ca\u5176\u952e\u503c\u8868\u793a<\/p>\n<p>\uff082\uff09radix\u6811slot\u6570<\/p>\n<p>Linux\u5185\u6838\u6839\u7528\u6237\u914d\u7f6e\u5c06\u6811\u7684slot\u6570\u5b9a\u4e49\u4e3a4\u62166\uff0c\u5373\u6bcf\u4e2a\u7ed3\u70b9\u670916\u621664\u4e2aslot\uff0c\u5982\u56fe2\u6240\u793a\uff0c\u5f53\u6811\u9ad8\u4e3a1\u65f6\uff0c64\u4e2aslot\u5bf9\u5e9464\u4e2a\u9875\uff0c\u5f53\u6811\u9ad8\u4e3a2\u65f6\uff0c\u5bf9\u5e9464*64\u4e2a\u9875\u3002<\/p>\n<p><span class=\"mw-headline\"><img decoding=\"async\" src=\"http:\/\/blog.chinaunix.net\/photo\/96790_101223120628.jpg\" alt=\"\" \/><\/span><br \/>\n\u56fe2 \u9ad8\u4e3a1\u548c2\u3001slot\u6570\u4e3a64\u7684radix\u6811<\/p>\n<p>Linux\u5185\u6838radix\u6811\u7684slot\u6570\u5b9a\u4e49\u5982\u4e0b\uff08\u5728lib\/radix-tree.c\u4e2d\uff09\uff1a<\/p>\n<p>#ifdef __KERNEL__<br \/>\n\/*\u503c\u4e3a6\u65f6\uff0c\u8868\u793a\u6bcf\u4e2a\u7ed3\u70b9\u67092^6\uff1d64\u4e2aslot\uff0c\u503c\u4e3a4\u65f6\uff0c\u8868\u793a\u67092^4=16\u4e2aslot*\/<br \/>\n#define RADIX_TREE_MAP_SHIFT (CONFIG_BASE_SMALL\u00a0? 4\u00a0: 6)<br \/>\n#else<br \/>\n#define RADIX_TREE_MAP_SHIFT 3 \/* \u7528\u4e8e\u66f4\u6709\u5f3a\u5ea6\u7684\u6d4b\u8bd5 *\/<br \/>\n#endif<br \/>\n\/*\u8868\u793a1\u4e2a\u53f6\u5b50\u7ed3\u70b9\u53ef\u6620\u5c04\u7684\u9875\u6570\uff0c\u5982\uff1a1&lt;&lt;6=64\uff0c\u8868\u793a\u53ef\u6620\u5c0464\u4e2aslot\u6620\u5c0464\u9875*\/<br \/>\n#define RADIX_TREE_MAP_SIZE (1UL &lt;&lt; RADIX_TREE_MAP_SHIFT)<br \/>\n#define RADIX_TREE_MAP_MASK (RADIX_TREE_MAP_SIZE-1)<br \/>\n\/*\u5b9a\u4e49slot\u6570\u5360\u7528\u7684long\u7c7b\u578b\u957f\u5ea6\u4e2a\u6570\uff0c\u6bcf\u4e2aslot\u7528\u4f4d\u56fe1\u4f4d\u8fdb\u884c\u6807\u8bb0\uff0c\u5982\uff1a64\u4e2aslot\u65f6\uff0c\u503c\u4e3a2*\/<br \/>\n#define RADIX_TREE_TAG_LONGS \\<br \/>\n((RADIX_TREE_MAP_SIZE + BITS_PER_LONG &#8211; 1) \/ BITS_PER_LONG)<\/p>\n<p>\uff083\uff09\u7ed3\u70b9\u7ed3\u6784<\/p>\n<p>\u6811\u7684\u6839\u7ed3\u70b9\u7ed3\u6784radix_tree_root\u5217\u51fa\u5982\u4e0b\uff08\u5728include\/linux\/radix-tree.h\u4e2d\uff09\uff1a<br \/>\n#define RADIX_TREE_MAX_TAGS 2 \/*\u6bcf\u4e2aslot\u9700\u8981\u7684\u6700\u5927\u6807\u7b7e\u4f4d\u6570*\/<\/p>\n<p>\/*\u6839\u7ed3\u70b9\u7684\u6807\u7b7e\u5b58\u653e\u5728gfp_mask\u4e2d\uff0c\u901a\u8fc7__GFP_BITS_SHIFT\u79fb\u4f4d\u5f97\u5230 *\/<br \/>\nstruct radix_tree_root {<!-- --><br \/>\nunsigned int height; \/* \u4ece\u53f6\u5b50\u5411\u4e0a\u8ba1\u7b97\u7684\u6811\u9ad8\u5ea6 *\/<br \/>\ngfp_t gfp_mask;<br \/>\n\/*\u95f4\u63a5\u6307\u9488\u6307\u5411\u7ed3\u70b9\u800c\u975e\u6570\u636e\u6761\u76ee\uff0c\u901a\u8fc7\u8bbe\u7f6eroot-&gt;rnode\u7684\u4f4e\u4f4d\u8868\u793a\u662f\u5426\u662f\u95f4\u6307\u9488*\/<br \/>\nstruct radix_tree_node *rnode;<br \/>\n#ifdef CONFIG_RADIX_TREE_CONCURRENT<br \/>\nstruct radix_tree_context *context;<br \/>\nstruct task_struct *owner;<br \/>\n#endif<br \/>\n};<\/p>\n<p>\u7ed3\u6784radix_tree_context\u5217\u51fa\u5982\u4e0b\uff1a<br \/>\nstruct radix_tree_context {<!-- --><br \/>\nstruct radix_tree_root *root;<br \/>\n#ifdef CONFIG_RADIX_TREE_CONCURRENT<br \/>\nspinlock_t *locked;<br \/>\n#endif<br \/>\n}<\/p>\n<p>\u6811\u7684\u7ed3\u70b9\u7ed3\u6784radix_tree_node\u5217\u51fa\u5982\u4e0b\uff08\u5728lib\/radix-tree.c\u4e2d\uff09\uff1a<\/p>\n<p>struct radix_tree_node {<!-- --><br \/>\nunsigned int height; \/* \u4ece\u53f6\u5b50\u5411\u4e0a\u8ba1\u7b97\u7684\u6811\u9ad8\u5ea6 *\/<br \/>\nunsigned int count; \/*\u975e\u53f6\u5b50\u7ed3\u70b9\u542b\u6709\u4e00\u4e2acount\u57df\uff0c\u8868\u793a\u51fa\u73b0\u5728\u8be5\u7ed3\u70b9\u7684\u5b69\u5b50\u7684\u6570\u91cf*\/<\/p>\n<p>struct rcu_head rcu_head;<br \/>\nvoid *slots[RADIX_TREE_MAP_SIZE]; \/*\u7ed3\u70b9\u7684slot\u6307\u9488*\/<br \/>\n\/* \u7ed3\u70b9\u6807\u7b7e\u6570\u7ec4=\u6bcf\u4e2aslot\u9700\u8981\u7684\u6700\u5927\u6807\u7b7e\u4f4d\u6570*slot\u6570\u6240\u9700\u7684long\u7c7b\u578b\u53d8\u91cf\u6570 *\/<br \/>\nunsigned long tags[RADIX_TREE_MAX_TAGS][RADIX_TREE_TAG_LONGS];<br \/>\n};<\/p>\n<p>Linux\u5185\u6838radix\u6811\u9ad8\u5ea6\u662f\u53ef\u53d8\u7684\uff0c\u5b83\u4f9d\u8d56\u4e8e\u5b58\u50a8\u7684\u7d22\u5f15\u952e\u503c\u3002radix\u6811\u67e5\u8be2\u5fc5\u987b\u77e5\u9053\u6811\u9ad8\u5ea6\uff0c\u4ee5\u4fbf\u77e5\u9053\u7ed3\u70b9\u7684slot\u662f\u6307\u5411\u53f6\u5b50\u7ed3\u70b9\u7684\u6307\u9488\u8fd8\u662f\u7ed3\u70b9\u7684\u5176\u4ed6\u5c42\u3002Linux\u5728\u7ed3\u70b9\u7ed3\u6784\u4e2d\u6709\u6210\u5458height\u5b58\u5165\u6811\u9ad8\u5ea6\u4fe1\u606f\u3002<br \/>\nLinux radix\u6811\u7684\u72ec\u7279\u7279\u5f81\u662f\u6807\u7b7e\u6269\u5c55\u3002\u56fe2\u663e\u793a\u4e86\u67092\u4e2a\u6807\u7b7e\uff0c\u5206\u522b\u8868\u793a\u6253\u5f00\u548c\u5173\u95ed\u3002\u8fd9\u4e9b\u6807\u7b7e\u662f\u7ed3\u70b9\u7ed3\u6784\u7684\u6210\u5458tags\uff0c\u8be5\u6807\u7b7e\u7528\u4e00\u4e2a\u4f4d\u56fe\u5b9e\u73b0\uff0c\u5728\u52a0\u9501\u7684\u60c5\u51b5\u4e0b\u8bbe\u7f6e\u548c\u6e05\u9664\u3002\u6bcf\u4e2a\u6807\u7b7e\u57fa\u672c\u4e0a\u662f\u4e00\u4e2a\u5728radix\u6811\u9876\u5c42\u4e0a\u7684\u4f4d\u56fe\u5e8f\u53f7\u3002\u6807\u7b7e\u4e0e\u7fa4\u67e5\u8be2\u4e00\u8d77\u7528\u6765\u5728\u7ed9\u5b9a\u7684\u8303\u56f4\u5185\u67e5\u627e\u7ed9\u5b9a\u6807\u7b7e\u96c6\u7684\u9875\u3002<br \/>\n\u6807\u7b7e\u5728\u6bcf\u7ed3\u70b9\u4f4d\u56fe\u4e2d\u7ef4\u62a4\uff0c\u5728\u6bcf\u4e2a\u7ed9\u5b9a\u7684\u7ea7\u522b\u4e0a\uff0c\u53ef\u4ee5\u5224\u5b9a\u4e0b\u4e00\u4e2a\u7ea7\u522b\u662f\u5426\u81f3\u5c11\u6709\u4e00\u4e2a\u6807\u7b7e\u96c6\u3002<\/p>\n<p><span class=\"mw-headline\"><img decoding=\"async\" src=\"http:\/\/blog.chinaunix.net\/photo\/96790_101223120839.jpg\" alt=\"\" \/><\/span><br \/>\n\u56fe2 \u5e26\u67092\u4f4d\u56fe\u6807\u7b7e\u6307\u793a\u76848\u4f4dradix\u6811<\/p>\n<p>\u5728\u7ed3\u70b9\u7ed3\u6784radix_tree_node\u4e2d\uff0ctags\u57df\u662f\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\uff0c\u6bcf\u4e2aslot\u75282\u4f4d\u6807\u8bc6\uff0c\u8fd9\u662f\u4e00\u4e2a\u5178\u578b\u7684\u7528\u7a7a\u95f4\u6362\u65f6\u95f4\u7684\u5e94\u7528\u3002tags\u57df\u7528\u4e8e\u8bb0\u5f55\u8be5\u7ed3\u70b9\u4e0b\u9762\u7684\u5b50\u7ed3\u70b9\u6709\u6ca1\u6709\u76f8\u5e94\u7684\u6807\u5fd7\u4f4d\u3002\u76ee\u524dRADIX_TREE_MAX_TAGS\u4e3a2\uff0c\u8868\u793a\u53ea\u8bb0\u5f55\u4e24\u4e2a\u6807\u5fd7\uff0c\u5176\u4e2dtags[0]\u4e3aPAGE_CACHE_DIRTY\uff0ctags[1]\u4e3aPAGE_CACHE_WRITEBACK\u3002\u5982\u679c\u5f53\u524d\u8282\u70b9\u7684tags[0]\u503c\u4e3a1\uff0c\u90a3\u4e48\u5b83\u7684\u5b50\u6811\u8282\u70b9\u5c31\u5b58\u5728PAGE_CACHE_DIRTY\u8282\u70b9\uff0c\u5426\u5219\u8fd9\u4e2a\u5b50\u6811\u5206\u679d\u5c31\u4e0d\u5b58\u5728\u7740\u8fd9\u6837\u7684\u8282\u70b9\uff0c\u5c31\u4e0d\u5fc5\u518d\u67e5\u627e\u8fd9\u4e2a\u5b50\u6811\u4e86\u3002\u6bd4\u5982\u5728\u67e5\u627ePG_dirty\u7684\u9875\u9762\u65f6\uff0c\u5c31\u4e0d\u9700\u8981\u904d\u5386\u6574\u4e2a\u6811\uff0c\u800c\u53ef\u4ee5\u8df3\u8fc7\u90a3\u4e9btags[0]\u4e3a0\u503c\u7684\u5b50\u6811\uff0c\u8fd9\u6837\u5c31\u63d0\u9ad8\u4e86\u67e5\u627e\u6548\u7387\u3002<br \/>\n\u201c\u52a0\u6807\u7b7e\u67e5\u8be2\u201d\u662f\u8fd4\u56de\u6709\u6307\u5b9a\u6807\u7b7e\u96c6radix\u6811\u6761\u76ee\u7684\u67e5\u8be2\uff0c\u53ef\u4ee5\u67e5\u8be2\u8bbe\u7f6e\u4e86\u6807\u7b7e\u7684\u6761\u76ee\u3002\u52a0\u6807\u7b7e\u67e5\u8be2\u53ef\u4ee5\u5e76\u884c\u6267\u884c\uff0c\u4f46\u5b83\u4eec\u9700\u8981\u901a\u8fc7\u8bbe\u7f6e\u6216\u6e05\u9664\u6807\u7b7e\u4ece\u64cd\u4f5c\u4e0a\u4e92\u65a5\u3002<\/p>\n<p>(4) \u5e76\u884c\u64cd\u4f5c\u7684\u4f18\u5316<\/p>\n<p>Linux radix\u6811\u5e76\u884c\u64cd\u4f5c\u5305\u62ec\u5e76\u884c\u67e5\u8be2\u548c\u5e76\u884c\u4fee\u6539\uff0c\u5176\u4e2d\uff0c\u5e76\u884c\u4fee\u6539\u5728\u6807\u51c6\u5185\u6838\u4e2d\u672a\u5b8c\u6ca1\u6709\u5b9e\u73b0\uff0c\u9700\u8981\u901a\u8fc7\u6253\u8865\u4e01\u83b7\u5f97\u8be5\u529f\u80fd\u3002\u5e76\u884c\u64cd\u4f5c\u8bf4\u660e\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li>RCU\u5e76\u53d1\u67e5\u8be2<\/li>\n<\/ul>\n<p>\u901a\u8fc7\u4f7f\u7528RCU\uff0cRCU Radix\u6811\u53ef\u4ee5\u8fdb\u884c\u5b8c\u5168\u5e76\u53d1\u7684\u67e5\u8be2\u64cd\u4f5c\u3002RCU\u4ece\u6839\u672c\u4e0a\u8981\u6c42\u539f\u5b50\u64cd\u4f5c\u5730\u79fb\u52a8\u6307\u9488\u4ece\u6570\u636e\u7ed3\u6784\u7684\u4e00\u4e2a\u7248\u672c\u5230\u65b0\u7684\u7248\u672c\uff0c\u4fdd\u6301\u65e7\u7248\u672c\u76f4\u5230\u7cfb\u7edf\u7ecf\u8fc7\u9759\u6b62\u72b6\u6001\u3002\u5728\u9759\u6b62\u72b6\u6001\u70b9\uff0c\u65e7\u7248\u672c\u6570\u636e\u7ed3\u6784\u5df2\u6ca1\u6709\u7528\u6237\uff0c\u56e0\u6b64\u53ef\u4ee5\u88ab\u5b89\u5168\u91ca\u653e\u3002<br \/>\nRCU radix\u6811\u7684\u4fee\u6539\u64cd\u4f5c\u4e4b\u95f4\u8fd8\u9700\u8981\u4e32\u884c\u5316\uff0c\u4f46\u662f\u67e5\u8be2\u4e0d\u518d\u9700\u8981\u4e0e\u4fee\u6539\u64cd\u4f5c\u4e32\u884c\u5316\u3002<\/p>\n<ul>\n<li>\u5e76\u53d1\u4fee\u6539<\/li>\n<\/ul>\n<p>RCU\u53ef\u4f7fRCU radix\u6811\u67e5\u8be2\u5b8c\u5168\u5e76\u884c\u5316\uff0c\u4f46\u4fee\u6539\u64cd\u4f5c\u6210\u4e86\u201c\u74f6\u9888\u201d\u3002\u8fd9\u53ef\u901a\u8fc7\u5c06\u5168\u6811\u7684\u9501\u7834\u788e\u6210\u8f83\u5c0f\u7684\u9501\u8fdb\u884c\u6539\u5584\uff0c\u518d\u660e\u663e\u7684\u65b9\u6cd5\u662f\u5bf9\u7ed3\u70b9\u8fdb\u884c\u52a0\u9501\u800c\u975e\u5bf9\u6574\u4e2a\u6811\u52a0\u9501\u3002<br \/>\nradix\u6811\u4fee\u6539\u64cd\u4f5c\u53ef\u5206\u4e3a\u5355\u5411\u548c\u53cc\u5411\u64cd\u4f5c\u3002\u5355\u5411\u64cd\u4f5c\u4ec5\u6267\u884c\u4ece\u6839\u8282\u70b9\u548c\u53f6\u5b50\u7ed3\u70b9\u7684\u5355\u65b9\u5411\u6307\u9488\u79fb\u52a8\uff0c\u5b83\u5305\u62ec\u63d2\u5165\u3001\u66f4\u65b0\u548c\u8bbe\u7f6e\u6807\u7b7e\u64cd\u4f5c\u3002\u53cc\u5411\u64cd\u4f5c\u8f83\u590d\u6742\uff0c\u5b83\u9700\u8981\u5728\u6307\u9488\u79fb\u5230\u53f6\u5b50\u540e\u53c8\u56de\u79fb\uff0c\u5b83\u5305\u62ec\u5220\u9664\u548c\u6e05\u9664\u6807\u7b7e\u64cd\u4f5c\u3002<br \/>\n\u68af\u7ea7\u52a0\u9501\uff08Ladder Locking\uff09\u548c\u9501\u8026\u5408\uff08Lock-Coupling\uff09\u6280\u672f\u5e38\u7528\u4e8e\u6570\u636e\u5e93\u65b9\u9762\uff0c\u5141\u8bb8\u5355\u5411\u904d\u5386\u7ed3\u70b9\u52a0\u9501\u7684\u6811\uff08\u53cc\u5411\u53ef\u80fd\u4ea7\u751f\u6b7b\u9501\uff09\u3002\u5982\u679c\u6240\u6709\u7684\u4fee\u6539\u8005\u4ece\u6811\u9876\u5230\u6811\u5e95\u8fdb\u884c\u4fee\u6539\uff0c\u5e76\u4e14\u4fee\u6539\u7684\u7ed3\u70b9\u6301\u6709\u9501\uff0c\u90a3\u4e48\uff0c\u5411\u4e0b\u904d\u5386\u65f6\u5bf9\u5b69\u5b50\u52a0\u9501\uff0c\u5728\u5b69\u5b50\u88ab\u9501\u4f4f\u65f6\u518d\u91ca\u653e\u8be5\u7ed3\u70b9\u9501\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u5e76\u53d1\u64cd\u4f5c\u662f\u53ef\u80fd\u7684\uff0c\u56e0\u4e3a\u53ea\u8981\u6839\u7ed3\u70b9\u89e3\u9501\uff0c\u53e6\u4e00\u4e2a\u64cd\u4f5c\u5c31\u53ef\u4ee5\u81ea\u4e0a\u5411\u4e0b\u8fdb\u884c\u3002\u5982\u679c\u4e24\u64cd\u4f5c\u7684\u8def\u5f84\u6ca1\u6709\u76f8\u540c\u64cd\u4f5c\u7ed3\u70b9\uff0c\u540e\u4e00\u4e2a\u64cd\u4f5c\u53ef\u80fd\u5728\u524d\u4e00\u4e2a\u64cd\u4f5c\u5b8c\u6210\u4e4b\u524d\u5b8c\u6210\u3002\u6700\u574f\u7684\u60c5\u51b5\u662f\u6d41\u6c34\u7ebf\u64cd\u4f5c\uff0c\u4f46\u8fd9\u8fd8\u662f\u6bd4\u4e32\u884c\u5316\u64cd\u4f5c\u597d\u5f88\u591a\u3002<\/p>\n<p>\u53cc\u5411\u64cd\u4f5c\u5305\u62ec\u5220\u9664\u548c\u6e05\u9664\u6807\u7b7e\u64cd\u4f5c\uff0c\u5206\u522b\u8bf4\u660e\u5982\u4e0b\uff1a<\/p>\n<p>1\uff09\u6e05\u9664\u6807\u7b7e<\/p>\n<p>\u5728radix\u6811\u4e2d\u6e05\u9664\u4e00\u4e2a\u6807\u7b7e\u5305\u62ec\u5411\u4e0b\u904d\u5386\u6811\u3001\u67e5\u627e\u5b9a\u4f4d\u6761\u76ee\u548c\u6e05\u9664\u6761\u76ee\u6807\u7b7e\u7684\u64cd\u4f5c\u3002\u53ea\u8981\u5b69\u5b50\u7ed3\u70b9\u6ca1\u6709\u6253\u6807\u7b7e\u7684\u6761\u76ee\uff0c\u5c31\u53ef\u4ee5\u5411\u4e0a\u904d\u5386\u7ed3\u70b9\u6e05\u9664\u6807\u7b7e\u3002\u7ed3\u675f\u6761\u4ef6\u662f\uff1a\u5982\u679c\u904d\u5386\u9047\u5230\u4e00\u4e2a\u7ed3\u70b9\uff0c\u5728\u6e05\u9664\u4e00\u4e2a\u6807\u7b7e\u540e\uff0c\u5b83\u8fd8\u6709\u4e00\u4e2a\u6216\u591a\u4e2a\u6761\u76ee\u5e26\u6709\u6807\u7b7e\u96c6\uff0c\u5c31\u53ef\u4ee5\u7ed3\u675f\u5411\u4e0a\u904d\u5386\u3002\u4e3a\u4e86\u4e0e\u5411\u4e0b\u904d\u5386\u671f\u95f4\u6709\u540c\u6837\u7684\u7ed3\u675f\u70b9\uff0c\u5c06\u7ec8\u6b62\u6761\u4ef6\u6539\u4e3a\uff1a\u5411\u4e0a\u904d\u5386\u5c06\u5728\u6709\u6bd4\u6e05\u9664\u6807\u7b7e\u6570\u66f4\u591a\u6807\u7b7e\u7684\u7ed3\u70b9\u5904\u7ed3\u675f\u3002\u8fd9\u6837\uff0c\u4e0d\u8bba\u4f55\u65f6\u9047\u5230\u8fd9\u6837\u7684\u7ed3\u70b9\uff0c\u5c06\u4f5c\u4e3a\u4e0a\u904d\u5386\u6811\u7684\u7ed3\u675f\u70b9\u3002<\/p>\n<p>2\uff09\u5220\u9664\u5143\u7d20<\/p>\n<p>\u5220\u9664\u5143\u7d20\u5728\u5220\u9664\u65e0\u7528\u7ed3\u70b9\u65f6\u8fd8\u9700\u8981\u5220\u9664\u8be5\u6761\u76ee\u7684\u6240\u6709\u6807\u7b7e\u3002\u5b83\u7684\u7ec8\u6b62\u6761\u4ef6\u9700\u8981\u6ee1\u8db3\u8fd9\u4e24\u4e2a\u65b9\u9762\u3002\u5411\u4e0a\u56de\u9000\u904d\u5386\u6811\u65f6\u9700\u8981\u6ee1\u8db3\u4e0b\u9762\u7684\u6761\u4ef6\uff1a\u5f53\u9047\u5230\u4e00\u4e2a\u975e\u7a7a\u7ed3\u70b9\u4e14\u6ca1\u6709\u65e0\u7528\u7684\u6807\u7b7e\u65f6\u5e94\u7ec8\u6b62\u5411\u4e0a\u56de\u9000\u904d\u5386\u6811\u3002<br \/>\n\u5728\u5411\u4e0b\u904d\u5386\u6811\u65f6\u9274\u522b\u6b64\u70b9\u7684\u6761\u4ef6\u662f\uff1a\u5f53\u9047\u5230\u6709\u8d85\u8fc72\u4e2a\u5b69\u5b50\u7684\u7ed3\u70b9\u3001\u5e76\u4e14\u6bcf\u4e2a\u6807\u7b7e\u6765\u8bf4\u7ed3\u70b9\u6709\u591a\u4e8e\u4e00\u4e2a\u6807\u7b7e\u6761\u76ee\u88ab\u6e05\u9664\u65f6\uff0c\u7ed3\u675f\u5411\u4e0a\u904d\u5386\u3002\u8be5\u6761\u4ef6\u7528\u6765\u9274\u522b\u5411\u4e0a\u56de\u9000\u904d\u5386\u7684\u7ec8\u6b62\u70b9\u3002<\/p>\n<p>\uff085\uff09radix\u6811API\u8bf4\u660e<\/p>\n<ul>\n<li>\u58f0\u660e\u548c\u521d\u59cb\u5316radix\u6811<\/li>\n<\/ul>\n<p>\u58f0\u660e\u548c\u521d\u59cb\u5316radix\u6811\u7684\u65b9\u6cd5\u5217\u51fa\u5982\u4e0b\uff1a<\/p>\n<p>#include &lt;linux\/radix-tree.h&gt;<br \/>\n\/* gfp_mask\u8868\u793a\u5982\u4f55\u6267\u884c\u5185\u5b58\u5206\u914d\uff0c\u5982\u679c\u64cd\u4f5c\uff08\u5982\uff1a\u63d2\u5165\uff09\u4ee5\u539f\u5b50\u6027\u4e0a\u4e0b\u6587\u4e2d\u6267\u884c\uff0c\u5176\u503c\u4e3aGFP_ATOMIC*\/<br \/>\nRADIX_TREE(name, gfp_mask); \/* \u58f0\u660e\u548c\u521d\u59cb\u5316\u540d\u4e3aname\u7684\u6811*\/<\/p>\n<p>struct radix_tree_root my_tree;<br \/>\nINIT_RADIX_TREE(my_tree, gfp_mask);<\/p>\n<ul>\n<li>\u63d2\u5165\u6761\u76ee<\/li>\n<\/ul>\n<p>\u63d2\u5165\u6761\u76ee\u7684\u51fd\u6570\u5b9a\u4e49\u5217\u51fa\u5982\u4e0b\uff1a<\/p>\n<p>int radix_tree_insert(struct radix_tree_root *root, unsigned long index, void *item)<\/p>\n<p>\u51fd\u6570radix_tree_insert\u63d2\u5165\u6761\u76eeitem\u5230\u6811root\u4e2d\uff0c\u5982\u679c\u63d2\u5165\u6761\u76ee\u4e2d\u5185\u5b58\u5206\u914d\u9519\u8bef\uff0c\u5c06\u8fd4\u56de\u9519\u8bef-ENOMEM\u3002\u8be5\u51fd\u6570\u4e0d\u80fd\u8986\u76d6\u5199\u6b63\u5b58\u5728\u7684\u6761\u76ee\u3002\u5982\u679c\u7d22\u5f15\u952e\u503cindex\u5df2\u5b58\u5728\u4e8e\u6811\u4e2d\uff0c\u8fd4\u56de\u9519\u8bef-EEXIST\u3002\u63d2\u5165\u64cd\u4f5c\u6210\u529f\u662f\uff0c\u8fd4\u56de0\u3002<br \/>\n\u5bf9\u4e8e\u63d2\u5165\u6761\u76ee\u64cd\u4f5c\u5931\u8d25\u5c06\u5f15\u8d77\u4e25\u91cd\u95ee\u9898\u7684\u573a\u5408\uff0c\u4e0b\u9762\u7684\u4e00\u5bf9\u51fd\u6570\u53ef\u907f\u514d\u63d2\u5165\u64cd\u4f5c\u5931\u8d25\uff1a<\/p>\n<p>int radix_tree_preload(gfp_t gfp_mask);<br \/>\nvoid radix_tree_preload_end(void);<\/p>\n<p>\u51fd\u6570radix_tree_preload\u5c1d\u8bd5\u7528\u7ed9\u5b9a\u7684gfp_mask\u5206\u914d\u8db3\u591f\u7684\u5185\u5b58\uff0c\u4fdd\u8bc1\u4e0b\u4e00\u4e2a\u63d2\u5165\u64cd\u4f5c\u4e0d\u4f1a\u5931\u8d25\u3002\u5728\u8c03\u7528\u63d2\u5165\u64cd\u4f5c\u51fd\u6570\u4e4b\u524d\u8c03\u7528\u6b64\u51fd\u6570\uff0c\u5206\u914d\u7684\u7ed3\u6784\u5c06\u5b58\u653e\u5728\u6bcfCPU\u53d8\u91cf\u4e2d\u3002\u51fd\u6570radix_tree_preload\u64cd\u4f5c\u6210\u529f\u540e\uff0c\u5c06\u5b8c\u6bd5\u5185\u6838\u62a2\u5360\u3002\u56e0\u6b64\uff0c\u5728\u63d2\u5165\u64cd\u4f5c\u5b8c\u6210\u4e4b\u540e\uff0c\u7528\u6237\u5e94\u8c03\u7528\u51fd\u6570radix_tree_preload_end\u6253\u5f00\u5185\u6838\u62a2\u5360\u3002<\/p>\n<ul>\n<li>\u5220\u9664\u6761\u76ee<\/li>\n<\/ul>\n<p>\u5220\u9664\u6761\u76ee\u7684\u51fd\u6570\u5b9a\u4e49\u5217\u51fa\u5982\u4e0b\uff1a<\/p>\n<p>void *radix_tree_delete(struct radix_tree_root *root, unsigned long index)<\/p>\n<p>\u51fd\u6570radix_tree_delete\u5220\u9664\u4e0e\u7d22\u5f15\u952e\u503cindex\u76f8\u5173\u7684\u6761\u76ee\uff0c\u5982\u679c\u5220\u9664\u6761\u76ee\u5728\u6811\u4e2d\uff0c\u8fd4\u56de\u8be5\u6761\u76ee\u7684\u6307\u9488\uff0c\u5426\u5219\u8fd4\u56deNULL\u3002<\/p>\n<ul>\n<li>\u67e5\u8be2\u64cd\u4f5c<\/li>\n<\/ul>\n<p>\u7528\u4e8e\u67e5\u8be2\u64cd\u4f5c\u7684\u51fd\u6570\u5b9a\u4e49\u5217\u51fa\u5982\u4e0b\uff1a<\/p>\n<p>\/*\u5728\u6811\u4e2d\u67e5\u627e\u6307\u5b9a\u952e\u503c\u7684\u6761\u76ee\uff0c\u67e5\u627e\u6210\u529f\uff0c\u8fd4\u56de\u8be5\u6761\u76ee\u7684\u6307\u9488\uff0c\u5426\u5219\uff0c\u8fd4\u56deNULL*\/<br \/>\nvoid *radix_tree_lookup(struct radix_tree_root *root, unsigned long index);<br \/>\n\/*\u8fd4\u56de\u6307\u5411slot\u7684\u6307\u9488\uff0c\u8be5slot\u542b\u6709\u6307\u5411\u67e5\u627e\u5230\u6761\u76ee\u7684\u6307\u9488*\/<br \/>\nvoid **radix_tree_lookup_slot(struct radix_tree_root *root, unsigned long index);<br \/>\n\/*\u67e5\u8be2\u8fd4\u56demax_items\u6761\u76ee\u5728results\u4e2d\u3002\u67e5\u8be2\u65f6\u952e\u503c\u7d22\u5f15\u4ecefirst_index\u5f00\u59cb*\/<br \/>\nradix_tree_gang_lookup(struct radix_tree_root *root, void **results, unsigned long first_index, unsigned int max_items);<\/p>\n<ul>\n<li>\u6807\u7b7e\u64cd\u4f5c<\/li>\n<\/ul>\n<p>\u4e0e\u6807\u7b7e\u64cd\u4f5c\u76f8\u5173\u7684\u51fd\u6570\u8bf4\u660e\u5217\u51fa\u5982\u4e0b\uff1a<\/p>\n<p>\/*\u5c06\u952e\u503cindex\u5bf9\u5e94\u7684\u6761\u76ee\u8bbe\u7f6e\u6807\u7b7etag\uff0c\u8fd4\u56de\u503c\u4e3a\u8bbe\u7f6e\u6807\u7b7e\u7684\u6761\u76ee*\/<br \/>\nvoid *radix_tree_tag_set(struct radix_tree_root *root, unsigned long index, unsigned int tag);<br \/>\n\/*\u4ece\u952e\u503cindex\u5bf9\u5e94\u7684\u6761\u76ee\u6e05\u9664\u6807\u7b7etag\uff0c\u8fd4\u56de\u503c\u4e3a\u6e05\u9664\u6807\u7b7e\u7684\u6761\u76ee*\/<br \/>\nvoid *radix_tree_tag_clear(struct radix_tree_root *root, unsigned long index, unsigned int tag);<br \/>\n\/*\u68c0\u67e5\u952e\u503cindex\u5bf9\u5e94\u7684\u6761\u76ee\u662f\u5426\u4e3a\u6807\u7b7etag\uff0c\u5982\u679c\u952e\u503c\u4e0d\u5b58\u5728\uff0c\u8fd4\u56de0\uff0c\u5982\u679c\u952e\u503c\u5b58\u5728\uff0c\u4f46\u6807\u7b7e\u672a\u8bbe\u7f6e\uff0c\u8fd4\u56de-1\uff1b\u5982\u679c\u952e\u503c\u5b58\u5728\uff0c\u4e14\u6807\u7b7e\u5df2\u8bbe\u7f6e\uff0c\u8fd4\u56de1*\/<br \/>\nint radix_tree_tag_get(struct radix_tree_root *root, unsigned long index, unsigned int tag);<br \/>\n\/*\u4ecefirst_index\u8d77\u67e5\u8be2\u6811root\u4e2d\u6807\u7b7e\u503c\u4e3atag\u7684\u6761\u76ee\uff0c\u5728results\u4e2d\u8fd4\u56de*\/<br \/>\nunsigned int radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results, unsigned long first_index, unsigned int max_items, unsigned int tag);<br \/>\n\/*\u5982\u679c\u6811root\u4e2d\u6709\u4efb\u4f55\u6761\u76ee\u4f7f\u7528tag\u6807\u7b7e\uff0c\u8fd4\u56de\u952e\u503c*\/<br \/>\nint radix_tree_tagged(struct radix_tree_root *root, unsigned int tag);<\/p>\n<p>\uff086\uff09\u5e76\u884c\u64cd\u4f5c\u4f7f\u7528radix\u6811API\u7684\u65b9\u6cd5<\/p>\n<ul>\n<li>\u67e5\u8be2\u83b7\u53d6slot\u64cd\u4f5c<\/li>\n<\/ul>\n<p>\u67e5\u8be2\u64cd\u4f5c\u652f\u6301RCU\u65e0\u963b\u585e\u5e76\u884c\u8bfb\u64cd\u4f5c\uff0c\u56e0\u6b64\uff0c\u9700\u8981\u9075\u5faaRCU\u7684\u7528\u6cd5\u52a0RCU\u8bfb\u9501\uff0c\u8fd8\u9700\u8981\u5c06rcu_dereference()\u7528\u4e8e\u83b7\u5f97\u7684slot\uff0c\u5728\u5199\uff08\u6216\u66f4\u65b0\uff09\u64cd\u4f5c\u65f6\uff0c\u9700\u8981\u7ed9\u65b0\u7684slot\u4f7f\u7528rcu_assign_pointer()\u3002\u67e5\u8be2\u64cd\u4f5c\u7684\u4f7f\u7528\u65b9\u6cd5\u5217\u51fa\u5982\u4e0b\uff1a<\/p>\n<p>struct page **slot, *page;<br \/>\nrcu_read_lock();<br \/>\nslot = radix_tree_lookup_slot(&amp;mapping-&gt;page_tree, index);<br \/>\npage = rcu_dereference(*slot);<br \/>\nrcu_read_unlock();<\/p>\n<ul>\n<li>\u67e5\u8be2\u4fee\u6539slot\u64cd\u4f5c<\/li>\n<\/ul>\n<p>Linux\u5185\u6838\u7684radix\u6811\u9700\u8981\u6253\u8865\u4e01\u624d\u652f\u6301\u5e76\u53d1\u4fee\u6539\u3002\u67e5\u8be2\u4ec5\u6709\u4e00\u4e2a\u5168\u5c40\u72b6\u6001\uff1aRCU\u9759\u6b62\u72b6\u6001\uff0c\u5e76\u53d1\u4fee\u6539\u9700\u8981\u8ddf\u8e2a\u6301\u6709\u4ec0\u4e48\u9501\u3002\u9501\u72b6\u6001\u5bf9\u4e8e\u64cd\u4f5c\u6765\u8bf4\u5fc5\u987b\u662f\u5916\u90e8\u7684\uff0c\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u5b9e\u4f8b\u5316\u4e00\u4e2a\u672c\u5730\u4e0a\u4e0b\u6587\u8ddf\u8e2a\u8fd9\u4e9b\u9501\u3002\u67e5\u8be2\u4fee\u6539slot\u7684\u65b9\u6cd5\u5217\u51fa\u5982\u4e0b\uff1a<\/p>\n<p>struct page **slot;<br \/>\nDEFINE_RADIX_TREE_CONTEXT(ctx,&amp;mapping-&gt;page_tree);<br \/>\nradix_tree_lock(&amp;ctx); \/*\u9501\u4f4f\u4e86\u6839\u7ed3\u70b9*\/<br \/>\n\/* ctx.tree\u4ee3\u66ff&amp;mapping-&gt;page_tree\u4f5c\u4e3a\u6839\uff0c\u53ef\u4ee5\u4f20\u9012\u4e0a\u4e0b\u6587<br \/>\nslot = radix_tree_lookup_slot(tx.tree, index);<br \/>\nrcu_assign_pointer(*slot, new_page);<br \/>\nradix_tree_unlock(&amp;ctx);<\/p>\n<p>radix\u6811API\u51fd\u6570radix_tree_lookup_slot\u542b\u6709\u9501\u4ece\u6811\u9876\u5411\u4e0b\u79fb\u52a8\u673a\u5236\uff0c\u9501\u79fb\u52a8\u7684\u4ee3\u7801\u90e8\u5206\u5217\u51fa\u5982\u4e0b\uff1a<\/p>\n<p>void **radix_tree_lookup_slot(struct radix_tree *root, unsigned long index)<br \/>\n{<!-- --><br \/>\n&#8230;<br \/>\nRADIX_TREE_CONTEXT(context, root); \/*\u63d0\u4f9b\u4e0a\u4e0b\u6587\u548c\u5b9e\u9645\u7684root\u6307\u9488*\u3001<br \/>\n&#8230;<br \/>\ndo {<!-- --><br \/>\n&#8230;<br \/>\n\/* \u4ece\u6811\u9876\u5411\u4e0b\u79fb\u52a8\u9501*\/<br \/>\nradix_ladder_lock(context, node);<br \/>\n&#8230;<br \/>\n} while (height &gt; 0);<br \/>\n&#8230;<br \/>\n}<\/p>\n<p>\uff087\uff09radix\u6811API\u7684\u5b9e\u73b0<\/p>\n<p>\u6811\u7684\u64cd\u4f5c\u901a\u5e38\u5305\u62ec\u67e5\u627e\u3001\u63d2\u5165\u3001\u5220\u9664\u548c\u6811\u8c03\u6574\uff0c\u4e0b\u9762\u5206\u522b\u8bf4\u660eradix\u6811\u8fd9\u4e9b\u64cd\u4f5c\u7684\u5b9e\u73b0\u3002<\/p>\n<ul>\n<li>\u67e5\u627e\u5355\u4e2a\u6761\u76ee<\/li>\n<\/ul>\n<p>radix\u6811\u901a\u8fc7\u7d22\u5f15\u952e\u503c\u8fdb\u884c\u67e5\u8be2\uff0c\u5982\u56fe1\u6240\u793a\uff0c\u5b83\u6309\u8def\u5f84\u7ec4\u6210\u7d22\u5f15\u952e\u503c\uff0c\u56fe\u4e2d3\u7ea7\u7ed3\u70b9\u5bf9\u5e943\u6bb56\u4f4d\u8868\u793a\u7684\u7d22\u5f15\u503c\uff0c\u56fe\u4e2dkey\u5bf9\u5e94\u7684\u53f6\u5b50slot\u7684\u7d22\u5f15\u952e\u503c\u4e3a\u201c2,63,1\u201d\u3002\u901a\u8fc7\u53f6\u5b50slot\u7684\u6307\u9488slot[1]\u5c31\u53ef\u5f97\u5230\u6240\u5b58\u50a8\u7684\u6570\u636eitem\u3002\u56e0\u6b64\uff0c\u67e5\u8be2\u8fed\u4ee3\u65f6\uff0c\u9700\u8981\u5c06\u7d22\u5f15\u952e\u503c\u201c2,63,1\u201d\u79fb\u53bb\u9ad82\u5c42\u201c2,63\u201d\uff0c\u5f97\u5230\u53f6\u5b50slot\u7684\u6307\u9488\u7d22\u5f15\u952e\u503c\u201c1\u201d\u3002<\/p>\n<p><span class=\"mw-headline\"><img decoding=\"async\" src=\"http:\/\/blog.chinaunix.net\/photo\/96790_101223120845.jpg\" alt=\"\" \/><\/span><br \/>\n\u56fe1 \u4e00\u4e2a3\u7ea7\u7ed3\u70b9\u7684radix\u6811\u53ca\u5176\u952e\u503c\u8868\u793a<\/p>\n<p>\u51fd\u6570radix_tree_lookup\u6267\u884c\u67e5\u627e\u64cd\u4f5c\uff0c\u67e5\u627e\u65b9\u6cd5\u662f\uff1a\u4ece\u53f6\u5b50\u5230\u6811\u9876\uff0c\u901a\u8fc7\u6570\u7ec4\u7d22\u5f15\u952e\u503c\u503c\u67e5\u770b\u6570\u7ec4\u5143\u7d20\u7684\u65b9\u6cd5\uff0c\u4e00\u5c42\u5c42\u5730\u67e5\u627eslot\u3002\u5176\u5217\u51fa\u5982\u4e0b\uff08\u5728lib\/radix-tree.c\u4e2d\uff09\uff1a<\/p>\n<p>void *radix_tree_lookup(struct radix_tree_root *root, unsigned long index)<br \/>\n{<!-- --><br \/>\nunsigned int height, shift;<br \/>\nstruct radix_tree_node *node, **slot;<\/p>\n<p>node = rcu_dereference(root-&gt;rnode); \/*\u83b7\u53d6\u6839\u7ed3\u70b9*\/<br \/>\nif (node == NULL)<br \/>\nreturn NULL;<\/p>\n<p>\/*\u95f4\u63a5\u6307\u9488\u6307\u5411\u7ed3\u70b9\u800c\u975e\u6570\u636e\u6761\u76ee\uff0c\u901a\u8fc7\u8bbe\u7f6eroot-&gt;rnode\u7684\u4f4e\u4f4d\u8868\u793a\u662f\u5426\u662f\u95f4\u6307\u9488\u3002\u5bf9\u4e8e\u95f4\u63a5\u6307\u9488\u6765\u8bf4\uff0c\u6811\u9ad8\u5ea6\u503croot-&gt;height\u5927\u4e8e0\uff0c\u4f46\u662fRCU\u67e5\u627e\u9700\u8981\u6d4b\u8bd5\u95f4\u63a5\u6307\u9488\uff0c\u56e0\u4e3aroot-&gt;height \u503c\u4e0d\u53ef\u9760\u3002\u8fd9\u79cd\u95ee\u9898\u4ec5\u7684RCU\u4e0b\u9700\u8981\u8003\u8651*\/<br \/>\nif (!radix_tree_is_indirect_ptr(node)) { \/*\u975e\u95f4\u63a5\u6307\u9488\uff0c\u8bf4\u660e\u53ea\u6709\u6839\u7ed3\u70b9*\/<br \/>\nif (index &gt; 0)<br \/>\nreturn NULL;<br \/>\nreturn node;<br \/>\n}<\/p>\n<p>\/*\u83b7\u53d6\u771f\u6b63\u7ed3\u70b9\u6307\u9488\uff0c\u56e0\u4e3a\u6839\u7ed3\u70b9\u6307\u9488\u7684\u7b2c0\u4f4d\u8bbe\u7f6e\u4e3a1\u8868\u793a\u4e3a\u95f4\u63a5\u6307\u9488\u3002\u5f53\u4f7f\u7528\u7ed3\u70b9\u6307\u9488\u65f6\uff0c\u5fc5\u987b\u5c06\u7b2c0\u4f4d\u8bbe\u7f6e\u4e3a0\uff0c\u56e0\u4e3a\u5730\u5740\u4ee5\u5b57\u5bf9\u9f50*\/<br \/>\nnode = radix_tree_indirect_to_ptr(node);<\/p>\n<p>height = node-&gt;height;<br \/>\nif (index &gt; radix_tree_maxindex(height)) \/*\u7d22\u5f15\u952e\u503c\u4e0d\u80fd\u8d85\u8fc7\u6700\u5927\u7d22\u5f15\u503c*\/<br \/>\nreturn NULL;<\/p>\n<p>\/*\u6bcf\u5c42\u7d22\u5f15\u504f\u79fb\u503c\u4e3aRADIX_TREE_MAP_SHIFT\uff0c\u53f6\u5b50\u7d22\u5f15\u503c\u504f\u79fb\u57fa\u6570\u4e3a\uff08\u6811\u9ad8-1\uff09*\u6bcf\u5c42\u7d22\u5f15\u504f\u79fb\u503c*\/<br \/>\nshift = (height-1) * RADIX_TREE_MAP_SHIFT;<\/p>\n<p>do { \/*\u4ece\u53f6\u5b50\u5230\u6811\u9876\uff0c\u901a\u8fc7\u6811\u8def\u5f84\u7ec4\u6210\u7684\u7d22\u5f15\u67e5\u627e\u6307\u5b9a\u7d22\u5f15\u952e\u503c\u7684slot*\/<br \/>\nslot = (struct radix_tree_node **)(node-&gt;slots + ((index&gt;&gt;shift) &amp; RADIX_TREE_MAP_MASK)); \/*\u5982\uff1aslots +1*\/<br \/>\nnode = rcu_dereference(*slot);<br \/>\nif (node == NULL)<br \/>\nreturn NULL;<\/p>\n<p>shift -= RADIX_TREE_MAP_SHIFT; \/*\u5411\u4e0a\u79fb\u4e00\u5c42\uff0c\u518d\u8fed\u4ee3\u67e5\u627e*\/<br \/>\nheight&#8211;;<br \/>\n} while (height &gt; 0);<\/p>\n<p>return node;<br \/>\n}<\/p>\n<ul>\n<li>\u67e5\u627e\u591a\u4e2a\u6761\u76ee<\/li>\n<\/ul>\n<p>\u51fd\u6570radix_tree_gang_lookup\u6267\u884c\u591a\u4e2a\u7d22\u5f15\u952e\u503c\u7684\u67e5\u627e\uff0c\u5176\u5217\u51fa\u5982\u4e0b\uff1a<\/p>\n<p>unsigned int radix_tree_gang_lookup(struct radix_tree_root *root, void **results, unsigned long first_index, unsigned int max_items)<br \/>\n{<!-- --><br \/>\nunsigned long max_index;<br \/>\nstruct radix_tree_node *node;<br \/>\nunsigned long cur_index = first_index;<br \/>\nunsigned int ret;<\/p>\n<p>node = rcu_dereference(root-&gt;rnode);<br \/>\nif (!node)<br \/>\nreturn 0;<\/p>\n<p>if (!radix_tree_is_indirect_ptr(node)) { \/*\u5982\u679c\u4e3a\u975e\u95f4\u63a5\u6307\u9488\uff0c\u8868\u793a\u53ea\u6709\u6839\u8282\u70b9*\/<br \/>\nif (first_index &gt; 0)<br \/>\nreturn 0;<br \/>\nresults[0] = node;<br \/>\nreturn 1;<br \/>\n}<br \/>\nnode = radix_tree_indirect_to_ptr(node); \/*\u6e05\u9664\u7528\u4e8e\u95f4\u63a5\u6307\u9488\u6807\u8bc6\u7684\u7b2c0\u4f4d*\/<\/p>\n<p>max_index = radix_tree_maxindex(node-&gt;height); \/*\u83b7\u53d6\u6811\u7684\u6700\u5927\u7d22\u5f15\u952e\u503c*\/<\/p>\n<p>ret = 0;<br \/>\nwhile (ret &lt; max_items) { \/* max_items\u4e3a\u67e5\u627e\u7684\u6700\u5927\u6761\u76ee\u6570*\/<br \/>\nunsigned int nr_found;<br \/>\nunsigned long next_index; \/* \u4e0b\u4e00\u4e2a\u641c\u7d22\u7684\u7d22\u5f15\u952e\u503c*\/<\/p>\n<p>if (cur_index &gt; max_index) \/*\u5df2\u67e5\u8be2\u5b8c\u6240\u9700\u67e5\u8be2\u7684\u7d22\u5f15\u952e\u503c*\/<br \/>\nbreak;<br \/>\nnr_found = __lookup(node, results + ret, cur_index,<br \/>\nmax_items &#8211; ret, &amp;next_index);<br \/>\nret += nr_found;<br \/>\nif (next_index == 0)<br \/>\nbreak;<br \/>\ncur_index = next_index;<br \/>\n}<\/p>\n<p>return ret;<br \/>\n}<\/p>\n<p>static unsigned int<br \/>\n__lookup(struct radix_tree_node *slot, void **results, unsigned long index, unsigned int max_items, unsigned long *next_index)<br \/>\n{<!-- --><br \/>\nunsigned int nr_found = 0;<br \/>\nunsigned int shift, height;<br \/>\nunsigned long i;<\/p>\n<p>height = slot-&gt;height;<br \/>\nif (height == 0)<br \/>\ngoto out;<br \/>\n\/*\u6240\u6709\u53f6\u5b50slot\u7684\u7d22\u5f15\u952e\u503c\u57fa\u6570\u504f\u79fb*\/<br \/>\nshift = (height-1) * RADIX_TREE_MAP_SHIFT;<\/p>\n<p>\/*\u4ece\u5e95\u5c42\u5411\u6811\u9876\u5c42\uff0c<br \/>\nfor (\u00a0; height &gt; 1; height&#8211;) { \/*\u4ece\u53f6\u5b50\u5411\u6811\u9876\u67e5\u627e*\/<br \/>\ni = (index &gt;&gt; shift) &amp; RADIX_TREE_MAP_MASK;<br \/>\nfor (;;) { \/*\u904d\u5386\u6bcf\u4e00\u5c42\u7684\u5404\u4e2a\u8def\u5f84\uff0c\u7531\u6811\u9876\u5230\u5f53\u524d\u5c42\u4e00\u6761\u8def\u5f84\u7ec4\u6210\u7d22\u5f15\u952e\u503c*\/<br \/>\n\/*\u5982\u679cslot\u4e0d\u4e3a\u7a7a\uff0c\u90a3\u4e48\u5b83\u6302\u6709\u5b50\u7ed3\u70b9\uff0c\u8df3\u51fa\u672c\u5faa\u73af\uff0c\u8fdb\u5165\u5b50\u7ed3\u70b9\u5c42\u8fdb\u884c\u672c\u5faa\u73af*\/<br \/>\nif (slot-&gt;slots[i]\u00a0!= NULL)<br \/>\nbreak;<br \/>\n\/*\u5982\u679cslot\u4e3a\u7a7a\uff0c\u5c31\u8df3\u8fc7slot\u5bf9\u5e94\u7684\u6240\u6709\u7d22\u5f15\u952e\u503c*\/<br \/>\n\/*\u6e05\u9664\u7d22\u5f15\u53f7\u4f4e\u4f4d.\u5c06\u7d22\u5f15\u53f7\u4e0e\u8be5\u5c42slot\u7684\u8d77\u59cb\u7d22\u5f15\u53f7\u5bf9\u9f50*\/<br \/>\nindex &amp;= ~((1UL &lt;&lt; shift) &#8211; 1);<br \/>\n\/*\u8df3\u8fc7\u4e00\u4e2aslot\u7684\u7d22\u5f15\u952e\u503c\u6570*\/<br \/>\nindex += 1UL &lt;&lt; shift;<br \/>\nif (index == 0)<br \/>\ngoto out; \/* 32-bit wraparound *\/<br \/>\ni++; \/*\u627e\u5230\u591a\u4e2aslot*\/<br \/>\nif (i == RADIX_TREE_MAP_SIZE)<br \/>\ngoto out;<br \/>\n}<\/p>\n<p>shift -= RADIX_TREE_MAP_SHIFT; \/*\u5411\u4e0a\u79fb\u4e00\u5c42\uff0c\u57fa\u6570\u504f\u79fb\u51cf\u5c11*\/<br \/>\nslot = rcu_dereference(slot-&gt;slots[i]);<br \/>\nif (slot == NULL)<br \/>\ngoto out;<\/p>\n<p>}<\/p>\n<p>\/* \u8fd4\u56de\u627e\u5230\u7684\u591a\u4e2aslot*\/<br \/>\nfor (i = index &amp; RADIX_TREE_MAP_MASK; i &lt; RADIX_TREE_MAP_SIZE; i++) {<!-- --><br \/>\nstruct radix_tree_node *node;<br \/>\nindex++;<br \/>\nnode = slot-&gt;slots[i];<br \/>\nif (node) {<!-- --><br \/>\nresults[nr_found++] = rcu_dereference(node);<br \/>\nif (nr_found == max_items)<br \/>\ngoto out;<br \/>\n}<br \/>\n}<br \/>\nout:<br \/>\n*next_index = index;<br \/>\nreturn nr_found;<br \/>\n}<\/p>\n<ul>\n<li>\u63d2\u5165\u6761\u76ee<\/li>\n<\/ul>\n<p>\u51fd\u6570radix_tree_insert\u627e\u5230\u7d22\u5f15\u952e\u503c\u5bf9\u5e94\u7684\u7ed3\u70b9\uff0c\u5c06item\u52a0\u5230\u8be5\u7ed3\u70b9\u7684slot\u6307\u9488\u4e0a\u3002\u5176\u5217\u51fa\u5982\u4e0b\uff1a<\/p>\n<p>int radix_tree_insert(struct radix_tree_root *root, unsigned long index, void *item)<br \/>\n{<!-- --><br \/>\nstruct radix_tree_node *node = NULL, *slot;<br \/>\nunsigned int height, shift;<br \/>\nint offset;<br \/>\nint error;<\/p>\n<p>BUG_ON(radix_tree_is_indirect_ptr(item));<\/p>\n<p>\/* \u5982\u679c\u6811\u7684\u9ad8\u5ea6\u4e0d\u591f\uff0c\u5c31\u6269\u5c55\u6811\u3002\u51fd\u6570radix_tree_maxindex\u8ba1\u7b97\u6811\u9ad8\u5bb9\u7eb3\u7684\u6700\u5927\u7d22\u5f15*\/<br \/>\nif (index &gt; radix_tree_maxindex(root-&gt;height)) {<!-- --><br \/>\nerror = radix_tree_extend(root, index);<br \/>\nif (error)<br \/>\nreturn error;<br \/>\n}<\/p>\n<p>slot = radix_tree_indirect_to_ptr(root-&gt;rnode);<\/p>\n<p>height = root-&gt;height;<br \/>\nshift = (height-1) * RADIX_TREE_MAP_SHIFT; \/*\u8ba1\u7b97\u504f\u79fb\u57fa\u6570*\/<\/p>\n<p>offset = 0;<br \/>\nwhile (height &gt; 0) {<!-- --><br \/>\nif (slot == NULL) { \/*\u5982\u679cslot\u4e3a\u7a7a\uff0c\u5219\u9700\u8981\u52a0\u5165\u5b69\u5b50\u7ed3\u70b9*\/<br \/>\n\/* \u5206\u914dslot *\/<br \/>\nif (!(slot = radix_tree_node_alloc(root)))<br \/>\nreturn -ENOMEM;<br \/>\nslot-&gt;height = height;<br \/>\nif (node) {\/*\u6dfb\u52a0slot*\/<br \/>\nrcu_assign_pointer(node-&gt;slots[offset], slot);<br \/>\nnode-&gt;count++;<br \/>\n} else<br \/>\nrcu_assign_pointer(root-&gt;rnode,radix_tree_ptr_to_indirect(slot));<br \/>\n}<\/p>\n<p>\/* \u8fdb\u5165\u4e0a\u4e00\u5c42*\/<br \/>\noffset = (index &gt;&gt; shift) &amp; RADIX_TREE_MAP_MASK;<br \/>\nnode = slot;<br \/>\nslot = node-&gt;slots[offset];<br \/>\nshift -= RADIX_TREE_MAP_SHIFT;<br \/>\nheight&#8211;;<br \/>\n}<br \/>\n\/*\u5982\u679cindex\u5bf9\u5e94\u7684slot\u5df2\u6709\u6620\u5c04\u9875\u9762\uff0c\u8fd4\u56de-EEXIST*\/<br \/>\nif (slot\u00a0!= NULL)<br \/>\nreturn -EEXIST;<\/p>\n<p>if (node) {<!-- --><br \/>\nnode-&gt;count++; \/*\u589e\u52a0\u5b50\u7ed3\u70b9\u7684\u8ba1\u6570*\/<br \/>\nrcu_assign_pointer(node-&gt;slots[offset], item);<br \/>\nBUG_ON(tag_get(node, 0, offset));<br \/>\nBUG_ON(tag_get(node, 1, offset));<br \/>\n} else { \/*\u4e3a\u9876\u5c42\u7ed3\u70b9*\/<br \/>\nrcu_assign_pointer(root-&gt;rnode, item);<br \/>\nBUG_ON(root_tag_get(root, 0));<br \/>\nBUG_ON(root_tag_get(root, 1));<br \/>\n}<\/p>\n<p>return 0;<br \/>\n}<\/p>\n<ul>\n<li>\u6269\u5c55\u6811\u7684\u9ad8\u5ea6<\/li>\n<\/ul>\n<p>\u5982\u679c\u5f53\u524d\u6811\u9ad8\u5ea6\u4e0d\u8db3\u4ee5\u5b58\u653eindex\uff0c\u5c31\u9700\u8981\u6269\u5c55\u6811\uff0c\u6269\u5c55\u65b9\u6cd5\u662f\u5728\u65e7\u6811\u9876\u4e0a\u52a0\u65b0\u7684\u6839\u7ed3\u70b9\uff0c\u5e76\u5c06\u539f\u6839\u7ed3\u70b9\u7684tag\u4fe1\u606f\u79fb\u5230\u65b0\u6839\u7ed3\u70b9\u7684\u7b2c1\u4e2aslot\u3002\u51fd\u6570radix_tree_extend \u5217\u51fa\u5982\u4e0b\uff1a<br \/>\nstatic int radix_tree_extend(struct radix_tree_root *root, unsigned long index)<br \/>\n{<!-- --><br \/>\nstruct radix_tree_node *node;<br \/>\nunsigned int height;<br \/>\nint tag;<\/p>\n<p>\/* \u8ba1\u7b97\u6269\u5c55\u7684\u6df1\u5ea6*\/<br \/>\nheight = root-&gt;height + 1;<br \/>\n\/*\u5982\u679cindex\u8d85\u8fc7\u6811\u9ad8\u6240\u5bb9\u7eb3\u7684\u6700\u5927\u7d22\u5f15\u503c\uff0c\u6811\u9ad8\u9012\u589e*\/<br \/>\nwhile (index &gt; radix_tree_maxindex(height))<br \/>\nheight++;<\/p>\n<p>\/*\u5230\u8fd9\u91cc\u5df2\u8ba1\u7b97\u597d\u5408\u9002\u7684\u9ad8\u5ea6height*\/<br \/>\nif (root-&gt;rnode == NULL) { \/*\u53ea\u6709\u6839\u7ed3\u70b9\uff0c\u8bbe\u7f6e\u597d\u6811\u9ad8\u5ea6\u5c31\u53ef\u8fd4\u56de*\/<br \/>\nroot-&gt;height = height;<br \/>\ngoto out;<br \/>\n}<\/p>\n<p>\/*\u5c06\u5f53\u524d\u6811\u6269\u5c55\u5230\u9ad8\u5ea6height*\/<br \/>\ndo {<!-- --><br \/>\nunsigned int newheight;<br \/>\nif (!(node = radix_tree_node_alloc(root))) \/*\u5206\u914d\u4e00\u4e2a\u7ed3\u70b9*\/<br \/>\nreturn -ENOMEM;<\/p>\n<p>\/* \u589e\u52a0\u6811\u9ad8\uff0c\u5728\u6811\u9876\u70b9\u4e4b\u4e0a\u589e\u52a0\u4e00\u4e2a\u7ed3\u70b9*\/<br \/>\nnode-&gt;slots[0] = radix_tree_indirect_to_ptr(root-&gt;rnode);<\/p>\n<p>\/*\u4f20\u64adtag\u4fe1\u606f\u5230\u65b0\u6839\u7ed3\u70b9*\/<br \/>\n\/*\u4ee5\u524d\u7684\u6839\u7ed3\u70b9\u73b0\u6210\u4e3a\u65b0\u9876\u7ed3\u70b9\u7684\u7b2c1\u4e2a\u63d2\u69fd\u3002 \u5982\u679c\u4ee5\u524d\u7684\u6839\u7ed3\u70b9\u6253\u4e0a\u4e86tag\uff0c\u5c31\u5c06\u65b0\u589e\u7ed3\u70b9\u7684\u7b2c1\u4e2a\u63d2\u69fd\u5bf9\u5e94\u7684\u5b50\u8282\u70b9\u6253\u4e0a\u76f8\u5e94\u7684tag*\/<br \/>\nfor (tag = 0; tag &lt; RADIX_TREE_MAX_TAGS; tag++) {<!-- --><br \/>\nif (root_tag_get(root, tag))<br \/>\ntag_set(node, tag, 0);<br \/>\n}<\/p>\n<p>newheight = root-&gt;height+1;<br \/>\nnode-&gt;height = newheight;<br \/>\nnode-&gt;count = 1;<br \/>\nnode = radix_tree_ptr_to_indirect(node);<br \/>\nrcu_assign_pointer(root-&gt;rnode, node);<br \/>\nroot-&gt;height = newheight;<br \/>\n} while (height &gt; root-&gt;height);<br \/>\nout:<br \/>\nreturn 0;<br \/>\n}<\/p>\n<ul>\n<li>\u5220\u9664\u6761\u76ee<\/li>\n<\/ul>\n<p>\u51fd\u6570radix_tree_delete\u5220\u9664index\u5bf9\u5e94\u7684\u6761\u76ee\uff0c\u5e76\u8fd4\u56de\u5220\u9664\u6761\u76ee\u7684\u5730\u5740\u3002\u5176\u5217\u51fa\u5982\u4e0b\uff1a<br \/>\nvoid *radix_tree_delete(struct radix_tree_root *root, unsigned long index)<br \/>\n{<!-- --><br \/>\n\/*\u6570\u7ec4path\u7528\u4e8e\u4fdd\u5b58\u8def\u5f84\u4e0a\u7684\u7ed3\u70b9\u53ca\u7d22\u5f15\u504f\u79fb\u503c*\/<br \/>\nstruct radix_tree_path path[RADIX_TREE_MAX_PATH + 1], *pathp = path;<br \/>\nstruct radix_tree_node *slot = NULL;<br \/>\nstruct radix_tree_node *to_free;<br \/>\nunsigned int height, shift;<br \/>\nint tag;<br \/>\nint offset;<\/p>\n<p>height = root-&gt;height;<br \/>\nif (index &gt; radix_tree_maxindex(height)) \/*index\u4e0d\u80fd\u8d85\u8fc7\u6811\u7684\u6700\u5927\u7d22\u5f15\u503c*\/<br \/>\ngoto out;<\/p>\n<p>slot = root-&gt;rnode;<br \/>\nif (height == 0) { \/*\u53ea\u6709\u6839\u7ed3\u70b9*\/<br \/>\nroot_tag_clear_all(root);<br \/>\nroot-&gt;rnode = NULL;<br \/>\ngoto out;<br \/>\n}<br \/>\nslot = radix_tree_indirect_to_ptr(slot);<\/p>\n<p>shift = (height &#8211; 1) * RADIX_TREE_MAP_SHIFT;<br \/>\npathp-&gt;node = NULL;<br \/>\n\/*\u83b7\u53d6\u5220\u9664\u6761\u76ee\u7684\u8def\u5f84*\/<br \/>\n\/*\u5c06index\u4ece\u6839\u5230\u53f6\u5b50\u7684\u8def\u5f84\u6240\u7ecf\u8fc7\u7684\u7ed3\u70b9\u53ca\u76f8\u5e94\u7d22\u5f15\u504f\u79fb\u503c\u5b58\u653e\u5728\u6570\u7ec4pathp\u4e2d*\/<br \/>\ndo {<!-- --><br \/>\nif (slot == NULL)<br \/>\ngoto out;<\/p>\n<p>pathp++;<br \/>\noffset = (index &gt;&gt; shift) &amp; RADIX_TREE_MAP_MASK;<br \/>\npathp-&gt;offset = offset;<br \/>\npathp-&gt;node = slot;<br \/>\nslot = slot-&gt;slots[offset];<br \/>\nshift -= RADIX_TREE_MAP_SHIFT;<br \/>\nheight&#8211;;<br \/>\n} while (height &gt; 0);<\/p>\n<p>if (slot == NULL)<br \/>\ngoto out;<\/p>\n<p>\/*\u6e05\u9664\u4e0e\u5220\u9664\u6761\u76ee\u76f8\u5173\u7684\u6240\u6709\u6807\u7b7e*\/<br \/>\nfor (tag = 0; tag &lt; RADIX_TREE_MAX_TAGS; tag++) {<!-- --><br \/>\nif (tag_get(pathp-&gt;node, tag, pathp-&gt;offset))<br \/>\nradix_tree_tag_clear(root, index, tag);<br \/>\n}<\/p>\n<p>to_free = NULL;<br \/>\n\/*\u91ca\u653e\u4e0d\u518d\u9700\u8981\u7684\u7ed3\u70b9 *\/<br \/>\nwhile (pathp-&gt;node) { \/*\u5220\u9664<br \/>\npathp-&gt;node-&gt;slots[pathp-&gt;offset] = NULL; \/*\u6e05\u9664slot*\/<br \/>\npathp-&gt;node-&gt;count&#8211;; \/*\u5b69\u5b50\u6570\u91cf\u51cf1*\/<br \/>\n\/*\u8c03\u7528RCU\u673a\u5236\u7684\u51fd\u6570call_rcu\u5728\u6700\u540e\u4e00\u4e2a\u5f15\u7528\u7ed3\u675f\u65f6\u5ef6\u8fdf\u91ca\u653e\u7ed3\u70b9to_free *\/<br \/>\nif (to_free)<br \/>\nradix_tree_node_free(to_free);<\/p>\n<p>if (pathp-&gt;node-&gt;count) { \/*\u8fd8\u6709\u5b69\u5b50\u5b58\u5728*\/<br \/>\nif (pathp-&gt;node == radix_tree_indirect_to_ptr(root-&gt;rnode)) \/*\u4e3a\u6839\u7ed3\u70b9\u7684\u5b69\u5b50*\/<br \/>\nradix_tree_shrink(root); \/*\u6811\u7f29\u5c0f*\/<br \/>\ngoto out;<br \/>\n}<\/p>\n<p>\/*\u91ca\u653e\u67090\u4e2aslot\u7684\u7ed3\u70b9 *\/<br \/>\nto_free = pathp-&gt;node;<br \/>\npathp&#8211;;<\/p>\n<p>}<br \/>\n\/*\u8fd0\u884c\u5230\u8fd9\u91cc\uff0c\u8bf4\u660e\u662f\u6839\u7ed3\u70b9*\/<br \/>\nroot_tag_clear_all(root);<br \/>\nroot-&gt;height = 0;<br \/>\nroot-&gt;rnode = NULL;<br \/>\nif (to_free)<br \/>\nradix_tree_node_free(to_free);<\/p>\n<p>out:<br \/>\nreturn slot;<br \/>\n}<\/p>\n<ul>\n<li>\u7f29\u5c0f\u6811\u7684\u9ad8\u5ea6<\/li>\n<\/ul>\n<p>\u51fd\u6570radix_tree_shrink\u7f29\u5c0f\u6811\u7684\u9ad8\u5ea6\u5230\u6700\u5c0f\u3002\u5176\u5217\u51fa\u5982\u4e0b\uff1a<\/p>\n<p>static inline void radix_tree_shrink(struct radix_tree_root *root)<br \/>\n{<!-- --><br \/>\n\/* \u5c1d\u8bd5\u7f29\u5c0f\u6811\u7684\u9ad8\u5ea6*\/<br \/>\nwhile (root-&gt;height &gt; 0) {<!-- --><br \/>\nstruct radix_tree_node *to_free = root-&gt;rnode;<br \/>\nvoid *newptr;<\/p>\n<p>BUG_ON(!radix_tree_is_indirect_ptr(to_free));<br \/>\nto_free = radix_tree_indirect_to_ptr(to_free);<\/p>\n<p>\/*\u5019\u9009\u7ed3\u70b9\u591a\u4e8e\u4e00\u4e2a\u5b69\u5b50\u6216\u5b69\u5b50\u4e0d\u5728\u6700\u5de6\u8fb9slot\uff0c\u4e0d\u80fd\u7f29\u5c0f\u6811\u7684\u9ad8\u5ea6\uff0c\u8df3\u51fa\u5faa\u73af*\/<br \/>\nif (to_free-&gt;count\u00a0!= 1)<br \/>\nbreak;<br \/>\nif (!to_free-&gt;slots[0])<br \/>\nbreak;<\/p>\n<p>\/*\u4e0d\u9700\u8981\u8c03\u7528rcu_assign_pointer()\uff0c\u56e0\u4e3a\u4ec5\u4ece\u6811\u7684\u4e00\u90e8\u5206\u5230\u53e6\u4e00\u90e8\u5206\u79fb\u52a8\u7ed3\u70b9\u3002\u5982\u679c\u91ca\u653e\u65e7\u6307\u9488\u7684\u5f15\u7528to_free-&gt;slots[0]\u662f\u5b89\u5168\u7684\uff0c\u90a3\u4e48\u91ca\u653e\u65b0\u6307\u9488\u7684\u5f15\u7528root-&gt;rnode\u4e5f\u662f\u5b89\u5168\u7684*\/<br \/>\nnewptr = to_free-&gt;slots[0];<br \/>\nif (root-&gt;height &gt; 1)<br \/>\nnewptr = radix_tree_ptr_to_indirect(newptr);<br \/>\nroot-&gt;rnode = newptr;<br \/>\nroot-&gt;height&#8211;;<br \/>\n\/* \u4ec5\u91ca\u653e0\u7ed3\u70b9*\/<br \/>\ntag_clear(to_free, 0, 0);<br \/>\ntag_clear(to_free, 1, 0);<br \/>\nto_free-&gt;slots[0] = NULL;<br \/>\nto_free-&gt;count = 0;<br \/>\nradix_tree_node_free(to_free);<br \/>\n}<br \/>\n}<\/p>\n<\/div>\n<\/div>\n<\/article>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u5730\u5740 \u00a0 Linux\u57fa\u6570\u6811\uff08radi<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[],"tags":[119],"class_list":["post-5306","post","type-post","status-publish","format-standard","hentry","tag-03-"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/5306","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/comments?post=5306"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/5306\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=5306"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=5306"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=5306"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}