FreeRTOS Heap Memory Management (3) - heap2源码分析
/* FreeRTOS Kernel V10.4.1 */
原文链接:https://www.cnblogs.com/yanpio/p/14818706.html
1 变量与结构定义
#include <stdlib.h>
#define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
#include "FreeRTOS.h"
#include "task.h"
#undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
#if ( configSUPPORT_DYNAMIC_ALLOCATION == 0 )
#error This file must not be used if configSUPPORT_DYNAMIC_ALLOCATION is 0
#endif
/* 见 FreeRTOS Heap Memory Management (2) - heap1源码分析 ( https://www.cnblogs.com/yanpio/p/14817341.html ) */
#define configADJUSTED_HEAP_SIZE ( configTOTAL_HEAP_SIZE - portBYTE_ALIGNMENT )
/* Initialises the heap structures before their first use.*/
static void prvHeapInit( void );
/* 见 FreeRTOS Heap Memory Management (2) - heap1源码分析 ( https://www.cnblogs.com/yanpio/p/14817341.html ) */
#if ( configAPPLICATION_ALLOCATED_HEAP == 1 )
/* The application writer has already defined the array used for the RTOS
* heap - probably so it can be placed in a special segment or address. */
extern uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
#else
static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
#endif /* configAPPLICATION_ALLOCATED_HEAP */
/* 定义单链表结构来分配内存 */
typedef struct A_BLOCK_LINK
{
struct A_BLOCK_LINK * pxNextFreeBlock; /*<< The next free block in the list. */
size_t xBlockSize; /*<< The size of the free block. */
} BlockLink_t;
/* 在分配内存时,每个分配的block都会附带一个结构体变量,包含对这个block的说明,所以需要对结构体变量进行对齐。
* ( & ~portBYTE_ALIGNMENT_MASK ) 操作相当于 ( % portBYTE_ALIGNMENT ) */
static const uint16_t heapSTRUCT_SIZE = ( ( sizeof( BlockLink_t ) + ( portBYTE_ALIGNMENT - 1 ) ) & ~portBYTE_ALIGNMENT_MASK );
/* 分配内存时,有时需要从一个较大的block中拆分出一段用于分配,剩下的一段继续留作下一次分配 。
* 而如果剩下的空间大小小于此值,则不再拆分,直接整个分配出去 */
#define heapMINIMUM_BLOCK_SIZE ( ( size_t ) ( heapSTRUCT_SIZE * 2 ) )
/* 定义两个节点,分别指向地址空间的首尾. */
static BlockLink_t xStart, xEnd;
/* Keeps track of the number of free bytes remaining, but says nothing about fragmentation. */
static size_t xFreeBytesRemaining = configADJUSTED_HEAP_SIZE;
2 prvInsertBlockIntoFreeList()
/*
* 将静态函数定义为宏,会增加编译时间,但是节省了运行时的函数调用时间.
* 这个宏的作用是在free block list 中,按照small block在前,large block在后的顺序,插入一个 free block
* 由于heap2 分配时采用 best fit algorithm 分配,所以需要按大小排序好,以方便寻找和所需尺寸最接近的 free block
*/
#define prvInsertBlockIntoFreeList( pxBlockToInsert ) \
{ \
BlockLink_t * pxIterator; \
size_t xBlockSize; \
\
xBlockSize = pxBlockToInsert->xBlockSize; \
\
/* 按照 BlockSize 升序,找到链表中合适的插入位置. */ \
for( pxIterator = &xStart; pxIterator->pxNextFreeBlock->xBlockSize < xBlockSize; pxIterator = pxIterator->pxNextFreeBlock)\
{ \
/* There is nothing to do here - just iterate to the correct position. */ \
} \
\
/* 在单向链表中插入新的节点. */ \
pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock; \
pxIterator->pxNextFreeBlock = pxBlockToInsert; \
}
/*-----------------------------------------------------------*/
3 prvHeapInit()
static void prvHeapInit( void )
{
BlockLink_t * pxFirstFreeBlock;
uint8_t * pucAlignedHeap;
/* 首地址对齐,参考 < FreeRTOS Heap Memory Management (2) - heap1源码分析 > 第2节注释(2)*/
pucAlignedHeap = ( uint8_t * ) ( ( ( portPOINTER_SIZE_TYPE ) & ucHeap[ portBYTE_ALIGNMENT ] ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) );
/* 初始化 xStart 节点,即链表的头结点 . */
xStart.pxNextFreeBlock = ( void * ) pucAlignedHeap;
xStart.xBlockSize = ( size_t ) 0;
/* 初始化 xEnd 节点,即链表的尾结点,用于判断搜索是否达到链表尽头. */
xEnd.xBlockSize = configADJUSTED_HEAP_SIZE;
xEnd.pxNextFreeBlock = NULL;
//初始化第一个节点信息,这个节点占据了整个内存空间。后面分配时不断将此节点指向的内存空间进行split
pxFirstFreeBlock = ( void * ) pucAlignedHeap;
pxFirstFreeBlock->xBlockSize = configADJUSTED_HEAP_SIZE;
pxFirstFreeBlock->pxNextFreeBlock = &xEnd;
}
经过初始化后,内存的布局如下图所示:
4 pvPortMalloc()
void * pvPortMalloc( size_t xWantedSize )
{
BlockLink_t * pxBlock, * pxPreviousBlock, * pxNewBlockLink;
static BaseType_t xHeapHasBeenInitialised = pdFALSE;
void * pvReturn = NULL;
vTaskSuspendAll(); /* 挂起其他任务,防止重入 */
{
/* 首次分配时,需要进行初始化 */
if( xHeapHasBeenInitialised == pdFALSE )
{
prvHeapInit();
xHeapHasBeenInitialised = pdTRUE;
}
/* 分配一个free block需要消耗两部分内存,一是xWantedSize, 而是结构体变量大小 heapSTRUCT_SIZE
* 相加后再进行内存对齐,调节实际大小。内存对齐的做法和heap1源码分析相同。*/
if( xWantedSize > 0 )
{
xWantedSize += heapSTRUCT_SIZE;
if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0 )
{
xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
}
}
if( ( xWantedSize > 0 ) && ( xWantedSize < configADJUSTED_HEAP_SIZE ) )
{
/* free block list 是按照free block 的尺寸进行排序的。
* 从xStart开始,逐个节点开始搜索,直到搜索到满足条件的节点 */
pxPreviousBlock = &xStart;
pxBlock = xStart.pxNextFreeBlock;
/* pxBlock->pxNextFreeBlock != NULL 条件是判断是否是xEnd */
while( ( pxBlock->xBlockSize < xWantedSize ) && ( pxBlock->pxNextFreeBlock != NULL ) )
{
pxPreviousBlock = pxBlock;
/* pxBlock,即待分配的block */
pxBlock = pxBlock->pxNextFreeBlock;
}
/* 如果满足 pxBlock->pxNextFreeBlock == NULL跳出循环,即达到了链表尾部,即没有搜索到 */
if( pxBlock != &xEnd )
{
/* 跳过结构体变量所占的内存,指向实际可用的内存 */
pvReturn = ( void * ) ( ( ( uint8_t * ) pxPreviousBlock->pxNextFreeBlock ) + heapSTRUCT_SIZE );
/* 将搜索到的free block从 list中移除,使上一个节点的next指针指向当前节点的next指针 . */
pxPreviousBlock->pxNextFreeBlock = pxBlock->pxNextFreeBlock;
/* 如果当前的block比所需的空间大(heapMINIMUM_BLOCK_SIZE),则需要将此block 分成两个,将多余部分进行回收. */
if( ( pxBlock->xBlockSize - xWantedSize ) > heapMINIMUM_BLOCK_SIZE )
{
/* 新节点pxNewBlockLink,即待回收的节点,计算其开始地址 */
pxNewBlockLink = ( void * ) ( ( ( uint8_t * ) pxBlock ) + xWantedSize );
/* 计算分裂出的两个block(一个即将分配出去,另一个回收到空闲列表)的大小. */
pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize;
pxBlock->xBlockSize = xWantedSize;
/* 将多余的部分重新插入free block list . */
prvInsertBlockIntoFreeList( ( pxNewBlockLink ) );
}
xFreeBytesRemaining -= pxBlock->xBlockSize;
}
}
traceMALLOC( pvReturn, xWantedSize );
}
( void ) xTaskResumeAll();
/* 如果使用钩子函数,则在分配失败时,调用钩子函数 */
#if ( configUSE_MALLOC_FAILED_HOOK == 1 )
{
if( pvReturn == NULL )
{
extern void vApplicationMallocFailedHook( void );
vApplicationMallocFailedHook();
}
}
#endif
return pvReturn;
}
5 vPortFree()
void vPortFree( void * pv )
{
uint8_t * puc = ( uint8_t * ) pv;
BlockLink_t * pxLink;
if( pv != NULL )
{
/* 回退heapSTRUCT_SIZE个字节,将结构体变量占据的空间也回收掉 */
puc -= heapSTRUCT_SIZE;
/* This unexpected casting is to keep some compilers from issuing byte alignment warnings. */
pxLink = ( void * ) puc;
/* 回收的过程防止重入 */
vTaskSuspendAll();
{
/* 将释放的内存块插入到free block list中 */
prvInsertBlockIntoFreeList( ( ( BlockLink_t * ) pxLink ) );
xFreeBytesRemaining += pxLink->xBlockSize;
traceFREE( pv, pxLink->xBlockSize );
}
( void ) xTaskResumeAll();
}
}
6 其他函数
size_t xPortGetFreeHeapSize( void )
{
return xFreeBytesRemaining;
}
/*-----------------------------------------------------------*/
void vPortInitialiseBlocks( void )
{
/* This just exists to keep the linker quiet. */
}
/*-----------------------------------------------------------*/