FreeRTOS内存管理
DSP所移植的Freerots使用的内存管理是heap4.c,以下对该源文件进行分析。
一、宏&变量解析
- heapMINIMUM_BLOCK_SIZE
/* Block sizes must not get too small.
* 内存块大小不能太小,最小值是堆结构体大小的2倍,DSP中堆结构体大小为4个字节,因此最小的内存块大小为8字节 */
#define heapMINIMUM_BLOCK_SIZE ( ( size_t ) ( xHeapStructSize << 1 ) )
- heapBITS_PER_BYTE
/* Assumes 8bit bytes!
* 每个字节包含8位
*/
#define heapBITS_PER_BYTE ( ( size_t ) 8 )
- ucHeap
/* Allocate the memory for the heap.
* 为堆空间分配内存
* configTOTAL_HEAP_SIZE 指的是用户分配给Freerots管理的空间大小,Freerots实际管理的内存是一个大数组
*/
static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
- BlockLink_t
/* Define the linked list structure. This is used to link free blocks in order
of their memory address.
定义链表结构。这用于按照内存地址的大小顺序的链接空闲内存块 */
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;
- xHeapStructSize
/* The size of the structure placed at the beginning of each allocated memory
block must by correctly byte aligned.
放置在每个分配的内存块开头的结构体的地址大小必须正确对齐字节
也就是说每个内存块都包含两部分,结构体+内存*/
static const size_t xHeapStructSize = ( sizeof( BlockLink_t ) + ( ( size_t ) ( portBYTE_ALIGNMENT - 1 ) ) ) & ~( ( size_t ) portBYTE_ALIGNMENT_MASK );
- xStart & *pxEnd
/* Create a couple of list links to mark the start and end of the list.
* 创建几个列表链接来标记列表的开始和结束
*/
static BlockLink_t xStart, *pxEnd = NULL;
- xFreeBytesRemaining & xMinimumEverFreeBytesRemaining
/* Keeps track of the number of free bytes remaining, but says nothing about
* fragmentation.
* 跟踪剩余的可用字节数,但对碎片一无所知
*/
static size_t xFreeBytesRemaining = 0U; /* 当前剩余的空闲内存空间大小 */
static size_t xMinimumEverFreeBytesRemaining = 0U; /* 历史最小剩余的空闲内存空间大小 */
- xBlockAllocatedBit
/* Gets set to the top bit of an size_t type. When this bit in the xBlockSize
* member of an BlockLink_t structure is set then the block belongs to the
* application. When the bit is free the block is still part of the free heap
* space.
获取设置为size_t类型的最高位的值。当BlockLink_t结构的xBlockSize成员中的此位被置1时,表示该内存块属于应用程序。当该位空闲时,表示该块仍然是空闲堆空间的一部分。 */
static size_t xBlockAllocatedBit = 0;
二、API函数解析
1. prvHeapInit
1.1 函数源码
/* 内存初始化 */
static void prvHeapInit( void )
{
BlockLink_t *pxFirstFreeBlock;
uint8_t *pucAlignedHeap;
size_t uxAddress;
size_t xTotalHeapSize = configTOTAL_HEAP_SIZE; /* 获取内存堆的总大小 */
/* Ensure the heap starts on a correctly aligned boundary.
* 确保堆在正确对齐的边界上开始 */
uxAddress = ( size_t ) ucHeap; /* 获取内存堆的起始地址 */
//4字节对齐
if( ( uxAddress & portBYTE_ALIGNMENT_MASK ) != 0 )
{
uxAddress += ( portBYTE_ALIGNMENT - 1 );
uxAddress &= ~( ( size_t ) portBYTE_ALIGNMENT_MASK ); /* 字节对齐后的起始地址 */
xTotalHeapSize -= uxAddress - ( size_t ) ucHeap; /* 字节对齐后剩余的内存堆大小 */
}
pucAlignedHeap = ( uint8_t * ) uxAddress;
/* 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用于保存指向空闲块列表中第一个项目的指针。void强制转换用于防止编译器警告 */
xStart.pxNextFreeBlock = ( void * ) pucAlignedHeap;
xStart.xBlockSize = ( size_t ) 0;
/* pxEnd is used to mark the end of the list of free blocks and is inserted
at the end of the heap space.
pxEnd用于标记空闲块列表的末尾,并插入堆空间的末尾 */
uxAddress = ( ( size_t ) pucAlignedHeap ) + xTotalHeapSize; /* 前边进行字节对齐后的地址+剩余堆大小 */
uxAddress -= xHeapStructSize; /* 减去内存块结构体大小 */
uxAddress &= ~( ( size_t ) portBYTE_ALIGNMENT_MASK ); /* 进行字节对齐 */
pxEnd = ( void * ) uxAddress;
pxEnd->xBlockSize = 0;
pxEnd->pxNextFreeBlock = NULL;
/* To start with there is a single free block that is sized to take up the
entire heap space, minus the space taken by pxEnd.
首先,有一个空闲块,其大小足以占用整个堆空间,减去pxEnd占用的空间 */
pxFirstFreeBlock = ( void * ) pucAlignedHeap;
pxFirstFreeBlock->xBlockSize = uxAddress - ( size_t ) pxFirstFreeBlock;
pxFirstFreeBlock->pxNextFreeBlock = pxEnd;
/* Only one block exists - and it covers the entire usable heap space. */
xMinimumEverFreeBytesRemaining = pxFirstFreeBlock->xBlockSize;
xFreeBytesRemaining = pxFirstFreeBlock->xBlockSize;
/* Work out the position of the top bit in a size_t variable. */
xBlockAllocatedBit = ( ( size_t ) 1 ) << ( ( sizeof( size_t ) * heapBITS_PER_BYTE ) - 1 );
}
1.2 函数解析

2. prvInsertBlockIntoFreeList
2.1 函数源码
/* 函数功能:将空闲内存块插入到空闲列表中 */
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();
}
}
2.2 函数解析
2.2.1 情况1:待插入的内存块的地址与插入位置前一个内存块的尾地址不连续、且待插入的内存块的尾地址与插入位置后一个内存块的首地址不连续

2.2.2 情况2:待插入的内存块的地址与插入位置前一个内存块的尾地址连续

2.2.3 情况3:待插入的内存块的尾地址与插入位置后一个内存块的首地址连续

2.2.4 情况4:待插入的内存块的地址与插入位置前一个内存块的尾地址连续、且待插入的内存块的尾地址与插入位置后一个内存块的首地址连续

2.2.5 情况5:待插入的内存块的尾地址与内存块末尾pxEnd连续

3. pvPortMalloc
3.1 函数源码
void *pvPortMalloc( size_t xWantedSize )
{
BlockLink_t *pxBlock, *pxPreviousBlock, *pxNewBlockLink;
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( pxEnd == NULL )
{
prvHeapInit();
}
else
{
mtCOVERAGE_TEST_MARKER();
}
/* Check the requested block size is not so large that the top bit is
set. The top bit of the block size member of the BlockLink_t structure
is used to determine who owns the block - the application or the
kernel, so it must be free.
检查请求的块大小是否太大,以至于设置了顶部位。
BlockLink_t结构的块大小成员的顶部用于确定谁拥有块——应用程序还是内核,因此它必须是自由的。 */
if( ( xWantedSize & xBlockAllocatedBit ) == 0 )
{
/* The wanted size is increased so it can contain a BlockLink_t
structure in addition to the requested amount of bytes.
所需内存大小是增加的,因此除了请求的字节数外,它还可以包含BlockLink_t结构 */
if( xWantedSize > 0 )
{
xWantedSize += xHeapStructSize; //加上一个结构体大小
/* Ensure that blocks are always aligned to the required number
of bytes. */
if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0x00 )
{
/* Byte alignment required. */
xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
configASSERT( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) == 0 );
}
else
{
mtCOVERAGE_TEST_MARKER();
}
}
else
{
mtCOVERAGE_TEST_MARKER();
}
if( ( xWantedSize > 0 ) && ( xWantedSize <= xFreeBytesRemaining ) )
{
/* Traverse the list from the start (lowest address) 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 the end marker was reached then a block of adequate size
was not found.
如果到达终点标记,则找不到足够大的块 */
if( pxBlock != pxEnd )
{
/* Return the memory space pointed to - jumping over the
BlockLink_t structure at its start. 跳过结构体,找到实际存放数据的内存地址 */
pvReturn = ( void * ) ( ( ( uint8_t * ) pxPreviousBlock->pxNextFreeBlock ) + xHeapStructSize );
/* 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.
这个内存块将被一分为二。根据请求的字节数创建一个新的。void强制转换用于防止编译器发出字节对齐警告。 */
pxNewBlockLink = ( void * ) ( ( ( uint8_t * ) pxBlock ) + xWantedSize );
configASSERT( ( ( ( size_t ) pxNewBlockLink ) & portBYTE_ALIGNMENT_MASK ) == 0 );
/* 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 );
}
else
{
mtCOVERAGE_TEST_MARKER();
}
xFreeBytesRemaining -= pxBlock->xBlockSize;
if( xFreeBytesRemaining < xMinimumEverFreeBytesRemaining )
{
xMinimumEverFreeBytesRemaining = xFreeBytesRemaining;
}
else
{
mtCOVERAGE_TEST_MARKER();
}
/* The block is being returned - it is allocated and owned
by the application and has no "next" block.
该块正在返回-它由应用程序分配和拥有,没有“下一个”块 */
pxBlock->xBlockSize |= xBlockAllocatedBit;
pxBlock->pxNextFreeBlock = NULL;
}
else
{
mtCOVERAGE_TEST_MARKER();
}
}
else
{
mtCOVERAGE_TEST_MARKER();
}
}
else
{
mtCOVERAGE_TEST_MARKER();
}
traceMALLOC( pvReturn, xWantedSize );
}
( void ) xTaskResumeAll();
#if( configUSE_MALLOC_FAILED_HOOK == 1 )
{
if( pvReturn == NULL )
{
extern void vApplicationMallocFailedHook( void );
vApplicationMallocFailedHook();
}
else
{
mtCOVERAGE_TEST_MARKER();
}
}
#endif
configASSERT( ( ( ( uint32_t ) pvReturn ) & portBYTE_ALIGNMENT_MASK ) == 0 );
return pvReturn;
}
3.2 函数解析

4. vPortFree
4.1 函数源码
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.
被释放的内存前面将有一个BlockLink_t结构 */
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 )
{
/* 如果内存块被使用过,其下一个空闲内存块指针应该为NULL */
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 free blocks.
* 将该内存块大小添加到空闲内存块总大小中*/
xFreeBytesRemaining += pxLink->xBlockSize;
traceFREE( pv, pxLink->xBlockSize );
/* 将该空闲内存块插入到空闲列表中 */
prvInsertBlockIntoFreeList( ( ( BlockLink_t * ) pxLink ) );
}
( void ) xTaskResumeAll();
}
else
{
mtCOVERAGE_TEST_MARKER();
}
}
else
{
mtCOVERAGE_TEST_MARKER();
}
}
}
4.3 函数解析

注意:在一段内存没有被释放之前绝对不能再调用一次函数pvPortMalloc()为其再次分配内存,否则会导致内存泄露
3198



被折叠的 条评论
为什么被折叠?



