FreeRTOS 内存管理源码分析

/* * A sample implementation of pvPortMalloc() and vPortFree() that permits * allocated blocks to be freed, but does not combine adjacent free blocks * into a single larger block. * * See heap_1.c and heap_3.c for alternative implementations, and the memory * management pages of http://www.FreeRTOS.org for more information. */#include "stdlib.h";/* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefiningall the API functions to use the MPU wrappers. That should only be done whentask.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/* Allocate the memory for the heap. The struct is used to force bytealignment without using any non-portable code. */static union xRTOS_HEAP{#if portBYTE_ALIGNMENT == 8volatile portDOUBLE dDummy;#elsevolatile unsigned long ulDummy;#endifunsigned char ucHeap[ configTOTAL_HEAP_SIZE ];} xHeap;/* Define the linked list structure. This is used to link free blocks in orderof 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. */} xBlockLink;static const unsigned short heapSTRUCT_SIZE = ( sizeof( xBlockLink ) + portBYTE_ALIGNMENT - ( sizeof( xBlockLink ) % portBYTE_ALIGNMENT ) );#define heapMINIMUM_BLOCK_SIZE ( ( size_t ) ( heapSTRUCT_SIZE * 2 ) )/* Create a couple of list links to mark the start and end of the list. */static xBlockLink xStart, xEnd;/* Keeps track of the number of free bytes remaining, but says nothing aboutfragmentation. */static size_t xFreeBytesRemaining = configTOTAL_HEAP_SIZE;/* STATIC FUNCTIONS ARE DEFINED AS MACROS TO MINIMIZE THE FUNCTION CALL DEPTH. *//* * 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 ) \{ \xBlockLink *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; \}/*-----------------------------------------------------------*/#define prvHeapInit() \{ \xBlockLink *pxFirstFreeBlock; \\/* 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 * ) xHeap.ucHeap; \xStart.xBlockSize = ( size_t ) 0; \\/* xEnd is used to mark the end of the list of free blocks. */ \xEnd.xBlockSize = configTOTAL_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 * ) xHeap.ucHeap; \pxFirstFreeBlock->xBlockSize = configTOTAL_HEAP_SIZE; \pxFirstFreeBlock->pxNextFreeBlock = &xEnd; \}/*-----------------------------------------------------------*/void *pvPortMalloc( size_t xWantedSize ){xBlockLink *pxBlock, *pxPreviousBlock, *pxNewBlockLink;static portBASE_TYPE xHeapHasBeenInitialised = pdFALSE;void *pvReturn = NULL;vTaskSuspendAll();{/* If this is the first call to malloc then the heap will requireinitialisation to setup the list of free blocks. */if( xHeapHasBeenInitialised == pdFALSE ){prvHeapInit();xHeapHasBeenInitialised = pdTRUE;}/* The wanted size is increased so it can contain a xBlockLinkstructure 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 ){/* Byte alignment required. */xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );}}if( ( xWantedSize > 0 ) && ( xWantedSize < configTOTAL_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 ) ){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 xBlockLink structureat its start. */pvReturn = ( void * ) ( ( ( unsigned char * ) pxPreviousBlock->pxNextFreeBlock ) + heapSTRUCT_SIZE );/* This block is being returned for use so must be taken our of thelist 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 blockfollowing the number of bytes requested. The void cast isused to prevent byte alignment warnings from the compiler. */pxNewBlockLink = ( void * ) ( ( ( unsigned char * ) pxBlock ) + xWantedSize );/* Calculate the sizes of two blocks split from the singleblock. */pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize;pxBlock->xBlockSize = xWantedSize;/* Insert the new block into the list of free blocks. */prvInsertBlockIntoFreeList( ( pxNewBlockLink ) );}xFreeBytesRemaining -= pxBlock->xBlockSize;}}}xTaskResumeAll();#if( configUSE_MALLOC_FAILED_HOOK == 1 ){if( pvReturn == NULL ){extern void vApplicationMallocFailedHook( void );vApplicationMallocFailedHook();}}#endifreturn pvReturn;}/*-----------------------------------------------------------*/void vPortFree( void *pv ){unsigned char *puc = ( unsigned char * ) pv;xBlockLink *pxLink;if( pv ){/* The memory being freed will have an xBlockLink structure immediatelybefore it. */puc -= heapSTRUCT_SIZE;/* This casting is to keep the compiler from issuing warnings. */pxLink = ( void * ) puc;vTaskSuspendAll();{/* Add this block to the list of free blocks. */prvInsertBlockIntoFreeList( ( ( xBlockLink * ) pxLink ) );xFreeBytesRemaining += pxLink->xBlockSize;}xTaskResumeAll();}}/*-----------------------------------------------------------*/size_t xPortGetFreeHeapSize( void ){return xFreeBytesRemaining;}/*-----------------------------------------------------------*/void vPortInitialiseBlocks( void ){/* This just exists to keep the linker quiet. */}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值