user3104363
user3104363

Reputation: 334

FreeRTOS Memory Management Scheme 1 - Request Help For Understanding Memory Alignment

I am trying to understand memory allocation scheme 1 in the freeRTOS.

In this function, following code is used.

    static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
    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 ) ) );
        }

I am working on arm cortex m3 mcu. So

portPOINTER_SIZE_TYPE defined as uint32_t
portBYTE_ALIGNMENT defined as 8 
portBYTE_ALIGNMENT_MASK defined as 0x0007

Why we can't use just pucAlignedHeap = &ucHeap; ?

Thanks for your answers.

Upvotes: 0

Views: 578

Answers (1)

Richard
Richard

Reputation: 3236

All the FreeRTOS heap allocation schemes do this because the C standard requires the start address of dynamically allocated memory to be aligned to the requirements of the processor. The code you posted is doing the alignment. Lots of code assumes malloc() is aligned so uses it to store items that must be aligned without having to perform additional checks, or having to allocate more RAM than is required in order to truncate to an aligned address themselves.

Upvotes: 1

Related Questions