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 1164e735d..2cdd8bbd4 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 @@ -105,54 +105,49 @@ void *pvPortMallocGeneric( size_t xWantedSize, size_t alignment); /*-----------------------------------------------------------*/ -void *pvPortMallocGeneric( size_t xWantedSize, size_t alignment) -{ -void *pvReturn = NULL; -static unsigned char *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 & mask ) - { - /* Byte alignment required. */ - xWantedSize += ( alignment - ( xWantedSize & mask ) ); - } - #endif +void *pvPortMallocGeneric(size_t xWantedSize, size_t alignment) { + void *pvReturn = NULL; + static unsigned char *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 & mask) { + /* Byte alignment required. */ + xWantedSize += (alignment - (xWantedSize & mask)); + } +#endif - vTaskSuspendAll(); - { - if( pucAlignedHeap == NULL ) - { - /* Ensure the heap starts on a correctly aligned boundary. */ - pucAlignedHeap = ( unsigned char * ) ( ( ( portPOINTER_SIZE_TYPE ) &ucHeap[ alignment ] ) & ( ( portPOINTER_SIZE_TYPE ) ~ mask ) ); - } + vTaskSuspendAll(); + { + if (pucAlignedHeap == NULL ) { + /* Ensure the heap starts on a correctly aligned boundary. */ + pucAlignedHeap = (unsigned char *) (((portPOINTER_SIZE_TYPE ) &ucHeap[alignment]) & ((portPOINTER_SIZE_TYPE ) ~mask)); + } - /* Check there is enough room left for the allocation. */ - if( ( ( xNextFreeByte + xWantedSize ) < configADJUSTED_HEAP_SIZE ) && - ( ( xNextFreeByte + xWantedSize ) > xNextFreeByte ) )/* Check for overflow. */ - { - /* Return the next free byte then increment the index past this - block. */ - pvReturn = pucAlignedHeap + xNextFreeByte; - xNextFreeByte += xWantedSize; - } - } - xTaskResumeAll(); + /* Check there is enough room left for the allocation. */ + if (((xNextFreeByte + xWantedSize) < configADJUSTED_HEAP_SIZE)&& + ( ( xNextFreeByte + xWantedSize ) > xNextFreeByte ) ){ + /* Check for overflow. */ + /* Return the next free byte then increment the index past this block. */ + pvReturn = pucAlignedHeap + xNextFreeByte; + xNextFreeByte += xWantedSize; + } + } + xTaskResumeAll(); - #if( configUSE_MALLOC_FAILED_HOOK == 1 ) - { - if( pvReturn == NULL ) - { - extern void vApplicationMallocFailedHook( void ); - vApplicationMallocFailedHook(); - } - } - #endif +#if( configUSE_MALLOC_FAILED_HOOK == 1 ) + { + if( pvReturn == NULL ) { + extern void vApplicationMallocFailedHook( void ); + vApplicationMallocFailedHook(); + } + } +#endif - return pvReturn; + return pvReturn; } -void *pvPortMalloc( size_t xWantedSize){ +void *pvPortMalloc(size_t xWantedSize) { return pvPortMallocGeneric(xWantedSize, portBYTE_HEAP_ALIGNMENT); } diff --git a/flight/PiOS/Common/Libraries/FreeRTOS/howto_upgrade.txt b/flight/PiOS/Common/Libraries/FreeRTOS/howto_upgrade.txt index c4ba4caae..aa64ebc24 100644 --- a/flight/PiOS/Common/Libraries/FreeRTOS/howto_upgrade.txt +++ b/flight/PiOS/Common/Libraries/FreeRTOS/howto_upgrade.txt @@ -8,6 +8,7 @@ Brief list of modifications: *Allow heap_1.c for modification of max heap size at runtime; *use section ".heap" for heap. +Memory manager now handles two different kind of alignments for F1, portBYTE_ALIGNMENT for standard mallocs, and portBYTE_HEAP_ALIGNMENT for stack(aligned) allocs diff --git a/flight/PiOS/STM32F10x/Libraries/FreeRTOS/Source/portable/GCC/ARM_CM3/portmacro.h b/flight/PiOS/STM32F10x/Libraries/FreeRTOS/Source/portable/GCC/ARM_CM3/portmacro.h index aff9f208b..a40ebd000 100644 --- a/flight/PiOS/STM32F10x/Libraries/FreeRTOS/Source/portable/GCC/ARM_CM3/portmacro.h +++ b/flight/PiOS/STM32F10x/Libraries/FreeRTOS/Source/portable/GCC/ARM_CM3/portmacro.h @@ -115,6 +115,8 @@ extern "C" { #define portBYTE_HEAP_ALIGNMENT 4 // this value is used to allocate heap /*-----------------------------------------------------------*/ +extern void *pvPortMallocGeneric( size_t xWantedSize, size_t alignment); +#define pvPortMallocAligned( x, puxStackBuffer ) ( ( ( puxStackBuffer ) == NULL ) ? ( pvPortMallocGeneric( ( x ) , portBYTE_ALIGNMENT) ) : ( puxStackBuffer ) ) /* Scheduler utilities. */ extern void vPortYieldFromISR( void ); diff --git a/flight/PiOS/STM32F4xx/Libraries/FreeRTOS/Source/portable/GCC/ARM_CM4F/portmacro.h b/flight/PiOS/STM32F4xx/Libraries/FreeRTOS/Source/portable/GCC/ARM_CM4F/portmacro.h index e99354d6f..2a5e983e1 100644 --- a/flight/PiOS/STM32F4xx/Libraries/FreeRTOS/Source/portable/GCC/ARM_CM4F/portmacro.h +++ b/flight/PiOS/STM32F4xx/Libraries/FreeRTOS/Source/portable/GCC/ARM_CM4F/portmacro.h @@ -111,7 +111,7 @@ extern "C" { /* Architecture specifics. */ #define portSTACK_GROWTH ( -1 ) #define portTICK_RATE_MS ( ( portTickType ) 1000 / configTICK_RATE_HZ ) -#define portBYTE_ALIGNMENT 8 +#define portBYTE_ALIGNMENT 8 // APCS 8 bytes aligned stack for external calls /*-----------------------------------------------------------*/ diff --git a/flight/targets/CopterControl/System/inc/FreeRTOSConfig.h b/flight/targets/CopterControl/System/inc/FreeRTOSConfig.h index 7dcb54fe8..0ed32c74d 100644 --- a/flight/targets/CopterControl/System/inc/FreeRTOSConfig.h +++ b/flight/targets/CopterControl/System/inc/FreeRTOSConfig.h @@ -91,9 +91,6 @@ do {\ #define configCHECK_FOR_STACK_OVERFLOW 1 #endif -void *pvPortMallocGeneric( size_t xWantedSize, size_t alignment); -#define pvPortMallocAligned( x, puxStackBuffer ) ( ( ( puxStackBuffer ) == NULL ) ? ( pvPortMallocGeneric( ( x ) , portBYTE_ALIGNMENT) ) : ( puxStackBuffer ) ) - /** * @} */