好记性不如铅笔头

ARM, FreeRTOS, 操作系统

FreeRTOS代码阅读笔记:heap_3.c

FreeRTOS中对于内存的管理当前一共有5种实现方式(作者当前的版本是8.2.1),均在【 \Source\portable\MemMang 】下面,这里笔记下。

heap_3.c:

/*
 * Implementation of pvPortMalloc() and vPortFree() that relies on the
 * compilers own malloc() and free() implementations.
 *
 * This file can only be used if the linker is configured to to generate
 * a heap memory area.
 *
 * See heap_1.c, heap_2.c and heap_4.c for alternative implementations, and the
 * memory management pages of http://www.FreeRTOS.org for more information.
 */
/*根据上面的注释可以知道,这里 pvPortMalloc() 和 vPortFree() 的实现是基于 malloc()和 free() 。
由于使用了标准C函数中的内存申请和释放,因此使用前一定要在工程中设置好堆内存。
*/
 
 
#include <stdlib.h>

/* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
all the API functions to use the MPU wrappers.  That should only be done when
task.h is included from an application file. */
#define MPU_WRAPPERS_INCLUDED_FROM_API_FILE

#include "FreeRTOS.h"
#include "task.h"

#undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE

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

/* 
pvPortMalloc的实现非常简单,其实就是内部调用了malloc
 */
void *pvPortMalloc( size_t xWantedSize )
{
void *pvReturn;

	vTaskSuspendAll();//首先暂停当前所有执行的任务
	{
		pvReturn = malloc( xWantedSize );
		traceMALLOC( pvReturn, xWantedSize );
	}
	( void ) xTaskResumeAll();//恢复执行

	//如果定义了钩子函数,那么申请失败时就调用钩子函数
	#if( configUSE_MALLOC_FAILED_HOOK == 1 )
	{
		if( pvReturn == NULL )
		{
			extern void vApplicationMallocFailedHook( void );
			vApplicationMallocFailedHook();
		}
	}
	#endif

	return pvReturn;
}
/*-----------------------------------------------------------*/

/* 
vPortFree的实现非常简单,其实就是内部调用了free
 */
void vPortFree( void *pv )
{
	if( pv )
	{
		vTaskSuspendAll();
		{
			free( pv );
			traceFREE( pv, 0 );
		}
		( void ) xTaskResumeAll();
	}
}

 

发表评论

5 + 12 =

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据