From ee8ffed465776db593c1c1a25a9abbcdc62d728d Mon Sep 17 00:00:00 2001 From: Alessio Morale Date: Tue, 10 Jun 2014 19:10:37 +0200 Subject: [PATCH] OP-1274: Force 8 bytes alignment for RTOS structures while using 4 bytes alignment for the remaining memory allocation operations to save RAM --- .../Source/portable/GCC/ARM_CM3/portmacro.h | 6 ++++++ .../FreeRTOS/Source/portable/MemMang/heap_1.c | 13 +++++++++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/flight/pios/common/libraries/FreeRTOS/Source/portable/GCC/ARM_CM3/portmacro.h b/flight/pios/common/libraries/FreeRTOS/Source/portable/GCC/ARM_CM3/portmacro.h index dbf20a419..17810e6fb 100644 --- a/flight/pios/common/libraries/FreeRTOS/Source/portable/GCC/ARM_CM3/portmacro.h +++ b/flight/pios/common/libraries/FreeRTOS/Source/portable/GCC/ARM_CM3/portmacro.h @@ -107,6 +107,12 @@ typedef unsigned long UBaseType_t; #define portSTACK_GROWTH ( -1 ) #define portTICK_PERIOD_MS ( ( TickType_t ) 1000 / configTICK_RATE_HZ ) #define portBYTE_ALIGNMENT 8 +#define portBYTE_HEAP_ALIGNMENT 4 // this value is used to allocate heap + +// Following define allow to keep a 8 bytes alignment for stack and other RTOS structures +// while using 4 bytes alignment for the remaining heap allocations to save ram +extern void *pvPortMallocGeneric( size_t xWantedSize, size_t alignment); +#define pvPortMallocAligned( x, puxStackBuffer ) ( ( ( puxStackBuffer ) == NULL ) ? ( pvPortMallocGeneric( ( x ) , portBYTE_ALIGNMENT) ) : ( puxStackBuffer ) ) /*-----------------------------------------------------------*/ diff --git a/flight/pios/common/libraries/FreeRTOS/Source/portable/MemMang/heap_1.c b/flight/pios/common/libraries/FreeRTOS/Source/portable/MemMang/heap_1.c index ee157d827..57ea722b2 100644 --- a/flight/pios/common/libraries/FreeRTOS/Source/portable/MemMang/heap_1.c +++ b/flight/pios/common/libraries/FreeRTOS/Source/portable/MemMang/heap_1.c @@ -90,22 +90,23 @@ static size_t currentTOTAL_HEAP_SIZE = configTOTAL_HEAP_SIZE; /* Allocate the memory for the heap. */ static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ] __attribute__ ((section (".heap"))); static size_t xNextFreeByte = ( size_t ) 0; +void *pvPortMallocGeneric( size_t xWantedSize, size_t alignment); /*-----------------------------------------------------------*/ -void *pvPortMalloc( size_t xWantedSize ) +void *pvPortMallocGeneric( size_t xWantedSize, size_t alignment) { void *pvReturn = NULL; static uint8_t *pucAlignedHeap = NULL; - +size_t mask = alignment - 1; /* Ensure that blocks are always aligned to the required number of bytes. */ #if portBYTE_ALIGNMENT != 1 if( xWantedSize & portBYTE_ALIGNMENT_MASK ) { /* Byte alignment required. */ - xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) ); + xWantedSize += ( alignment - ( xWantedSize & mask ) ); } #endif @@ -114,7 +115,7 @@ static uint8_t *pucAlignedHeap = NULL; if( pucAlignedHeap == NULL ) { /* Ensure the heap starts on a correctly aligned boundary. */ - pucAlignedHeap = ( uint8_t * ) ( ( ( portPOINTER_SIZE_TYPE ) &ucHeap[ portBYTE_ALIGNMENT ] ) & ( ( portPOINTER_SIZE_TYPE ) ~portBYTE_ALIGNMENT_MASK ) ); + pucAlignedHeap = ( uint8_t * ) ( ( ( portPOINTER_SIZE_TYPE ) &ucHeap[ alignment ] ) & ( ( portPOINTER_SIZE_TYPE ) ~mask ) ); } /* Check there is enough room left for the allocation. */ @@ -143,8 +144,12 @@ static uint8_t *pucAlignedHeap = NULL; return pvReturn; } +/*-----------------------------------------------------------*/ +void *pvPortMalloc(size_t xWantedSize) { + return pvPortMallocGeneric(xWantedSize, portBYTE_HEAP_ALIGNMENT); +} /*-----------------------------------------------------------*/ void vPortFree( void *pv )