mirror of
https://github.com/FreeRTOS/FreeRTOS-Kernel.git
synced 2025-08-20 01:58:32 -04:00
Heap improvements (#462)
* Heap improvements This commit makes the following improvements: 1. Add a check to heap_2 to track if a memory block is allocated to the application or not. The MSB of the size field is used for this purpose. The same check already exists in heap_4 and heap_5. This check prevents against double free. 2. Add a new flag configHEAP_CLEAR_MEMORY_ON_FREE to heap_2, heap_4 and heap_5. The application writer can set it to 1 in their FreeRTOSConfig.h to ensure that a block of memory allocated using pvPortMalloc is cleared (i.e. set to zero) when it is freed using vPortFree. If left undefined, configHEAP_CLEAR_MEMORY_ON_FREE defaults to 0 for backward compatibility. We recommend setting configHEAP_CLEAR_MEMORY_ON_FREE to 1 for better security. 3. Add a new API pvPortCalloc to heap_2, heap_4 and heap_5. This API has the following signature: void * pvPortCalloc( size_t xNum, size_t xSize ); It allocates memory for an array of xNum objects each of which is of xSize and initializes all bytes in the allocated storage to zero. If allocation succeeds, it returns a pointer to the lowest byte in the allocated memory block. On failure, it returns a null pointer. Signed-off-by: Gaurav Aggarwal <aggarg@amazon.com>
This commit is contained in:
parent
4539e1c574
commit
82be77995e
4 changed files with 249 additions and 98 deletions
|
@ -35,6 +35,7 @@
|
|||
* memory management pages of https://www.FreeRTOS.org for more information.
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
#include <string.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
|
||||
|
@ -50,12 +51,31 @@
|
|||
#error This file must not be used if configSUPPORT_DYNAMIC_ALLOCATION is 0
|
||||
#endif
|
||||
|
||||
#ifndef configHEAP_CLEAR_MEMORY_ON_FREE
|
||||
#define configHEAP_CLEAR_MEMORY_ON_FREE 0
|
||||
#endif
|
||||
|
||||
/* Block sizes must not get too small. */
|
||||
#define heapMINIMUM_BLOCK_SIZE ( ( size_t ) ( xHeapStructSize << 1 ) )
|
||||
|
||||
/* Assumes 8bit bytes! */
|
||||
#define heapBITS_PER_BYTE ( ( size_t ) 8 )
|
||||
|
||||
/* Check if multiplying a and b will result in overflow. */
|
||||
#define heapMULTIPLY_WILL_OVERFLOW( a, b, max ) ( ( ( a ) > 0 ) && ( ( b ) > ( ( max ) / ( a ) ) ) )
|
||||
|
||||
/* MSB of the xBlockSize member of an BlockLink_t structure is used to track
|
||||
* the allocation status of a block. When MSB of 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. */
|
||||
#define heapBLOCK_ALLOCATED_BITMASK ( ( ( size_t ) 1 ) << ( ( sizeof( size_t ) * heapBITS_PER_BYTE ) - 1 ) )
|
||||
#define heapBLOCK_SIZE_IS_VALID( xBlockSize ) ( ( ( xBlockSize ) & heapBLOCK_ALLOCATED_BITMASK ) == 0 )
|
||||
#define heapBLOCK_IS_ALLOCATED( pxBlock ) ( ( ( pxBlock->xBlockSize ) & heapBLOCK_ALLOCATED_BITMASK ) != 0 )
|
||||
#define heapALLOCATE_BLOCK( pxBlock ) ( ( pxBlock->xBlockSize ) |= heapBLOCK_ALLOCATED_BITMASK )
|
||||
#define heapFREE_BLOCK( pxBlock ) ( ( pxBlock->xBlockSize ) &= ~heapBLOCK_ALLOCATED_BITMASK )
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
/* Allocate the memory for the heap. */
|
||||
#if ( configAPPLICATION_ALLOCATED_HEAP == 1 )
|
||||
|
||||
|
@ -106,12 +126,6 @@ PRIVILEGED_DATA static size_t xMinimumEverFreeBytesRemaining = 0U;
|
|||
PRIVILEGED_DATA static size_t xNumberOfSuccessfulAllocations = 0;
|
||||
PRIVILEGED_DATA static size_t xNumberOfSuccessfulFrees = 0;
|
||||
|
||||
/* 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. */
|
||||
PRIVILEGED_DATA static size_t xBlockAllocatedBit = 0;
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
void * pvPortMalloc( size_t xWantedSize )
|
||||
|
@ -136,7 +150,7 @@ void * pvPortMalloc( size_t xWantedSize )
|
|||
* 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. */
|
||||
if( ( xWantedSize & xBlockAllocatedBit ) == 0 )
|
||||
if( heapBLOCK_SIZE_IS_VALID( xWantedSize ) )
|
||||
{
|
||||
/* The wanted size must be increased so it can contain a BlockLink_t
|
||||
* structure in addition to the requested amount of bytes. */
|
||||
|
@ -232,7 +246,7 @@ void * pvPortMalloc( size_t xWantedSize )
|
|||
|
||||
/* The block is being returned - it is allocated and owned
|
||||
* by the application and has no "next" block. */
|
||||
pxBlock->xBlockSize |= xBlockAllocatedBit;
|
||||
heapALLOCATE_BLOCK( pxBlock );
|
||||
pxBlock->pxNextFreeBlock = NULL;
|
||||
xNumberOfSuccessfulAllocations++;
|
||||
}
|
||||
|
@ -288,17 +302,21 @@ void vPortFree( void * pv )
|
|||
/* 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( heapBLOCK_IS_ALLOCATED( pxLink ) );
|
||||
configASSERT( pxLink->pxNextFreeBlock == NULL );
|
||||
|
||||
if( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 )
|
||||
if( heapBLOCK_IS_ALLOCATED( pxLink ) )
|
||||
{
|
||||
if( pxLink->pxNextFreeBlock == NULL )
|
||||
{
|
||||
/* The block is being returned to the heap - it is no longer
|
||||
* allocated. */
|
||||
pxLink->xBlockSize &= ~xBlockAllocatedBit;
|
||||
heapFREE_BLOCK( pxLink );
|
||||
#if ( configHEAP_CLEAR_MEMORY_ON_FREE == 1 )
|
||||
{
|
||||
( void ) memset( puc + xHeapStructSize, 0, pxLink->xBlockSize - xHeapStructSize );
|
||||
}
|
||||
#endif
|
||||
|
||||
vTaskSuspendAll();
|
||||
{
|
||||
|
@ -341,6 +359,26 @@ void vPortInitialiseBlocks( void )
|
|||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
void * pvPortCalloc( size_t xNum,
|
||||
size_t xSize )
|
||||
{
|
||||
void * pv = NULL;
|
||||
const size_t xSizeMaxValue = ~( ( size_t ) 0 );
|
||||
|
||||
if( !heapMULTIPLY_WILL_OVERFLOW( xNum, xSize, xSizeMaxValue ) )
|
||||
{
|
||||
pv = pvPortMalloc( xNum * xSize );
|
||||
|
||||
if( pv != NULL )
|
||||
{
|
||||
( void ) memset( pv, 0, xNum * xSize );
|
||||
}
|
||||
}
|
||||
|
||||
return pv;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
static void prvHeapInit( void ) /* PRIVILEGED_FUNCTION */
|
||||
{
|
||||
BlockLink_t * pxFirstFreeBlock;
|
||||
|
@ -383,9 +421,6 @@ static void prvHeapInit( void ) /* PRIVILEGED_FUNCTION */
|
|||
/* 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 );
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
|
@ -502,3 +537,4 @@ void vPortGetHeapStats( HeapStats_t * pxHeapStats )
|
|||
}
|
||||
taskEXIT_CRITICAL();
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue