{"id":341,"date":"2025-04-02T00:07:29","date_gmt":"2025-04-01T16:07:29","guid":{"rendered":"http:\/\/www.tgwttt.xyz\/?p=341"},"modified":"2025-11-24T16:43:41","modified_gmt":"2025-11-24T08:43:41","slug":"%e5%86%85%e5%ad%98%e9%93%b6%e8%a1%8c%ef%bc%88%e5%9f%ba%e4%ba%8ec%e7%9a%84%e9%ab%98%e6%80%a7%e8%83%bd%e5%86%85%e5%ad%98%e6%b1%a0%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.tgwttt.xyz\/?p=341","title":{"rendered":"\u5185\u5b58\u94f6\u884c\uff08\u57fa\u4e8ec++\u7684\u9ad8\u6027\u80fd\u5185\u5b58\u6c60\uff09"},"content":{"rendered":"\n<p>\u9879\u76ee\u5b8c\u6574\u4ee3\u7801\uff1a<a href=\"https:\/\/gitee.com\/tgwTTT\/high-concurrency-memory-pool\">https:\/\/gitee.com\/tgwTTT\/high-concurrency-memory-pool<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u524d\u8a00<\/strong><\/h2>\n\n\n\n<p><strong>\u4e3a\u4ec0\u4e48\u8981\u81ea\u5df1\u9020\u8f6e\u5b50\uff1f<\/strong><\/p>\n\n\n\n<p>\u73b0\u4ee3\u5f88\u591a\u7684\u5f00\u53d1\u73af\u5883\u90fd\u662f\u591a\u6838\u591a\u7ebf\u7a0b\uff0c\u5728\u7533\u8bf7\u5185\u5b58\u7684\u573a\u6700\u4e0b\uff0c\u5fc5\u7136\u5b58\u5728\u6fc0\u70c8\u7684\u9501\u7ade\u4e89\u95ee\u9898\u3002malloc\u672c\u8eab\u5176\u5b9e\u5df2\u7ecf\u5f88\u4f18\u79c0\uff0c\u4f46\u662f\u4f20\u7edf\u7684glibc\u4e2d\u7684malloc\u548cFree\u9875\u5b58\u5728\u5f88\u591a\u7f3a\u9677\uff0c<\/p>\n\n\n\n<p>\u6bd4\u5982\uff1a1.\u591a\u7ebf\u7a0b\u5171\u4eab\u540c\u4e00\u628a arena \u9501\uff0c\u6838\u6570\u8d8a\u591a\uff0c\u8d8a\u50cf\u5355\u8f66\u9053\u6536\u8d39\u7ad9\uff0cCPU \u7a7a\u8f6c\u6392\u961f\u3002<\/p>\n\n\n\n<p>           2.\u6bcf\u6b21\u6309\u9700 brk\/mmap\uff0c\u8fdb\u5185\u6838\u3001\u51fa\u5185\u6838\uff0c\u51e0\u5341\u5fae\u79d2\u8d77\u6b65\u7b49\u3002<\/p>\n\n\n\n<p>\u57fa\u4e8e\u4ee5\u4e0a\u60c5\u51b5\uff1a\u6211\u4eec\u7684\u5185\u5b58\u94f6\u884c\u9700\u8981\u8003\u8651\u8bb8\u591a\u60c5\u51b5<\/p>\n\n\n\n<p id=\"u149e5e6b\">1.\u6027\u80fd\u95ee\u9898\u3002<\/p>\n\n\n\n<p id=\"ue67928d0\">2.\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\uff0c\u9501\u7ade\u4e89\u95ee\u9898\u3002<\/p>\n\n\n\n<p id=\"uc7d4ed14\">3.\u5185\u5b58\u788e\u7247\u95ee\u9898\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u9879\u76ee\u6574\u4f53\u6846\u67b6<\/h2>\n\n\n\n<p>\u9879\u76ee\u6574\u4f53\u6846\u67b6\u5206\u4e3a3\u4e2a\u90e8\u5206\uff1a<\/p>\n\n\n\n<p>1.threadCache <\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"605\" height=\"381\" src=\"http:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/09\/image-30.png\" alt=\"\" class=\"wp-image-347\" srcset=\"https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/09\/image-30.png 605w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/09\/image-30-300x189.png 300w\" sizes=\"auto, (max-width: 605px) 100vw, 605px\" \/><\/figure>\n\n\n\n<p>2.CnetralCache<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"601\" height=\"314\" src=\"http:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/09\/image-31.png\" alt=\"\" class=\"wp-image-348\" srcset=\"https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/09\/image-31.png 601w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/09\/image-31-300x157.png 300w\" sizes=\"auto, (max-width: 601px) 100vw, 601px\" \/><\/figure>\n\n\n\n<p>3.PageCache<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"383\" height=\"394\" src=\"http:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/09\/image-32.png\" alt=\"\" class=\"wp-image-349\" srcset=\"https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/09\/image-32.png 383w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/09\/image-32-292x300.png 292w\" sizes=\"auto, (max-width: 383px) 100vw, 383px\" \/><\/figure>\n\n\n\n<p>\u9879\u76ee\u56fe\u53ca\u5176\u6574\u4f53\u6846\u67b6\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"546\" src=\"http:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/09\/image-26-1024x546.png\" alt=\"\" class=\"wp-image-342\" srcset=\"https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/09\/image-26-1024x546.png 1024w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/09\/image-26-300x160.png 300w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/09\/image-26-768x409.png 768w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/09\/image-26.png 1304w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><strong>\u5177\u4f53\u8bbe\u8ba1\u89e3\u6790<\/strong><\/p>\n\n\n\n<p><strong>\u4ec0\u4e48\u662f\u5185\u5b58\u6c60<\/strong>\uff1f<\/p>\n\n\n\n<p>1.\u6c60\u5316\u6280\u672f <\/p>\n\n\n\n<p>\u6240\u8c13\u201c\u6c60\u5316\u6280\u672f\u201d\uff0c\u5c31\u662f\u7a0b\u5e8f\u5148\u5411\u7cfb\u7edf\u7533\u8bf7\u8fc7\u91cf\u7684\u8d44\u6e90\uff0c\u7136\u540e\u2f83\u2f30\u7ba1\u7406\uff0c\u4ee5\u5907\u4e0d\u65f6\u4e4b\u9700\u3002\u4e4b\u6240\u4ee5\u8981\u7533\u8bf7\u8fc7\u91cf\u7684\u8d44\u6e90\uff0c\u662f\u56e0\u4e3a\u6bcf\u6b21\u7533\u8bf7\u8be5\u8d44\u6e90\u90fd\u6709\u8f83\u2f24\u7684\u5f00\u9500\uff0c\u4e0d\u5982\u63d0\u524d\u7533\u8bf7\u597d\u4e86\uff0c\u8fd9\u6837\u4f7f\u2f64\u65f6\u5c31\u4f1a\u53d8\u5f97\u2fae\u5e38\u5feb\u6377\uff0c\u2f24\u2f24\u63d0\u2fbc\u7a0b\u5e8f\u8fd0\u2f8f\u6548\u7387\u3002\u5728\u8ba1\u7b97\u673a\u4e2d\uff0c\u6709\u5f88\u591a\u4f7f\u2f64\u201c\u6c60\u201d\u8fd9\u79cd\u6280\u672f\u7684\u5730\u2f45\uff0c\u9664\u4e86\u5185\u5b58\u6c60\uff0c\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\uff0c\u5b83\u7684\u4e3b\u8981\u601d\u60f3\u662f\uff1a\u5148\u542f\u52a8\u82e5\u2f32\u6570\u91cf\u7684\u7ebf\u7a0b\uff0c\u8ba9\u5b83\u4eec\u5904\u4e8e\u7761\u7720\u72b6\u6001\uff0c\u5f53\u63a5\u6536\u5230\u5ba2\u2f3e\u7aef\u7684\u8bf7\u6c42\u65f6\uff0c\u5524\u9192\u6c60\u4e2d\u67d0\u4e2a\u7761\u7720\u7684\u7ebf\u7a0b\uff0c\u8ba9\u5b83\u6765\u5904\u7406\u5ba2\u2f3e\u7aef\u7684\u8bf7\u6c42\uff0c\u5f53\u5904\u7406\u5b8c\u8fd9\u4e2a\u8bf7\u6c42\uff0c\u7ebf\u7a0b\u2f1c\u8fdb\u2f0a\u7761\u7720\u72b6\u6001\u3002<\/p>\n\n\n\n<p>2.\u5185\u5b58\u6c60 <\/p>\n\n\n\n<p>\u5185\u5b58\u6c60\u662f\u6307\u7a0b\u5e8f\u9884\u5148\u4ece\u64cd\u4f5c\u7cfb\u7edf\u7533\u8bf7\u2f00\u5757\u2f9c\u591f\u2f24\u5185\u5b58\uff0c\u6b64\u540e\uff0c\u5f53\u7a0b\u5e8f\u4e2d\u9700\u8981\u7533\u8bf7\u5185\u5b58\u7684\u65f6\u5019\uff0c\u4e0d\u662f\u76f4\u63a5\u5411\u64cd\u4f5c\u7cfb\u7edf\u7533\u8bf7\uff0c\u2f7d\u662f\u76f4\u63a5\u4ece\u5185\u5b58\u6c60\u4e2d\u83b7\u53d6\uff1b\u540c\u7406\uff0c\u5f53\u7a0b\u5e8f\u91ca\u653e\u5185\u5b58\u7684\u65f6\u5019\uff0c\u5e76\u4e0d\u771f\u6b63\u5c06\u5185\u5b58\u8fd4\u56de\u7ed9\u64cd\u4f5c\u7cfb\u7edf\uff0c\u2f7d\u662f\u8fd4\u56de\u5185\u5b58\u6c60\u3002\u5f53\u7a0b\u5e8f\u9000\u51fa(\u6216\u8005\u7279\u5b9a\u65f6\u95f4)\u65f6\uff0c\u5185\u5b58\u6c60\u624d\u5c06\u4e4b\u524d\u7533\u8bf7\u7684\u5185\u5b58\u771f\u6b63\u91ca\u653e\u3002<\/p>\n\n\n\n<p>3.\u5185\u5b58\u6c60\u4e3b\u8981\u89e3\u51b3\u7684\u95ee\u9898 <\/p>\n\n\n\n<p>\u5185\u5b58\u6c60\u4e3b\u8981\u89e3\u51b3\u7684\u5f53\u7136\u662f\u6548\u7387\u7684\u95ee\u9898\uff0c\u5176\u6b21\u5982\u679c\u4f5c\u4e3a\u7cfb\u7edf\u7684\u5185\u5b58\u5206\u914d\u5668\u7684\u2ec6\u5ea6\uff0c\u8fd8\u9700\u8981\u89e3\u51b3\u2f00\u4e0b\u5185\u5b58\u788e\u2f5a\u7684\u95ee\u9898\u3002<\/p>\n\n\n\n<p>\u90a3\u4e48\u4ec0\u4e48\u662f\u5185\u5b58\u788e\u2f5a\u5462\uff1f<br><\/p>\n\n\n\n<p>\u5185\u5b58\u788e\u2f5a\u5206\u4e3a\u5916\u788e\u2f5a\u548c\u5185\u788e\u2f5a<\/p>\n\n\n\n<p>\u5916\u788e\u7247\uff1a<\/p>\n\n\n\n<p>\u5916\u90e8\u788e\u2f5a\u662f\u2f00\u4e9b\u7a7a\u95f2\u7684\u8fde\u7eed\u5185\u5b58\u533a\u57df\u592a\u2f29\uff0c\u8fd9\u4e9b\u5185\u5b58\u7a7a\u95f4\u4e0d\u8fde\u7eed\uff0c\u4ee5\u2f84\u4e8e\u5408\u8ba1\u7684\u5185\u5b58\u2f9c\u591f\uff0c\u4f46\u662f\u4e0d\u80fd\u6ee1\u2f9c\u2f00\u4e9b\u7684\u5185\u5b58\u5206\u914d\u7533\u8bf7\u9700\u6c42<br><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"581\" src=\"http:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/09\/image-28-1024x581.png\" alt=\"\" class=\"wp-image-345\" srcset=\"https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/09\/image-28-1024x581.png 1024w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/09\/image-28-300x170.png 300w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/09\/image-28-768x435.png 768w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/09\/image-28.png 1254w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u5185\u90e8\u788e\u2f5a\u662f\u7531\u4e8e\u2f00\u4e9b\u5bf9\u2eec\u7684\u9700\u6c42\uff0c\u5bfc\u81f4\u5206\u914d\u51fa\u53bb\u7684\u7a7a\u95f4\u4e2d\u2f00\u4e9b\u5185\u5b58\u2f46\u6cd5\u88ab\u5229\u2f64\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u5f00\u80c3\u83dc<\/strong><\/h2>\n\n\n\n<p>\u5148\u8bbe\u8ba1\u2f00\u4e2a\u5b9a\u2ed3\u7684\u5185\u5b58\u6c60 \u4f5c\u4e3a\u7a0b\u5e8f\u5458(C\/C++)\u6211\u4eec\u77e5\u9053\u7533\u8bf7\u5185\u5b58\u4f7f\u2f64\u7684\u662fmalloc\uff0cmalloc\u5176\u5b9e\u5c31\u662f\u2f00\u4e2a\u901a\u2f64\u7684\u2f24\u4f17\u8d27\uff0c\u4ec0\u4e48\u573a\u666f\u4e0b\u90fd\u53ef\u4ee5\u2f64\uff0c\u4f46\u662f\u4ec0\u4e48\u573a\u666f\u4e0b\u90fd\u53ef\u4ee5\u2f64\u5c31\u610f\u5473\u7740\u4ec0\u4e48\u573a\u666f\u4e0b\u90fd\u4e0d\u4f1a\u6709\u5f88\u2fbc\u7684\u6027\u80fd\uff0c\u4e0b\u2faf\u6211\u4eec\u5c31\u5148\u6765\u8bbe\u8ba1\u2f00\u4e2a\u5b9a\u2ed3\u5185\u5b58\u6c60\u505a\u4e2a\u5f00\u80c3\u83dc\uff0c\u5f53\u7136\u8fd9\u4e2a\u5b9a\u2ed3\u5185\u5b58\u6c60\u5728\u6211\u4eec\u540e\u2faf\u7684\u2fbc\u5e76\u53d1\u5185\u5b58\u6c60\u4e2d\u4e5f\u662f\u6709\u4ef7\u503c\u7684\uff0c\u6240\u4ee5\u5b66\u4e60\u4ed6\u2f6c\u7684\u6709\u4e24\u5c42\uff0c\u5148\u719f\u6089\u2f00\u4e0b\u7b80\u5355\u5185\u5b58\u6c60\u662f\u5982\u4f55\u63a7\u5236\u7684\uff0c\u7b2c\u2f06\u4ed6\u4f1a\u4f5c\u4e3a\u6211\u4eec\u540e\u2faf\u5185\u5b58\u6c60\u7684\u2f00\u4e2a\u57fa\u7840\u7ec4\u4ef6\u3002<\/p>\n\n\n\n<p>\u5177\u4f53\u8bf7\u53c2\u8003\u6587\u7ae0\uff1a<a href=\"http:\/\/www.tgwttt.xyz\/?p=118\">http:\/\/www.tgwttt.xyz\/?p=118<\/a><\/p>\n\n\n\n<p>\u9ad8\u6027\u80fd\u5185\u5b58\u6c60\u4e09\u5c42\u5177\u4f53\u4ecb\u7ecd\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ThreadCache (TLS)  \u2192  CentralCache (\u5168\u5c40)  \u2192  PageCache (\u5168\u5c40)\n   \u65e0\u9501\u5206\u914d            \u6876+\u81ea\u65cb\u9501             \u6309\u9875\u5408\u5e76\/\u62c6\u5206<\/code><\/pre>\n\n\n\n<p>1. thread cache\uff1a\u7ebf\u7a0b\u7f13\u5b58\u662f\u6bcf\u4e2a\u7ebf\u7a0b\u72ec\u6709\u7684\uff0c\u2f64\u4e8e\u2f29\u4e8e256KB\u7684\u5185\u5b58\u7684\u5206\u914d\uff0c\u7ebf\u7a0b\u4ece\u8fd9\u2fa5\u7533\u8bf7\u5185\u5b58\u4e0d\u9700\u8981\u52a0\u9501\uff0c\u6bcf\u4e2a\u7ebf\u7a0b\u72ec\u4eab 208 \u6761 FreeList\uff088 B ~ 256 KB\uff0c\u6309 Size-Class \u5bf9\u9f50\uff09\u3002<br>\u5206\u914d\u8def\u5f84\uff1a<br><code>Allocate()<\/code> \u2192 \u672c\u7ebf\u7a0b freelist.Pop() \u2192 \u547d\u4e2d\u5219\u76f4\u63a5\u8fd4\u56de\uff0c\u5168\u7a0b\u65e0\u9501\u3002<\/p>\n\n\n\n<p>2.CentralCache:\u5168\u5c40 208 \u4e2a SpanList\uff0c\u6bcf\u6761\u94fe\u6302\u300c\u5df2\u5207\u597d\u300d\u7684 Span\uff08\u5c0f\u5757\u5185\u5b58\uff09\u3002<br>ThreadCache \u7a7a\u65f6\u6279\u91cf\u62ff batchNum \u4e2a\u5bf9\u8c61\u56de\u53bb\uff1b\u56de\u6536\u65f6\u94fe\u8868\u8fc7\u957f\u518d\u6279\u91cf\u5f52\u8fd8\u3002<br>\u9501\u7c92\u5ea6\uff1a\u6bcf\u6761 SpanList \u4e00\u628a\u81ea\u65cb\u9501<\/p>\n\n\n\n<p>3.PageCache:<\/p>\n\n\n\n<p>\u5411 OS \u6279\u91cf\u62ff\u5185\u5b58\uff08<code>VirtualAlloc<\/code>\/<code>mmap<\/code>\uff09<\/p>\n\n\n\n<p>\u5408\u5e76\u7a7a\u95f2 Span\uff0c\u7f13\u89e3\u5916\u788e\u7247<\/p>\n\n\n\n<p>\u5efa\u7acb\u9875\u53f7 \u2192 Span \u7684\u54c8\u5e0c\u6620\u5c04\uff0c\u65b9\u4fbf\u56de\u6536\u65f6\u53cd\u5411\u67e5\u627e<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u6838\u5fc3\u529f\u80fd\u53ca\u5176\u4ee3\u7801\u5b9e\u73b0<\/strong><\/h2>\n\n\n\n<p><strong>\u4e00\u3001ThreadCache \u5c42\uff08TLS\uff0c\u65e0\u9501\uff09<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>extern thread_local ThreadCache* pTLSThreadCache;   \/\/ \u6bcf\u4e2a\u7ebf\u7a0b\u4e00\u4efd\n\n\/\/ ThreadCache.cc\nvoid* ThreadCache::Allocate(size_t size){\n    size_t index = SizeClass::Index(size);          \/\/  \u6620\u5c04\u5230\u6876\n    if (!_freeLists[index].Empty())                 \/\/  \u672c\u7ebf\u7a0b freelist \u6709\u5b58\u8d27\n        return _freeLists[index].Pop();             \/\/  \u76f4\u63a5 Pop\uff0c\u65e0\u9501\u8def\u5f84\u7ed3\u675f\n    return FetchFromCentralCache(index, size);      \/\/  \u7f3a\u8d27\u2192\u4e0b\u4e00\u5c42\n}\nTLS\u4ecb\u7ecd\uff1a\u7ebf\u7a0b\u5c40\u90e8\u5b58\u50a8\uff08TLS\uff09\uff0c\u662f\u4e00\u79cd\u53d8\u91cf\u7684\u5b58\u50a8\u65b9\u6cd5\uff0c\u8fd9\u4e2a\u53d8\u91cf\u5728\u5b83\u6240\u5728\u7684\u7ebf\u7a0b\u5185\u662f\u5168\u5c40\u53ef\u8bbf\u95ee\u7684\uff0c\u4f46\u662f\u4e0d\u80fd\u88ab\u5176\u4ed6\u7ebf\u7a0b\u8bbf\u95ee\u5230\uff0c\u8fd9\u6837\u5c31\u4fdd\u6301\u4e86\u6570\u636e\u7684\u7ebf\u7a0b\u72ec\u7acb\u6027\u3002\u800c\u719f\u77e5\u7684\u5168\u5c40\u53d8\u91cf\uff0c\u662f\u6240\u6709\u7ebf\u7a0b\u90fd\u53ef\u4ee5\u8bbf\u95ee\u7684\uff0c\u8fd9\u6837\u5c31\u4e0d\u53ef\u907f\u514d\u9700\u8981\u9501\u6765\u63a7\u5236\uff0c\u589e\u52a0\u4e86\u63a7\u5236\u6210\u672c\u548c\u4ee3\u7801\u590d\u6742\u5ea6\u3002\n\u672c\u8d28\uff1a\u7ebf\u7a0b\u5c40\u90e8\u5b58\u50a8\uff08TLS\uff09\uff0c\u53ef\u4ee5\u5b58\u50a8\u53ea\u80fd\u8be5\u7ebf\u7a0b\u4f7f\u7528\u7684\u6570\u636e\u3002\n\n<strong>\u4e8c\u3001ThreadCache \u2192 CentralCache \u6279\u91cf\u8865\u8d27<\/strong><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ ThreadCache.cc\nvoid* ThreadCache::FetchFromCentralCache(size_t index, size_t size){\n    size_t batchNum = min(_freeLists[index].MaxSize(),\n                          SizeClass::NumMoveSize(size)); \/\/  \u6162\u5f00\u59cb\u7b97\u6cd5\n    void* start = nullptr;\n    void* end   = nullptr;\n    size_t actual = CentralCache::getinstance()\n                    ->FetchRangeObj(start,end,batchNum,size); \/\/ \u6279\u91cf\u62ff\n    if (actual > 1)\n        _freeLists[index].PushRange(NextObj(start),end,actual-1); \/\/  \u5269\u7684\u7559\u672c\u5730\n    return start;                                               \/\/  \u8fd4\u56de\u4e00\u4e2a\u7ed9\u7528\u6237\n}\n<\/code><\/pre>\n\n\n\n<p><strong>\u4e09\u3001CentralCache \u5c42\uff08\u6876\u7ea7\u81ea\u65cb\u9501\uff09\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nsize_t CentralCache::FetchRangeObj(void*& start,void*& end,size_t n,size_t size){\n    size_t index = SizeClass::Index(size);\n    SpanList& list = _spanLists[index];\n    list._mtx.lock();                      \/\/  \u6876\u9501\uff1a\u4e00\u6761\u94fe\u4e00\u628a\n    Span* span = GetOneSpan(list,size);    \/\/  \u786e\u4fdd\u6709\u975e\u7a7a Span\n    start = span->_freelist;               \/\/  \u4ece Span \u5207 batch\n    ...\n    list._mtx.unlock();                    \/\/  \u653e\u9501\n    return actual;\n}<\/code><\/pre>\n\n\n\n<p>\u5185\u90e8\u5982\u679c\u53d1\u73b0 \u672c\u6876\u6ca1 Span\uff0c\u4f1a\u4e34\u65f6\u653e\u9501\u2192\u5411 PageCache \u8981\u6574\u6bb5\u5185\u5b58\u2192\u91cd\u65b0\u52a0\u9501\u6302\u56de\u6765\uff08\u9632\u6b62\u6b7b\u9501\uff09\u3002<\/p>\n\n\n\n<p><strong>\u56db\u3001CentralCache \u2192 PageCache \u62ff\u6574\u6bb5\u5185\u5b58<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ CentralCache.cc\nSpan* CentralCache::GetOneSpan(SpanList& list,size_t byte_size){\n    list._mtx.unlock();                              \/\/  \u89e3\u9501\uff0c\u4fdd\u6301\u5c42\u7ea7\u5355\u8c03\n    PageCache::Instance()->_pagemtx.lock();          \/\/  \u5168\u5c40\u9875\u9501\n    Span* span = PageCache::Instance()->NewPage(\n                     SizeClass::NumMovePage(byte_size)); \/\/  \u8981\u51e0\u9875\n    PageCache::Instance()->_pagemtx.unlock();        \/\/  \u653e\u9875\u9501\n    list._mtx.lock();                                \/\/  \u518d\u62ff\u6876\u9501\n    list.PushFront(span);                            \/\/  \u628a\u65b0 Span \u6302\u6876\n    return span;\n}<\/code><\/pre>\n\n\n\n<p><strong>\u4e94\u3001PageCache \u5c42\uff08\u6309\u9875\u5408\u5e76 \/ \u62c6\u5206\uff09<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ PageCache.cc\nSpan* PageCache::NewPage(size_t k){\n    if (!_spanLists[k].Empty())               \/\/  \u6709\u73b0\u6210 k \u9875 Span\n        return _spanLists[k].PopFront();      \/\/     \u76f4\u63a5\u8fd4\u56de\n    for (size_t n = k+1; n < MAX_SPANS; ++n)  \/\/  \u5f80\u540e\u627e\u66f4\u5927\u5757\n        if (!_spanLists[n].Empty()){\n            Span* big = _spanLists[n].PopFront();\n            Span* spl = new Span;             \/\/ \u5207\u51fa k \u9875\n            spl->_pageID = big->_pageID;\n            spl->_n      = k;\n            big->_pageID += k;\n            big->_n      -= k;\n            _spanLists[big->_n].PushFront(big); \/\/ \u5269\u4f59\u6302\u56de\u53bb\n            return spl;                         \/\/ \u8fd4\u56de\u7ed9 CentralCache\n        }\n    \/\/ \u4ecd\u6ca1\u6709 \u2192 \u5411 OS \u62ff 128 \u9875\u518d\u9012\u5f52\u5207\n    void* ptr = SystemAlloc(MAX_SPANS-1);       \/\/  \u6700\u540e\u624d mmap \/ VirtualAlloc\n    ...\n}\n<\/code><\/pre>\n\n\n\n<p>\u771f\u6b63\u8d70\u5230\u7cfb\u7edf\u8c03\u7528\u65f6\uff0c\u4e00\u6b21\u6027\u62ff 1 MB\uff08128\u00d78 KB\uff09\uff0c\u540e\u9762\u6162\u6162\u5207\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u6027\u80fd\u5bf9\u6bd4\uff1a<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include \"CentralCache.h\"\n#include \"PageCache.h\"\n\nCentralCache CentralCache::_sInst;\n\n\/\/ \u83b7\u53d6\u4e00\u4e2a\u975e\u7a7a\u7684span\nSpan* CentralCache::GetOneSpan(SpanList& list, size_t size)\n{\n\t\/\/ \u67e5\u770b\u5f53\u524d\u7684spanlist\u4e2d\u662f\u5426\u6709\u8fd8\u6709\u672a\u5206\u914d\u5bf9\u8c61\u7684span\n\tSpan* it = list.Begin();\n\twhile (it != list.End())\n\t{\n\t\tif (it->_freeList != nullptr)\n\t\t{\n\t\t\treturn it;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tit = it->_next;\n\t\t}\n\t}\n\n\t\/\/ \u5148\u628acentral cache\u7684\u6876\u9501\u89e3\u6389\uff0c\u8fd9\u6837\u5982\u679c\u5176\u4ed6\u7ebf\u7a0b\u91ca\u653e\u5185\u5b58\u5bf9\u8c61\u56de\u6765\uff0c\u4e0d\u4f1a\u963b\u585e\n\tlist._mtx.unlock();\n\n\t\/\/ \u8d70\u5230\u8fd9\u91cc\u8bf4\u6ca1\u6709\u7a7a\u95f2span\u4e86\uff0c\u53ea\u80fd\u627epage cache\u8981\n\tPageCache::GetInstance()->_pageMtx.lock();\n\tSpan* span = PageCache::GetInstance()->NewSpan(SizeClass::NumMovePage(size));\n\tspan->_isUse = true;\n\tspan->_objSize = size;\n\tPageCache::GetInstance()->_pageMtx.unlock();\n\n\t\/\/ \u5bf9\u83b7\u53d6span\u8fdb\u884c\u5207\u5206\uff0c\u4e0d\u9700\u8981\u52a0\u9501\uff0c\u56e0\u4e3a\u8fd9\u4f1a\u5176\u4ed6\u7ebf\u7a0b\u8bbf\u95ee\u4e0d\u5230\u8fd9\u4e2aspan\n\n\t\/\/ \u8ba1\u7b97span\u7684\u5927\u5757\u5185\u5b58\u7684\u8d77\u59cb\u5730\u5740\u548c\u5927\u5757\u5185\u5b58\u7684\u5927\u5c0f(\u5b57\u8282\u6570)\n\tchar* start = (char*)(span->_pageId << PAGE_SHIFT);\n\tsize_t bytes = span->_n << PAGE_SHIFT;\n\tchar* end = start + bytes;\n\n\t\/\/ \u628a\u5927\u5757\u5185\u5b58\u5207\u6210\u81ea\u7531\u94fe\u8868\u94fe\u63a5\u8d77\u6765\n\t\/\/ 1\u3001\u5148\u5207\u4e00\u5757\u4e0b\u6765\u53bb\u505a\u5934\uff0c\u65b9\u4fbf\u5c3e\u63d2\n\tspan->_freeList = start;\n\tstart += size;\n\tvoid* tail = span->_freeList;\n\tint i = 1;\n\twhile (start < end)\n\t{\n\t\t++i;\n\t\tNextObj(tail) = start;\n\t\ttail = NextObj(tail); \/\/ tail = start;\n\t\tstart += size;\n\t}\n\n\tNextObj(tail) = nullptr;\n\n\t\/\/ 1\u3001\u6761\u4ef6\u65ad\u70b9\n\t\/\/ 2\u3001\u7591\u4f3c\u6b7b\u5faa\u73af\uff0c\u53ef\u4ee5\u4e2d\u65ad\u7a0b\u5e8f\uff0c\u7a0b\u5e8f\u4f1a\u5728\u6b63\u5728\u8fd0\u884c\u7684\u5730\u65b9\u505c\u4e0b\u6765\n\t\/\/int j = 0;\n\t\/\/void* cur = span->_freeList;\n\t\/\/while (cur)\n\t\/\/{\n\t\/\/\tcur = NextObj(cur);\n\t\/\/\t++j;\n\t\/\/}\n\n\t\/\/if (j != (bytes \/ size))\n\t\/\/{\n\t\/\/\tint x = 0;\n\t\/\/}\n\n\t\/\/ \u5207\u597dspan\u4ee5\u540e\uff0c\u9700\u8981\u628aspan\u6302\u5230\u6876\u91cc\u9762\u53bb\u7684\u65f6\u5019\uff0c\u518d\u52a0\u9501\n\tlist._mtx.lock();\n\tlist.PushFront(span);\n\n\treturn span;\n}\n\n\/\/ \u4ece\u4e2d\u5fc3\u7f13\u5b58\u83b7\u53d6\u4e00\u5b9a\u6570\u91cf\u7684\u5bf9\u8c61\u7ed9thread cache\nsize_t CentralCache::FetchRangeObj(void*& start, void*& end, size_t batchNum, size_t size)\n{\n\tsize_t index = SizeClass::Index(size);\n\t_spanLists[index]._mtx.lock();\n\n\tSpan* span = GetOneSpan(_spanLists[index], size);\n\tassert(span);\n\tassert(span->_freeList);\n\n\t\/\/ \u4ecespan\u4e2d\u83b7\u53d6batchNum\u4e2a\u5bf9\u8c61\n\t\/\/ \u5982\u679c\u4e0d\u591fbatchNum\u4e2a\uff0c\u6709\u591a\u5c11\u62ff\u591a\u5c11\n\tstart = span->_freeList;\n\tend = start;\n\tsize_t i = 0;\n\tsize_t actualNum = 1;\n\twhile ( i < batchNum - 1 &#038;&#038; NextObj(end) != nullptr)\n\t{\n\t\tend = NextObj(end);\n\t\t++i;\n\t\t++actualNum;\n\t}\n\tspan->_freeList = NextObj(end);\n\tNextObj(end) = nullptr;\n\tspan->_useCount += actualNum;\n\n\t\/\/\/\/ \u6761\u4ef6\u65ad\u70b9\n\tint j = 0;\n\tvoid* cur = start;\n\twhile (cur)\n\t{\n\t\tcur = NextObj(cur);\n\t\t++j;\n\t}\n\n\tif (j != actualNum)\n\t{\n\t\tint x = 0;\n\t}\n\n\t_spanLists[index]._mtx.unlock();\n\n\treturn actualNum;\n}\n\nvoid CentralCache::ReleaseListToSpans(void* start, size_t size)\n{\n\tsize_t index = SizeClass::Index(size);\n\t_spanLists[index]._mtx.lock();\n\twhile (start)\n\t{\n\t\tvoid* next = NextObj(start);\n\n\t\tSpan* span = PageCache::GetInstance()->MapObjectToSpan(start);\n\t\tNextObj(start) = span->_freeList;\n\t\tspan->_freeList = start;\n\t\tspan->_useCount--;\n\n\t\t\/\/ \u8bf4\u660espan\u7684\u5207\u5206\u51fa\u53bb\u7684\u6240\u6709\u5c0f\u5757\u5185\u5b58\u90fd\u56de\u6765\u4e86\n\t\t\/\/ \u8fd9\u4e2aspan\u5c31\u53ef\u4ee5\u518d\u56de\u6536\u7ed9page cache\uff0cpagecache\u53ef\u4ee5\u518d\u5c1d\u8bd5\u53bb\u505a\u524d\u540e\u9875\u7684\u5408\u5e76\n\t\tif (span->_useCount == 0)\n\t\t{\n\t\t\t_spanLists[index].Erase(span);\n\t\t\tspan->_freeList = nullptr;\n\t\t\tspan->_next = nullptr;\n\t\t\tspan->_prev = nullptr;\n\n\t\t\t\/\/ \u91ca\u653espan\u7ed9page cache\u65f6\uff0c\u4f7f\u7528page cache\u7684\u9501\u5c31\u53ef\u4ee5\u4e86\n\t\t\t\/\/ \u8fd9\u65f6\u628a\u6876\u9501\u89e3\u6389\n\t\t\t_spanLists[index]._mtx.unlock();\n\n\t\t\tPageCache::GetInstance()->_pageMtx.lock();\n\t\t\tPageCache::GetInstance()->ReleaseSpanToPageCache(span);\n\t\t\tPageCache::GetInstance()->_pageMtx.unlock();\n\n\t\t\t_spanLists[index]._mtx.lock();\n\t\t}\n\n\t\tstart = next;\n\t}\n\n\t_spanLists[index]._mtx.unlock();\n}<\/code><\/pre>\n\n\n\n<p><strong>\u6d4b\u8bd5\u7ed3\u679c<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"853\" height=\"83\" src=\"http:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/09\/image-29.png\" alt=\"\" class=\"wp-image-346\" srcset=\"https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/09\/image-29.png 853w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/09\/image-29-300x29.png 300w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/09\/image-29-768x75.png 768w\" sizes=\"auto, (max-width: 853px) 100vw, 853px\" \/><\/figure>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\u6211\u4eec\u7684\u5185\u5b58\u6c60\u662f\u6709\u660e\u663e\u4f18\u52bf\u7684\uff01\uff01\uff01<\/p>\n\n\n\n<p>\u4eca\u5929\u7684\u66f4\u65b0\u5c31\u5230\u8fd9\u91cc\uff0c\u5982\u6709\u9519\u8bef\u6b22\u8fce\u6307\u51fa<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9879\u76ee\u5b8c\u6574\u4ee3\u7801\uff1ahttps:\/\/gitee.com\/tgwTTT\/high-concurrency-memory&#8230;<\/p>\n","protected":false},"author":1,"featured_media":342,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[4],"tags":[],"class_list":["post-341","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c"],"_links":{"self":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/341","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=341"}],"version-history":[{"count":6,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/341\/revisions"}],"predecessor-version":[{"id":894,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/341\/revisions\/894"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/media\/342"}],"wp:attachment":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=341"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=341"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=341"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}