heap_1 内存管理阅读
直接数组向后一直申请,不支持释放
heap_2 内存管理阅读
除了不合并空闲块以外,其他和heap_4一样
heap_3 内存管理阅读
直接调用系统的malloc和free
heap_4 内存管理阅读
在创建任务时,会给TCB申请内存空间、给任务的栈申请空间、给状态链表申请空间;在使用队列以及信号量时,会给队列申请空间。申请空间时会调用vPortMalloc函数进行申请。下面就来介绍heap_4的管理机制。
首先根据FreeRTOSConfig.h文件下的configTOTAL_HEAP_SIZE大小定义一个unsigned char的数组,这个数组就是整个内存管理的整个堆空间。整个对空间使用类型为BlockList_t的链表进行维护。
在第一次调用vPortMalloc时,会先调用static void prvHeapInit( void )函数将整个RTOS的堆进行初始化,流程如下:
当堆初始化完成后,对数组这段内存的分块如下图:
字节对齐舍弃字节 | FirstFreeBlock | FreeHeap | pxEnd | 字节对齐舍弃字节 |
xStart |
堆空间初始化完成后,继续进行堆内存申请的工作。流程如下:此时xStart.pxNextFreeBlock只想FirstFreeBlock。
申请完堆内存以后,内存表如下图:
字节对齐舍弃字节 | MallocedBlock | MallocedHeap | FreeBlock1 | FreeHeap | pxEnd | 字节对齐舍弃字节 |
xStart |
调用vPortFree函数可以将指定的堆内存块释放掉;代码和流程如下:
void vPortFree( void *pv )
{
uint8_t *puc = ( uint8_t * ) pv;
BlockLink_t *pxLink;
if( pv != NULL )
{
/* The memory being freed will have an BlockLink_t structure immediately
before it. */
puc -= xHeapStructSize;
/* This casting is to keep the compiler from issuing warnings. */
pxLink = ( void * ) puc;
/* Check the block is actually allocated. */
configASSERT( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 );
configASSERT( pxLink->pxNextFreeBlock == NULL );
if( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 )
{
if( pxLink->pxNextFreeBlock == NULL )
{
/* The block is being returned to the heap - it is no longer
allocated. */
pxLink->xBlockSize &= ~xBlockAllocatedBit;
vTaskSuspendAll();
{
/* Add this block to the list of vPortFree blocks. */
xFreeBytesRemaining += pxLink->xBlockSize;
traceFREE( pv, pxLink->xBlockSize );
prvInsertBlockIntoFreeList( ( ( BlockLink_t * ) pxLink ) );
}
( void ) xTaskResumeAll();
}
else
{
mtCOVERAGE_TEST_MARKER();
}
}
else
{
mtCOVERAGE_TEST_MARKER();
}
}
}
将空闲堆内存块添加到空闲链表中代码和过程如下:
static void prvInsertBlockIntoFreeList( BlockLink_t *pxBlockToInsert )
{
BlockLink_t *pxIterator;
uint8_t *puc;
/* Iterate through the list until a block is found that has a higher address
than the block being inserted. */
for( pxIterator = &xStart; pxIterator->pxNextFreeBlock < pxBlockToInsert; pxIterator = pxIterator->pxNextFreeBlock )
{
/* Nothing to do here, just iterate to the right position. */
}
/* Do the block being inserted, and the block it is being inserted after
make a contiguous block of memory? */
puc = ( uint8_t * ) pxIterator;
if( ( puc + pxIterator->xBlockSize ) == ( uint8_t * ) pxBlockToInsert )
{
pxIterator->xBlockSize += pxBlockToInsert->xBlockSize;
pxBlockToInsert = pxIterator;
}
else
{
mtCOVERAGE_TEST_MARKER();
}
/* Do the block being inserted, and the block it is being inserted before
make a contiguous block of memory? */
puc = ( uint8_t * ) pxBlockToInsert;
if( ( puc + pxBlockToInsert->xBlockSize ) == ( uint8_t * ) pxIterator->pxNextFreeBlock )
{
if( pxIterator->pxNextFreeBlock != pxEnd )
{
/* Form one big block from the two blocks. */
pxBlockToInsert->xBlockSize += pxIterator->pxNextFreeBlock->xBlockSize;
pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock->pxNextFreeBlock;
}
else
{
pxBlockToInsert->pxNextFreeBlock = pxEnd;
}
}
else
{
pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock;
}
/* If the block being inserted plugged a gab, so was merged with the block
before and the block after, then it's pxNextFreeBlock pointer will have
already been set, and should not be set here as that would make it point
to itself. */
if( pxIterator != pxBlockToInsert )
{
pxIterator->pxNextFreeBlock = pxBlockToInsert;
}
else
{
mtCOVERAGE_TEST_MARKER();
}
}
经过多次的Malloc和Free后内存表有可能成为如下这个样子:
字节对齐舍弃字节 | MallocedBlock1 | MallocedHeap1 | FreeBlock1 | FreeHeap | MallocedBlock2 | MallocedHeap2 | FreeBlock2 | FreeHeap2 | MallocedBlock3 | MallocedHeap3 | pxEnd | 字节对齐舍弃字节 | ||
xStart | FreeBlock2 | BlockSize | pxEnd | BlockSize |
如果此时调用vPortFree去释放MallocedHeap1,这个时候查找可插入的位置从xStart开始,xStart->pxNextFreeBlock 是 Free Block1,Free Block1的地址 大于 MallocedBlock1,由于xStart是一个静态变量地址不和这个堆内存连续,所以不满足查找到的空闲块(pxIterator)的尾部和将要插入的内存块(pxBlockToInsert)头部相连,继续查询pxBlockToInsert的尾部和pxIterator->pxNextFreeBlock的头部是否相连,此时是相连的,这个时候将这两个内存块链接起来,之后再添加到链表中去。
pxBlockToInsert->xBlockSize += pxIterator->pxNextFreeBlock->xBlockSize;
pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock->pxNextFreeBlock;
合并之后,内存表如下:
字节对齐舍弃字节 | MallocedBlock1 + FreeBlock1 | MallocedHeap1 + FreeHeap | MallocedBlock2 | MallocedHeap2 | FreeBlock2 | FreeHeap2 | MallocedBlock3 | MallocedHeap3 | pxEnd | 字节对齐舍弃字节 | ||
xStart | FreeBlock2 | BlockSize | pxEnd | BlockSize |
heap_5 内存管理阅读
相比于heap_4增加了多段内存的管理