{"id":40,"date":"2025-05-02T22:10:57","date_gmt":"2025-05-02T14:10:57","guid":{"rendered":"https:\/\/monykate.com\/?p=40"},"modified":"2026-03-31T19:22:15","modified_gmt":"2026-03-31T11:22:15","slug":"freertos%e7%9a%84%e5%87%a0%e7%a7%8dheap","status":"publish","type":"post","link":"https:\/\/monykate.com\/index.php\/2025\/05\/02\/freertos%e7%9a%84%e5%87%a0%e7%a7%8dheap\/","title":{"rendered":"FreeRTOS\u7684\u51e0\u79cdHeap"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u4e00\u3001FreeRTOS\u76845\u79cdheap\u5904\u7406\u65b9\u6cd5<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>heap_1.c\uff0c\u6700\u7b80\u5355\u7684\u5904\u7406\uff0c\u5728\u9759\u6001\u533a\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u8d85\u5927\u6570\u7ec4\u4f5c\u4e3aheap\uff0c\u6bcf\u6b21\u7cfb\u7edf\u7533\u8bf7heap\u7a7a\u95f4\u65f6\uff0c\u901a\u8fc7\u5927heap\u6570\u7ec4\u7684\u6307\u9488\u53bb\u63a7\u5236\u7533\u8bf7\u6216\u91ca\u653e<br>a. \u7533\u8bf7\u5185\u5b58\uff1apvPortMalloc (\u7533\u8bf7\u957f\u5ea6)<br>i. \u5f53\u7cfb\u7edf\u914d\u7f6e\u4e3a\u4e0d\u662fbyte\u5bf9\u9f50\u7684\u65f6\u5019\uff0c\u9700\u8981\u68c0\u67e5\u7533\u8bf7\u957f\u5ea6\u5bf9\u9f50\u548c\u5806\u5730\u5740\u5bf9\u9f50<br>ii. \u6302\u8d77\u6240\u6709\u4efb\u52a1<br>iii. \u4ece\u9759\u6001\u5168\u5c40\u53d8\u91cf\u6570\u7ec4\u4e2d\uff0c\u5f53\u524dheap\u9996\u5730\u5740\uff0c\u4f5c\u4e3a\u7533\u8bf7\u7a7a\u95f4\u7684\u5730\u5740<br>iv. \u5c06heap\u9996\u5730\u5740\u5411\u540e\u79fb\u52a8\u7533\u8bf7\u957f\u5ea6<br>v. \u6062\u590d\u6240\u6709\u4efb\u52a1<br>vi. \u8fd4\u56de\u7533\u8bf7\u7a7a\u95f4\u7684\u9996\u5730\u5740<br>b. \u91ca\u653e\u5185\u5b58\uff1apvPortFree \uff08\u5185\u5b58\u6307\u9488\uff09<br>i. \u5c06\u6307\u9488\u7f6e\u7a7a<br>c. \u83b7\u53d6\u5269\u4f59\u5185\u5b58\uff1axPortGetFreeHeapSize\uff0c\u83b7\u53d6\u5f53\u524dheap\u5927\u6570\u7ec4\u5269\u4f59\u957f\u5ea6<br>d. \u7f3a\u70b9\uff1a\u8fd9\u79cd\u65b9\u6cd5\u4f1a\u4ea7\u751f\u5f88\u591a\u7684\u5185\u5b58\u788e\u7247\u548c\u5185\u5b58\u6cc4\u6f0f\uff0c\u56e0\u4e3a\u5185\u5b58\u91ca\u653e\u91ca\u653e\u4e86\u4e2a\u5bc2\u5bde\uff0c\u53ea\u662f\u628a\u6307\u9488\u91ca\u653e\u6389\u4e86\uff0c\u5927heap\u6570\u7ec4\u4e2d\u88ab\u7533\u8bf7\u7684\u6307\u9488\u65e0\u6cd5\u91cd\u590d\u4f7f\u7528\uff1b<br>e. \u4f18\u70b9\uff1a\u7b80\u5355\u53ef\u63a7\uff0c\u4e00\u4e9b\u5bf9\u5185\u5b58\u63a7\u5236\u4e25\u683c\u7684\u3001\u4e0d\u4f7f\u7528\u52a8\u6001\u5185\u5b58\u7684\u65b9\u6848\u4f1a\u4f7f\u7528<br>f. \u7533\u8bf7heap\u65f6\u6302\u8d77\u6240\u6709\u4efb\u52a1\u7684\u76ee\u7684\uff0c\u56e0\u4e3aheap\u662f\u52a8\u6001\u7533\u8bf7\u7684\uff0c\u5982\u679c\u5176\u4ed6\u4efb\u52a1\u4e5f\u6709\u7533\u8bf7\uff0c\u5219\u4f1a\u51fa\u73b0\u5185\u5b58\u7533\u8bf7\u51b2\u7a81\u3002<\/li>\n\n\n\n<li>heap_2.c\uff0c\u589e\u52a0\u4e86\u5185\u5b58\u5757\u7684\u94fe\u8868\u7ed3\u6784\u4f53\uff0c\u5c06\u6240\u6709\u7684\u5185\u5b58\u4ee5\u94fe\u8868\u7684\u5f62\u5f0f\u7ec4\u7ec7\u8d77\u6765\uff0c\u5185\u5b58\u5757\u9996\u5c3e\u76f8\u8fde\u3002\u4f7f\u7528\u7684\u662f\u6700\u4f73\u5339\u914d\u539f\u5219\uff0c\u9002\u7528\u4e8e\u7533\u8bf7\u548c\u91ca\u653e\u7684\u5185\u5b58\u7a7a\u95f4\u5927\u5c0f\u4e00\u81f4\uff0c\u4e5f\u80fd\u907f\u514d\u5185\u5b58\u6cc4\u9732<br>a. \u94fe\u8868\u7ba1\u7406\uff0c\u5b9a\u4e49\u4e00\u4e2a\u94fe\u8868\u5934\u548c\u94fe\u8868\u5c3e\uff0c\u7533\u8bf7\u4e00\u4e2a\u7a7a\u95f2\u94fe\u9879\uff0c\u8868\u5934\u7684next\u6307\u5411\u7a7a\u95f2\uff0c\u7a7a\u95f2\u7684next\u6307\u5411\u8868\u5c3e\uff0c\u5f62\u6210\u94fe\u5f0f<br>b. \u5185\u5b58\u7533\u8bf7\uff1apvPortMalloc (\u7533\u8bf7\u957f\u5ea6)<br>i. \u6302\u8d77\u6240\u6709\u4efb\u52a1<br>ii. heap\u5982\u9700\u521d\u59cb\u5316\u5219\u521d\u59cb\u5316<br>iii. \u8ba1\u7b97\u94fe\u8868\u9644\u52a0\u957f\u5ea6\uff0c\u4e3b\u8981\u662f\u94fe\u8868\u7ed3\u6784\u4f53\u957f\u5ea6\uff0c\u6700\u540e\u603b\u957f\u5ea6\u4e3a \u9644\u52a0\u957f\u5ea6\u52a0\u4e0a\u7533\u8bf7\u957f\u5ea6<br>iv. \u521b\u5efa\u4e34\u65f6\u9879\uff0c\u9879\u6307\u9488\u6302\u5230\u8868\u5934\u7684next\u4e0a\uff0c\u9879\u7684next\u6307\u5411freeblock\u7a7a\u95f2\u9879<br>v. \u6062\u590d\u6240\u6709\u4efb\u52a1<br>vi. \u8fd4\u56de\u7533\u8bf7\u7a7a\u95f4\u5730\u5740<br>c. \u91ca\u653e\u5185\u5b58\uff1apvPortFree \uff08\u5185\u5b58\u6307\u9488\uff09\uff0c\u652f\u6301\u91ca\u653e\u5185\u5b58\uff0c\u51cf\u5c11\u5185\u5b58\u6cc4\u9732<\/li>\n\n\n\n<li>heap_3.c\uff0c\u8c03\u7528C\u5e93\u7684malloc\u548cfree\u7533\u8bf7\u548c\u91ca\u653e\u5185\u5b58\u3002\u8fd9\u79cd\u65b9\u5f0f\u7684\u6267\u884c\u5b58\u5728\u4e0d\u786e\u5b9a\u6027\uff0c\u4ee3\u7801\u91cf\u6bd4\u8f83\u5927\uff0c\u4e5f\u5b58\u5728\u7ebf\u7a0b\u5b89\u5168\u7684\u95ee\u9898\uff0c\u4e0d\u592a\u9002\u5408RTOS\u3002\u6240\u8bf4\u7684\u7ebf\u7a0b\u5b89\u5168\u4e3b\u8981\u662f\u5bf9\u5185\u5b58\u7684\u64cd\u4f5c\u6ca1\u6709\u505a\u5230\u7ebf\u7a0b\u4e92\u65a5\uff0c\u53ef\u80fd\u4f1a\u51fa\u73b0\u5185\u5b58\u8bbf\u95ee\u51b2\u7a81\uff0c\u5728heap1heap2heap4\u4e2d\u7533\u8bf7\u5185\u5b58\u524d\u90fd\u4f1a\u5c06\u8c03\u5ea6\u5668\u6302\u8d77\uff0c\u786e\u4fdd\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u64cd\u4f5c\u5185\u5b58\u3002<\/li>\n\n\n\n<li>heap_4.c\uff0c\u662fheap2\u7684\u589e\u5f3a\u7248\uff0c\u540c\u6837\u662f\u901a\u8fc7\u94fe\u8868\u7ba1\u7406\u5185\u5b58\u5757\uff0c\u5339\u914d\u7b97\u6cd5\u7f16\u7a0b\u4e86\u9996\u6b21\u5339\u914d\u539f\u5219\uff0c\u540c\u65f6\u652f\u6301\u5185\u5b58\u7684\u5408\u5e76\uff0c\u51cf\u5c11\u5185\u5b58\u7684\u788e\u7247\u5316\u3002<br>a. \u94fe\u8868\u7ba1\u7406\uff0c\u5b9a\u4e49\u4e00\u4e2a\u94fe\u8868\u5934\u548c\u94fe\u8868\u5c3e\uff0c\u7533\u8bf7\u4e00\u4e2a\u7a7a\u95f2\u94fe\u9879\uff0c\u8868\u5934\u7684next\u6307\u5411\u7a7a\u95f2\uff0c\u7a7a\u95f2\u7684next\u6307\u5411\u8868\u5c3e\uff0c\u5f62\u6210\u94fe\u5f0f<br>b. \u5185\u5b58\u7533\u8bf7<br>c. \u5185\u5b58\u91ca\u653e<\/li>\n\n\n\n<li>heap_5.c\uff0c\u5728heap4\u7684\u57fa\u7840\u4e0a\u652f\u6301\u4e86\u5185\u5b58\u5206\u533a\u3002<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e8c\u3001heap4\u4ee3\u7801\u5206\u6790<br>typedef struct {<br>BlockLink_t *pxNextFreeBlock;<br>size_t xBlockSize;<br>}BlockLink_t;<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5168\u5c40\u53d8\u91cf\uff0c<br>a. \u7528\u4e8e\u5355\u5411\u94fe\u8868\u63a7\u5236\u94fe\u8868\u5934BlockLink_t xStart\uff0c\u94fe\u8868\u5c3eBlockLink_t *xEnd = NULL\uff0c\u6ce8\u610f\u94fe\u8868\u5934\u662f\u94fe\u8868\u9879\u5b9e\u4f53\uff0c\u94fe\u8868\u5c3e\u662f\u94fe\u8868\u9879\u6307\u9488\uff0c\u8bf4\u660e\u8868\u5c3e\u53ea\u505a\u4e00\u4e2a\u7ed3\u675f\u6807\u8bb0\uff0c\u7533\u8bf7\u65f6\u5224\u65ad\u65b0\u8868\u9879\u6307\u9488\u662f\u5426\u7b49\u4e8e\u8868\u5c3e\u3002<br>b. \u7f13\u51b2\u6c60\u5269\u4f59Byte\u6570\uff1asize_t xFreeBytesRemaining = 0;<br>c. xMinimumEverFreeByteRemaining = 0;<br>d. \u5df2\u5206\u914d\u5b57\u8282\u6570\uff1axNumberOfSuccessfulAllocations = 0;<br>e. \u672a\u5206\u914d\u5b57\u8282\u6570\uff1axNumberOfSuccessfulFrees = 0;<\/li>\n\n\n\n<li>heap\u521d\u59cb\u5316<br>heap\u7684\u539f\u7406\u662f\u7533\u8bf7\u4e00\u7247\u5927\u7684\u5185\u5b58\u6c60\uff0c\u5c06\u5185\u5b58\u6c60\u5206\u5272\u6210free\u5185\u5b58\u5757\uff0c\u6bcf\u4e2afree\u5185\u5b58\u5757\u7ec4\u7ec7\u4e3a\u8fde\u7eed\u5730\u5740\u7684\u3010block\u7ed3\u6784\u4f53 + \u5185\u5b58\u7a7a\u95f4\u3011\uff0c<br>\u5c06\u6240\u6709freeblock\u901a\u8fc7\u94fe\u8868\u7684\u65b9\u5f0f\u8fde\u63a5\u8d77\u6765\uff0c\u5f53\u8981\u7533\u8bf7\u7a7a\u95f4\u65f6\u53d6\u4e00\u4e2a\u6ee1\u8db3\u5927\u5c0f\u7684freeblock\uff0c\u5c06\u5185\u5b58\u7a7a\u95f4\u8fd4\u56de\u5e94\u7528\uff0c\u5c06\u5269\u4f59\u7684\u5185\u5b58<br>\u91cd\u65b0\u521b\u5efa\u4e00\u4e2a\u5c0f\u7684\u65b0freeblock\uff0c\u63d2\u5165\u5230\u94fe\u8868\u4e2d\u53bb\u3002\u91ca\u653e\u4e5f\u662f\u4e00\u6837\uff0c\u5c06\u62ff\u5230\u7684\u7a7a\u95f4\u8f6c\u6362\u6210freeblock\u683c\u5f0f\u5e76\u505a\u4e00\u4e9b\u6570\u636e\u6e050\uff0c\u63d2\u5165\u5230<br>freeblock\u94fe\u8868\u4e2d\u53bb\u3002\u90a3\u4e48\u521d\u59cb\u5316heap\uff0c\u9700\u8981\u8bbe\u7f6e\u51e0\u4e2a\u5168\u5c40\u53d8\u91cf\u7684\u503c\uff0c\u5269\u4f59\u5b57\u8282\u6570\u3001\u5df2\u5206\u914d\u5b57\u8282\u6570\u3001\u672a\u5206\u914d\u5b57\u8282\u6570\u4ee5\u53ca<br>\u3010\u8868\u5934 + freeblock + \u8868\u5c3e\u3011\uff0c\u8fd9\u65f6\u5019\u7684freeblock\u5360\u7528\u5185\u5b58\u6c60\u51fa\u53bb\u8868\u5934\u8868\u5c3e\u7684\u5168\u90e8\u7a7a\u95f4\u3002<br>i. \u8868\u5934\u7684nextfreeblock\u6307\u5411\u5185\u5b58\u6c60\uff0c\u6b64\u65f6freeblock\u53ea\u6709\u4e00\u4e2a\uff0c\u6574\u4e2a\u5185\u5b58\u6c60\uff0c\u6bcf\u6b21\u7533\u8bf7\u662f\u5220\u4e00\u4e2a\u63d2\u4e00\u4e2a\uff0c\u6309\u65f6\u6bcf\u6b21\u91ca\u653e\u5219\u662f\u63d2\u5165\u65b0\u7684\uff0c<br>\u6240\u4ee5\u968f\u7740\u540e\u9762\u91ca\u653e\u7a7a\u95f4\u800c\u63d2\u5165\u7684freeblock\u9010\u6e10\u589e\u591a\u3002<br>ii. \u8868\u5934\u7684\u5927\u5c0f\u4e3a0\uff0c\u8868\u793a\u8868\u5934\u6ca1\u6709\u4f7f\u7528\u7a7a\u95f4<br>iii. \u8bbe\u7f6e\u5269\u4f59\u957f\u5ea6<\/li>\n\n\n\n<li>\u5185\u5b58\u7533\u8bf7<br>a. \u8ba1\u7b97\u9644\u52a0\u957f\u5ea6\uff1a\u9644\u52a0\u957f\u5ea6 = Block\u7ed3\u6784\u4f53\u957f\u5ea6 + \u8bf7\u6c42\u957f\u5ea6\u540e\u7684\u975e\u5bf9\u9f50\u5269\u4f59\u957f\u5ea6\u3002\u6bd4\u5982\u8bf7\u6c429\u4e2a\u5b57\u8282\uff0c4Byte\u5bf9\u9f50\u7684\uff0c\u90a3\u4e48\u9644\u52a0\u957f\u5ea6 = sizeof(Block) + ( 4 &#8211; (9&amp; 0x11))<br>b. \u8ba1\u7b97\u9644\u52a0\u957f\u5ea6\u52a0\u4e0a\u7533\u8bf7\u957f\u5ea6\u540e\u662f\u5426\u4f1a\u5bfc\u81f4\u5185\u5b58\u6c60\u6ea2\u51fa\uff0c\u4e0d\u4f1a\u5219 \u7533\u8bf7\u957f\u5ea6+= \u9644\u52a0\u957f\u5ea6\uff1b<br>c. \u7533\u8bf7\u957f\u5ea6\u7684\u6700\u9ad8bit\u88ab\u7cfb\u7edf\u5360\u7528\uff0c\u7528\u4e8e\u5224\u65ad\u5f53\u524d\u5185\u5b58\u5c5e\u4e8e\u5e94\u7528\u8fd8\u662f\u5185\u6838\uff0c\u5b9e\u9645\u662f\u5224\u65ad\u957f\u5ea6\u662f\u5426\u8d85\u51fa\u8303\u56f4\uff0c\u5982\u679c\u662f0\u5219\u6ca1\u8d85\uff0c1\u5c31\u662f\u8d85\u4e86\u3002Free RTOS\u727a\u7272\u4e86\u6700\u5927\u957f\u5ea6\u7684\u4e00\u4e2abit\uff0c\u6bd4\u5982\u6700\u5927\u80fd\u7533\u8bf74G\u7684\uff0c\u53d8\u6210\u6700\u59272G\uff0c\u4e5f\u6362\u6765\u4e86\u66f4\u5927\u597d\u5904\uff0c\u540e\u7eed\u5206\u6790\u3002<br>d. \u5224\u65ad\u7533\u8bf7\u957f\u5ea6\u5927\u4e8e0\u4e14\u5c0f\u4e8e\u5269\u4f59\u957f\u5ea6\uff0c\u662f\u5219\u7ee7\u7eed\u5982\u4e0b\u7533\u8bf7<br>e. first fit\u539f\u5219\uff0c\u4ece\u8868\u5934\u5f00\u59cb\u5faa\u73af\u67e5\u627e\u6bcf\u4e00\u4e2afree\u5185\u5b58block\uff0c\u627e\u5230\u5927\u4e8e\u7533\u8bf7\u957f\u5ea6\u7684\u7b2c\u4e00\u4e2ablock\uff0c\u5c31\u662f\u76ee\u6807block\uff0c\u4e00\u8d77\u83b7\u53d6\u5230\u7684\u8fd8\u6709\u4e0a\u4e00\u4e2ablock\u9879\u3002<br>f. \u4e0a\u4e00\u4e2ablock\u9879\u7684\u6210\u5458nextfreeblock\u7684\u5730\u5740\u52a0\u4e0a\u9879\u5934\u957f\u5ea6\u5c31\u80fd\u5b9a\u4f4d\u5230\u5b9e\u9645\u80fd\u4f7f\u7528\u7684\u5185\u5b58\u7a7a\u95f4\uff1avreturn = \u4e0a\u4e00\u4e2a\u8868\u7684pxNextFreeBlock + \u5f53\u524d\u8868\u5934\u7ed3\u6784\u4f53\u957f\u5ea6sizeof(Block)<br>g. \u6574\u4e2a\u8fc7\u7a0b\u662f\uff0c\u4ecefreeblock\u94fe\u8868\u4e2d\u627e\u5230\u7b2c\u4e00\u4e2a\u957f\u5ea6\u6ee1\u8db3\u7684freeblock\uff0c\u5c06\u5176\u4ecefreeblock\u94fe\u8868\u4e2d\u5220\u6389\uff0c\u4ece\u8fd9\u4e2ablock\u4e2d\u5206\u914d\u4e00\u4e2a\u7533\u8bf7\u7684\u957f\u5ea6\u7684block\uff0c\u7136\u540e\u7528\u5269\u4f59\u7684\u7a7a\u95f4\u521b\u5efa\u4e00\u4e2a\u65b0\u7684freeblock\uff0c\u7136\u540e\u63d2\u5165\u5230freeblock\u94fe\u8868\u4e2d\u3002<\/li>\n\n\n\n<li>\u91ca\u653e\u5185\u5b58<br>a. \u91ca\u653e\u5185\u5b58\u4e0e\u7533\u8bf7\u5185\u5b58\u7684\u64cd\u4f5c\u76f8\u53cd\uff0c\u62ff\u5230\u4e00\u7247\u5185\u5b58\u7684\u5730\u5740\u540e\uff0c\u5f80\u524d\u79fb\u52a8block\u5934\u7ed3\u6784\u4f53\u7684\u957f\u5ea6\uff0c\u53ef\u4ee5\u5b9a\u4f4d\u5230\u8be5\u5185\u5b58\u7a7a\u95f4\u7684block\u7ba1\u7406\u7ed3\u6784\u4f53<br>b. \u5c06\u5185\u5b58\u7a7a\u95f4\u6e050<br>c. \u5168\u5c40freeremain\u589e\u52a0\u6574\u4e2ablock\u7684\u957f\u5ea6<br>d. \u5c06\u9996\u5730\u5740\u8f6c\u6362\u6210\u4e00\u4e2afreeblock\u5757<br>e. \u63d2\u5165\u5230freeblock\u94fe\u8868\u4e2d\u5373\u5b8c\u6210\u5185\u5b58\u7684free\u3002<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001FreeRTOS\u76845\u79cdheap\u5904\u7406\u65b9\u6cd5 \u4e8c\u3001heap4\u4ee3\u7801\u5206\u6790typedef struct {BlockL [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_gspb_post_css":"","footnotes":""},"categories":[1,30],"tags":[],"class_list":["post-40","post","type-post","status-publish","format-standard","hentry","category-uncategorized","category-30"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/monykate.com\/index.php\/wp-json\/wp\/v2\/posts\/40","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/monykate.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/monykate.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/monykate.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/monykate.com\/index.php\/wp-json\/wp\/v2\/comments?post=40"}],"version-history":[{"count":3,"href":"https:\/\/monykate.com\/index.php\/wp-json\/wp\/v2\/posts\/40\/revisions"}],"predecessor-version":[{"id":71,"href":"https:\/\/monykate.com\/index.php\/wp-json\/wp\/v2\/posts\/40\/revisions\/71"}],"wp:attachment":[{"href":"https:\/\/monykate.com\/index.php\/wp-json\/wp\/v2\/media?parent=40"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/monykate.com\/index.php\/wp-json\/wp\/v2\/categories?post=40"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/monykate.com\/index.php\/wp-json\/wp\/v2\/tags?post=40"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}