安信可(云知声蜂鸟US516P6)SDK开发学习---freertos heap管理

安信可(云知声蜂鸟US516P6)SDK开发学习—freertos heap管理
在这里插入图片描述

Heap_5
heap_5用于分配和释放内存的算法与heap_4所使用的算法相同。与heap_4不同,heap_5不局限于从单个静态声明的数组中分配内存;heap_5可以从多个分离的内存空间分配内存。当运行FreeRTOS的系统提供的RAM在系统的内存映射中没有显示为单个连续(没有空间)块时,Heap_5非常有用。

heap_5是唯一提供的必须在调用pvPortMalloc()之前显式初始化的内存分配方案。Heap_5使用vPortDefineHeapRegions() API函数初始化。当使用heap_5时,必须在创建任何内核对象(任务、队列、信号量等)之前调用vPortDefineHeapRegions()。

vPortDefineHeapRegions()
vPortDefineHeapRegions()用于指定开始地址和每个单独的内存区域的大小,这些区域共同构成了heap_5使用的总内存.

void prvInitialiseHeap(void)
{
    
    
	extern char _end;
	HeapRegion_t xHeapRegions[2];

	xHeapRegions[0].pucStartAddress = (uint8_t*)&_end;
	xHeapRegions[0].xSizeInBytes = gSramEndAddr-(uint32_t)&_end;

	xHeapRegions[1].pucStartAddress = NULL;
	xHeapRegions[1].xSizeInBytes = 0;

	vPortDefineHeapRegions( (HeapRegion_t *)xHeapRegions );
}

斜体样式
每个单独的内存区域都由类型为HeapRegion_t的结构体来描述。所有可用内存区域的描述作为数组传递给vPortDefineHeapRegions() HeapRegion_t结构。

typedef struct HeapRegion
{
    
    
       /* 堆一部分的内存块的起始地址.*/
       uint8_t *pucStartAddress;
       /*内存块的大小,以字节为单位 */
       size_t xSizeInBytes;
} HeapRegion_t;

OS封装层函数

/**
* @brief Allocate a memory block from a memory pool
* @param  osWantedSize Allocate memory word(1word=4byte) size
* @retval  address of the allocated memory block or NULL in case of no memory available.
*/
void *osPortMalloc(uint16_t osWantedSize)
{
    
    
	void *ospvReturn = NULL;

	ospvReturn=pvPortMalloc(osWantedSize);
	//DBG("Malloc11:%d->%d, Add=%x\n", (int)osWantedSize, (int)xPortGetFreeHeapSize(), ospvReturn);
	return ospvReturn;
}
void *pvPortMallocFromEnd( size_t xWantedSize );
/**
* @brief Allocate a memory block from a memory pool
* @param  osWantedSize Allocate memory word(1word=4byte) size
* @retval  address of the allocated memory block or NULL in case of no memory available.
*/
void *osPortMallocFromEnd(uint32_t osWantedSize)
{
    
    
	void *ospvReturn = NULL;

	//DBG("\nMalloc22:%d, %d\n", xPortGetFreeHeapSize(), osWantedSize);
	ospvReturn = pvPortMallocFromEnd(osWantedSize);
	//DBG("Malloc33:%d->%d, Add=%x\n", (int)osWantedSize, (int)xPortGetFreeHeapSize(), ospvReturn);
	return ospvReturn;
}

int osPortRemainMem(void)
{
    
    
	return (int)xPortGetFreeHeapSize();
}

/**
* @brief Free a memory block
* @param address of the allocated memory block.
*/
void osPortFree(void *ospv)
{
    
    
	//DBG("Add=%x\n", ospv);
	vPortFree(ospv);
}

猜你喜欢

转载自blog.csdn.net/xushx_bigbear/article/details/130874601
今日推荐