|
测试接口函数为:pvPortMalloc 和 vPortFree 调整动态堆大小如下: 修改freertos_project_config.h中的 #define configTOTAL_HEAP_SIZE ((size_t)32768) 每次申请下来的大小会在申请大小的基础上多12个字节左右,这个多余的字节是一些链表信息和字节对齐额外增加的。测试工程如下: void malloc_use_test(void)
{
LOG_DBG("--------------test malloc space start---------------\n");
LOG_DBG("1. test --- 20 Bytes --- space");
size_t before = xPortGetFreeHeapSize();
LOG_DBG("before malloc space %d",before);
void *ptr1 = pvPortMalloc(20);
size_t after = xPortGetFreeHeapSize();
LOG_DBG("after malloc space %d",after);
size_t allocated = before - after;
LOG_DBG("allocated space %d\n",allocated);
vPortFree(ptr1);
LOG_DBG("2. test --- 100 --- Bytes space");
before = xPortGetFreeHeapSize();
LOG_DBG("before malloc space %d",before);
void *ptr2 = pvPortMalloc(100);
after = xPortGetFreeHeapSize();
LOG_DBG("after malloc space %d",after);
allocated = before - after;
LOG_DBG("allocated space %d\n",allocated);
vPortFree(ptr2);
LOG_DBG("3. test --- 1000 --- Bytes space");
before = xPortGetFreeHeapSize();
LOG_DBG("before malloc space %d",before);
void *ptr3 = pvPortMalloc(1000);
after = xPortGetFreeHeapSize();
LOG_DBG("after malloc space %d",after);
allocated = before - after;
LOG_DBG("allocated space %d",allocated);
LOG_DBG("--------------test malloc space end---------------\n");
}
测试log如下:
--------------test malloc space start---------------
1. test --- 20 Bytes --- space
before malloc space 52008
after malloc space 51976
allocated space 32
2. test --- 100 --- Bytes space
before malloc space 52008
after malloc space 51896
allocated space 112
3. test --- 1000 --- Bytes space
before malloc space 52008
after malloc space 51000
allocated space 1008
--------------test malloc space end--------------- |
