{"id":340763,"date":"2021-02-13T18:57:42","date_gmt":"2021-02-13T10:57:42","guid":{"rendered":"http:\/\/4563.org\/?p=340763"},"modified":"2021-02-13T18:57:42","modified_gmt":"2021-02-13T10:57:42","slug":"%e4%bd%bf%e7%94%a8-c-%e5%ae%9e%e7%8e%b0%e7%9a%84%e7%ae%80%e5%8d%95%e5%86%85%e5%ad%98%e6%b1%a0","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=340763","title":{"rendered":"\u4f7f\u7528 c++ \u5b9e\u73b0\u7684\u7b80\u5355\u5185\u5b58\u6c60"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u4f7f\u7528 c++ \u5b9e\u73b0\u7684\u7b80\u5355\u5185\u5b58\u6c60               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Aidenboss <\/span>  <span><i><\/i> 0<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u4f5c\u4e3a c++ \u65b0\u4eba\uff0c\u5199\u4e00\u4e9b\u7b80\u5355\u7684\u5c0f\u5de5\u5177\uff0c\u9879\u76ee\u5730\u5740\uff1a https:\/\/github.com\/yemingfeng\/cmm\/<\/p>\n<h3>\u4f7f\u7528 c++ \u5b9e\u73b0\u7684\u7b80\u5355\u5185\u5b58\u6c60<\/h3>\n<ul>\n<li>&lt;input checked=&#8221;&#8221; disabled=&#8221;&#8221; type=&#8221;checkbox&#8221;&gt; \u7b80\u5355\u7684\u5185\u5b58\u5206\u914d\u548c\u91ca\u653e<\/li>\n<li>&lt;input disabled=&#8221;&#8221; type=&#8221;checkbox&#8221;&gt; \u589e\u52a0\u5185\u5b58\u5757\u7533\u8bf7\u7684\u6700\u4f18\u5339\u914d\u7b97\u6cd5<\/li>\n<li>&lt;input disabled=&#8221;&#8221; type=&#8221;checkbox&#8221;&gt; \u591a\u7ebf\u7a0b\u652f\u6301<\/li>\n<li>&lt;input disabled=&#8221;&#8221; type=&#8221;checkbox&#8221;&gt; &#8230;<\/li>\n<\/ul>\n<h4>\u8fd0\u884c<\/h4>\n<pre><code>mkdir build cd build cmake -j 6 .. make .\/cmm <\/code><\/pre>\n<h4>\u539f\u7406<\/h4>\n<h5>Node \u8282\u70b9(\u7528\u4e8e\u5b58\u50a8\u771f\u5b9e\u7684\u5185\u5b58\u7a7a\u95f4)<\/h5>\n<pre><code>    class Node {     public:         \/\/ \u771f\u5b9e\u5f00\u8f9f\u7684\u7a7a\u95f4         char *data;         \/\/ \u5f00\u8f9f\u7a7a\u95f4\u7684\u5927\u5c0f         size_t size;         \/\/ \u6307\u5411\u4e0b\u4e00\u5757\u7684 node         Node *next;          Node() {             data = nullptr;             size = 0;             next = nullptr;         }          Node(size_t size) {             this-&gt;size = size;             this-&gt;data = new char[size];             next = nullptr;         }          ~Node() {             delete data;         }     }; <\/code><\/pre>\n<h5>Pool \u903b\u8f91<\/h5>\n<p>pool \u7ef4\u62a4\u4e00\u4e2a Node* free\uff0c\u7528\u4e8e\u8868\u793a\u5f53\u524d\u5df2\u7ecf\u7533\u8bf7\u5e76\u4e14\u53ef\u7528\u7684 Node \u5217\u8868\uff0c\u6ce8\u610f\u8fd9\u662f\u4e00\u4e2a<strong>\u54e8\u5175 node<\/strong><\/p>\n<h6>\u7533\u8bf7\u5185\u5b58<\/h6>\n<ul>\n<li>\u5f53\u7533\u8bf7\u4e00\u5757\u5185\u5b58\u7a7a\u95f4\uff0c\u9996\u5148\u4ece free \u5217\u8868\u67e5\u627e\uff0c\u5982\u679c\u5b58\u5728 node \u6ee1\u8db3 size_t &gt;= size\uff0c\u5219\u4f7f\u7528\u8be5 node<\/li>\n<li>\u5982\u679c\u4e0d\u5b58\u5728\u6ee1\u8db3\u6761\u4ef6\u7684 node\uff0c\u5219\u5f00\u8f9f\u4e00\u4e2a\u65b0\u7684\u7a7a\u95f4\uff0c\u6838\u5fc3\u903b\u8f91\u5982\u4e0b <\/li>\n<\/ul>\n<pre><code>        Node *findCanUse(size_t size) {             Node *result = nullptr;             \/\/ \u67e5\u627e\u80fd\u591f\u6ee1\u8db3\u6b64\u6b21\u5f00\u8f9f\u7a7a\u95f4\u7684 node             \/\/ \u6ce8\u610f\uff1a\u8fd9\u91cc\u662f\u5224\u65ad it-&gt;next \u662f\u5426\u6ee1\u8db3\u6761\u4ef6\uff0c\u8fd9\u6837\u7684\u597d\u5904\u662f\u6613\u4e8e\u6839\u636e it \u8282\u70b9\u79fb\u9664 it-&gt;next \u8282\u70b9             Node *it = free;             while (it) {                 if (it-&gt;next &amp;&amp; it-&gt;next-&gt;size &gt;= size) {                     \/\/ \u5c06\u8fd9\u4e2a\u8282\u70b9\u79fb\u9664                     result = it-&gt;next;                     it-&gt;next = it-&gt;next-&gt;next;                     break;                 }                 it = it-&gt;next;             }             return result;         }          \/\/ \u7533\u8bf7\u4e00\u5757\u5185\u5b58\u7a7a\u95f4         Node *allocate(size_t size) {             \/\/ \u67e5\u627e free \u5217\u8868\u53ef\u7528\u7684 node             Node *result = findCanUse(size);              \/\/ \u8bf4\u660e free \u5217\u8868\u672a\u627e\u5230\u53ef\u7528\u7684 node\uff0c\u9700\u8981\u91cd\u65b0\u5f00\u8f9f\u8fd9\u5757\u7a7a\u95f4             if (!result) {                 std::cout &lt;&lt; \"\u672a\u627e\u5230\u5408\u9002\u7684\u7a7a\u95f4\uff0c\u9700\u8981\u91cd\u65b0\u5f00\u8f9f\uff1a\" &lt;&lt; size &lt;&lt; std::endl;                 result = new Node(size);             }              return result;         } <\/code><\/pre>\n<h6>\u91ca\u653e\u5185\u5b58<\/h6>\n<p>\u76f4\u63a5\u5c06 node \u52a0\u5165\u5230 free \u5217\u8868\u4e2d<\/p>\n<pre><code>        \/\/ \u91ca\u653e\u4e00\u5757\u5185\u5b58\u7a7a\u95f4         void deallocate(Node *node) {             \/\/ \u7b80\u5355\u7684\u65f6\u95f4\uff1a\u5c06 node \u52a0\u5165\u5230 free \u5217\u8868\u6700\u540e             Node *it = free;             while (it-&gt;next) {                 it = it-&gt;next;             }             it-&gt;next = node;         } <\/code><\/pre>\n<h4>\u4f7f\u7528<\/h4>\n<pre><code>class Person { public:     int id;     int sex;      friend std::ostream &amp;operator&lt;&lt;(std::ostream &amp;out, Person &amp;p) {         out &lt;&lt; \"id = \" &lt;&lt; p.id &lt;&lt; \"tsex = \" &lt;&lt; p.sex &lt;&lt; \"tpointer = \" &lt;&lt; (&amp;p) &lt;&lt; std::endl;         return out;     } };  void mock(Person *person) {     srand((unsigned) time(nullptr));     int id = std::abs(std::rand() % 100);     person-&gt;id = id;     person-&gt;sex = id; }  int main() {     CMM::Pool pool;      size_t size = sizeof(Person);      auto oneNode = pool.allocate(size);     auto *one = reinterpret_cast&lt;Person *&gt;(oneNode-&gt;data);     mock(one);     std::cout &lt;&lt; *one;     \/\/ \u6d4b\u8bd5\u91ca\u653e\u5185\u5b58\u903b\u8f91     pool.deallocate(oneNode);      auto twoNode = pool.allocate(size);     auto *two = reinterpret_cast&lt;Person *&gt;(twoNode-&gt;data);     mock(two);     \/\/ \u8fd9\u91cc\u8f93\u51fa\u7684\u5185\u5b58\u5730\u5740\u548c oneNode \u662f\u4e00\u6837\u7684     std::cout &lt;&lt; *two;      auto threeNode = pool.allocate(size);     auto *three = reinterpret_cast&lt;Person *&gt;(threeNode-&gt;data);     mock(three);     \/\/ \u8fd9\u91cc\u8f93\u51fa\u7684\u5185\u5b58\u5730\u5740\u548c oneNode \u662f\u4e0d\u4e00\u6837\u7684     std::cout &lt;&lt; *three;      return 0; } <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>9<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5266904\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : QBugHunter <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5355\u5411\u94fe\u8868\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5266905\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : QBugHunter <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6211\u6ca1\u592a\u4ed4\u7ec6\u770b\uff0c\u4f60\u8bf4\u652f\u6301\u591a\u7ebf\u7a0b\u7684\uff0c\u4f46\u6211\u770b findCanUse \u51fd\u6570\u5c31\u4e0d\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u591a\u7ebf\u7a0b\u7f16\u7a0b\u73af\u5883\u4e0b\uff0c\u90a3\u4e9b\u51fd\u6570\u662f\u5426\u7ebf\u7a0b\u5b89\u5168\u6807\u6ce8\u4e0b\u5427                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5266906\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : cxytz01 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f60\u4e3a\u4ec0\u4e48\u8981\u5b9e\u73b0\u5185\u5b58\u6c60\uff0cmalloc \u5c31\u662f\u4e00\u4e2a\u5185\u5b58\u6c60\uff0c\u8fd8\u4e0d\u591f\u597d\uff0c\u4e0d\u591f\u5feb\u5417\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5266907\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : dangyuluo <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @cxytz01 \u591f\u597d\uff0c\u591f\u5feb\uff0c\u5c31\u662f\u5f88\u591a\u5730\u65b9\u5c31\u662f\u4e0d\u80fd\u7528\u5b83\u3002\u5f88\u591a safety critical \u7684\u7a0b\u5e8f\u662f\u4e0d\u80fd\u7b80\u5355\u7684\u7528 malloc\/new \u7684\uff0c\u5fc5\u987b\u5b9e\u73b0\u81ea\u5df1\u7684\u5185\u5b58\u7ba1\u7406\u3002\u53ef\u4ee5\u53c2\u8003\u4e0b std::pmr \u7684\u76ee\u7684                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5266908\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : edimetia3d <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8fd9\u4e2a\u5e94\u8be5\u53ea\u80fd\u7b97\u662f allocator \u5427, \u5185\u5b58\u6c60\u7684&#8221;\u6c60&#8221;\u5462. <br \/>\u800c\u4e14\u8fd9\u4e2a O(N)\u7684 alloc \u548c free \u771f\u7684\u5927\u4e08\u592b?<\/p>\n<p>\u4e3b\u53ef\u4ee5\u641c\u641c\u5185\u5b58\u6c60\u7684\u5e94\u7528\u573a\u666f,\u770b\u770b\u80fd\u4e0d\u80fd\u89e3\u51b3\u5e94\u7528\u573a\u666f\u7684\u75db\u70b9. \u5206\u914d\u5668\u8fd9\u5757\u53ef\u4ee5\u53c2\u8003\u8457\u540d\u7684 tcmalloc                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5266909\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : alazysun <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8fd9\u3002\u3002\u3002\u65b0\u4eba\u6211\u5efa\u8bae\u4f60\u4ece\u5185\u5b58\u961f\u5217\u5f00\u59cb\uff0c\u5f88\u663e\u7136\u4f60\u6ca1\u641e\u660e\u767d\u6c60\u5b50\u5bf9\u6982\u5ff5                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5266910\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : SmallZheng <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             unique_ptr                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5266911\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wasd6267016 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5927\u90e8\u5206\u5185\u5b58\u6c60\u6ca1\u4e2a\u4e09\u5e74\u7ef4\u62a4\u90fd\u4e0d\u5982\u539f\u751f\u7684\u597d                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5266912\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : yasaminekldkxf <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u60f3\u8d77 redis \u6e90\u7801\u4e2d\u7684 zmalloc                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528 c++ \u5b9e\u73b0\u7684\u7b80\u5355\u5185\u5b58\u6c60 \u8cc7&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/340763"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=340763"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/340763\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=340763"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=340763"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=340763"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}