FreeRTOS --(3)内存管理 heap2
阅读原文时间:2022年05月22日阅读:1

在《FreeRTOS --(2)内存管理 heap1》知道 heap 1 的内存管理其实只是简单的实现了内存对齐的分配策略,heap 2 的实现策略相比 heap 1 稍微复杂一点,不仅仅是提供了分配内存的接口,同时也提供了释放内存的接口;

但是 heap 2 的内存分配策略中,并没有提供空闲内存的合并策略,对内存碎片没有处理;换句话来说,如果有多次的,大小各异的内存申请和释放的场景下,很可能导致很多内存碎片;

1、内存大小

和 heap 1 一样,用于内存管理的内存大小来自于一个大数组,数组的下标就是整个需要被管理的内存的大小,这个是和具体芯片所支持的 RAM 大小相关:

configTOTAL_HEAP_SIZE

被管理的内存定义为:

static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];

ucHeap 就是管理的对象;

2、对齐

有的处理器是对内存对齐有要求的,比如 ARM-CM3 等,AAPCS规则要求堆栈保持8字节对齐。给任务分配栈时需要保证栈是8字节对齐的。所以这里 FreeRTOS 就需要涉及到对齐操作;针对 ARM-CM3 这类处理器来说,在portmacro.h 文件中,定义了对齐的字节数:

/* Hardware specifics. */
#define portBYTE_ALIGNMENT 8

而在 portable.h 中,定义了对应的 Mask(8字节对齐,那么都要是 8 的倍数,也就是二进制的 4'b1000,所以 MASK 是 4'b0111 也就是 0x07):

#if portBYTE_ALIGNMENT == 8
#define portBYTE_ALIGNMENT_MASK ( 0x0007 )
#endif

和 heap 1 一样,在处理对齐的时候,由于可能 ucHeap 初始的地址就没对齐,所以这里真正可以对齐分配的内存的 SIZE 就要做一些调整和妥协,由于是 8 字节对齐,所以最多妥协的大小就是 8 字节,也就是真正被管理的内存大小只有  configADJUSTED_HEAP_SIZE,这里可能造成几个字节的浪费(浪费多少,取决于ucHeap 初始地址 ),不过为了对齐,也就忽略了;

/* A few bytes might be lost to byte aligning the heap start address. */
#define configADJUSTED_HEAP_SIZE ( configTOTAL_HEAP_SIZE - portBYTE_ALIGNMENT )

3、内存块

与 heap 1 不同,heap 2 可以支持分配和释放,那么管理内存的手段势必比 heap 1 复杂一些,heap 2 对内存进行分块管理,将每块内存通过一个表征该内存块的的数据结构表示,以单向链表串在一起;

表达一个内存块的数据结构是 BlockLink_t,它的定义是:

/* Define the linked list structure. This is used to link free blocks in order
of their size. */
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;

pxNextFreeBlock 指向下一个内存块的 BlockLink_t 结构;

xBlockSize 代表本内存块的大小;

当然内存块也需要对齐:

static const uint16_t heapSTRUCT_SIZE = ( ( sizeof ( BlockLink_t ) + ( portBYTE_ALIGNMENT - 1 ) ) & ~portBYTE_ALIGNMENT_MASK );

FreeRTOS 为内存管理,定义了两个 BlockLink_t 结构体,xStart 和 xEnd:

/* Create a couple of list links to mark the start and end of the list. */
static BlockLink_t xStart, xEnd;

xStart 和 xEnd 仅仅作为 mark,标记内存块的起始和结束;

在 heap2 中定义了 xFreeBytesRemaining 来代表当前可用于分配的内存,每当内存被分配出去,这个值会减,内存被free 后,该值增加:

/* Keeps track of the number of free bytes remaining, but says nothing about
fragmentation. */
static size_t xFreeBytesRemaining = configADJUSTED_HEAP_SIZE;

4、分配内存

和 heap 1 一样,内存分配使用 pvPortMalloc 函数,传入的是希望拿到的内存,返回值拿到的内存起始地址,如果分配失败返回 NULL;

/*-----------------------------------------------------------*/

void *pvPortMalloc( size_t xWantedSize )
{
BlockLink_t *pxBlock, *pxPreviousBlock, *pxNewBlockLink;
static BaseType_t xHeapHasBeenInitialised = pdFALSE;
void *pvReturn = NULL;

vTaskSuspendAll();  
{  
    /\* If this is the first call to malloc then the heap will require  
    initialisation to setup the list of free blocks. \*/  
    if( xHeapHasBeenInitialised == pdFALSE )  
    {  
        prvHeapInit();  
        xHeapHasBeenInitialised = pdTRUE;  
    }

    /\* The wanted size is increased so it can contain a BlockLink\_t  
    structure in addition to the requested amount of bytes. \*/  
    if( xWantedSize > 0 )  
    {  
        xWantedSize += heapSTRUCT\_SIZE;

        /\* Ensure that blocks are always aligned to the required number of bytes. \*/  
        if( ( xWantedSize & portBYTE\_ALIGNMENT\_MASK ) != 0 )  
        {  
            /\* Byte alignment required. \*/  
            xWantedSize += ( portBYTE\_ALIGNMENT - ( xWantedSize & portBYTE\_ALIGNMENT\_MASK ) );  
        }  
    }

    if( ( xWantedSize > 0 ) && ( xWantedSize < configADJUSTED\_HEAP\_SIZE ) )  
    {  
        /\* Blocks are stored in byte order - traverse the list from the start  
        (smallest) block until one of adequate size is found. \*/  
        pxPreviousBlock = &xStart;  
        pxBlock = xStart.pxNextFreeBlock;  
        while( ( pxBlock->xBlockSize < xWantedSize ) && ( pxBlock->pxNextFreeBlock != NULL ) )  
        {  
            pxPreviousBlock = pxBlock;  
            pxBlock = pxBlock->pxNextFreeBlock;  
        }

        /\* If we found the end marker then a block of adequate size was not found. \*/  
        if( pxBlock != &xEnd )  
        {  
            /\* Return the memory space - jumping over the BlockLink\_t structure  
            at its start. \*/  
            pvReturn = ( void \* ) ( ( ( uint8\_t \* ) pxPreviousBlock->pxNextFreeBlock ) + heapSTRUCT\_SIZE );

            /\* This block is being returned for use so must be taken out of the  
            list of free blocks. \*/  
            pxPreviousBlock->pxNextFreeBlock = pxBlock->pxNextFreeBlock;

            /\* If the block is larger than required it can be split into two. \*/  
            if( ( pxBlock->xBlockSize - xWantedSize ) > heapMINIMUM\_BLOCK\_SIZE )  
            {  
                /\* This block is to be split into two.  Create a new block  
                following the number of bytes requested. The void cast is  
                used to prevent byte alignment warnings from the compiler. \*/  
                pxNewBlockLink = ( void \* ) ( ( ( uint8\_t \* ) pxBlock ) + xWantedSize );

                /\* Calculate the sizes of two blocks split from the single  
                block. \*/  
                pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize;  
                pxBlock->xBlockSize = xWantedSize;

                /\* Insert the new block into the list of free blocks. \*/  
                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;  

}
/*-----------------------------------------------------------*/

首先调用 vTaskSuspendAll(); 来挂起所有任务,不允许进程调度;

接着调用 prvHeapInit(); 来初始化相关的内存管理的链表结构:

static void prvHeapInit( void )
{
BlockLink_t *pxFirstFreeBlock;
uint8_t *pucAlignedHeap;

/\* Ensure the heap starts on a correctly aligned boundary. \*/  
pucAlignedHeap = ( uint8\_t \* ) ( ( ( portPOINTER\_SIZE\_TYPE ) &ucHeap\[ portBYTE\_ALIGNMENT \] ) & ( ~( ( portPOINTER\_SIZE\_TYPE ) portBYTE\_ALIGNMENT\_MASK ) ) );

/\* xStart is used to hold a pointer to the first item in the list of free  
blocks.  The void cast is used to prevent compiler warnings. \*/  
xStart.pxNextFreeBlock = ( void \* ) pucAlignedHeap;  
xStart.xBlockSize = ( size\_t ) 0;

/\* xEnd is used to mark the end of the list of free blocks. \*/  
xEnd.xBlockSize = configADJUSTED\_HEAP\_SIZE;  
xEnd.pxNextFreeBlock = NULL;

/\* To start with there is a single free block that is sized to take up the  
entire heap space. \*/  
pxFirstFreeBlock = ( void \* ) pucAlignedHeap;  
pxFirstFreeBlock->xBlockSize = configADJUSTED\_HEAP\_SIZE;  
pxFirstFreeBlock->pxNextFreeBlock = &xEnd;  

}

在初始化内存相关的结构的时候,首先将 ucHeap 的地址进行对齐操作,得到可以对齐后用于真实的内存管理的起始地址为:

pucAlignedHeap

然后初始化 xStart 和 xEnd,这两个 marker,然后将整个可用的内存视为一块,可用的内存的开始地方,放置了一个 BlockLink_t 结构体并初始化它的 xBlockSize 为之前调整过的 configADJUSTED_HEAP_SIZE;

我们在回到 pvPortMalloc 的地方,继续分析;

prvHeapInit() 初始化完成后,便可用分配内存了;分配内存的时候,需要对每一个内存块分配一个标志它的描述符,也就是 BlockLink_t 结构体,所以如果要分配 xWantedSize,那么就要分配 :

xWantedSize += heapSTRUCT_SIZE;

然后,对 xWantedSize 进行字节对齐操作;

接下来便进行链表搜寻,找到 Size 合适的地方,将其分配出来;

值得注意的是,内存块链表是有排序的,开始是 xStart 后面跟的内存块,内存块由小到大,最后是 xEnd;

/*
* Insert a block into the list of free blocks - which is ordered by size of
* the block. Small blocks at the start of the list and large blocks at the end
* of the list.
*/
#define prvInsertBlockIntoFreeList( pxBlockToInsert ) \
{ \
BlockLink_t *pxIterator; \
size_t xBlockSize; \
\
xBlockSize = pxBlockToInsert->xBlockSize; \
\
/* Iterate through the list until a block is found that has a larger size */ \
/* than the block we are inserting. */ \
for( pxIterator = &xStart; pxIterator->pxNextFreeBlock->xBlockSize < xBlockSize; pxIterator = pxIterator->pxNextFreeBlock ) \
{ \
/* There is nothing to do here - just iterate to the correct position. */ \
} \
\
/* Update the list to include the block being inserted in the correct */ \
/* position. */ \
pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock; \
pxIterator->pxNextFreeBlock = pxBlockToInsert; \
}

继续看代码;

如果 pxBlock 不是 xEnd 的话,那么说明找到有 Size 大于期望分配的 Size 的 Block 了;

那么就将返回值:

/* Return the memory space - jumping over the BlockLink_t structure at its start. */
pvReturn = ( void * ) ( ( ( uint8_t * ) pxPreviousBlock->pxNextFreeBlock ) + heapSTRUCT_SIZE );

这里,分配内存,能够实际给调用这个 API 接口使用的内存要从起始的 Block 地址加上 heapSTRUCT_SIZE 开始算,因为 heapSTRUCT_SIZE 已经用来表示这个 Block 的信息了;

然后判断剩余的 SIZE 是否大于最小的可用的空间分配的阈值 heapMINIMUM_BLOCK_SIZE :

#define heapMINIMUM_BLOCK_SIZE ( ( size_t ) ( heapSTRUCT_SIZE * 2 ) )

如果剩余的内存空间还足够那么:

/* If the block is larger than required it can be split into two. */
if( ( pxBlock->xBlockSize - xWantedSize ) > heapMINIMUM_BLOCK_SIZE )
{
/* This block is to be split into two. Create a new block
following the number of bytes requested. The void cast is
used to prevent byte alignment warnings from the compiler. */
pxNewBlockLink = ( void * ) ( ( ( uint8_t * ) pxBlock ) + xWantedSize );

/\* Calculate the sizes of two blocks split from the single block. \*/  
 pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize;  
 pxBlock->xBlockSize = xWantedSize;

/\* Insert the new block into the list of free blocks. \*/  
prvInsertBlockIntoFreeList( ( pxNewBlockLink ) );  

}

使用新的 pxNewBlockLink 结构表示摘除 pxBlock 内存块后的下一个内存块,并将其初始化,然后按照排序(从小到大的顺序)插入到以 xStart 开始的地方;

所以,被初始化后的内存

分配一次的结果是:

5、释放内存

heap2 支持释放内存:

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 -= heapSTRUCT\_SIZE;

    /\* This unexpected casting is to keep some compilers from issuing  
    byte alignment warnings. \*/  
    pxLink = ( void \* ) puc;

    vTaskSuspendAll();  
    {  
        /\* Add this block to the list of free blocks. \*/  
        prvInsertBlockIntoFreeList( ( ( BlockLink\_t \* ) pxLink ) );  
        xFreeBytesRemaining += pxLink->xBlockSize;  
        traceFREE( pv, pxLink->xBlockSize );  
    }  
    ( void ) xTaskResumeAll();  
}  

}

来自用户释放的指针 pv 是实际的数据指针,代表这个内存的结构体在他前面 heapSTRUCT_SIZE 的位置,所以该 pv 的 BlockLink_t 结构体指针 pxLink = ( void * )(puc - heapSTRUCT_SIZE);

调用  prvInsertBlockIntoFreeList  将其插入到链表中;并且更新当前剩余的内存量;

释放后的内存如下所示:

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章