1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-01-18 03:52:11 +01:00

OP-1274: Force 8 bytes alignment for RTOS structures while using 4 bytes alignment for the remaining memory allocation operations to save RAM

This commit is contained in:
Alessio Morale 2014-06-10 19:10:37 +02:00
parent 070eccbf1f
commit ee8ffed465
2 changed files with 15 additions and 4 deletions

View File

@ -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 ) )
/*-----------------------------------------------------------*/

View File

@ -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 )