【FreeRTOS源码阅读】<3> heap_x.c

 

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的堆进行初始化,流程如下:

当堆初始化完成后,对数组这段内存的分块如下图:

字节对齐舍弃字节FirstFreeBlockFreeHeappxEnd字节对齐舍弃字节
xStart    

堆空间初始化完成后,继续进行堆内存申请的工作。流程如下:此时xStart.pxNextFreeBlock只想FirstFreeBlock。

申请完堆内存以后,内存表如下图:

字节对齐舍弃字节MallocedBlockMallocedHeapFreeBlock1FreeHeappxEnd字节对齐舍弃字节
  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后内存表有可能成为如下这个样子:

字节对齐舍弃字节MallocedBlock1MallocedHeap1FreeBlock1FreeHeapMallocedBlock2MallocedHeap2FreeBlock2FreeHeap2MallocedBlock3MallocedHeap3pxEnd字节对齐舍弃字节
  xStartFreeBlock2BlockSize   pxEndBlockSize     

 

如果此时调用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 + FreeBlock1MallocedHeap1 + FreeHeapMallocedBlock2MallocedHeap2FreeBlock2FreeHeap2MallocedBlock3MallocedHeap3pxEnd字节对齐舍弃字节
xStartFreeBlock2BlockSize   pxEndBlockSize     

 


heap_5 内存管理阅读

相比于heap_4增加了多段内存的管理

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值