{"id":67562,"date":"2026-01-28T22:33:02","date_gmt":"2026-01-28T14:33:02","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/67562.html"},"modified":"2026-01-28T22:33:02","modified_gmt":"2026-01-28T14:33:02","slug":"%e3%80%90%e9%a1%b9%e7%9b%ae%e3%80%91%e9%ab%98%e5%b9%b6%e5%8f%91%e5%86%85%e5%ad%98%e6%b1%a0","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/67562.html","title":{"rendered":"\u3010\u9879\u76ee\u3011\u9ad8\u5e76\u53d1\u5185\u5b58\u6c60"},"content":{"rendered":"<p>&#x1f308;\u4e2a\u4eba\u4e3b\u9875&#xff1a;\u79e6jh__https:\/\/blog.csdn.net\/qinjh_?spm&#061;1010.2135.3001.5343 &#x1f525;\u00a0\u7cfb\u5217\u4e13\u680f&#xff1a;https:\/\/blog.csdn.net\/qinjh_\/category_13016668.html<\/p>\n<p>\u00a0\u00a0<img decoding=\"async\" alt=\"9efbcbc3d25747719da38c01b3fa9b4f.gif\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260128143258-697a1e1ad5fcc.gif\" \/><\/p>\n<p id=\"main-toc\">\u76ee\u5f55<\/p>\n<p id=\"%E4%BB%8E%E9%9B%B6%E5%AE%9E%E7%8E%B0%E4%B8%80%E4%B8%AA%E9%AB%98%E5%B9%B6%E5%8F%91%E5%86%85%E5%AD%98%E6%B1%A0-toc\" style=\"margin-left:0px\">\u4ece\u96f6\u5b9e\u73b0\u4e00\u4e2a\u9ad8\u5e76\u53d1\u5185\u5b58\u6c60<\/p>\n<p id=\"%E9%A1%B9%E7%9B%AE%E4%BB%8B%E7%BB%8D-toc\" style=\"margin-left:40px\">\u9879\u76ee\u4ecb\u7ecd<\/p>\n<p id=\"%E8%BF%99%E4%B8%AA%E9%A1%B9%E7%9B%AE%E5%81%9A%E7%9A%84%E6%98%AF%E4%BB%80%E4%B9%88%EF%BC%9F%C2%A0-toc\" style=\"margin-left:80px\">\u8fd9\u4e2a\u9879\u76ee\u505a\u7684\u662f\u4ec0\u4e48&#xff1f;\u00a0<\/p>\n<p id=\"%E4%BB%80%E4%B9%88%E6%98%AF%E5%86%85%E5%AD%98%E6%B1%A0%C2%A0-toc\" style=\"margin-left:40px\">\u4ec0\u4e48\u662f\u5185\u5b58\u6c60\u00a0<\/p>\n<p id=\"1.%E6%B1%A0%E5%8C%96%E6%8A%80%E6%9C%AF%C2%A0-toc\" style=\"margin-left:80px\">1.\u6c60\u5316\u6280\u672f\u00a0<\/p>\n<p id=\"2.%E5%86%85%E5%AD%98%E6%B1%A0%C2%A0-toc\" style=\"margin-left:80px\">2.\u5185\u5b58\u6c60\u00a0<\/p>\n<p id=\"3.%E5%86%85%E5%AD%98%E6%B1%A0%E4%B8%BB%E8%A6%81%E8%A7%A3%E5%86%B3%E7%9A%84%E9%97%AE%E9%A2%98%C2%A0-toc\" style=\"margin-left:80px\">3.\u5185\u5b58\u6c60\u4e3b\u8981\u89e3\u51b3\u7684\u95ee\u9898\u00a0<\/p>\n<p id=\"%C2%A04.malloc-toc\" style=\"margin-left:80px\">\u00a04.malloc<\/p>\n<p id=\"%E5%BC%80%E8%83%83%E8%8F%9C--%E5%85%88%E8%AE%BE%E8%AE%A1%E4%B8%80%E4%B8%AA%E5%AE%9A%E9%95%BF%E7%9A%84%E5%86%85%E5%AD%98%E6%B1%A0%C2%A0-toc\" style=\"margin-left:80px\">\u5f00\u80c3\u83dc&#8211;\u5148\u8bbe\u8ba1\u4e00\u4e2a\u5b9a\u957f\u7684\u5185\u5b58\u6c60\u00a0<\/p>\n<p id=\"%E9%AB%98%E5%B9%B6%E5%8F%91%E5%86%85%E5%AD%98%E6%B1%A0%E6%95%B4%E4%BD%93%E6%A1%86%E6%9E%B6%E8%AE%BE%E8%AE%A1-toc\" style=\"margin-left:80px\">\u9ad8\u5e76\u53d1\u5185\u5b58\u6c60\u6574\u4f53\u6846\u67b6\u8bbe\u8ba1<\/p>\n<p id=\"%E9%AB%98%E5%B9%B6%E5%8F%91%E5%86%85%E5%AD%98%E6%B1%A0--thread%20cache%C2%A0-toc\" style=\"margin-left:80px\">\u9ad8\u5e76\u53d1\u5185\u5b58\u6c60&#8211;thread cache\u00a0<\/p>\n<p id=\"%E9%AB%98%E5%B9%B6%E5%8F%91%E5%86%85%E5%AD%98%E6%B1%A0--central%20cache-toc\" style=\"margin-left:80px\">\u9ad8\u5e76\u53d1\u5185\u5b58\u6c60&#8211;central cache<\/p>\n<p id=\"%C2%A0%E9%AB%98%E5%B9%B6%E5%8F%91%E5%86%85%E5%AD%98%E6%B1%A0--page%20cache-toc\" style=\"margin-left:80px\">\u00a0\u9ad8\u5e76\u53d1\u5185\u5b58\u6c60&#8211;page cache<\/p>\n<hr id=\"hr-toc\" \/>\n<h2>\u524d\u8a00<\/h2>\n<p>\u2002\u2002\u2002\u2002&#x1f4ac; hello! \u5404\u4f4d\u94c1\u5b50\u4eec\u5927\u5bb6\u597d\u54c7\u3002<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u4eca\u65e5\u66f4\u65b0\u4e86\u9ad8\u5e76\u53d1\u5185\u5b58\u6c60\u7684\u5185\u5bb9 \u2002\u2002\u2002\u2002&#x1f389; \u6b22\u8fce\u5927\u5bb6\u5173\u6ce8&#x1f50d;\u70b9\u8d5e&#x1f44d;\u6536\u85cf\u2b50\ufe0f\u7559\u8a00&#x1f4dd;<\/p>\n<\/p>\n<h2 id=\"%E4%BB%8E%E9%9B%B6%E5%AE%9E%E7%8E%B0%E4%B8%80%E4%B8%AA%E9%AB%98%E5%B9%B6%E5%8F%91%E5%86%85%E5%AD%98%E6%B1%A0\">\u4ece\u96f6\u5b9e\u73b0\u4e00\u4e2a\u9ad8\u5e76\u53d1\u5185\u5b58\u6c60<\/h2>\n<h3 id=\"%E9%A1%B9%E7%9B%AE%E4%BB%8B%E7%BB%8D\">\u9879\u76ee\u4ecb\u7ecd<\/h3>\n<h4 id=\"%E8%BF%99%E4%B8%AA%E9%A1%B9%E7%9B%AE%E5%81%9A%E7%9A%84%E6%98%AF%E4%BB%80%E4%B9%88%EF%BC%9F%C2%A0\">\u8fd9\u4e2a\u9879\u76ee\u505a\u7684\u662f\u4ec0\u4e48&#xff1f;\u00a0<\/h4>\n<p>\u5f53\u524d\u9879\u76ee\u662f\u5b9e\u73b0\u4e00\u4e2a\u9ad8\u5e76\u53d1\u7684\u5185\u5b58\u6c60&#xff0c;\u4ed6\u7684\u539f\u578b\u662fgoogle\u7684\u4e00\u4e2a\u5f00\u6e90\u9879\u76eetcmalloc&#xff0c;tcmalloc\u5168\u79f0 Thread-Caching Malloc&#xff0c;\u5373\u7ebf\u7a0b\u7f13\u5b58\u7684malloc&#xff0c;\u5b9e\u73b0\u4e86\u9ad8\u6548\u7684\u591a\u7ebf\u7a0b\u5185\u5b58\u7ba1\u7406&#xff0c;\u7528\u4e8e\u66ff\u4ee3\u7cfb\u7edf\u7684\u5185\u5b58 \u5206\u914d\u76f8\u5173\u7684\u51fd\u6570&#xff08;malloc\u3001free&#xff09;\u3002<\/p>\n<p>\u8fd9\u4e2a\u9879\u76ee\u662f\u628atcmalloc\u6700\u6838\u5fc3\u7684\u6846\u67b6\u7b80\u5316\u540e\u62ff\u51fa\u6765&#xff0c;\u6a21\u62df\u5b9e\u73b0\u51fa\u4e00\u4e2a\u81ea\u5df1\u7684\u9ad8\u5e76\u53d1\u5185\u5b58\u6c60&#xff0c;\u76ee\u7684\u5c31 \u662f\u5b66\u4e60tcamlloc\u7684\u7cbe\u534e\u3002<\/p>\n<h3 id=\"%E4%BB%80%E4%B9%88%E6%98%AF%E5%86%85%E5%AD%98%E6%B1%A0%C2%A0\">\u4ec0\u4e48\u662f\u5185\u5b58\u6c60\u00a0<\/h3>\n<h4 id=\"1.%E6%B1%A0%E5%8C%96%E6%8A%80%E6%9C%AF%C2%A0\">1.\u6c60\u5316\u6280\u672f\u00a0<\/h4>\n<p>\u6240\u8c13\u201c\u6c60\u5316\u6280\u672f\u201d&#xff0c;\u5c31\u662f\u7a0b\u5e8f\u5148\u5411\u7cfb\u7edf\u7533\u8bf7\u8fc7\u91cf\u7684\u8d44\u6e90&#xff0c;\u7136\u540e\u81ea\u5df1\u7ba1\u7406&#xff0c;\u4ee5\u5907\u4e0d\u65f6\u4e4b\u9700\u3002\u4e4b\u6240\u4ee5\u8981\u7533\u8bf7\u8fc7 \u91cf\u7684\u8d44\u6e90&#xff0c;\u662f\u56e0\u4e3a\u6bcf\u6b21\u7533\u8bf7\u8be5\u8d44\u6e90\u90fd\u6709\u8f83\u5927\u7684\u5f00\u9500&#xff0c;\u4e0d\u5982\u63d0\u524d\u7533\u8bf7\u597d\u4e86&#xff0c;\u8fd9\u6837\u4f7f\u7528\u65f6\u5c31\u4f1a\u53d8\u5f97\u975e\u5e38\u5feb \u6377&#xff0c;\u5927\u5927\u63d0\u9ad8\u7a0b\u5e8f\u8fd0\u884c\u6548\u7387\u3002\u00a0<\/p>\n<p>\u00a0\u5728\u8ba1\u7b97\u673a\u4e2d&#xff0c;\u6709\u5f88\u591a\u4f7f\u7528\u201c\u6c60\u201d\u8fd9\u79cd\u6280\u672f\u7684\u5730\u65b9&#xff0c;\u9664\u4e86\u5185\u5b58\u6c60&#xff0c;\u8fd8\u6709\u8fde\u63a5\u6c60\u3001\u7ebf\u7a0b\u6c60\u3001\u5bf9\u8c61\u6c60\u7b49\u3002\u4ee5\u670d\u52a1 \u5668\u4e0a\u7684\u7ebf\u7a0b\u6c60\u4e3a\u4f8b&#xff0c;\u5b83\u7684\u4e3b\u8981\u601d\u60f3\u662f&#xff1a;\u5148\u542f\u52a8\u82e5\u5e72\u6570\u91cf\u7684\u7ebf\u7a0b&#xff0c;\u8ba9\u5b83\u4eec\u5904\u4e8e\u7761\u7720\u72b6\u6001&#xff0c;\u5f53\u63a5\u6536\u5230\u5ba2\u6237\u7aef \u7684\u8bf7\u6c42\u65f6&#xff0c;\u5524\u9192\u6c60\u4e2d\u67d0\u4e2a\u7761\u7720\u7684\u7ebf\u7a0b&#xff0c;\u8ba9\u5b83\u6765\u5904\u7406\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42&#xff0c;\u5f53\u5904\u7406\u5b8c\u8fd9\u4e2a\u8bf7\u6c42&#xff0c;\u7ebf\u7a0b\u53c8\u8fdb\u5165\u7761\u7720 \u72b6\u6001\u3002<\/p>\n<h4 id=\"2.%E5%86%85%E5%AD%98%E6%B1%A0%C2%A0\">2.\u5185\u5b58\u6c60\u00a0<\/h4>\n<p>\u5185\u5b58\u6c60\u662f\u6307\u7a0b\u5e8f\u9884\u5148\u4ece\u64cd\u4f5c\u7cfb\u7edf\u7533\u8bf7\u4e00\u5757\u8db3\u591f\u5927\u5185\u5b58&#xff0c;\u6b64\u540e&#xff0c;\u5f53\u7a0b\u5e8f\u4e2d\u9700\u8981\u7533\u8bf7\u5185\u5b58\u7684\u65f6\u5019&#xff0c;\u4e0d\u662f\u76f4\u63a5 \u5411\u64cd\u4f5c\u7cfb\u7edf\u7533\u8bf7&#xff0c;\u800c\u662f\u76f4\u63a5\u4ece\u5185\u5b58\u6c60\u4e2d\u83b7\u53d6&#xff1b;\u540c\u7406&#xff0c;\u5f53\u7a0b\u5e8f\u91ca\u653e\u5185\u5b58\u7684\u65f6\u5019&#xff0c;\u5e76\u4e0d\u771f\u6b63\u5c06\u5185\u5b58\u8fd4\u56de\u7ed9\u64cd \u4f5c\u7cfb\u7edf&#xff0c;\u800c\u662f\u8fd4\u56de\u5185\u5b58\u6c60\u3002\u5f53\u7a0b\u5e8f\u9000\u51fa(\u6216\u8005\u7279\u5b9a\u65f6\u95f4)\u65f6&#xff0c;\u5185\u5b58\u6c60\u624d\u5c06\u4e4b\u524d\u7533\u8bf7\u7684\u5185\u5b58\u771f\u6b63\u91ca\u653e\u3002\u00a0<\/p>\n<h4 id=\"3.%E5%86%85%E5%AD%98%E6%B1%A0%E4%B8%BB%E8%A6%81%E8%A7%A3%E5%86%B3%E7%9A%84%E9%97%AE%E9%A2%98%C2%A0\">3.\u5185\u5b58\u6c60\u4e3b\u8981\u89e3\u51b3\u7684\u95ee\u9898\u00a0<\/h4>\n<p>\u5185\u5b58\u6c60\u4e3b\u8981\u89e3\u51b3\u7684\u5f53\u7136\u662f\u6548\u7387\u7684\u95ee\u9898&#xff0c;\u5176\u6b21\u5982\u679c\u4f5c\u4e3a\u7cfb\u7edf\u7684\u5185\u5b58\u5206\u914d\u5668\u7684\u89d2\u5ea6&#xff0c;\u8fd8\u9700\u8981\u89e3\u51b3\u4e00\u4e0b\u5185\u5b58\u788e \u7247\u7684\u95ee\u9898\u3002\u90a3\u4e48\u4ec0\u4e48\u662f\u5185\u5b58\u788e\u7247\u5462&#xff1f;\u00a0<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"677\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260128143258-697a1e1ae7ff9.png\" width=\"1225\" \/>\u200b<\/p>\n<p>\u518d\u9700\u8981\u8865\u5145\u8bf4\u660e\u7684\u662f\u5185\u5b58\u788e\u7247\u5206\u4e3a\u5916\u788e\u7247\u548c\u5185\u788e\u7247&#xff0c;\u4e0a\u9762\u6211\u4eec\u8bb2\u7684\u5916\u788e\u7247\u95ee\u9898\u3002\u5916\u90e8\u788e\u7247\u662f\u4e00\u4e9b\u7a7a\u95f2\u7684\u8fde\u7eed\u5185\u5b58\u533a\u57df\u592a\u5c0f&#xff0c;\u8fd9\u4e9b\u5185\u5b58\u7a7a\u95f4\u4e0d\u8fde\u7eed&#xff0c;\u4ee5\u81f3\u4e8e\u5408\u8ba1\u7684\u5185\u5b58\u8db3\u591f&#xff0c;\u4f46\u662f\u4e0d\u80fd\u6ee1\u8db3\u4e00\u4e9b\u7684\u5185\u5b58\u5206\u914d\u7533\u8bf7 \u9700\u6c42\u3002\u5185\u90e8\u788e\u7247\u662f\u7531\u4e8e\u4e00\u4e9b\u5bf9\u9f50\u7684\u9700\u6c42&#xff0c;\u5bfc\u81f4\u5206\u914d\u51fa\u53bb\u7684\u7a7a\u95f4\u4e2d\u4e00\u4e9b\u5185\u5b58\u65e0\u6cd5\u88ab\u5229\u7528\u3002\u00a0<\/p>\n<h4 id=\"%C2%A04.malloc\">\u00a04.malloc<\/h4>\n<p>C\/C&#043;&#043;\u4e2d\u6211\u4eec\u8981\u52a8\u6001\u7533\u8bf7\u5185\u5b58\u90fd\u662f\u901a\u8fc7malloc\u53bb\u7533\u8bf7\u5185\u5b58&#xff0c;\u4f46\u662f\u6211\u4eec\u8981\u77e5\u9053&#xff0c;\u5b9e\u9645\u6211\u4eec\u4e0d\u662f\u76f4\u63a5\u53bb\u5806\u83b7 \u53d6\u5185\u5b58\u7684&#xff0c;\u800cmalloc\u5c31\u662f\u4e00\u4e2a\u5185\u5b58\u6c60\u3002malloc() \u76f8\u5f53\u4e8e\u5411\u64cd\u4f5c\u7cfb\u7edf\u201c\u6279\u53d1\u201d\u4e86\u4e00\u5757\u8f83\u5927\u7684\u5185\u5b58\u7a7a\u95f4&#xff0c;\u7136\u540e\u201c\u96f6\u552e\u201d\u7ed9\u7a0b \u5e8f\u7528\u3002\u5f53\u5168\u90e8\u201c\u552e\u5b8c\u201d\u6216\u7a0b\u5e8f\u6709\u5927\u91cf\u7684\u5185\u5b58\u9700\u6c42\u65f6&#xff0c;\u518d\u6839\u636e\u5b9e\u9645\u9700\u6c42\u5411\u64cd\u4f5c\u7cfb\u7edf\u201c\u8fdb\u8d27\u201d\u3002malloc\u7684\u5b9e\u73b0\u65b9 \u5f0f\u6709\u5f88\u591a\u79cd&#xff0c;\u4e00\u822c\u4e0d\u540c\u7f16\u8bd1\u5668\u5e73\u53f0\u7528\u7684\u90fd\u662f\u4e0d\u540c\u7684\u3002\u6bd4\u5982windows\u7684vs\u7cfb\u5217\u7528\u7684\u5fae\u8f6f\u81ea\u5df1\u5199\u7684\u4e00\u5957&#xff0c; linux gcc\u7528\u7684glibc\u4e2d\u7684ptmalloc\u00a0\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"688\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260128143259-697a1e1b44f3b.png\" width=\"1225\" \/>\u200b<\/p>\n<h4 id=\"%E5%BC%80%E8%83%83%E8%8F%9C--%E5%85%88%E8%AE%BE%E8%AE%A1%E4%B8%80%E4%B8%AA%E5%AE%9A%E9%95%BF%E7%9A%84%E5%86%85%E5%AD%98%E6%B1%A0%C2%A0\">\u5f00\u80c3\u83dc&#8211;\u5148\u8bbe\u8ba1\u4e00\u4e2a\u5b9a\u957f\u7684\u5185\u5b58\u6c60\u00a0<\/h4>\n<p>\u4f5c\u4e3a\u7a0b\u5e8f\u5458(C\/C&#043;&#043;)\u6211\u4eec\u77e5\u9053\u7533\u8bf7\u5185\u5b58\u4f7f\u7528\u7684\u662fmalloc&#xff0c;malloc\u5176\u5b9e\u5c31\u662f\u4e00\u4e2a\u901a\u7528\u7684\u5927\u4f17\u8d27&#xff0c;\u4ec0\u4e48\u573a\u666f \u4e0b\u90fd\u53ef\u4ee5\u7528&#xff0c;\u4f46\u662f\u4ec0\u4e48\u573a\u666f\u4e0b\u90fd\u53ef\u4ee5\u7528\u5c31\u610f\u5473\u7740\u4ec0\u4e48\u573a\u666f\u4e0b\u90fd\u4e0d\u4f1a\u6709\u5f88\u9ad8\u7684\u6027\u80fd&#xff0c;\u4e0b\u9762\u6211\u4eec\u5c31\u5148\u6765\u8bbe\u8ba1 \u4e00\u4e2a\u5b9a\u957f\u5185\u5b58\u6c60\u505a\u4e2a\u5f00\u80c3\u83dc&#xff0c;\u5f53\u7136\u8fd9\u4e2a\u5b9a\u957f\u5185\u5b58\u6c60\u5728\u6211\u4eec\u540e\u9762\u7684\u9ad8\u5e76\u53d1\u5185\u5b58\u6c60\u4e2d\u4e5f\u662f\u6709\u4ef7\u503c\u7684&#xff0c;\u6240\u4ee5\u5b66 \u4e60\u4ed6\u76ee\u7684\u6709\u4e24\u5c42&#xff0c;\u5148\u719f\u6089\u4e00\u4e0b\u7b80\u5355\u5185\u5b58\u6c60\u662f\u5982\u4f55\u63a7\u5236\u7684&#xff0c;\u7b2c\u4e8c\u4ed6\u4f1a\u4f5c\u4e3a\u6211\u4eec\u540e\u9762\u5185\u5b58\u6c60\u7684\u4e00\u4e2a\u57fa\u7840\u7ec4 \u4ef6\u3002\u00a0<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"893\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260128143259-697a1e1bba4b3.png\" width=\"812\" \/>\u200b<img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"353\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260128143300-697a1e1c1e597.png\" width=\"1023\" \/>\u200b<\/p>\n<p>\u00a0\u4ee3\u7801\u6837\u4f8b&#xff1a;<\/p>\n<p>ObjectPool.h<\/p>\n<p> template&lt;class T&gt;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<br \/>\nclass ObjectPool<br \/>\n{<br \/>\npublic:<\/p>\n<p>T* New()<br \/>\n{<br \/>\nT* obj &#061; nullptr;<\/p>\n<p>\/\/\u4f18\u5148\u628a\u8fd8\u56de\u6765\u7684\u5185\u5b58\u5757\u5bf9\u8c61&#xff0c;\u518d\u6b21\u91cd\u590d\u5229\u7528<br \/>\nif (_freeList)<br \/>\n{<br \/>\nvoid* next &#061; *((void**)_freeList); \/\/next\u662f\u81ea\u7531\u94fe\u8868\u7684\u7b2c\u4e00\u4e2a\u8282\u70b9\u7684\u5934\u56db\/\u516b\u4e2a\u5b57\u8282&#xff0c;\u5b83\u5b58\u653e\u4e0b\u4e00\u4e2a\u8282\u70b9\u7684\u5730\u5740<br \/>\nobj &#061; (T*)_freeList;\/\/obj\u5c31\u662f\u7b2c\u4e00\u5757\u5185\u5b58\u5757<br \/>\n_freeList &#061; next;\/\/\u518d\u8ba9next\u53d8\u6210\u7b2c\u4e00\u4e2a\u8282\u70b9<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\nif (_remainBytes &lt; sizeof(T))\/\/\u5982\u679c\u5269\u4f59\u5185\u5b58\u5c0f\u4e8e\u6240\u9700\u5185\u5b58&#xff0c;\u5c31\u53bb\u7533\u8bf7\u5927\u5757\u7a7a\u95f4<br \/>\n{<br \/>\n_remainBytes &#061; 128 * 1024;<br \/>\n_memory &#061; (char*)malloc(_remainBytes);<br \/>\nif (_memory &#061;&#061; nullptr)<br \/>\n{<br \/>\nthrow std::bad_alloc();<br \/>\n}<br \/>\n}<\/p>\n<p>obj &#061; (T*)_memory;\/\/\u628a\u5927\u5757\u5185\u5b58\u5207\u6210\u5c0f\u5757&#xff0c;\u5206\u51fa\u53bb<br \/>\nsize_t objSize&#061;sizeof(T)&lt;sizeof(void*)?sizeof(void*):sizeof(T);\/\/\u4fdd\u8bc1\u81f3\u5c11\u80fd\u5b58\u653e\u4e0b\u4e00\u4e2a\u5bf9\u8c61\u7684\u5730\u5740<br \/>\n_memory &#043;&#061; objSize;<br \/>\n_remainBytes -&#061; objSize;<br \/>\n}<\/p>\n<p>\/\/malloc\u53ea\u5f00\u4e86\u7a7a\u95f4&#xff0c;\u6ca1\u6709\u521d\u59cb\u5316&#xff0c;\u8981\u8fdb\u884c\u521d\u59cb\u5316<br \/>\n\/\/\u5b9a\u4f4dnew&#xff0c;\u663e\u793a\u8c03\u7528T\u7684\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316<br \/>\nnew(obj)T;<\/p>\n<p>return obj;<br \/>\n}<\/p>\n<p>void Delete(T* obj)<br \/>\n{<br \/>\n\/\/if (_freeList &#061;&#061; nullptr)<br \/>\n\/\/{<br \/>\n\/\/_freeList &#061; obj;<br \/>\n\/\/\/\/*(int*)obj&#061;nullptr;    \u5f3a\u8f6c\u6210int*\u7c7b\u578b&#xff0c;\u6b64\u65f6\u53ea\u80fd\u572832\u4f4d\u673a\u5668\u4e0a\u8dd1&#xff0c;\u56e0\u4e3a64\u4f4d\u673a\u5668\u4e0a&#xff0c;\u6307\u9488\u5927\u5c0f\u662f8\u5b57\u8282\u3002<br \/>\n\/\/*(void**)obj &#061; nullptr; \/\/\u8fd9\u91cc\u4f7f\u7528\u4e8c\u7ea7\u6307\u9488\u5373\u53ef\u89e3\u51b3&#xff0c;void**\u89e3\u5f15\u7528\u540e&#xff0c;\u5c31\u662fvoid*&#xff0c;\u6b64\u65f6\u8fd8\u662f\u6307\u9488&#xff0c;\u572832\u4f4d\u673a\u5668\u4e0a\u5c31\u662f4\u5b57\u8282&#xff0c;\u572864\u4f4d\u673a\u5668\u4e0a\u5c31\u662f8\u5b57\u8282&#xff0c;\u53ef\u4ee5\u81ea\u52a8\u9002\u5e94<br \/>\n\/\/}<br \/>\n\/\/else<br \/>\n\/\/{<br \/>\n\/\/\/\/\u5934\u63d2<br \/>\n\/\/*(void**)obj &#061; _freeList; \/\/\u5148\u8ba9\u5c0f\u5185\u5b58\u5757\u6307\u5411\u81ea\u7531\u94fe\u8868\u7684\u7b2c\u4e00\u4e2a\u8282\u70b9&#xff0c;\u56e0\u4e3afreeList\u6307\u5411\u7684\u5c31\u662f\u7b2c\u4e00\u4e2a\u8282\u70b9\u3002<br \/>\n\/\/_freeList &#061; obj;    \/\/\u63a5\u7740\u518d\u8ba9obj\u53d8\u6210\u7b2c\u4e00\u4e2a\u8282\u70b9&#xff0c;\u8ba9freelist\u6307\u5411boj\u3002<br \/>\n\/\/}<\/p>\n<p>\/\/\u663e\u793a\u8c03\u7528\u6790\u6784\u51fd\u6570\u6e05\u7406\u5bf9\u8c61<br \/>\nobj-&gt;~T();<\/p>\n<p>\/\/\u5934\u63d2<br \/>\n*(void**)obj &#061; _freeList;  \/\/\u5b9e\u9645\u4e0a\u4e0d\u9700\u8981\u533a\u5206\u60c5\u51b5&#xff0c;\u56e0\u4e3a\u7a7a\u7684\u65f6\u5019&#xff0c;\u76f4\u63a5\u5934\u63d2\u4e5f\u53ef\u4ee5<br \/>\n_freeList &#061; obj; <\/p>\n<p>}<\/p>\n<p>private:<br \/>\nchar* _memory&#061;nullptr;\/\/\u56e0\u4e3a\u4e00\u4e2achar\u662f\u4e00\u4e2a\u5b57\u8282&#xff0c;\u5207\u5185\u5b58\u5757\u65f6\u597d\u7528\u3002\u6307\u5411\u5927\u5757\u5185\u5b58\u7684\u6307\u9488<br \/>\nsize_t _remainBytes &#061; 0;\/\/\u5927\u5757\u5185\u5b58\u5728\u5207\u5206\u8fc7\u7a0b\u4e2d\u5269\u4f59\u5b57\u8282\u6570<br \/>\nvoid* _freeList&#061;nullptr;\/\/\u8fd8\u56de\u6765\u8fc7\u7a0b\u4e2d\u94fe\u63a5\u7684\u81ea\u7531\u94fe\u8868\u7684\u5934\u6307\u9488<br \/>\n}; <\/p>\n<h4 id=\"%E9%AB%98%E5%B9%B6%E5%8F%91%E5%86%85%E5%AD%98%E6%B1%A0%E6%95%B4%E4%BD%93%E6%A1%86%E6%9E%B6%E8%AE%BE%E8%AE%A1\">\u9ad8\u5e76\u53d1\u5185\u5b58\u6c60\u6574\u4f53\u6846\u67b6\u8bbe\u8ba1<\/h4>\n<p>\u73b0\u4ee3\u5f88\u591a\u7684\u5f00\u53d1\u73af\u5883\u90fd\u662f\u591a\u6838\u591a\u7ebf\u7a0b&#xff0c;\u5728\u7533\u8bf7\u5185\u5b58\u7684\u573a\u666f\u4e0b&#xff0c;\u5fc5\u7136\u5b58\u5728\u6fc0\u70c8\u7684\u9501\u7ade\u4e89\u95ee\u9898\u3002malloc\u672c\u8eab \u5176\u5b9e\u5df2\u7ecf\u5f88\u4f18\u79c0&#xff0c;\u90a3\u4e48\u6211\u4eec\u9879\u76ee\u7684\u539f\u578btcmalloc\u5c31\u662f\u5728\u591a\u7ebf\u7a0b\u9ad8\u5e76\u53d1\u7684\u573a\u666f\u4e0b\u66f4\u80dc\u4e00\u7b79&#xff0c;\u6240\u4ee5\u8fd9\u6b21\u6211\u4eec \u5b9e\u73b0\u7684\u5185\u5b58\u6c60\u9700\u8981\u8003\u8651\u4ee5\u4e0b\u51e0\u65b9\u9762\u7684\u95ee\u9898\u3002\u00a0<\/p>\n<li>\u00a0\u6027\u80fd\u95ee\u9898\u3002<\/li>\n<li>\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b&#xff0c;\u9501\u7ade\u4e89\u95ee\u9898\u3002<\/li>\n<li>\u00a0\u5185\u5b58\u788e\u7247\u95ee\u9898\u3002<\/li>\n<p>concurrent memory pool\u4e3b\u8981\u7531\u4ee5\u4e0b3\u4e2a\u90e8\u5206\u6784\u6210&#xff1a;<\/p>\n<li>thread cache&#xff1a;\u7ebf\u7a0b\u7f13\u5b58\u662f\u6bcf\u4e2a\u7ebf\u7a0b\u72ec\u6709\u7684&#xff0c;\u7528\u4e8e\u5c0f\u4e8e256KB\u7684\u5185\u5b58\u7684\u5206\u914d&#xff0c;\u7ebf\u7a0b\u4ece\u8fd9\u91cc\u7533\u8bf7\u5185 \u5b58\u4e0d\u9700\u8981\u52a0\u9501&#xff0c;\u6bcf\u4e2a\u7ebf\u7a0b\u72ec\u4eab\u4e00\u4e2acache&#xff0c;\u8fd9\u4e5f\u5c31\u662f\u8fd9\u4e2a\u5e76\u53d1\u7ebf\u7a0b\u6c60\u9ad8\u6548\u7684\u5730\u65b9\u3002<\/li>\n<li>central cache&#xff1a;\u4e2d\u5fc3\u7f13\u5b58\u662f\u6240\u6709\u7ebf\u7a0b\u6240\u5171\u4eab&#xff0c;thread cache\u662f\u6309\u9700\u4ececentral cache\u4e2d\u83b7\u53d6\u7684\u5bf9 \u8c61\u3002central cache\u5408\u9002\u7684\u65f6\u673a\u56de\u6536thread cache\u4e2d\u7684\u5bf9\u8c61&#xff0c;\u907f\u514d\u4e00\u4e2a\u7ebf\u7a0b\u5360\u7528\u4e86\u592a\u591a\u7684\u5185\u5b58&#xff0c;\u800c\u5176\u4ed6\u7ebf\u7a0b\u7684\u5185\u5b58\u5403\u7d27&#xff0c;\u8fbe\u5230\u5185\u5b58\u5206\u914d\u5728\u591a\u4e2a\u7ebf\u7a0b\u4e2d\u66f4\u5747\u8861\u7684\u6309\u9700\u8c03\u5ea6\u7684\u76ee\u7684\u3002central cache\u662f\u5b58 \u5728\u7ade\u4e89\u7684&#xff0c;\u6240\u4ee5\u4ece\u8fd9\u91cc\u53d6\u5185\u5b58\u5bf9\u8c61\u662f\u9700\u8981\u52a0\u9501&#xff0c;\u9996\u5148\u8fd9\u91cc\u7528\u7684\u662f\u6876\u9501&#xff0c;\u5176\u6b21\u53ea\u6709thread cache\u7684\u5bf9\u8c61\u6ca1\u6709\u5185\u5b58\u65f6\u624d\u4f1a\u627ecentral cache&#xff0c;\u6240\u4ee5\u8fd9\u91cc\u7ade\u4e89\u4e0d\u4f1a\u5f88\u6fc0\u70c8\u3002<\/li>\n<li>page cache&#xff1a;\u9875\u7f13\u5b58\u662f\u5728central cache\u7f13\u5b58\u4e0a\u9762\u7684\u4e00\u5c42\u7f13\u5b58&#xff0c;\u5b58\u50a8\u7684\u5185\u5b58\u662f\u4ee5\u9875\u4e3a\u5355\u4f4d\u5b58\u50a8\u53ca\u5206 \u914d\u7684&#xff0c;central cache\u6ca1\u6709\u5185\u5b58\u5bf9\u8c61\u65f6&#xff0c;\u4ecepage cache\u5206\u914d\u51fa\u4e00\u5b9a\u6570\u91cf\u7684page&#xff0c;\u5e76\u5207\u5272\u6210\u5b9a\u957f\u5927\u5c0f \u7684\u5c0f\u5757\u5185\u5b58&#xff0c;\u5206\u914d\u7ed9central cache\u3002\u5f53\u4e00\u4e2aspan\u7684\u51e0\u4e2a\u8de8\u5ea6\u9875\u7684\u5bf9\u8c61\u90fd\u56de\u6536\u4ee5\u540e&#xff0c;page cache \u4f1a\u56de\u6536central cache\u6ee1\u8db3\u6761\u4ef6\u7684span\u5bf9\u8c61&#xff0c;\u5e76\u4e14\u5408\u5e76\u76f8\u90bb\u7684\u9875&#xff0c;\u7ec4\u6210\u66f4\u5927\u7684\u9875&#xff0c;\u7f13\u89e3\u5185\u5b58\u788e\u7247 \u7684\u95ee\u9898\u3002<\/li>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"797\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260128143300-697a1e1c483f9.png\" width=\"1188\" \/>\u200b<\/p>\n<h4 id=\"%E9%AB%98%E5%B9%B6%E5%8F%91%E5%86%85%E5%AD%98%E6%B1%A0--thread%20cache%C2%A0\">\u9ad8\u5e76\u53d1\u5185\u5b58\u6c60&#8211;thread cache\u00a0<\/h4>\n<p>thread cache\u662f\u54c8\u5e0c\u6876\u7ed3\u6784&#xff0c;\u6bcf\u4e2a\u6876\u662f\u4e00\u4e2a\u6309\u6876\u4f4d\u7f6e\u6620\u5c04\u5927\u5c0f\u7684\u5185\u5b58\u5757\u5bf9\u8c61\u7684\u81ea\u7531\u94fe\u8868\u3002\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u4f1a \u6709\u4e00\u4e2athread cache\u5bf9\u8c61&#xff0c;\u8fd9\u6837\u6bcf\u4e2a\u7ebf\u7a0b\u5728\u8fd9\u91cc\u83b7\u53d6\u5bf9\u8c61\u548c\u91ca\u653e\u5bf9\u8c61\u65f6\u662f\u65e0\u9501\u7684\u3002\u00a0<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"867\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260128143300-697a1e1ca732b.png\" width=\"1297\" \/>\u200b<\/p>\n<p>\u7533\u8bf7\u5185\u5b58&#xff1a;<\/p>\n<li>\u5f53\u5185\u5b58\u7533\u8bf7size&lt;&#061;256KB&#xff0c;\u5148\u83b7\u53d6\u5230\u7ebf\u7a0b\u672c\u5730\u5b58\u50a8\u7684thread cache\u5bf9\u8c61&#xff0c;\u8ba1\u7b97size\u6620\u5c04\u7684\u54c8\u5e0c\u6876\u81ea \u7531\u94fe\u8868\u4e0b\u6807i\u3002<\/li>\n<li>\u5982\u679c\u81ea\u7531\u94fe\u8868_freeLists[i]\u4e2d\u6709\u5bf9\u8c61&#xff0c;\u5219\u76f4\u63a5Pop\u4e00\u4e2a\u5185\u5b58\u5bf9\u8c61\u8fd4\u56de\u3002<\/li>\n<li>\u5982\u679c_freeLists[i]\u4e2d\u6ca1\u6709\u5bf9\u8c61\u65f6&#xff0c;\u5219\u6279\u91cf\u4ececentral cache\u4e2d\u83b7\u53d6\u4e00\u5b9a\u6570\u91cf\u7684\u5bf9\u8c61&#xff0c;\u63d2\u5165\u5230\u81ea\u7531\u94fe\u8868 \u5e76\u8fd4\u56de\u4e00\u4e2a\u5bf9\u8c61\u3002\u00a0<\/li>\n<p>\u91ca\u653e\u5185\u5b58&#xff1a;\u00a0<\/p>\n<li>\u5f53\u91ca\u653e\u5185\u5b58\u5c0f\u4e8e256k\u65f6\u5c06\u5185\u5b58\u91ca\u653e\u56dethread cache&#xff0c;\u8ba1\u7b97size\u6620\u5c04\u81ea\u7531\u94fe\u8868\u6876\u4f4d\u7f6ei&#xff0c;\u5c06\u5bf9\u8c61Push \u5230_freeLists[i]\u3002<\/li>\n<li>\u5f53\u94fe\u8868\u7684\u957f\u5ea6\u8fc7\u957f&#xff0c;\u5219\u56de\u6536\u4e00\u90e8\u5206\u5185\u5b58\u5bf9\u8c61\u5230central cache\u3002\u00a0<\/li>\n<h4 id=\"%E9%AB%98%E5%B9%B6%E5%8F%91%E5%86%85%E5%AD%98%E6%B1%A0--central%20cache\">\u9ad8\u5e76\u53d1\u5185\u5b58\u6c60&#8211;central cache<\/h4>\n<p>central cache\u4e5f\u662f\u4e00\u4e2a\u54c8\u5e0c\u6876\u7ed3\u6784&#xff0c;\u4ed6\u7684\u54c8\u5e0c\u6876\u7684\u6620\u5c04\u5173\u7cfb\u8ddfthread cache\u662f\u4e00\u6837\u7684\u3002\u4e0d\u540c\u7684\u662f\u4ed6\u7684\u6bcf \u4e2a\u54c8\u5e0c\u6876\u4f4d\u7f6e\u6302\u662fSpanList\u94fe\u8868\u7ed3\u6784&#xff0c;\u4e0d\u8fc7\u6bcf\u4e2a\u6620\u5c04\u6876\u4e0b\u9762\u7684span\u4e2d\u7684\u5927\u5185\u5b58\u5757\u88ab\u6309\u6620\u5c04\u5173\u7cfb\u5207\u6210\u4e86\u4e00 \u4e2a\u4e2a\u5c0f\u5185\u5b58\u5757\u5bf9\u8c61\u6302\u5728span\u7684\u81ea\u7531\u94fe\u8868\u4e2d\u3002\u00a0<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"723\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260128143301-697a1e1d21365.png\" width=\"1265\" \/>\u200b<\/p>\n<p>\u7533\u8bf7\u5185\u5b58&#xff1a;\u00a0<\/p>\n<li>\u5f53thread cache\u4e2d\u6ca1\u6709\u5185\u5b58\u65f6&#xff0c;\u5c31\u4f1a\u6279\u91cf\u5411central cache\u7533\u8bf7\u4e00\u4e9b\u5185\u5b58\u5bf9\u8c61&#xff0c;\u8fd9\u91cc\u7684\u6279\u91cf\u83b7\u53d6\u5bf9 \u8c61\u7684\u6570\u91cf\u4f7f\u7528\u4e86\u7c7b\u4f3c\u7f51\u7edctcp\u534f\u8bae\u62e5\u585e\u63a7\u5236\u7684\u6162\u5f00\u59cb\u7b97\u6cd5&#xff1b;central cache\u4e5f\u6709\u4e00\u4e2a\u54c8\u5e0c\u6620\u5c04\u7684 spanlist&#xff0c;spanlist\u4e2d\u6302\u7740span&#xff0c;\u4ecespan\u4e2d\u53d6\u51fa\u5bf9\u8c61\u7ed9thread cache&#xff0c;\u8fd9\u4e2a\u8fc7\u7a0b\u662f\u9700\u8981\u52a0\u9501\u7684&#xff0c;\u4e0d \u8fc7\u8fd9\u91cc\u4f7f\u7528\u7684\u662f\u4e00\u4e2a\u6876\u9501&#xff0c;\u5c3d\u53ef\u80fd\u63d0\u9ad8\u6548\u7387\u3002<\/li>\n<li>central cache\u6620\u5c04\u7684spanlist\u4e2d\u6240\u6709span\u7684\u90fd\u6ca1\u6709\u5185\u5b58\u4ee5\u540e&#xff0c;\u5219\u9700\u8981\u5411page cache\u7533\u8bf7\u4e00\u4e2a\u65b0\u7684 span\u5bf9\u8c61&#xff0c;\u62ff\u5230span\u4ee5\u540e\u5c06span\u7ba1\u7406\u7684\u5185\u5b58\u6309\u5927\u5c0f\u5207\u597d\u4f5c\u4e3a\u81ea\u7531\u94fe\u8868\u94fe\u63a5\u5230\u4e00\u8d77\u3002\u7136\u540e\u4ecespan \u4e2d\u53d6\u5bf9\u8c61\u7ed9thread cache\u3002<\/li>\n<li>central cache\u7684\u4e2d\u6302\u7684span\u4e2duse_count\u8bb0\u5f55\u5206\u914d\u4e86\u591a\u5c11\u4e2a\u5bf9\u8c61\u51fa\u53bb&#xff0c;\u5206\u914d\u4e00\u4e2a\u5bf9\u8c61\u7ed9thread cache&#xff0c;\u5c31&#043;&#043;use_count\u00a0<\/li>\n<p>\u91ca\u653e\u5185\u5b58&#xff1a;\u00a0<\/p>\n<p>\u5f53thread_cache\u8fc7\u957f\u6216\u8005\u7ebf\u7a0b\u9500\u6bc1&#xff0c;\u5219\u4f1a\u5c06\u5185\u5b58\u91ca\u653e\u56decentral cache\u4e2d\u7684&#xff0c;\u91ca\u653e\u56de\u6765\u65f6&#8211; use_count\u3002\u5f53use_count\u51cf\u52300\u65f6\u5219\u8868\u793a\u6240\u6709\u5bf9\u8c61\u90fd\u56de\u5230\u4e86span&#xff0c;\u5219\u5c06span\u91ca\u653e\u56depage cache&#xff0c; page cache\u4e2d\u4f1a\u5bf9\u524d\u540e\u76f8\u90bb\u7684\u7a7a\u95f2\u9875\u8fdb\u884c\u5408\u5e76\u3002<\/p>\n<h4 id=\"%C2%A0%E9%AB%98%E5%B9%B6%E5%8F%91%E5%86%85%E5%AD%98%E6%B1%A0--page%20cache\">\u00a0\u9ad8\u5e76\u53d1\u5185\u5b58\u6c60&#8211;page cache<\/h4>\n<p>\u7533\u8bf7\u5185\u5b58&#xff1a;\u00a0<\/p>\n<li>\u5f53central cache\u5411page cache\u7533\u8bf7\u5185\u5b58\u65f6&#xff0c;page cache\u5148\u68c0\u67e5\u5bf9\u5e94\u4f4d\u7f6e\u6709\u6ca1\u6709span&#xff0c;\u5982\u679c\u6ca1\u6709 \u5219\u5411\u66f4\u5927\u9875\u5bfb\u627e\u4e00\u4e2aspan&#xff0c;\u5982\u679c\u627e\u5230\u5219\u5206\u88c2\u6210\u4e24\u4e2a\u3002\u6bd4\u5982&#xff1a;\u7533\u8bf7\u7684\u662f4\u9875page&#xff0c;4\u9875page\u540e\u9762\u6ca1 \u6709\u6302span&#xff0c;\u5219\u5411\u540e\u9762\u5bfb\u627e\u66f4\u5927\u7684span&#xff0c;\u5047\u8bbe\u572810\u9875page\u4f4d\u7f6e\u627e\u5230\u4e00\u4e2aspan&#xff0c;\u5219\u5c0610\u9875page span\u5206\u88c2\u4e3a\u4e00\u4e2a4\u9875page span\u548c\u4e00\u4e2a6\u9875page span\u3002<\/li>\n<li>\u5982\u679c\u627e\u5230_spanList[128]\u90fd\u6ca1\u6709\u5408\u9002\u7684span&#xff0c;\u5219\u5411\u7cfb\u7edf\u4f7f\u7528mmap\u3001brk\u6216\u8005\u662fVirtualAlloc\u7b49\u65b9\u5f0f \u7533\u8bf7128\u9875page span\u6302\u5728\u81ea\u7531\u94fe\u8868\u4e2d&#xff0c;\u518d\u91cd\u590d1\u4e2d\u7684\u8fc7\u7a0b\u3002<\/li>\n<li>\u9700\u8981\u6ce8\u610f\u7684\u662fcentral cache\u548cpage cache \u7684\u6838\u5fc3\u7ed3\u6784\u90fd\u662fspanlist\u7684\u54c8\u5e0c\u6876&#xff0c;\u4f46\u662f\u4ed6\u4eec\u662f\u6709\u672c\u8d28 \u533a\u522b\u7684&#xff0c;central cache\u4e2d\u54c8\u5e0c\u6876&#xff0c;\u662f\u6309\u8ddfthread cache\u4e00\u6837\u7684\u5927\u5c0f\u5bf9\u9f50\u5173\u7cfb\u6620\u5c04\u7684&#xff0c;\u4ed6\u7684spanlist \u4e2d\u6302\u7684span\u4e2d\u7684\u5185\u5b58\u90fd\u88ab\u6309\u6620\u5c04\u5173\u7cfb\u5207\u597d\u94fe\u63a5\u6210\u5c0f\u5757\u5185\u5b58\u7684\u81ea\u7531\u94fe\u8868\u3002\u800cpage cache \u4e2d\u7684 spanlist\u5219\u662f\u6309\u4e0b\u6807\u6876\u53f7\u6620\u5c04\u7684&#xff0c;\u4e5f\u5c31\u662f\u8bf4\u7b2ci\u53f7\u6876\u4e2d\u6302\u7684span\u90fd\u662fi\u9875\u5185\u5b58\u3002\u00a0<\/li>\n<p>\u91ca\u653e\u5185\u5b58&#xff1a;<\/p>\n<li>\u5982\u679ccentral cache\u91ca\u653e\u56de\u4e00\u4e2aspan&#xff0c;\u5219\u4f9d\u6b21\u5bfb\u627espan\u7684\u524d\u540epage id\u7684\u6ca1\u6709\u5728\u4f7f\u7528\u7684\u7a7a\u95f2span&#xff0c; \u770b\u662f\u5426\u53ef\u4ee5\u5408\u5e76&#xff0c;\u5982\u679c\u5408\u5e76\u7ee7\u7eed\u5411\u524d\u5bfb\u627e\u3002\u8fd9\u6837\u5c31\u53ef\u4ee5\u5c06\u5207\u5c0f\u7684\u5185\u5b58\u5408\u5e76\u6536\u7f29\u6210\u5927\u7684span&#xff0c;\u51cf\u5c11 \u5185\u5b58\u788e\u7247\u3002\u00a0<\/li>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"854\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260128143301-697a1e1d85837.png\" width=\"872\" \/>\u200b<\/p>\n<h2>\u6574\u4f53\u4ee3\u7801<\/h2>\n<p>\u7801\u4e91&#xff1a;https:\/\/gitee.com\/qinjianhao0\/project-warehouse\/tree\/master\/FAPool<\/p>\n<h3>CentralCache.h<\/h3>\n<p>#pragma once<\/p>\n<p>#include&#034;Common.h&#034;<\/p>\n<p>\/\/\u5355\u4f8b\u6a21\u5f0f  \u61d2\u6c49<br \/>\nclass CentralCache<br \/>\n{<br \/>\npublic:<br \/>\nstatic CentralCache* GetInstance()<br \/>\n{<br \/>\nreturn &amp;_sInst;<br \/>\n}<\/p>\n<p>\/\/ \u83b7\u53d6\u4e00\u4e2a\u975e\u7a7a\u7684span<br \/>\nSpan* GetOneSpan(SpanList&amp; list, size_t byte_size);<\/p>\n<p>\/\/ \u4ece\u4e2d\u5fc3\u7f13\u5b58\u83b7\u53d6\u4e00\u5b9a\u6570\u91cf\u7684\u5bf9\u8c61\u7ed9thread cache<br \/>\nsize_t FetchRangeObj(void*&amp; start, void*&amp; end, size_t batchNum, size_t size);<\/p>\n<p>\/\/ \u5c06\u4e00\u5b9a\u6570\u91cf\u7684\u5bf9\u8c61\u91ca\u653e\u5230span\u8de8\u5ea6<br \/>\nvoid ReleaseListToSpans(void* start, size_t byte_size);<br \/>\nprivate:<br \/>\nSpanList _spanLists[NFREELIST];<br \/>\nprivate:<br \/>\nCentralCache()<br \/>\n{}<\/p>\n<p>CentralCache(const CentralCache&amp;) &#061; delete;<\/p>\n<p>static CentralCache _sInst;<br \/>\n};<\/p>\n<h3>Common.h<\/h3>\n<p>#pragma once<\/p>\n<p>#include&lt;iostream&gt;<br \/>\n#include&lt;vector&gt;<br \/>\n#include&lt;unordered_map&gt;<br \/>\n#include&lt;algorithm&gt;<\/p>\n<p>#include&lt;thread&gt;<br \/>\n#include&lt;mutex&gt;<\/p>\n<p>#include&lt;time.h&gt;<br \/>\n#include&lt;assert.h&gt;<\/p>\n<p>using std::cout;<br \/>\nusing std::endl;<\/p>\n<p>#ifdef _WIN32<br \/>\n#include&lt;windows.h&gt;<br \/>\n#else<br \/>\n\/\/&#8230;<br \/>\n#endif<\/p>\n<p>\/\/ \u5c0f\u4e8e\u7b49\u4e8eMAX_BYTES&#xff0c;\u5c31\u627ethread cache\u7533\u8bf7<br \/>\n\/\/ \u5927\u4e8eMAX_BYTES&#xff0c;\u5c31\u76f4\u63a5\u627epage cache\u6216\u8005\u7cfb\u7edf\u5806\u7533\u8bf7<br \/>\nstatic const size_t MAX_BYTES &#061; 256 * 1024;<br \/>\nstatic const size_t NFREELIST &#061; 208;\/\/ thread cache \u548c central cache\u81ea\u7531\u94fe\u8868\u54c8\u5e0c\u6876\u7684\u8868\u5927\u5c0f\u3002thread cache\u4e2d\u6876\u7684\u4e2a\u6570&#xff0c;\u4e5f\u5c31\u662f\u81ea\u7531\u94fe\u8868\u7684\u4e2a\u6570\u662f208\u3002<br \/>\nstatic const size_t NPAGES &#061; 129;<br \/>\nstatic const size_t PAGE_SHIFT &#061; 13; \/\/ &lt;&lt;PAGE_SHIFT   \u8868\u793a\u4e58\u4ee58k\u7684\u5927\u5c0f\u3002\u9875\u5927\u5c0f\u8f6c\u6362\u504f\u79fb, \u5373\u4e00\u9875\u5b9a\u4e49\u4e3a2^13,\u4e5f\u5c31\u662f8KB<\/p>\n<p>#ifdef _WIN64<br \/>\ntypedef unsigned long long PAGE_ID;  \/\/64\u4f4d\u4e0b8\u5b57\u8282<br \/>\n#elif _WIN32\/\/32\u4f4d\u4e0b4\u5b57\u8282<br \/>\ntypedef size_t PAGE_ID;<br \/>\n#else<br \/>\n\/\/linux<br \/>\n#endif <\/p>\n<p>\/\/ \u76f4\u63a5\u53bb\u5806\u4e0a\u6309\u9875\u7533\u8bf7\u7a7a\u95f4<br \/>\ninline static void* SystemAlloc(size_t kpage)<br \/>\n{<br \/>\n#ifdef _WIN32<br \/>\nvoid* ptr &#061; VirtualAlloc(0, kpage * (1 &lt;&lt; 12), MEM_COMMIT | MEM_RESERVE,<br \/>\nPAGE_READWRITE);<br \/>\n#else<br \/>\n\/\/ linux\u4e0bbrk mmap\u7b49<br \/>\n#endif<br \/>\nif (ptr &#061;&#061; nullptr)<br \/>\nthrow std::bad_alloc();<br \/>\nreturn ptr;<br \/>\n}<\/p>\n<p>inline static void SystemFree(void* ptr)<br \/>\n{<br \/>\n#ifdef _WIN32<br \/>\nVirtualFree(ptr, 0, MEM_RELEASE);<br \/>\n#else<br \/>\n\/\/ sbrk unmmap\u7b49<br \/>\n#endif<br \/>\n}<\/p>\n<p>\/\/ \u83b7\u53d6\u5185\u5b58\u5bf9\u8c61\u4e2d\u5b58\u50a8\u7684\u59344 or 8\u5b57\u8282\u503c&#xff0c;\u5373\u94fe\u63a5\u7684\u4e0b\u4e00\u4e2a\u5bf9\u8c61\u7684\u5730\u5740<br \/>\nstatic void*&amp; NextObj(void* obj)\/\/\u8fd4\u56de\u5f15\u7528\u540e&#xff0c;\u5c31\u53ef\u4ee5\u8d4b\u503c<br \/>\n{<br \/>\nreturn *(void**)obj;<br \/>\n}<\/p>\n<p>\/\/\u7ba1\u7406\u5207\u5206\u597d\u7684\u5c0f\u5bf9\u8c61\u7684\u81ea\u7531\u94fe\u8868<br \/>\nclass FreeList<br \/>\n{<br \/>\npublic:<br \/>\nvoid Push(void* obj)<br \/>\n{<br \/>\nassert(obj);<\/p>\n<p>\/\/\u5934\u63d2<br \/>\n\/\/*(void**)obj &#061; _freeList;<br \/>\nNextObj(obj) &#061; _freeList;<br \/>\n_freeList &#061; obj;<\/p>\n<p>&#043;&#043;_size;<br \/>\n}<\/p>\n<p>void PushRange(void* start, void* end,size_t n)<br \/>\n{<br \/>\nNextObj(end) &#061; _freeList;<br \/>\n_freeList &#061; start;<\/p>\n<p>_size &#043;&#061; n;<br \/>\n}<\/p>\n<p>void PopRange(void*&amp; start, void*&amp; end, size_t n)<br \/>\n{<br \/>\nassert(n &gt;&#061; _size);<br \/>\nstart &#061; _freeList;<br \/>\nend &#061; start;<\/p>\n<p>for (size_t i &#061; 0; i &lt; n &#8211; 1; i&#043;&#043;)<br \/>\n{<br \/>\nend &#061; NextObj(end);<br \/>\n}<\/p>\n<p>_freeList &#061; NextObj(end);<br \/>\nNextObj(end) &#061; nullptr;<br \/>\n_size -&#061; n;<br \/>\n}<\/p>\n<p>void* Pop()<br \/>\n{<br \/>\nassert(_freeList);<\/p>\n<p>\/\/\u5934\u5220<br \/>\nvoid* obj &#061; _freeList;<br \/>\n_freeList &#061; NextObj(obj);<br \/>\nreturn obj;<\/p>\n<p>&#8211;_size;<br \/>\n}<\/p>\n<p>bool Empty()<br \/>\n{<br \/>\nreturn _freeList &#061;&#061; nullptr;<br \/>\n}<\/p>\n<p>size_t&amp; MaxSize()<br \/>\n{<br \/>\nreturn _maxSize;<br \/>\n}<\/p>\n<p>size_t Size()<br \/>\n{<br \/>\nreturn _size;<br \/>\n}<\/p>\n<p>private:<br \/>\nvoid* _freeList&#061;nullptr;<br \/>\nsize_t _maxSize &#061; 1;<br \/>\nsize_t _size &#061; 0;<br \/>\n};<\/p>\n<p>\/\/\u8ba1\u7b97\u5bf9\u8c61\u5927\u5c0f\u7684\u5bf9\u9f50\u6620\u5c04\u89c4\u5219<br \/>\nclass SizeClass<br \/>\n{<br \/>\npublic:<br \/>\n\/\/ \u6574\u4f53\u63a7\u5236\u5728\u6700\u591a10%\u5de6\u53f3\u7684\u5185\u788e\u7247\u6d6a\u8d39<br \/>\n \/\/ [1,128]     8byte\u5bf9\u9f50freelist[0,16)   \/\/\u6700\u5f00\u59cb\u6700\u5c11\u5c31\u662f8\u5b57\u8282\u5bf9\u9f50&#xff0c;\u56e0\u4e3a\u6307\u9488\u662f4\u6216\u80058\u5b57\u8282&#xff0c;\u8981\u53d68\u5b57\u8282\u7b97&#xff0c;\u8fd9\u6837\u80fd\u517c\u5bb94\u5b57\u8282\u3002<br \/>\n \/\/ [128&#043;1,1024] 16byte\u5bf9\u9f50freelist[16,72)<br \/>\n \/\/ [1024&#043;1,8*1024] 128byte\u5bf9\u9f50freelist[72,128)<br \/>\n \/\/ [8*1024&#043;1,64*1024]   1024byte\u5bf9\u9f50freelist[128,184)<br \/>\n \/\/ [64*1024&#043;1,256*1024] 8*1024byte\u5bf9\u9f50     freelist[184,208)<\/p>\n<p>\/\/size_t _RoundUp(size_t size, size_t alignNum)<br \/>\n\/\/{<br \/>\n\/\/size_t alignSize;<br \/>\n\/\/if (size % alignNum !&#061; 0)\/\/\u5982\u679c\u4e0d\u80fd\u6574\u9664<br \/>\n\/\/{<br \/>\n\/\/alignSize &#061; (size \/ alignNum &#043; 1) * alignNum;\/\/\u5411\u4e0a\u5bf9\u9f50&#xff0c;\u5982&#xff1a;&#xff08;5\/8&#043;1&#xff09;*8&#061;8&#xff0c;\u5373\u53ef\u628a5\u5bf9\u9f50\u52308\u3002<br \/>\n\/\/}<br \/>\n\/\/else<br \/>\n\/\/{<br \/>\n\/\/alignSize &#061; size;<br \/>\n\/\/}<br \/>\n\/\/return alignSize;<br \/>\n\/\/}<\/p>\n<p>static inline size_t _RoundUp(size_t bytes, size_t alignNum)<br \/>\n{<br \/>\nreturn (((bytes)&#043;alignNum &#8211; 1) &amp; ~(alignNum &#8211; 1));<br \/>\n}<\/p>\n<p>\/\/\u641e\u6210\u9759\u6001\u7684&#xff0c;\u5c31\u53ef\u4ee5\u4e0d\u7528\u5bf9\u8c61\u53bb\u8c03&#xff0c;\u91cc\u9762\u4e5f\u6ca1\u6709\u4ec0\u4e48\u6210\u5458<br \/>\nstatic inline size_t RoundUp(size_t size)\/\/\u628asize\u5411\u4e0a\u5bf9\u9f50<br \/>\n{<br \/>\nif (size &lt;&#061; 128)<br \/>\n{<br \/>\nreturn _RoundUp(size, 8);<br \/>\n}<br \/>\nelse if (size &lt;&#061; 1024)<br \/>\n{<br \/>\nreturn _RoundUp(size, 16);<br \/>\n}<br \/>\nelse if (size &lt;&#061;8* 1024)<br \/>\n{<br \/>\nreturn _RoundUp(size, 128);<br \/>\n}<br \/>\nelse if (size &lt;&#061; 64*1024)<br \/>\n{<br \/>\nreturn _RoundUp(size, 1024);<br \/>\n}<br \/>\nelse if (size &lt;&#061; 256*1024)<br \/>\n{<br \/>\nreturn _RoundUp(size, 8 * 1024);<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\nreturn _RoundUp(size, 1 &lt;&lt; PAGE_SHIFT);<br \/>\n}<br \/>\n}<\/p>\n<p>\/\/size_t _Index(size_t bytes,size_t alignNum)   alignNum\u6307\u5bf9\u9f50\u6570<br \/>\n\/\/{<br \/>\n\/\/if (bytes%alignNum&#061;&#061;0)<br \/>\n\/\/{<br \/>\n\/\/return bytes \/ alignNum &#8211; 1;<br \/>\n\/\/}<br \/>\n\/\/else<br \/>\n\/\/{<br \/>\n\/\/return bytes \/ alignNum;<br \/>\n\/\/}<br \/>\n\/\/}<\/p>\n<p>static inline size_t _Index(size_t bytes, size_t align_shift)   \/\/align_shift\u63072\u7684\u51e0\u6b21\u65b9&#xff0c;align_shift&#061;&#061;3\u65f6&#xff0c;\u5de6\u79fb3\u4f4d\u5c31\u662f\u4e58\u4ee58&#xff0c;\u53732\u76843\u6b21\u65b9\u3002<br \/>\n{<br \/>\nreturn ((bytes &#043; (1 &lt;&lt; align_shift) &#8211; 1) &gt;&gt; align_shift) &#8211; 1;<br \/>\n}<\/p>\n<p>\/\/ \u8ba1\u7b97\u6620\u5c04\u5230\u54ea\u4e00\u4e2a\u81ea\u7531\u94fe\u8868\u6876<br \/>\nstatic inline size_t Index(size_t bytes)<br \/>\n{<br \/>\nassert(bytes &lt;&#061; MAX_BYTES);<br \/>\n\/\/ \u6bcf\u4e2a\u533a\u95f4\u6709\u591a\u5c11\u4e2a\u94fe<br \/>\nstatic int group_array[4] &#061; { 16, 56, 56, 56 };<br \/>\nif (bytes &lt;&#061; 128) {<br \/>\nreturn _Index(bytes, 3);<br \/>\n}<br \/>\nelse if (bytes &lt;&#061; 1024) {<br \/>\nreturn _Index(bytes &#8211; 128, 4) &#043; group_array[0];\/\/\u8bb0\u5f97\u52a0\u4e0a\u524d\u9762\u7684\u6876\u7684\u6570\u91cf<br \/>\n}<br \/>\nelse if (bytes &lt;&#061; 8 * 1024) {<br \/>\nreturn _Index(bytes &#8211; 1024, 7) &#043; group_array[1] &#043; group_array[0];<br \/>\n}<br \/>\nelse if (bytes &lt;&#061; 64 * 1024) {<br \/>\nreturn _Index(bytes &#8211; 8 * 1024, 10) &#043; group_array[2] &#043; group_array[1]<br \/>\n&#043; group_array[0];<br \/>\n}<br \/>\nelse if (bytes &lt;&#061; 256 * 1024) {<br \/>\nreturn _Index(bytes &#8211; 64 * 1024, 13) &#043; group_array[3] &#043;<br \/>\ngroup_array[2] &#043; group_array[1] &#043; group_array[0];<br \/>\n}<br \/>\nelse {<br \/>\nassert(false);<br \/>\n}<br \/>\nreturn -1;<br \/>\n}<br \/>\n\/\/ \u4e00\u6b21thread cache\u4ece\u4e2d\u5fc3\u7f13\u5b58\u83b7\u53d6\u591a\u5c11\u4e2a<br \/>\nstatic size_t NumMoveSize(size_t size) \/\/\u6279\u91cf\u8981\u591a\u4e2a&#xff0c;\u8fd9\u6837\u591a\u4f59\u7684&#xff0c;\u4e0b\u6b21\u5c31\u53ef\u4ee5\u76f4\u63a5\u5728thread cache\u4e2d\u62ff<br \/>\n{<br \/>\nassert(size &gt; 0);<\/p>\n<p>\/\/ [2, 512]&#xff0c;\u4e00\u6b21\u6279\u91cf\u79fb\u52a8\u591a\u5c11\u4e2a\u5bf9\u8c61\u7684(\u6162\u542f\u52a8)\u4e0a\u9650\u503c<br \/>\n\/\/ \u5c0f\u5bf9\u8c61\u4e00\u6b21\u6279\u91cf\u4e0a\u9650\u9ad8<br \/>\n\/\/ \u5927\u5bf9\u8c61\u4e00\u6b21\u6279\u91cf\u4e0a\u9650\u4f4e<br \/>\nsize_t num &#061; MAX_BYTES \/ size;<br \/>\nif (num &lt; 2)<br \/>\nnum &#061; 2;<br \/>\nif (num &gt; 512)<br \/>\nnum &#061; 512;<br \/>\nreturn num;<br \/>\n}<\/p>\n<p>\/\/ \u8ba1\u7b97\u4e00\u6b21\u5411\u7cfb\u7edf\u83b7\u53d6\u51e0\u4e2a\u9875<br \/>\n \/\/ \u5355\u4e2a\u5bf9\u8c61 8byte<br \/>\n \/\/ &#8230;<br \/>\n \/\/ \u5355\u4e2a\u5bf9\u8c61 256KB<br \/>\nstatic size_t NumMovePage(size_t size)<br \/>\n{<br \/>\nsize_t num &#061; NumMoveSize(size);<br \/>\nsize_t npage &#061; num * size;\/\/\u7b97\u51fa\u8981\u591a\u5c11\u4e2a\u5b57\u8282<br \/>\nnpage &gt;&gt;&#061; PAGE_SHIFT;\/\/\u603b\u5b57\u8282\u6570\u9664\u4ee5\u4e00\u9875\u7684\u5b57\u8282\u6570&#xff0c;\u5f97\u51fa\u9700\u8981\u51e0\u9875\u3002<br \/>\nif (npage &#061;&#061; 0)\/\/&#xff0c;\u4e0d\u8db3\u4e00\u9875\u7684&#xff0c;\u6700\u5c11\u4e5f\u9700\u8981\u4e00\u9875<br \/>\nnpage &#061; 1;<br \/>\nreturn npage;<br \/>\n}<\/p>\n<p>};<\/p>\n<p>\/\/\u7ba1\u7406\u591a\u4e2a\u8fde\u7eed\u9875\u5927\u5757\u5185\u5b58\u8de8\u5ea6\u7ed3\u6784<br \/>\nstruct Span<br \/>\n{<br \/>\nPAGE_ID _pageId&#061;0;\/\/\u5927\u5757\u5185\u5b58\u8d77\u59cb\u9875\u7684\u9875\u53f7<br \/>\nsize_t _n&#061;0;\/\/\u9875\u7684\u6570\u91cf<\/p>\n<p>Span* _next&#061;nullptr;\/\/\u53cc\u5411\u94fe\u8868\u7684\u7ed3\u6784<br \/>\nSpan* _prev&#061;nullptr;<\/p>\n<p>size_t _objSize &#061; 0;\/\/\u5207\u597d\u7684\u5c0f\u5bf9\u8c61\u7684\u5927\u5c0f<br \/>\nsize_t _useCount&#061;0;\/\/\u5207\u597d\u7684\u5c0f\u5757\u5185\u5b58&#xff0c;\u88ab\u5206\u914d\u7ed9thread cache\u7684\u8ba1\u6570<br \/>\nvoid* _freeList&#061;nullptr;\/\/\u5207\u597d\u7684\u5c0f\u5757\u5185\u5b58\u7684\u81ea\u7531\u94fe\u8868<\/p>\n<p>bool _isUse&#061;false;\/\/\u662f\u5426\u5728\u88ab\u4f7f\u7528<br \/>\n};<\/p>\n<p>\/\/\u5e26\u5934\u53cc\u5411\u5faa\u73af\u94fe\u8868<br \/>\nclass SpanList<br \/>\n{<br \/>\npublic:<br \/>\nSpanList()<br \/>\n{<br \/>\n_head &#061; new Span;<br \/>\n_head-&gt;_next &#061; _head;<br \/>\n_head-&gt;_prev &#061; _head;<br \/>\n}<\/p>\n<p>Span* Begin()<br \/>\n{<br \/>\nreturn _head-&gt;_next;<br \/>\n}<\/p>\n<p>Span* End()<br \/>\n{<br \/>\nreturn _head;<br \/>\n}<\/p>\n<p>bool Empty()<br \/>\n{<br \/>\nreturn _head-&gt;_next &#061; _head;<br \/>\n}<\/p>\n<p>void PushFront(Span* span)<br \/>\n{<br \/>\nInsert(Begin(), span);<br \/>\n}<\/p>\n<p>Span* PopFront()<br \/>\n{<br \/>\nSpan* front &#061; _head-&gt;_next;<br \/>\nErase(front);<br \/>\nreturn front;<br \/>\n}<\/p>\n<p>void Insert(Span* pos, Span* newSpan)<br \/>\n{<br \/>\nassert(pos);<br \/>\nassert(newSpan);<\/p>\n<p>Span* prev &#061; pos-&gt;_prev;<br \/>\n\/\/prev newspan pos<br \/>\nprev-&gt;_next &#061; newSpan;<br \/>\nnewSpan-&gt;_prev &#061; prev;<br \/>\nnewSpan-&gt;_next &#061; pos;<br \/>\npos-&gt;_prev &#061; newSpan;<br \/>\n} <\/p>\n<p>void Erase(Span* pos)<br \/>\n{<br \/>\nassert(pos);<br \/>\nassert(pos !&#061; _head);<\/p>\n<p>Span* prev &#061; pos-&gt;_prev;<br \/>\nSpan* next &#061; pos-&gt;_next;<\/p>\n<p>prev-&gt;_next &#061; next;<br \/>\nnext-&gt;_prev &#061; prev;<br \/>\n}<\/p>\n<p>private:<br \/>\nSpan* _head;<br \/>\npublic:<br \/>\nstd::mutex _mtx;\/\/\u6876\u9501<br \/>\n}; <\/p>\n<h3>ConcurrentAlloc.h<\/h3>\n<p>#pragma once<\/p>\n<p>#include&#034;Common.h&#034;<br \/>\n#include&#034;ThreadCache.h&#034;<br \/>\n#include&#034;PageCache.h&#034;<br \/>\n#include&#034;ObjectPool.h&#034;<\/p>\n<p>\/\/\u641e\u6210\u9759\u6001\u7684&#xff0c;\u4fdd\u6301\u5728\u5f53\u524d\u6587\u4ef6\u53ef\u89c1\u3002 \u5982\u679c\u5168\u5c40\u7684\u4e0d\u7ed9\u9759\u6001\u7684\u8bdd&#xff0c;\u5728\u591a\u4e2a\u6587\u4ef6\u91cc\u9762&#xff0c;\u94fe\u63a5\u5c5e\u6027\u4f1a\u51b2\u7a81\u3002<br \/>\nstatic void* ConcurrentAlloc(size_t size)<br \/>\n{<\/p>\n<p>if (size&gt;MAX_BYTES)<br \/>\n{<br \/>\nsize_t alignSize &#061; SizeClass::RoundUp(size);<br \/>\nsize_t kpage &#061; alignSize &gt;&gt; PAGE_SHIFT;<\/p>\n<p>PageCache::GetInstance()-&gt;_pageMtx.lock();<br \/>\nSpan* span &#061; PageCache::GetInstance()-&gt;NewSpan(kpage);<br \/>\nspan-&gt;_objSize &#061; size;<br \/>\nPageCache::GetInstance()-&gt;_pageMtx.unlock();<\/p>\n<p>void* ptr &#061; (void*)(span-&gt;_pageId &lt;&lt; PAGE_SHIFT);<br \/>\nreturn ptr;<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\n\/\/\u901a\u8fc7TLS,\u6bcf\u4e2a\u7ebf\u7a0b\u65e0\u9501\u7684\u83b7\u53d6\u81ea\u5df1\u4e13\u5c5e\u7684ThreadCache\u5bf9\u8c61<br \/>\nif (pTLSThreadCache &#061;&#061; nullptr)<br \/>\n{<br \/>\nstatic ObjectPool&lt;ThreadCache&gt; tcPool;<br \/>\n\/\/pTLSThreadCache &#061; new ThreadCache;<br \/>\npTLSThreadCache &#061; tcPool.New();<br \/>\n}<\/p>\n<p>\/\/cout &lt;&lt; std::this_thread::get_id() &lt;&lt; &#034;:&#034; &lt;&lt; pTLSThreadCache &lt;&lt; endl;<\/p>\n<p>return pTLSThreadCache-&gt;Allocate(size);<br \/>\n}<br \/>\n}<\/p>\n<p>static void ConcurrentFree(void* ptr)<br \/>\n{<br \/>\nSpan* span &#061; PageCache::GetInstance()-&gt;MapObjectToSpan(ptr);<br \/>\nsize_t size &#061; span-&gt;_objSize;<\/p>\n<p>if (size &gt; MAX_BYTES)<br \/>\n{<\/p>\n<p>PageCache::GetInstance()-&gt;_pageMtx.lock();<br \/>\nPageCache::GetInstance()-&gt;ReleaseSpanToPageCache(span);<br \/>\nPageCache::GetInstance()-&gt;_pageMtx.unlock();<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\nassert(pTLSThreadCache);<br \/>\npTLSThreadCache-&gt;Deallocate(ptr, size);<br \/>\n}<br \/>\n} <\/p>\n<h3>ObjectPool.h<\/h3>\n<p>#pragma once<\/p>\n<p>#include&#034;Common.h&#034;<\/p>\n<p>template&lt;class T&gt;<br \/>\nclass ObjectPool<br \/>\n{<br \/>\npublic:<\/p>\n<p>T* New()<br \/>\n{<br \/>\nT* obj &#061; nullptr;<\/p>\n<p>\/\/\u4f18\u5148\u628a\u8fd8\u56de\u6765\u7684\u5185\u5b58\u5757\u5bf9\u8c61&#xff0c;\u518d\u6b21\u91cd\u590d\u5229\u7528<br \/>\nif (_freeList)<br \/>\n{<br \/>\nvoid* next &#061; *((void**)_freeList); \/\/next\u662f\u81ea\u7531\u94fe\u8868\u7684\u7b2c\u4e00\u4e2a\u8282\u70b9\u7684\u5934\u56db\/\u516b\u4e2a\u5b57\u8282&#xff0c;\u5b83\u5b58\u653e\u4e0b\u4e00\u4e2a\u8282\u70b9\u7684\u5730\u5740<br \/>\nobj &#061; (T*)_freeList;\/\/obj\u5c31\u662f\u7b2c\u4e00\u5757\u5185\u5b58\u5757<br \/>\n_freeList &#061; next;\/\/\u518d\u8ba9next\u53d8\u6210\u7b2c\u4e00\u4e2a\u8282\u70b9<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\nif (_remainBytes &lt; sizeof(T))\/\/\u5982\u679c\u5269\u4f59\u5185\u5b58\u5c0f\u4e8e\u6240\u9700\u5185\u5b58&#xff0c;\u5c31\u53bb\u7533\u8bf7\u5927\u5757\u7a7a\u95f4<br \/>\n{<br \/>\n_remainBytes &#061; 128 * 1024;<br \/>\n_memory &#061; (char*)malloc(_remainBytes);<br \/>\nif (_memory &#061;&#061; nullptr)<br \/>\n{<br \/>\nthrow std::bad_alloc();<br \/>\n}<br \/>\n}<\/p>\n<p>obj &#061; (T*)_memory;\/\/\u628a\u5927\u5757\u5185\u5b58\u5207\u6210\u5c0f\u5757&#xff0c;\u5206\u51fa\u53bb<br \/>\nsize_t objSize &#061; sizeof(T) &lt; sizeof(void*) ? sizeof(void*) : sizeof(T);\/\/\u4fdd\u8bc1\u81f3\u5c11\u80fd\u5b58\u653e\u4e0b\u4e00\u4e2a\u5bf9\u8c61\u7684\u5730\u5740<br \/>\n_memory &#043;&#061; objSize;<br \/>\n_remainBytes -&#061; objSize;<br \/>\n}<\/p>\n<p>\/\/malloc\u53ea\u5f00\u4e86\u7a7a\u95f4&#xff0c;\u6ca1\u6709\u521d\u59cb\u5316&#xff0c;\u8981\u8fdb\u884c\u521d\u59cb\u5316<br \/>\n\/\/\u5b9a\u4f4dnew&#xff0c;\u663e\u793a\u8c03\u7528T\u7684\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316<br \/>\nnew(obj)T;<\/p>\n<p>return obj;<br \/>\n}<\/p>\n<p>void Delete(T* obj)<br \/>\n{<br \/>\n\/\/if (_freeList &#061;&#061; nullptr)<br \/>\n\/\/{<br \/>\n\/\/_freeList &#061; obj;<br \/>\n\/\/\/\/*(int*)obj&#061;nullptr;    \u5f3a\u8f6c\u6210int*\u7c7b\u578b&#xff0c;\u6b64\u65f6\u53ea\u80fd\u572832\u4f4d\u673a\u5668\u4e0a\u8dd1&#xff0c;\u56e0\u4e3a64\u4f4d\u673a\u5668\u4e0a&#xff0c;\u6307\u9488\u5927\u5c0f\u662f8\u5b57\u8282\u3002<br \/>\n\/\/*(void**)obj &#061; nullptr; \/\/\u8fd9\u91cc\u4f7f\u7528\u4e8c\u7ea7\u6307\u9488\u5373\u53ef\u89e3\u51b3&#xff0c;void**\u89e3\u5f15\u7528\u540e&#xff0c;\u5c31\u662fvoid*&#xff0c;\u6b64\u65f6\u8fd8\u662f\u6307\u9488&#xff0c;\u572832\u4f4d\u673a\u5668\u4e0a\u5c31\u662f4\u5b57\u8282&#xff0c;\u572864\u4f4d\u673a\u5668\u4e0a\u5c31\u662f8\u5b57\u8282&#xff0c;\u53ef\u4ee5\u81ea\u52a8\u9002\u5e94<br \/>\n\/\/}<br \/>\n\/\/else<br \/>\n\/\/{<br \/>\n\/\/\/\/\u5934\u63d2<br \/>\n\/\/*(void**)obj &#061; _freeList; \/\/\u5148\u8ba9\u5c0f\u5185\u5b58\u5757\u6307\u5411\u81ea\u7531\u94fe\u8868\u7684\u7b2c\u4e00\u4e2a\u8282\u70b9&#xff0c;\u56e0\u4e3afreeList\u6307\u5411\u7684\u5c31\u662f\u7b2c\u4e00\u4e2a\u8282\u70b9\u3002<br \/>\n\/\/_freeList &#061; obj;    \/\/\u63a5\u7740\u518d\u8ba9obj\u53d8\u6210\u7b2c\u4e00\u4e2a\u8282\u70b9&#xff0c;\u8ba9freelist\u6307\u5411boj\u3002<br \/>\n\/\/}<\/p>\n<p>\/\/\u663e\u793a\u8c03\u7528\u6790\u6784\u51fd\u6570\u6e05\u7406\u5bf9\u8c61<br \/>\nobj-&gt;~T();<\/p>\n<p>\/\/\u5934\u63d2<br \/>\n*(void**)obj &#061; _freeList;  \/\/\u5b9e\u9645\u4e0a\u4e0d\u9700\u8981\u533a\u5206\u60c5\u51b5&#xff0c;\u56e0\u4e3a\u7a7a\u7684\u65f6\u5019&#xff0c;\u76f4\u63a5\u5934\u63d2\u4e5f\u53ef\u4ee5<br \/>\n_freeList &#061; obj; <\/p>\n<p>}<\/p>\n<p>private:<br \/>\nchar* _memory&#061;nullptr;\/\/\u56e0\u4e3a\u4e00\u4e2achar\u662f\u4e00\u4e2a\u5b57\u8282&#xff0c;\u5207\u5185\u5b58\u5757\u65f6\u597d\u7528\u3002\u6307\u5411\u5927\u5757\u5185\u5b58\u7684\u6307\u9488<br \/>\nsize_t _remainBytes &#061; 0;\/\/\u5927\u5757\u5185\u5b58\u5728\u5207\u5206\u8fc7\u7a0b\u4e2d\u5269\u4f59\u5b57\u8282\u6570<br \/>\nvoid* _freeList&#061;nullptr;\/\/\u8fd8\u56de\u6765\u8fc7\u7a0b\u4e2d\u94fe\u63a5\u7684\u81ea\u7531\u94fe\u8868\u7684\u5934\u6307\u9488<br \/>\n};<\/p>\n<p>\/\/struct TreeNode<br \/>\n\/\/{<br \/>\n\/\/int _val;<br \/>\n\/\/TreeNode* _left;<br \/>\n\/\/TreeNode* _right;<br \/>\n\/\/TreeNode()<br \/>\n\/\/:_val(0)<br \/>\n\/\/, _left(nullptr)<br \/>\n\/\/, _right(nullptr)<br \/>\n\/\/{}<br \/>\n\/\/};<br \/>\n\/\/void TestObjectPool()<br \/>\n\/\/{<br \/>\n\/\/\/\/ \u7533\u8bf7\u91ca\u653e\u7684\u8f6e\u6b21<br \/>\n\/\/const size_t Rounds &#061; 3;<br \/>\n\/\/\/\/ \u6bcf\u8f6e\u7533\u8bf7\u91ca\u653e\u591a\u5c11\u6b21<br \/>\n\/\/const size_t N &#061; 100000;<br \/>\n\/\/std::vector&lt;TreeNode*&gt; v1;<br \/>\n\/\/v1.reserve(N);<br \/>\n\/\/<br \/>\n\/\/size_t begin1 &#061; clock();<br \/>\n\/\/for (size_t j &#061; 0; j &lt; Rounds; &#043;&#043;j)<br \/>\n\/\/{<br \/>\n\/\/for (int i &#061; 0; i &lt; N; &#043;&#043;i)<br \/>\n\/\/{<br \/>\n\/\/v1.push_back(new TreeNode);<br \/>\n\/\/}<br \/>\n\/\/for (int i &#061; 0; i &lt; N; &#043;&#043;i)<br \/>\n\/\/{<br \/>\n\/\/delete v1[i];<br \/>\n\/\/}<br \/>\n\/\/v1.clear();<br \/>\n\/\/}<br \/>\n\/\/size_t end1 &#061; clock();<br \/>\n\/\/std::vector&lt;TreeNode*&gt; v2;<br \/>\n\/\/v2.reserve(N);<br \/>\n\/\/ObjectPool&lt;TreeNode&gt; TNPool;<br \/>\n\/\/size_t begin2 &#061; clock();<br \/>\n\/\/<br \/>\n\/\/for (size_t j &#061; 0; j &lt; Rounds; &#043;&#043;j)<br \/>\n\/\/{<br \/>\n\/\/for (int i &#061; 0; i &lt; N; &#043;&#043;i)<br \/>\n\/\/{<br \/>\n\/\/v2.push_back(TNPool.New());<br \/>\n\/\/}<br \/>\n\/\/for (int i &#061; 0; i &lt; N; &#043;&#043;i)<br \/>\n\/\/{<br \/>\n\/\/TNPool.Delete(v2[i]);<br \/>\n\/\/}<br \/>\n\/\/v2.clear();<br \/>\n\/\/}<br \/>\n\/\/size_t end2 &#061; clock();<br \/>\n\/\/cout &lt;&lt; &#034;new cost time:&#034; &lt;&lt; end1 &#8211; begin1 &lt;&lt; endl;<br \/>\n\/\/cout &lt;&lt; &#034;object pool cost time:&#034; &lt;&lt; end2 &#8211; begin2 &lt;&lt; endl;<br \/>\n\/\/}<\/p>\n<h3>PageCache.h<\/h3>\n<p>#pragma once<\/p>\n<p>#include&#034;Common.h&#034;<br \/>\n#include&#034;ObjectPool.h&#034;<br \/>\n#include&#034;PageMap.h&#034;<\/p>\n<p>class PageCache<br \/>\n{<br \/>\npublic:<br \/>\nstatic PageCache* GetInstance()<br \/>\n{<br \/>\nreturn &amp;_sInst;<br \/>\n}<\/p>\n<p>\/\/ \u83b7\u53d6\u4ece\u5bf9\u8c61\u5230span\u7684\u6620\u5c04<br \/>\nSpan* MapObjectToSpan(void* obj);<\/p>\n<p>\/\/ \u91ca\u653e\u7a7a\u95f2span\u56de\u5230Pagecache&#xff0c;\u5e76\u5408\u5e76\u76f8\u90bb\u7684span<br \/>\nvoid ReleaseSpanToPageCache(Span* span);<\/p>\n<p>\/\/ \u83b7\u53d6\u4e00\u4e2aK\u9875\u7684span<br \/>\nSpan* NewSpan(size_t k);<\/p>\n<p>std::mutex _pageMtx;<\/p>\n<p>private:<br \/>\nSpanList _spanLists[NPAGES];<br \/>\nObjectPool&lt;Span&gt; _spanPool;<\/p>\n<p>\/\/std::unordered_map&lt;PAGE_ID, Span*&gt; _idSpanMap;<br \/>\nTCMalloc_PageMap1&lt;32 &#8211; PAGE_SHIFT&gt; _idSpanMap;<\/p>\n<p>PageCache()<br \/>\n{}<\/p>\n<p>PageCache(const PageCache&amp;) &#061; delete;<\/p>\n<p>static PageCache _sInst;<\/p>\n<p>}; <\/p>\n<h3>PageMap.h<\/h3>\n<p>#pragma once<\/p>\n<p>#include&#034;Common.h&#034;<\/p>\n<p>\/\/ Single-level array<br \/>\ntemplate &lt;int BITS&gt;<br \/>\nclass TCMalloc_PageMap1 {<br \/>\nprivate:<br \/>\nstatic const int LENGTH &#061; 1 &lt;&lt; BITS;<br \/>\nvoid** array_;<br \/>\npublic:<br \/>\ntypedef uintptr_t Number;<br \/>\n\/\/explicit TCMalloc_PageMap1(void* (*allocator)(size_t)) {<br \/>\nexplicit TCMalloc_PageMap1() {<br \/>\n\/\/array_ &#061; reinterpret_cast&lt;void**&gt;((*allocator)(sizeof(void*) &lt;&lt; BITS));<br \/>\nsize_t size &#061; sizeof(void*) &lt;&lt; BITS;<br \/>\nsize_t alignSize &#061; SizeClass::_RoundUp(size, 1 &lt;&lt; PAGE_SHIFT);<br \/>\narray_ &#061; (void**)SystemAlloc(alignSize&gt;&gt;PAGE_SHIFT);<br \/>\nmemset(array_, 0, sizeof(void*) &lt;&lt; BITS);<br \/>\n}<br \/>\n\/\/ Return the current value for KEY. Returns NULL if not yet set,<br \/>\n\/\/ or if k is out of range.<br \/>\nvoid* get(Number k) const {<br \/>\nif ((k &gt;&gt; BITS) &gt; 0) {<br \/>\nreturn NULL;<br \/>\n}<br \/>\nreturn array_[k];<br \/>\n}<br \/>\n\/\/ REQUIRES &#034;k&#034; is in range &#034;[0,2^BITS-1]&#034;.<br \/>\n\/\/ REQUIRES &#034;k&#034; has been ensured before.<br \/>\n\/\/<br \/>\n\/\/ Sets the value &#039;v&#039; for key &#039;k&#039;.<br \/>\nvoid set(Number k, void* v) {<br \/>\narray_[k] &#061; v;<br \/>\n}<br \/>\n};<br \/>\n\/\/ Two-level radix tree<br \/>\ntemplate &lt;int BITS&gt;<br \/>\nclass TCMalloc_PageMap2 {<br \/>\nprivate:<br \/>\n\/\/ Put 32 entries in the root and (2^BITS)\/32 entries in each leaf.<br \/>\nstatic const int ROOT_BITS &#061; 5;<br \/>\nstatic const int ROOT_LENGTH &#061; 1 &lt;&lt; ROOT_BITS;<br \/>\nstatic const int LEAF_BITS &#061; BITS &#8211; ROOT_BITS;<br \/>\nstatic const int LEAF_LENGTH &#061; 1 &lt;&lt; LEAF_BITS;<br \/>\n\/\/ Leaf node<br \/>\nstruct Leaf {<br \/>\nvoid* values[LEAF_LENGTH];<br \/>\n};<br \/>\nLeaf* root_[ROOT_LENGTH];             \/\/ Pointers to 32 child nodes<br \/>\nvoid* (*allocator_)(size_t);          \/\/ Memory allocator<br \/>\npublic:<br \/>\ntypedef uintptr_t Number;<br \/>\n\/\/explicit TCMalloc_PageMap2(void* (*allocator)(size_t)) {<br \/>\nexplicit TCMalloc_PageMap2() {<br \/>\n\/\/allocator_ &#061; allocator;<br \/>\nmemset(root_, 0, sizeof(root_));<\/p>\n<p>PreallocateMoreMemory();<br \/>\n}<br \/>\nvoid* get(Number k) const {<br \/>\nconst Number i1 &#061; k &gt;&gt; LEAF_BITS;<br \/>\nconst Number i2 &#061; k &amp; (LEAF_LENGTH &#8211; 1);<br \/>\nif ((k &gt;&gt; BITS) &gt; 0 || root_[i1] &#061;&#061; NULL) {<br \/>\nreturn NULL;<br \/>\n}<br \/>\nreturn root_[i1]-&gt;values[i2];<br \/>\n}<br \/>\nvoid set(Number k, void* v) {<br \/>\nconst Number i1 &#061; k &gt;&gt; LEAF_BITS;<br \/>\nconst Number i2 &#061; k &amp; (LEAF_LENGTH &#8211; 1);<br \/>\nASSERT(i1 &lt; ROOT_LENGTH);<br \/>\nroot_[i1]-&gt;values[i2] &#061; v;<br \/>\n}<br \/>\nbool Ensure(Number start, size_t n) {<br \/>\nfor (Number key &#061; start; key &lt;&#061; start &#043; n &#8211; 1;) {<br \/>\nconst Number i1 &#061; key &gt;&gt; LEAF_BITS;<br \/>\n\/\/ Check for overflow<br \/>\nif (i1 &gt;&#061; ROOT_LENGTH)<br \/>\nreturn false;<br \/>\n\/\/ Make 2nd level node if necessary<br \/>\nif (root_[i1] &#061;&#061; NULL) {<br \/>\n\/\/Leaf* leaf &#061; reinterpret_cast&lt;Leaf*&gt;((*allocator_)(sizeof(Leaf)));<br \/>\n\/\/if (leaf &#061;&#061; NULL) return false;<br \/>\nstatic ObjectPool&lt;Leaf&gt; leafPool;<br \/>\nLeaf* leaf &#061; (Leaf*)leafPool.New();<\/p>\n<p>memset(leaf, 0, sizeof(*leaf));<br \/>\nroot_[i1] &#061; leaf;<br \/>\n}<br \/>\n\/\/ Advance key past whatever is covered by this leaf node<br \/>\nkey &#061; ((key &gt;&gt; LEAF_BITS) &#043; 1) &lt;&lt; LEAF_BITS;<br \/>\n}<br \/>\nreturn true;<br \/>\n}<br \/>\nvoid PreallocateMoreMemory() {<br \/>\n\/\/ Allocate enough to keep track of all possible pages<br \/>\nEnsure(0, 1 &lt;&lt; BITS);<br \/>\n}<br \/>\n};<br \/>\n\/\/ Three-level radix tree<br \/>\ntemplate &lt;int BITS&gt;<br \/>\nclass TCMalloc_PageMap3 {<br \/>\nprivate:<br \/>\n\/\/ How many bits should we consume at each interior level<br \/>\nstatic const int INTERIOR_BITS &#061; (BITS &#043; 2) \/ 3; \/\/ Round-up<br \/>\nstatic const int INTERIOR_LENGTH &#061; 1 &lt;&lt; INTERIOR_BITS;<br \/>\n\/\/ How many bits should we consume at leaf level<br \/>\nstatic const int LEAF_BITS &#061; BITS &#8211; 2 * INTERIOR_BITS;<br \/>\nstatic const int LEAF_LENGTH &#061; 1 &lt;&lt; LEAF_BITS;<br \/>\n\/\/ Interior node<br \/>\nstruct Node {<br \/>\nNode* ptrs[INTERIOR_LENGTH];<br \/>\n};<br \/>\n\/\/ Leaf node<br \/>\nstruct Leaf {<br \/>\nvoid* values[LEAF_LENGTH];<br \/>\n};<br \/>\nNode* root_;                          \/\/ Root of radix tree<br \/>\nvoid* (*allocator_)(size_t);          \/\/ Memory allocator<br \/>\nNode* NewNode() {<br \/>\nNode* result &#061; reinterpret_cast&lt;Node*&gt;((*allocator_)(sizeof(Node)));<br \/>\nif (result !&#061; NULL) {<br \/>\nmemset(result, 0, sizeof(*result));<br \/>\n}<br \/>\nreturn result;<br \/>\n}<br \/>\npublic:<br \/>\ntypedef uintptr_t Number;<br \/>\nexplicit TCMalloc_PageMap3(void* (*allocator)(size_t)) {<br \/>\nallocator_ &#061; allocator;<br \/>\nroot_ &#061; NewNode();<br \/>\n}<br \/>\nvoid* get(Number k) const {<br \/>\nconst Number i1 &#061; k &gt;&gt; (LEAF_BITS &#043; INTERIOR_BITS);<br \/>\nconst Number i2 &#061; (k &gt;&gt; LEAF_BITS) &amp; (INTERIOR_LENGTH &#8211; 1);<br \/>\nconst Number i3 &#061; k &amp; (LEAF_LENGTH &#8211; 1);<br \/>\nif ((k &gt;&gt; BITS) &gt; 0 ||<br \/>\nroot_-&gt;ptrs[i1] &#061;&#061; NULL || root_-&gt;ptrs[i1]-&gt;ptrs[i2] &#061;&#061; NULL) {<br \/>\nreturn NULL;<br \/>\n}<br \/>\nreturn reinterpret_cast&lt;Leaf*&gt;(root_-&gt;ptrs[i1]-&gt;ptrs[i2])-&gt;values[i3];<br \/>\n}<br \/>\nvoid set(Number k, void* v) {<br \/>\nASSERT(k &gt;&gt; BITS &#061;&#061; 0);<br \/>\nconst Number i1 &#061; k &gt;&gt; (LEAF_BITS &#043; INTERIOR_BITS);<br \/>\nconst Number i2 &#061; (k &gt;&gt; LEAF_BITS) &amp; (INTERIOR_LENGTH &#8211; 1);<br \/>\nconst Number i3 &#061; k &amp; (LEAF_LENGTH &#8211; 1);<br \/>\nreinterpret_cast&lt;Leaf*&gt;(root_-&gt;ptrs[i1]-&gt;ptrs[i2])-&gt;values[i3] &#061; v;<br \/>\n}<br \/>\nbool Ensure(Number start, size_t n) {<br \/>\nfor (Number key &#061; start; key &lt;&#061; start &#043; n &#8211; 1;) {<br \/>\nconst Number i1 &#061; key &gt;&gt; (LEAF_BITS &#043; INTERIOR_BITS);<br \/>\nconst Number i2 &#061; (key &gt;&gt; LEAF_BITS) &amp; (INTERIOR_LENGTH &#8211; 1);<br \/>\n\/\/ Check for overflow<br \/>\nif (i1 &gt;&#061; INTERIOR_LENGTH || i2 &gt;&#061; INTERIOR_LENGTH)<br \/>\nreturn false;<br \/>\n\/\/ Make 2nd level node if necessary<br \/>\nif (root_-&gt;ptrs[i1] &#061;&#061; NULL) {<br \/>\nNode* n &#061; NewNode();<br \/>\nif (n &#061;&#061; NULL) return false;<br \/>\nroot_-&gt;ptrs[i1] &#061; n;<br \/>\n}<br \/>\n\/\/ Make leaf node if necessary<br \/>\nif (root_-&gt;ptrs[i1]-&gt;ptrs[i2] &#061;&#061; NULL) {<br \/>\nLeaf* leaf &#061; reinterpret_cast&lt;Leaf*&gt;((*allocator_)<br \/>\n(sizeof(Leaf)));<br \/>\nif (leaf &#061;&#061; NULL) return false;<br \/>\nmemset(leaf, 0, sizeof(*leaf));<br \/>\nroot_-&gt;ptrs[i1]-&gt;ptrs[i2] &#061; reinterpret_cast&lt;Node*&gt;(leaf);<br \/>\n}<br \/>\n\/\/ Advance key past whatever is covered by this leaf node<br \/>\nkey &#061; ((key &gt;&gt; LEAF_BITS) &#043; 1) &lt;&lt; LEAF_BITS;<br \/>\n}<br \/>\nreturn true;<br \/>\n}<br \/>\nvoid PreallocateMoreMemory() {<br \/>\n}<br \/>\n}; <\/p>\n<p>\u4f7f\u7528tcmalloc\u6e90\u7801\u4e2d\u5b9e\u73b0\u57fa\u6570\u6811\u8fdb\u884c\u4f18\u5316<\/p>\n<h3>ThreadCache.h<\/h3>\n<p>#pragma once<\/p>\n<p>#include&#034;Common.h&#034;<\/p>\n<p>class ThreadCache<br \/>\n{<br \/>\npublic:<br \/>\n\/\/\u7533\u8bf7\u548c\u91ca\u653e\u5185\u5b58\u5bf9\u8c61<br \/>\nvoid* Allocate(size_t size);<br \/>\nvoid Deallocate(void* ptr, size_t size); <\/p>\n<p>\/\/ \u4ece\u4e2d\u5fc3\u7f13\u5b58\u83b7\u53d6\u5bf9\u8c61<br \/>\nvoid* FetchFromCentralCache(size_t index, size_t size);<\/p>\n<p>\/\/ \u91ca\u653e\u5bf9\u8c61\u65f6&#xff0c;\u94fe\u8868\u8fc7\u957f\u65f6&#xff0c;\u56de\u6536\u5185\u5b58\u56de\u5230\u4e2d\u5fc3\u7f13\u5b58<br \/>\nvoid ListTooLong(FreeList&amp; list, size_t size);<br \/>\nprivate:<br \/>\nFreeList _freeLists[NFREELIST];<br \/>\n};<\/p>\n<p>\/\/TLS thread local storage    \/\/\u6bcf\u4e2a\u7ebf\u7a0b\u8d77\u6765\u540e&#xff0c;\u5c31\u4f1a\u8c03\u7528\u7533\u8bf7\u5185\u5b58\u7684\u63a5\u53e3\u83b7\u5f97thread cache,\u800c\u4e0d\u662f\u76f4\u63a5\u5c31\u6709\u5404\u81ea\u7684thread cache<br \/>\nstatic _declspec(thread) ThreadCache* pTLSThreadCache &#061; nullptr;  \/\/\u8ba9\u53d8\u91cf\u90fd\u53ea\u5728\u5b83\u6240\u5728\u7684\u7ebf\u7a0b\u53ef\u5168\u5c40\u8bbf\u95ee&#xff0c;\u4f46\u4e0d\u80fd\u88ab\u5176\u4ed6\u7ebf\u7a0b\u8bbf\u95ee\u5230\u3002TLS(\u7ebf\u7a0b\u5c40\u90e8\u5b58\u50a8) <\/p>\n<h3>Benchmark.h<\/h3>\n<p>#include&#034;ConcurrentAlloc.h&#034;<\/p>\n<p>\/\/ntimes \u4e00\u8f6e\u7533\u8bf7\u548c\u91ca\u653e\u5185\u5b58\u7684\u6b21\u6570<br \/>\n\/\/rounds \u8f6e\u6b21<br \/>\nvoid BenchmarkMalloc(size_t ntimes, size_t nworks, size_t rounds)<br \/>\n{<br \/>\nstd::vector&lt;std::thread&gt; vthread(nworks);<br \/>\nstd::atomic&lt;size_t&gt; malloc_costtime &#061; 0;<br \/>\nstd::atomic&lt;size_t&gt; free_costtime &#061; 0;<br \/>\nfor (size_t k &#061; 0; k &lt; nworks; &#043;&#043;k)<br \/>\n{<br \/>\nvthread[k] &#061; std::thread([&amp;, k]() {<br \/>\nstd::vector&lt;void*&gt; v;<br \/>\nv.reserve(ntimes);<br \/>\nfor (size_t j &#061; 0; j &lt; rounds; &#043;&#043;j)<br \/>\n{<br \/>\nsize_t begin1 &#061; clock();<br \/>\nfor (size_t i &#061; 0; i &lt; ntimes; i&#043;&#043;)<br \/>\n{<br \/>\nv.push_back(malloc(16));<br \/>\n\/\/v.push_back(malloc((16 &#043; i) % 8192 &#043; 1));<br \/>\n}<br \/>\nsize_t end1 &#061; clock();<br \/>\nsize_t begin2 &#061; clock();<br \/>\nfor (size_t i &#061; 0; i &lt; ntimes; i&#043;&#043;)<br \/>\n{<br \/>\nfree(v[i]);<br \/>\n}<br \/>\nsize_t end2 &#061; clock();<br \/>\nv.clear();<br \/>\nmalloc_costtime &#043;&#061; (end1 &#8211; begin1);<br \/>\nfree_costtime &#043;&#061; (end2 &#8211; begin2);<br \/>\n}<br \/>\n});<br \/>\n}<br \/>\nfor (auto&amp; t : vthread)<br \/>\n{<br \/>\nt.join();<br \/>\n}<br \/>\nprintf(&#034;%u\u4e2a\u7ebf\u7a0b\u5e76\u53d1\u6267\u884c%u\u8f6e\u6b21&#xff0c;\u6bcf\u8f6e\u6b21malloc %u\u6b21: \u82b1\u8d39&#xff1a;%u ms\\\\n&#034;,<br \/>\nnworks, rounds, ntimes, malloc_costtime);<br \/>\nprintf(&#034;%u\u4e2a\u7ebf\u7a0b\u5e76\u53d1\u6267\u884c%u\u8f6e\u6b21&#xff0c;\u6bcf\u8f6e\u6b21free %u\u6b21: \u82b1\u8d39&#xff1a;%u ms\\\\n&#034;,<br \/>\nnworks, rounds, ntimes, free_costtime);<br \/>\nprintf(&#034;%u\u4e2a\u7ebf\u7a0b\u5e76\u53d1malloc&amp;free %u\u6b21&#xff0c;\u603b\u8ba1\u82b1\u8d39&#xff1a;%u ms\\\\n&#034;,<br \/>\nnworks, nworks * rounds * ntimes, malloc_costtime &#043; free_costtime);<br \/>\n}<br \/>\n\/\/ \u5355\u8f6e\u6b21\u7533\u8bf7\u91ca\u653e\u6b21\u6570 \u7ebf\u7a0b\u6570 \u8f6e\u6b21<br \/>\nvoid BenchmarkConcurrentMalloc(size_t ntimes, size_t nworks, size_t rounds)<br \/>\n{<br \/>\nstd::vector&lt;std::thread&gt; vthread(nworks);<br \/>\nstd::atomic&lt;size_t&gt; malloc_costtime &#061; 0;<br \/>\nstd::atomic&lt;size_t&gt; free_costtime &#061; 0;<br \/>\nfor (size_t k &#061; 0; k &lt; nworks; &#043;&#043;k)<br \/>\n{<br \/>\nvthread[k] &#061; std::thread([&amp;]() {<br \/>\nstd::vector&lt;void*&gt; v;<br \/>\nv.reserve(ntimes);<br \/>\nfor (size_t j &#061; 0; j &lt; rounds; &#043;&#043;j)<br \/>\n{<br \/>\nsize_t begin1 &#061; clock();<br \/>\nfor (size_t i &#061; 0; i &lt; ntimes; i&#043;&#043;)<br \/>\n{<br \/>\nv.push_back(ConcurrentAlloc(16));<br \/>\n\/\/v.push_back(ConcurrentAlloc((16 &#043; i) % 8192 &#043; 1));<br \/>\n}<br \/>\nsize_t end1 &#061; clock();<br \/>\nsize_t begin2 &#061; clock();<br \/>\nfor (size_t i &#061; 0; i &lt; ntimes; i&#043;&#043;)<br \/>\n{<br \/>\nConcurrentFree(v[i]);<br \/>\n}<br \/>\nsize_t end2 &#061; clock();<br \/>\nv.clear();<br \/>\nmalloc_costtime &#043;&#061; (end1 &#8211; begin1);<br \/>\nfree_costtime &#043;&#061; (end2 &#8211; begin2);<br \/>\n}<br \/>\n});<br \/>\n}<br \/>\nfor (auto&amp; t : vthread)<br \/>\n{<br \/>\nt.join();<br \/>\n}<br \/>\nprintf(&#034;%u\u4e2a\u7ebf\u7a0b\u5e76\u53d1\u6267\u884c%u\u8f6e\u6b21&#xff0c;\u6bcf\u8f6e\u6b21concurrent alloc %u\u6b21: \u82b1\u8d39&#xff1a;%u ms\\\\n&#034;,<br \/>\nnworks, rounds, ntimes, malloc_costtime);<br \/>\nprintf(&#034;%u\u4e2a\u7ebf\u7a0b\u5e76\u53d1\u6267\u884c%u\u8f6e\u6b21&#xff0c;\u6bcf\u8f6e\u6b21concurrent dealloc %u\u6b21: \u82b1\u8d39&#xff1a;%u ms\\\\n&#034;,<br \/>\nnworks, rounds, ntimes, free_costtime);<br \/>\nprintf(&#034;%u\u4e2a\u7ebf\u7a0b\u5e76\u53d1concurrent alloc&amp;dealloc %u\u6b21&#xff0c;\u603b\u8ba1\u82b1\u8d39&#xff1a;%u ms\\\\n&#034;,<br \/>\nnworks, nworks * rounds * ntimes, malloc_costtime &#043; free_costtime);<br \/>\n}<\/p>\n<p>int main()<br \/>\n{<br \/>\nsize_t n &#061; 10000;<br \/>\ncout &lt;&lt; &#034;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#034; &lt;&lt;<br \/>\nendl;<br \/>\nBenchmarkConcurrentMalloc(n, 4, 10);<br \/>\ncout &lt;&lt; endl &lt;&lt; endl;<br \/>\nBenchmarkMalloc(n, 4, 10);<br \/>\ncout &lt;&lt; &#034;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#034; &lt;&lt;<br \/>\nendl;<br \/>\nreturn 0;<br \/>\n} <\/p>\n<p>\u591a\u7ebf\u7a0b\u5e76\u53d1\u73af\u5883\u4e0b&#xff0c;\u5bf9\u6bd4malloc\u548cConcurrentAlloc\u7533\u8bf7\u548c\u91ca\u653e\u5185\u5b58\u6548\u7387\u5bf9\u6bd4<\/p>\n<h3>CentralCache.cpp<\/h3>\n<p>#include&#034;CentralCache.h&#034;<br \/>\n#include&#034;PageCache.h&#034;<\/p>\n<p>CentralCache CentralCache::_sInst;<\/p>\n<p>\/\/ \u83b7\u53d6\u4e00\u4e2a\u975e\u7a7a\u7684span<br \/>\nSpan* CentralCache::GetOneSpan(SpanList&amp; list, size_t size)<br \/>\n{<br \/>\n\/\/\u67e5\u770b\u5f53\u524d\u7684spanlist\u4e2d\u662f\u5426\u8fd8\u6709\u672a\u5206\u914d\u5bf9\u8c61\u7684span<br \/>\nSpan* it &#061; list.Begin();<br \/>\nwhile (it !&#061; list.End())<br \/>\n{<br \/>\nif (it-&gt;_freeList !&#061; nullptr)<br \/>\n{<br \/>\nreturn it;<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\nit &#061; it-&gt;_next;<br \/>\n}<br \/>\n}<\/p>\n<p>\/\/\u5148\u628acentral cache\u7684\u6876\u9501\u89e3\u6389&#xff0c;\u8fd9\u6837\u5982\u679c\u5176\u4ed6\u7ebf\u7a0b\u91ca\u653e\u5185\u5b58\u5bf9\u8c61\u56de\u6765&#xff0c;\u4e0d\u4f1a\u963b\u585e<br \/>\nlist._mtx.unlock();<\/p>\n<p>\/\/\u8d70\u5230\u8fd9\u91cc\u8bf4\u660e\u6ca1\u6709\u7a7a\u95f2\u7684span\u4e86&#xff0c;\u53ea\u80fd\u627epage cache\u8981<br \/>\nPageCache::GetInstance()-&gt;_pageMtx.lock();<br \/>\nSpan* span &#061; PageCache::GetInstance()-&gt;NewSpan(SizeClass::NumMovePage(size));<br \/>\nspan-&gt;_isUse &#061; true;<br \/>\nspan-&gt;_objSize &#061; size;<br \/>\nPageCache::GetInstance()-&gt;_pageMtx.unlock();<\/p>\n<p>\/\/\u5bf9\u83b7\u53d6span\u8fdb\u884c\u5207\u5206&#xff0c;\u4e0d\u9700\u8981\u52a0\u9501&#xff0c;\u56e0\u4e3a\u8fd9\u65f6\u5019\u5176\u4ed6\u7ebf\u7a0b\u8bbf\u95ee\u4e0d\u5230\u8fd9\u4e2aspan<\/p>\n<p>\/\/\u8ba1\u7b97span\u7684\u5927\u5757\u5185\u5b58\u7684\u8d77\u59cb\u5730\u5740\u548c\u5927\u5757\u5185\u5b58\u7684\u5927\u5c0f&#xff08;\u5b57\u8282\u6570&#xff09;<br \/>\nchar* start &#061; (char*)(span-&gt;_pageId &lt;&lt; PAGE_SHIFT);<br \/>\nsize_t bytes &#061; span-&gt;_n &lt;&lt; PAGE_SHIFT;<br \/>\nchar* end &#061; start &#043; bytes;<\/p>\n<p>\/\/\u628a\u5927\u5757\u5185\u5b58\u5207\u6210\u81ea\u7531\u94fe\u8868\u94fe\u63a5\u8d77\u6765<br \/>\n\/\/1.\u5148\u5207\u4e00\u5757\u4e0b\u6765\u53bb\u505a\u5934&#xff0c;\u65b9\u4fbf\u5c3e\u63d2<br \/>\nspan-&gt;_freeList &#061; start;<br \/>\nstart &#043;&#061; size;<br \/>\nvoid* tail &#061; span-&gt;_freeList;<\/p>\n<p>int i &#061; 1;<br \/>\nwhile (start &lt; end)<br \/>\n{<br \/>\n&#043;&#043;i;<br \/>\nNextObj(tail) &#061; start;<br \/>\ntail &#061; NextObj(tail);\/\/ \u6216\u8005tail&#061;start<br \/>\nstart &#043;&#061; size;<br \/>\n}<\/p>\n<p>NextObj(tail) &#061; nullptr; <\/p>\n<p>\/\/\u5207\u597dspan\u4ee5\u540e&#xff0c;\u9700\u8981\u628aspan\u6302\u5230\u6876\u91cc\u53bb\u7684\u65f6\u5019&#xff0c;\u518d\u52a0\u9501<br \/>\nlist._mtx.lock();<br \/>\nlist.PushFront(span);<\/p>\n<p>return span;<br \/>\n}<\/p>\n<p>\/\/ \u4ece\u4e2d\u5fc3\u7f13\u5b58\u83b7\u53d6\u4e00\u5b9a\u6570\u91cf\u7684\u5bf9\u8c61\u7ed9thread cache<br \/>\nsize_t CentralCache::FetchRangeObj(void*&amp; start, void*&amp; end, size_t batchNum, size_t size)<br \/>\n{<br \/>\nsize_t index &#061; SizeClass::Index(size);\/\/\u627e\u51fa\u662f\u54ea\u4e2a\u6876\u7684<br \/>\n_spanLists[index]._mtx.lock(); <\/p>\n<p>Span* span &#061; GetOneSpan(_spanLists[index], size);<br \/>\nassert(span);<br \/>\nassert(span-&gt;_freeList);<\/p>\n<p>\/\/\u4ecespan\u4e2d\u83b7\u53d6batchNum\u4e2a\u5bf9\u8c61<br \/>\n\/\/\u5982\u679c\u4e0d\u591fbatchNum\u4e2a&#xff0c;\u6709\u591a\u5c11\u62ff\u591a\u5c11<br \/>\nstart &#061; span-&gt;_freeList;<br \/>\nend &#061; start;<br \/>\nsize_t i &#061; 0;<br \/>\nsize_t actualNum &#061; 1;\/\/\u56e0\u4e3a\u662f\u5c11\u8d70\u4e00\u6b65\u7684&#xff0c;\u6240\u4ee5\u8d77\u59cb\u5c31\u5df2\u7ecf\u6709\u4e00\u4e2a\u4e86<br \/>\nwhile(i &lt; batchNum &#8211; 1 &amp;&amp; NextObj(end)!&#061;nullptr)<br \/>\n{<br \/>\nend &#061; NextObj(end);<br \/>\ni&#043;&#043;;<br \/>\n&#043;&#043;actualNum;<br \/>\n}<br \/>\nspan-&gt;_freeList &#061; NextObj(end);<br \/>\nNextObj(end) &#061; nullptr;<br \/>\nspan-&gt;_useCount &#043;&#061; actualNum;<\/p>\n<p>_spanLists[index]._mtx.unlock();<br \/>\nreturn actualNum;<br \/>\n}<\/p>\n<p>void CentralCache::ReleaseListToSpans(void* start, size_t size)<br \/>\n{<br \/>\nsize_t index &#061; SizeClass::Index(size);\/\/\u6839\u636esize\u7b97\u51fa\u662f\u54ea\u4e2a\u6876\u7684<br \/>\n_spanLists[index]._mtx.lock();<\/p>\n<p>while (start)<br \/>\n{<br \/>\nvoid* next &#061; NextObj(start);<\/p>\n<p>Span* span &#061; PageCache::GetInstance()-&gt;MapObjectToSpan(start);\/\/\u627e\u5230\u5bf9\u5e94\u7684span<br \/>\nNextObj(start) &#061; span-&gt;_freeList;<br \/>\nspan-&gt;_freeList &#061; start;<br \/>\nspan-&gt;_useCount&#8211;;<\/p>\n<p>\/\/&#061;&#061;0&#xff0c;\u8bf4\u660espan\u5207\u5206\u51fa\u53bb\u7684\u6240\u6709\u5c0f\u5757\u5185\u5b58\u90fd\u56de\u6765\u4e86<br \/>\n\/\/\u8fd9\u4e2aspan\u5c31\u53ef\u4ee5\u518d\u56de\u6536\u7ed9page cache&#xff0c;pagecache\u5c31\u53ef\u4ee5\u518d\u5c1d\u8bd5\u53bb\u505a\u524d\u540e\u9875\u7684\u5408\u5e76<br \/>\nif (span-&gt;_useCount &#061;&#061; 0)<br \/>\n{<br \/>\n_spanLists[index].Erase(span);\/\/\u628a\u6ee1\u4e86\u7684span\u4ece\u94fe\u8868\u91cc\u62ff\u51fa\u6765<br \/>\nspan-&gt;_freeList &#061; nullptr;\/\/\u867d\u7136\u8fd9\u4e2aspan\u91cc\u7684\u5c0f\u5757\u5185\u5b58\u5df2\u7ecf\u65e0\u5e8f&#xff0c;\u4f46\u662f\u6211\u4eec\u5df2\u7ecf\u77e5\u9053\u8d77\u59cb\u548c\u7ec8\u6b62\u5730\u5740&#xff0c;\u6240\u4ee5\u53ef\u4ee5\u7f6e\u7a7a<br \/>\nspan-&gt;_next &#061; nullptr;<br \/>\nspan-&gt;_prev &#061; nullptr;<\/p>\n<p>\/\/\u91ca\u653espan\u7ed9page cache\u65f6&#xff0c;\u4f7f\u7528page cache\u7684\u9501\u5373\u53ef<br \/>\n\/\/\u8fd9\u65f6\u8981\u628a\u6876\u9501\u89e3\u6389<br \/>\n_spanLists[index]._mtx.unlock();<\/p>\n<p>PageCache::GetInstance()-&gt;_pageMtx.lock();<br \/>\nPageCache::GetInstance()-&gt;ReleaseSpanToPageCache(span);<br \/>\nPageCache::GetInstance()-&gt;_pageMtx.unlock(); <\/p>\n<p>_spanLists[index]._mtx.lock();<br \/>\n}<\/p>\n<p>start &#061; next;<br \/>\n}<\/p>\n<p>_spanLists[index]._mtx.unlock();<\/p>\n<p>} <\/p>\n<h3>PageCache.cpp<\/h3>\n<p>#include&#034;PageCache.h&#034;<\/p>\n<p>PageCache PageCache::_sInst;<\/p>\n<p>\/\/ \u83b7\u53d6\u4e00\u4e2aK\u9875\u7684span<br \/>\nSpan* PageCache::NewSpan(size_t k)<br \/>\n{<br \/>\nassert(k &gt; 0);<\/p>\n<p>\/\/\u5927\u4e8e128 page\u7684\u76f4\u63a5\u5411\u5806\u7533\u8bf7<br \/>\nif (k &gt; NPAGES &#8211; 1)<br \/>\n{<br \/>\nvoid* ptr &#061; SystemAlloc(k);<br \/>\n\/\/Span* span &#061; new Span;<br \/>\nSpan* span &#061; _spanPool.New();<br \/>\nspan-&gt;_pageId &#061; (PAGE_ID)ptr &gt;&gt; PAGE_SHIFT;<br \/>\nspan-&gt;_n &#061; k;<\/p>\n<p>\/\/_idSpanMap[span-&gt;_pageId] &#061; span;<br \/>\n_idSpanMap.set(span-&gt;_pageId, span);<\/p>\n<p>return span;<br \/>\n}<\/p>\n<p>\/\/\u5148\u68c0\u67e5\u7b2ck\u4e2a\u6876\u91cc\u9762\u6709\u6ca1\u6709span<br \/>\nif (!_spanLists[k].Empty())<br \/>\n{<br \/>\nSpan* kSpan &#061; _spanLists[k].PopFront();<br \/>\n\/\/\u5efa\u7acbid\u548cspan\u7684\u6620\u5c04&#xff0c;\u65b9\u4fbfcentral cache\u56de\u6536\u5c0f\u5757\u5185\u5b58\u65f6&#xff0c;\u67e5\u627e\u5bf9\u5e94\u7684span<br \/>\nfor (PAGE_ID i &#061; 0; i &lt; kSpan-&gt;_n; &#043;&#043;i)<br \/>\n{<br \/>\n\/\/_idSpanMap[kSpan-&gt;_pageId &#043; i] &#061; kSpan;<br \/>\n_idSpanMap.set(kSpan-&gt;_pageId &#043; i,kSpan);<br \/>\n}<br \/>\nreturn kSpan;<br \/>\n}<\/p>\n<p>\/\/\u68c0\u67e5\u4e00\u4e0b\u540e\u9762\u7684\u6876\u91cc\u6709\u6ca1\u6709span&#xff0c;\u5982\u679c\u6709\u53ef\u4ee5\u628a\u5b83\u8fdb\u884c\u5207\u5206<br \/>\nfor (size_t i &#061; k &#043; 1; i &lt; NPAGES; i&#043;&#043;)<br \/>\n{<br \/>\nif (!_spanLists[i].Empty())<br \/>\n{<br \/>\nSpan* nSpan &#061; _spanLists[i].PopFront();<br \/>\n\/\/Span* kSpan &#061; new Span;<br \/>\nSpan* kSpan &#061; _spanPool.New();<\/p>\n<p>\/\/\u5728nSpan\u7684\u5934\u90e8\u5207\u4e00\u4e2ak\u9875\u4e0b\u6765<br \/>\n\/\/k\u9875\u7684span\u8fd4\u56de<br \/>\n\/\/nSpan\u518d\u6302\u5230\u5bf9\u5e94\u6620\u5c04\u7684\u4f4d\u7f6e<br \/>\nkSpan-&gt;_pageId &#061; nSpan-&gt;_pageId; \/\/\u5207\u51fa\u6765\u7684\u9875\u53f7\u7ed9kSpan<br \/>\nkSpan-&gt;_n &#061; k; \/\/\u5207\u51fa\u6765\u7684\u9875\u6570\u662fk<\/p>\n<p>nSpan-&gt;_pageId &#043;&#061; k;\/\/nSpan\u7684\u8d77\u59cb\u9875\u53f7\u589e\u52a0<br \/>\nnSpan-&gt;_n -&#061; k;\/\/nSpan\u7684\u9875\u6570\u51cf\u5c11k<\/p>\n<p>_spanLists[nSpan-&gt;_n].PushFront(nSpan);\/\/\u628anSpan\u5934\u63d2\u5230\u5bf9\u5e94\u7684\u6876\u4e0a<\/p>\n<p>\/\/\u8fd9\u91cc\u53ea\u9700\u8981\u9996\u5c3e\u6620\u5c04&#xff0c;\u56e0\u4e3a\u5408\u5e76\u7684\u65f6\u5019&#xff0c;\u53ea\u9700\u8981\u627e\u5230\u9996\u6216\u5c3e\u7684\u5373\u53ef<br \/>\n\/\/\u5b58\u50a8nSpan\u7684\u9996\u5c3e\u9875\u53f7\u8ddfnSpan\u6620\u5c04&#xff0c;\u65b9\u4fbfpage cache\u56de\u6536\u5185\u5b58\u65f6\u8fdb\u884c\u5408\u5e76\u67e5\u627e<br \/>\n\/\/_idSpanMap[nSpan-&gt;_pageId] &#061; nSpan;<br \/>\n\/\/_idSpanMap[nSpan-&gt;_pageId&#043;nSpan-&gt;_n-1] &#061; nSpan;<br \/>\n_idSpanMap.set(nSpan-&gt;_pageId, nSpan);<br \/>\n_idSpanMap.set(nSpan-&gt;_pageId &#043; nSpan-&gt;_n &#8211; 1, nSpan);<\/p>\n<p>\/\/\u8fd9\u91cc\u662f\u56e0\u4e3akspan\u4f1a\u88ab\u5207\u6210\u4e00\u5c0f\u5757\u4e00\u5c0f\u5757\u7684&#xff0c;\u6240\u4ee5\u9700\u8981\u6bcf\u4e00\u4e2a\u5c0f\u5757\u5185\u5b58\u90fd\u6620\u5c04<br \/>\n\/\/\u5efa\u7acbid\u548cspan\u7684\u6620\u5c04&#xff0c;\u65b9\u4fbfcentral cache\u56de\u6536\u5c0f\u5757\u5185\u5b58\u65f6&#xff0c;\u67e5\u627e\u5bf9\u5e94\u7684span<br \/>\nfor (PAGE_ID i &#061; 0; i &lt; kSpan-&gt;_n;&#043;&#043;i)<br \/>\n{<br \/>\n\/\/_idSpanMap[kSpan-&gt;_pageId &#043; i] &#061; kSpan;<br \/>\n_idSpanMap.set(kSpan-&gt;_pageId &#043; i, kSpan);<br \/>\n}<\/p>\n<p>return kSpan;<br \/>\n}<br \/>\n}<\/p>\n<p>\/\/\u8d70\u5230\u8fd9\u4e2a\u4f4d\u7f6e\u5c31\u8bf4\u660e\u540e\u9762\u6ca1\u6709\u5927\u9875\u7684span\u4e86<br \/>\n\/\/\u8fd9\u65f6\u5c31\u53bb\u627e\u5806\u8981\u4e00\u4e2a128\u9875\u7684span<br \/>\n\/\/Span* bigSpan &#061; new Span;<br \/>\nSpan* bigSpan &#061; _spanPool.New();<br \/>\nvoid* ptr &#061; SystemAlloc(NPAGES &#8211; 1);<br \/>\nbigSpan-&gt;_pageId &#061; (PAGE_ID)ptr &gt;&gt; PAGE_SHIFT;<br \/>\nbigSpan-&gt;_n &#061; NPAGES &#8211; 1;<\/p>\n<p>_spanLists[bigSpan-&gt;_n].PushFront(bigSpan);<\/p>\n<p>return NewSpan(k);<\/p>\n<p>}<\/p>\n<p>Span* PageCache::MapObjectToSpan(void* obj)<br \/>\n{<br \/>\nPAGE_ID id &#061; ((PAGE_ID)obj &gt;&gt; PAGE_SHIFT);<\/p>\n<p>\/\/std::unique_lock&lt;std::mutex&gt; lock(_pageMtx);<br \/>\n\/\/<br \/>\n\/\/auto ret &#061; _idSpanMap.find(id);<br \/>\n\/\/if (ret !&#061; _idSpanMap.end())<br \/>\n\/\/{<br \/>\n\/\/return ret-&gt;second;<br \/>\n\/\/}<br \/>\n\/\/else<br \/>\n\/\/{<br \/>\n\/\/assert(false);<br \/>\n\/\/return nullptr;<br \/>\n\/\/}<\/p>\n<p>auto ret &#061; (Span*)_idSpanMap.get(id);<br \/>\nassert(ret !&#061; nullptr);<br \/>\nreturn ret;<br \/>\n}<\/p>\n<p>void PageCache::ReleaseSpanToPageCache(Span* span)<br \/>\n{<br \/>\n\/\/\u5927\u4e8e128 page\u7684\u76f4\u63a5\u8fd8\u7ed9\u5806<br \/>\nif (span-&gt;_n &gt; NPAGES &#8211; 1)<br \/>\n{<br \/>\nvoid* ptr &#061; (void*)(span-&gt;_pageId &lt;&lt; PAGE_SHIFT);<br \/>\nSystemFree(ptr);<br \/>\n\/\/delete span;<br \/>\n_spanPool.Delete(span);<\/p>\n<p>return;<br \/>\n}<\/p>\n<p>\/\/\u5bf9span\u524d\u540e\u7684\u9875&#xff0c;\u5c1d\u8bd5\u8fdb\u884c\u5408\u5e76&#xff0c;\u7f13\u89e3\u5185\u5b58\u788e\u7247\u95ee\u9898<br \/>\nwhile (1)<br \/>\n{<br \/>\nPAGE_ID prevId &#061; span-&gt;_pageId &#8211; 1;<br \/>\n\/\/auto ret &#061; _idSpanMap.find(prevId);<br \/>\n\/\/\/\/\u524d\u9762\u7684\u9875\u53f7\u6ca1\u6709&#xff0c;\u4e0d\u5408\u5e76\u4e86<br \/>\n\/\/if (ret &#061;&#061; _idSpanMap.end())<br \/>\n\/\/{<br \/>\n\/\/break;<br \/>\n\/\/}<br \/>\nauto ret &#061; (Span*)_idSpanMap.get(prevId);<br \/>\nif (ret &#061;&#061; nullptr)<br \/>\n{<br \/>\nbreak;<br \/>\n}<\/p>\n<p>\/\/\u524d\u9762\u76f8\u90bb\u9875\u7684span\u5728\u4f7f\u7528&#xff0c;\u4e0d\u5408\u5e76\u4e86<br \/>\nSpan* prevSpan &#061; ret;<br \/>\nif (prevSpan-&gt;_isUse &#061;&#061; true)<br \/>\n{<br \/>\nbreak;<br \/>\n}<\/p>\n<p>\/\/\u5408\u5e76\u51fa\u8d85\u8fc7128\u9875\u7684span\u6ca1\u529e\u6cd5\u7ba1\u7406&#xff0c;\u4e5f\u4e0d\u5408\u5e76<br \/>\nif (prevSpan-&gt;_n &#043; span-&gt;_n &gt; NPAGES &#8211; 1)<br \/>\n{<br \/>\nbreak;<br \/>\n}<\/p>\n<p>span-&gt;_pageId &#061; prevSpan-&gt;_pageId;<br \/>\nspan-&gt;_n &#043;&#061; prevSpan-&gt;_n;<\/p>\n<p>_spanLists[prevSpan-&gt;_n].Erase(prevSpan);<br \/>\n\/\/delete prevSpan;<br \/>\n_spanPool.Delete(prevSpan);<br \/>\n}<\/p>\n<p>\/\/\u5411\u540e\u5408\u5e76<br \/>\nwhile (1)<br \/>\n{<br \/>\nPAGE_ID nextId &#061; span-&gt;_pageId &#043; span-&gt;_n;<br \/>\n\/*auto ret &#061; _idSpanMap.find(nextId);<br \/>\nif (ret &#061;&#061; _idSpanMap.end())<br \/>\n{<br \/>\nbreak;<br \/>\n}*\/<br \/>\nauto ret &#061; (Span*)_idSpanMap.get(nextId);<br \/>\nif (ret &#061;&#061; nullptr)<br \/>\n{<br \/>\nbreak;<br \/>\n}<\/p>\n<p>Span* nextSpan &#061; ret;<br \/>\nif (nextSpan-&gt;_isUse &#061;&#061; true)<br \/>\n{<br \/>\nbreak;<br \/>\n}<\/p>\n<p>if (nextSpan-&gt;_n &#043; span-&gt;_n &gt; NPAGES &#8211; 1)<br \/>\n{<br \/>\nbreak;<br \/>\n}<\/p>\n<p>span-&gt;_n &#043;&#061; nextSpan-&gt;_n;<\/p>\n<p>_spanLists[nextSpan-&gt;_n].Erase(nextSpan);<br \/>\n\/\/delete nextSpan;<br \/>\n_spanPool.Delete(nextSpan);<br \/>\n}<\/p>\n<p>_spanLists[span-&gt;_n].PushFront(span);<br \/>\nspan-&gt;_isUse &#061; false;<br \/>\n\/\/_idSpanMap[span-&gt;_pageId] &#061; span;<br \/>\n\/\/_idSpanMap[span-&gt;_pageId &#043; span-&gt;_n &#8211; 1] &#061; span;<\/p>\n<p>_idSpanMap.set(span-&gt;_pageId, span);<br \/>\n_idSpanMap.set(span-&gt;_pageId &#043; span-&gt;_n &#8211; 1, span);<br \/>\n} <\/p>\n<h3>ThreadCache.cpp<\/h3>\n<p>#include&#034;ThreadCache.h&#034;<br \/>\n#include&#034;CentralCache.h&#034;<\/p>\n<p>void* ThreadCache::FetchFromCentralCache(size_t index, size_t size)<br \/>\n{<br \/>\n\/\/\u6162\u5f00\u59cb\u53cd\u9988\u8c03\u8282\u7b97\u6cd5<br \/>\n\/\/1.\u6700\u5f00\u59cb\u4e0d\u4f1a\u4e00\u6b21\u5411central cache\u4e00\u6b21\u6279\u91cf\u8981\u592a\u591a&#xff0c;\u56e0\u4e3a\u8981\u592a\u591a\u53ef\u80fd\u7528\u4e0d\u5b8c<br \/>\n\/\/2.\u5982\u679c\u4f60\u4e0d\u8981\u8fd9\u4e2asize\u5927\u5c0f\u5185\u5b58\u9700\u6c42&#xff0c;\u90a3\u4e48batchNum\u5c31\u4f1a\u4e0d\u65ad\u589e\u957f&#xff0c;\u76f4\u5230\u4e0a\u9650<br \/>\n\/\/3.size\u8d8a\u5927&#xff0c;\u4e00\u6b21\u5411central cache\u8981\u7684batchNum\u5c31\u8d8a\u5c0f<br \/>\n\/\/4.size\u8d8a\u5c0f&#xff0c;\u4e00\u6b21\u5411central cache\u8981\u7684batchNum\u5c31\u8d8a\u5927<br \/>\nsize_t batchNum &#061;min(_freeLists[index].MaxSize(), SizeClass::NumMoveSize(size));<br \/>\nif (_freeLists[index].MaxSize() &#061;&#061; batchNum)<br \/>\n{<br \/>\n_freeLists[index].MaxSize() &#043;&#061; 1;\/\/\u6700\u5f00\u59cbMaxSize\u662f1&#xff0c;\u6240\u4ee5\u6162\u6162\u589e\u957f&#xff0c;\u4e00\u6b21\u589e\u957f\u4e00\u4e2a&#xff0c;\u8d8a\u5230\u540e\u9762&#xff0c;\u4e00\u6b21\u6279\u91cf\u7ed9\u7684\u5c31\u8d8a\u591a\u3002<br \/>\n}<\/p>\n<p>void* start &#061; nullptr;<br \/>\nvoid* end &#061; nullptr;<br \/>\nsize_t actualNum &#061; CentralCache::GetInstance()-&gt;FetchRangeObj(start, end, batchNum, size);\/\/\u5b9e\u9645span\u91cc\u6302\u7684\u94fe\u8868\u7684\u5bf9\u8c61\u53ef\u80fd\u4e0d\u8db3batchNum\u4e2a<br \/>\nassert(actualNum &gt; 0); <\/p>\n<p>if (actualNum &#061;&#061; 1)<br \/>\n{<br \/>\nassert(start &#061;&#061; end);<br \/>\nreturn start;<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\n_freeLists[index].PushRange(NextObj(start), end,actualNum-1); \/\/\u7b2c\u4e00\u4e2astart\u8981\u8fd4\u56de<br \/>\nreturn start;<br \/>\n}<br \/>\n} <\/p>\n<p>void* ThreadCache::Allocate(size_t size)<br \/>\n{<br \/>\nassert(size &lt;&#061; MAX_BYTES);<br \/>\nsize_t alignSize &#061; SizeClass::RoundUp(size);<br \/>\nsize_t index &#061; SizeClass::Index(size); \/\/\u8ba1\u7b97\u5728\u54ea\u4e00\u4e2a\u6876&#xff08;\u81ea\u7531\u94fe\u8868&#xff09;\u91cc\u9762\u53d6<\/p>\n<p>if (!_freeLists[index].Empty()) \/\/\u5982\u679c\u81ea\u7531\u94fe\u8868\u4e0d\u4e3a\u7a7a&#xff0c;\u5c31\u5728\u8fd9\u91cc\u62ff<br \/>\n{<br \/>\nreturn _freeLists[index].Pop();<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\nreturn FetchFromCentralCache(index, alignSize); \/\/\u5426\u5219\u5c31\u53bb\u4e2d\u5fc3\u7f13\u5b58\u62ff<br \/>\n}<br \/>\n}<\/p>\n<p>void ThreadCache::Deallocate(void* ptr, size_t size)<br \/>\n{<br \/>\nassert(ptr);<br \/>\nassert(size &lt;&#061; MAX_BYTES);<\/p>\n<p>\/\/\u627e\u5bf9\u5e94\u6620\u5c04\u7684\u81ea\u7531\u94fe\u8868\u6876&#xff0c;\u628a\u5bf9\u8c61\u63d2\u5165\u8fdb\u53bb<br \/>\nsize_t index &#061; SizeClass::Index(size);<br \/>\n_freeLists[index].Push(ptr);<\/p>\n<p>\/\/\u5f53\u94fe\u8868\u957f\u5ea6\u5927\u4e8e\u4e00\u6b21\u6279\u91cf\u7533\u8bf7\u7684\u5185\u5b58\u65f6\u5c31\u5f00\u59cb\u8fd8\u4e00\u6bb5list\u7ed9central cache<br \/>\nif (_freeLists[index].Size() &gt;&#061; _freeLists[index].MaxSize())<br \/>\n{<br \/>\nListTooLong(_freeLists[index], size);<br \/>\n}<br \/>\n}<\/p>\n<p>void ThreadCache::ListTooLong(FreeList&amp; list, size_t size)<br \/>\n{<br \/>\nvoid* start &#061; nullptr;<br \/>\nvoid* end &#061; nullptr;<br \/>\nlist.PopRange(start, end, list.MaxSize());<\/p>\n<p>CentralCache::GetInstance()-&gt;ReleaseListToSpans(start, size);<br \/>\n} <\/p>\n<h3>UnitTest.cpp<\/h3>\n<p>#include&#034;ObjectPool.h&#034;<br \/>\n#include&#034;ConcurrentAlloc.h&#034;<\/p>\n<p>void Alloc1()<br \/>\n{<br \/>\nfor (size_t i &#061; 0; i &lt; 5; i&#043;&#043;)<br \/>\n{<br \/>\nvoid* ptr &#061; ConcurrentAlloc(6);<\/p>\n<p>}<br \/>\n}<\/p>\n<p>void Alloc2()<br \/>\n{<br \/>\nfor (size_t i &#061; 0; i &lt;5 ; i&#043;&#043;)<br \/>\n{<br \/>\nvoid* ptr &#061; ConcurrentAlloc(7);<\/p>\n<p>}<br \/>\n}<\/p>\n<p>void TLSTest()<br \/>\n{<br \/>\nstd::thread t1(Alloc1);<br \/>\nt1.join();<\/p>\n<p>std::thread t2(Alloc2);<br \/>\nt2.join();<br \/>\n}<\/p>\n<p>void TestConcurrentAlloc1()<br \/>\n{<br \/>\nvoid* p1 &#061; ConcurrentAlloc(6);<br \/>\nvoid* p2 &#061; ConcurrentAlloc(8);<br \/>\nvoid* p3 &#061; ConcurrentAlloc(1);<br \/>\nvoid* p4 &#061; ConcurrentAlloc(7);<br \/>\nvoid* p5 &#061; ConcurrentAlloc(8);<\/p>\n<p>cout &lt;&lt; p1 &lt;&lt; endl;<br \/>\ncout &lt;&lt; p2 &lt;&lt; endl;<br \/>\ncout &lt;&lt; p3 &lt;&lt; endl;<br \/>\ncout &lt;&lt; p4 &lt;&lt; endl;<br \/>\ncout &lt;&lt; p5 &lt;&lt; endl;<\/p>\n<p>ConcurrentFree(p1);<br \/>\nConcurrentFree(p2);<br \/>\nConcurrentFree(p3);<br \/>\nConcurrentFree(p4);<br \/>\nConcurrentFree(p5);<\/p>\n<p>}<\/p>\n<p>void TestConcurrentAlloc2()<br \/>\n{<br \/>\nfor (size_t i &#061; 0; i &lt; 1024; i&#043;&#043;)<br \/>\n{<br \/>\nvoid* p1 &#061; ConcurrentAlloc(6);<br \/>\ncout &lt;&lt; p1 &lt;&lt; endl;<br \/>\n}<\/p>\n<p>void* p2 &#061; ConcurrentAlloc(8);<br \/>\ncout &lt;&lt; p2 &lt;&lt; endl;<br \/>\n}<\/p>\n<p>void TestAddressShift()<br \/>\n{<br \/>\nPAGE_ID id1 &#061; 2000;<br \/>\nPAGE_ID id2 &#061; 2000;<br \/>\nchar* p1 &#061; (char*)(id1 &lt;&lt; PAGE_SHIFT);<br \/>\nchar* p2 &#061; (char*)(id1 &lt;&lt; PAGE_SHIFT);<\/p>\n<p>while (p1 &lt; p2)<br \/>\n{<br \/>\ncout &lt;&lt; (void*)p1 &lt;&lt; &#034;:&#034; &lt;&lt; ((PAGE_ID)p1 &gt;&gt; PAGE_SHIFT) &lt;&lt; endl;<br \/>\np1 &#043;&#061; 8;<br \/>\n}<br \/>\n}<\/p>\n<p>\/\/int main()<br \/>\n\/\/{<br \/>\n\/\/\/\/TestObjectPool();<br \/>\n\/\/\/\/TLSTest();<br \/>\n\/\/TestConcurrentAlloc1();<br \/>\n\/\/return 0;<br \/>\n\/\/} <\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#x1f308;\u4e2a\u4eba\u4e3b\u9875&#xff1a;\u79e6jh__https:\/\/blog.csdn.net\/qinjh_?spm1010.2135.3001.5343 &#x1f525;\u00a0\u7cfb\u5217\u4e13\u680f&#xff1a;https:\/\/blog.csdn.net\/qinjh_\/category_13016668.html \u76ee\u5f55<br \/>\n\u4ece\u96f6\u5b9e\u73b0\u4e00\u4e2a\u9ad8\u5e76\u53d1\u5185\u5b58\u6c60<br \/>\n\u9879\u76ee\u4ecb\u7ecd<br \/>\n\u8fd9\u4e2a\u9879\u76ee\u505a\u7684\u662f\u4ec0\u4e48&#xff1f;\u00a0<br \/>\n\u4ec0\u4e48\u662f\u5185\u5b58\u6c60\u00a0<br \/>\n1.\u6c60\u5316\u6280\u672f\u00a0<br \/>\n2.\u5185\u5b58\u6c60\u00a0<br \/>\n3.\u5185\u5b58\u6c60\u4e3b\u8981\u89e3\u51b3\u7684\u95ee\u9898\u00a0 4.ma<\/p>\n","protected":false},"author":2,"featured_media":67553,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55,58,190,100],"topic":[],"class_list":["post-67562","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-c","tag-linux","tag-190","tag-100"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u3010\u9879\u76ee\u3011\u9ad8\u5e76\u53d1\u5185\u5b58\u6c60 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/67562.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010\u9879\u76ee\u3011\u9ad8\u5e76\u53d1\u5185\u5b58\u6c60 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"&#x1f308;\u4e2a\u4eba\u4e3b\u9875&#xff1a;\u79e6jh__https:\/\/blog.csdn.net\/qinjh_?spm1010.2135.3001.5343 &#x1f525;\u00a0\u7cfb\u5217\u4e13\u680f&#xff1a;https:\/\/blog.csdn.net\/qinjh_\/category_13016668.html \u76ee\u5f55 \u4ece\u96f6\u5b9e\u73b0\u4e00\u4e2a\u9ad8\u5e76\u53d1\u5185\u5b58\u6c60 \u9879\u76ee\u4ecb\u7ecd \u8fd9\u4e2a\u9879\u76ee\u505a\u7684\u662f\u4ec0\u4e48&#xff1f;\u00a0 \u4ec0\u4e48\u662f\u5185\u5b58\u6c60\u00a0 1.\u6c60\u5316\u6280\u672f\u00a0 2.\u5185\u5b58\u6c60\u00a0 3.\u5185\u5b58\u6c60\u4e3b\u8981\u89e3\u51b3\u7684\u95ee\u9898\u00a0 4.ma\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/67562.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-01-28T14:33:02+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260128143258-697a1e1ad5fcc.gif\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"24 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/67562.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/67562.html\",\"name\":\"\u3010\u9879\u76ee\u3011\u9ad8\u5e76\u53d1\u5185\u5b58\u6c60 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-01-28T14:33:02+00:00\",\"dateModified\":\"2026-01-28T14:33:02+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/67562.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/67562.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/67562.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010\u9879\u76ee\u3011\u9ad8\u5e76\u53d1\u5185\u5b58\u6c60\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u3010\u9879\u76ee\u3011\u9ad8\u5e76\u53d1\u5185\u5b58\u6c60 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/67562.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010\u9879\u76ee\u3011\u9ad8\u5e76\u53d1\u5185\u5b58\u6c60 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"&#x1f308;\u4e2a\u4eba\u4e3b\u9875&#xff1a;\u79e6jh__https:\/\/blog.csdn.net\/qinjh_?spm1010.2135.3001.5343 &#x1f525;\u00a0\u7cfb\u5217\u4e13\u680f&#xff1a;https:\/\/blog.csdn.net\/qinjh_\/category_13016668.html \u76ee\u5f55 \u4ece\u96f6\u5b9e\u73b0\u4e00\u4e2a\u9ad8\u5e76\u53d1\u5185\u5b58\u6c60 \u9879\u76ee\u4ecb\u7ecd \u8fd9\u4e2a\u9879\u76ee\u505a\u7684\u662f\u4ec0\u4e48&#xff1f;\u00a0 \u4ec0\u4e48\u662f\u5185\u5b58\u6c60\u00a0 1.\u6c60\u5316\u6280\u672f\u00a0 2.\u5185\u5b58\u6c60\u00a0 3.\u5185\u5b58\u6c60\u4e3b\u8981\u89e3\u51b3\u7684\u95ee\u9898\u00a0 4.ma","og_url":"https:\/\/www.wsisp.com\/helps\/67562.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-01-28T14:33:02+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260128143258-697a1e1ad5fcc.gif"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"24 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/67562.html","url":"https:\/\/www.wsisp.com\/helps\/67562.html","name":"\u3010\u9879\u76ee\u3011\u9ad8\u5e76\u53d1\u5185\u5b58\u6c60 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-01-28T14:33:02+00:00","dateModified":"2026-01-28T14:33:02+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/67562.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/67562.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/67562.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010\u9879\u76ee\u3011\u9ad8\u5e76\u53d1\u5185\u5b58\u6c60"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/67562","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=67562"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/67562\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/67553"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=67562"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=67562"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=67562"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=67562"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}