From 96235995cccc6a8d8d3aee35f82872349ccca21a Mon Sep 17 00:00:00 2001 From: richardbarry Date: Mon, 5 Oct 2009 11:16:38 +0000 Subject: [PATCH] Add xPortGetFreeHeapSize() function. git-svn-id: https://svn.code.sf.net/p/freertos/code/trunk@907 1d2547de-c912-0410-9cb9-b8ca96c0e9e2 --- Source/include/mpu_wrappers.h | 2 ++ Source/include/portable.h | 1 + Source/portable/GCC/ARM_CM3_MPU/port.c | 24 ++++++++++++++++++++++++ Source/portable/MemMang/heap_1.c | 7 +++++++ Source/portable/MemMang/heap_2.c | 13 +++++++++++++ 5 files changed, 47 insertions(+) diff --git a/Source/include/mpu_wrappers.h b/Source/include/mpu_wrappers.h index e6621330d..49bf7a183 100644 --- a/Source/include/mpu_wrappers.h +++ b/Source/include/mpu_wrappers.h @@ -99,6 +99,8 @@ only for ports that are using the MPU. */ #define pvPortMalloc MPU_pvPortMalloc #define vPortFree MPU_vPortFree + #define xPortGetFreeHeapSize MPU_xPortGetFreeHeapSize + #define vPortInitialiseBlocks MPU_vPortInitialiseBlocks /* Remove the privileged function macro. */ #define PRIVILEGED_FUNCTION diff --git a/Source/include/portable.h b/Source/include/portable.h index 67275c8ad..369573b17 100644 --- a/Source/include/portable.h +++ b/Source/include/portable.h @@ -350,6 +350,7 @@ extern "C" { void *pvPortMalloc( size_t xSize ) PRIVILEGED_FUNCTION; void vPortFree( void *pv ) PRIVILEGED_FUNCTION; void vPortInitialiseBlocks( void ) PRIVILEGED_FUNCTION; +size_t xPortGetFreeHeapSize( void ) PRIVILEGED_FUNCTION; /* * Setup the hardware ready for the scheduler to take control. This generally diff --git a/Source/portable/GCC/ARM_CM3_MPU/port.c b/Source/portable/GCC/ARM_CM3_MPU/port.c index 731dd1583..155eb2708 100644 --- a/Source/portable/GCC/ARM_CM3_MPU/port.c +++ b/Source/portable/GCC/ARM_CM3_MPU/port.c @@ -1025,3 +1025,27 @@ portBASE_TYPE xRunningPrivileged = prvRaisePrivilege(); portRESET_PRIVILEGE( xRunningPrivileged ); } +/*-----------------------------------------------------------*/ + +void MPU_vPortInitialiseBlocks( void ) +{ +portBASE_TYPE xRunningPrivileged = prvRaisePrivilege(); + + vPortInitialiseBlocks(); + + portRESET_PRIVILEGE( xRunningPrivileged ); +} +/*-----------------------------------------------------------*/ + +size_t MPU_xPortGetFreeHeapSize( void ) +{ +size_t xReturn; +portBASE_TYPE xRunningPrivileged = prvRaisePrivilege(); + + xReturn = xPortGetFreeHeapSize(); + + portRESET_PRIVILEGE( xRunningPrivileged ); + + return xReturn; +} + diff --git a/Source/portable/MemMang/heap_1.c b/Source/portable/MemMang/heap_1.c index 27582c17f..f81ce116e 100644 --- a/Source/portable/MemMang/heap_1.c +++ b/Source/portable/MemMang/heap_1.c @@ -136,5 +136,12 @@ void vPortInitialiseBlocks( void ) /* Only required when static memory is not cleared. */ xNextFreeByte = ( size_t ) 0; } +/*-----------------------------------------------------------*/ + +size_t xPortGetFreeHeapSize( void ) +{ + return ( configTOTAL_HEAP_SIZE - xNextFreeByte ); +} + diff --git a/Source/portable/MemMang/heap_2.c b/Source/portable/MemMang/heap_2.c index e1f22dcaa..1f81a08a5 100644 --- a/Source/portable/MemMang/heap_2.c +++ b/Source/portable/MemMang/heap_2.c @@ -93,6 +93,10 @@ static const unsigned short heapSTRUCT_SIZE = ( sizeof( xBlockLink ) + portBYTE /* 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 about +fragmentation. */ +static size_t xFreeBytesRemaining = configTOTAL_HEAP_SIZE; + /* STATIC FUNCTIONS ARE DEFINED AS MACROS TO MINIMIZE THE FUNCTION CALL DEPTH. */ /* @@ -211,6 +215,8 @@ void *pvReturn = NULL; /* Insert the new block into the list of free blocks. */ prvInsertBlockIntoFreeList( ( pxNewBlockLink ) ); } + + xFreeBytesRemaining -= xWantedSize; } } } @@ -248,9 +254,16 @@ xBlockLink *pxLink; { /* Add this block to the list of free blocks. */ prvInsertBlockIntoFreeList( ( ( xBlockLink * ) pxLink ) ); + xFreeBytesRemaining += pxLink->xBlockSize; } xTaskResumeAll(); } } /*-----------------------------------------------------------*/ +size_t xPortGetFreeHeapSize( void ) +{ + return xFreeBytesRemaining; +} +/*-----------------------------------------------------------*/ + -- 2.39.2