0
0
FreeRTOSprogramming~10 mins

FreeRTOS heap implementations (heap_1 to heap_5) - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to select the simplest FreeRTOS heap implementation that does not allow memory to be freed.

FreeRTOS
void *pvPortMalloc( size_t xSize ) {
    static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
    static size_t xNextFreeByte = 0;

    if( ( xNextFreeByte + xSize ) < configTOTAL_HEAP_SIZE ) {
        void *pvReturn = &ucHeap[ xNextFreeByte ];
        xNextFreeByte += [1];
        return pvReturn;
    }
    return NULL;
}
Drag options to blanks, or click blank then click option'
AxSize
BconfigTOTAL_HEAP_SIZE
Csizeof(ucHeap)
DxNextFreeByte
Attempts:
3 left
💡 Hint
Common Mistakes
Using the total heap size instead of the requested size.
Incrementing by the current pointer value instead of the requested size.
2fill in blank
medium

Complete the code to implement a FreeRTOS heap that supports memory freeing by using a linked list of free blocks.

FreeRTOS
typedef struct A_BLOCK_LINK {
    struct A_BLOCK_LINK *pxNextFreeBlock;
    size_t xBlockSize;
} BlockLink_t;

static BlockLink_t xStart, *pxEnd = NULL;

void vPortFree( void *pv ) {
    BlockLink_t *pxLink = ( BlockLink_t * ) pv - 1;
    pxLink->pxNextFreeBlock = xStart.pxNextFreeBlock;
    xStart.pxNextFreeBlock = [1];
}
Drag options to blanks, or click blank then click option'
ApxLink
BpxEnd
Cpv
DxStart
Attempts:
3 left
💡 Hint
Common Mistakes
Linking the free list to the end pointer instead of the freed block.
Using the void pointer directly instead of casting to block link.
3fill in blank
hard

Fix the error in the heap_3 implementation where the allocation function does not correctly align the block size.

FreeRTOS
size_t xWantedSize = xSize + sizeof( BlockLink_t );

/* Align the size to the nearest multiple of [1]. */
xWantedSize = ( xWantedSize + ( [2] - 1 ) ) & ~ ( [3] - 1 );
Drag options to blanks, or click blank then click option'
Asizeof( void * )
Bsizeof( BlockLink_t )
CconfigMINIMAL_STACK_SIZE
DconfigCPU_CLOCK_HZ
Attempts:
3 left
💡 Hint
Common Mistakes
Using unrelated constants for alignment.
Not aligning the size at all, causing misaligned memory.
4fill in blank
hard

Fill both blanks to complete the heap_4 allocation function that uses a best-fit algorithm to find a free block.

FreeRTOS
BlockLink_t *pxBlock = xStart.pxNextFreeBlock;
BlockLink_t *pxBestBlock = NULL;
size_t xBestSize = ( size_t ) -1;

while( pxBlock != pxEnd ) {
    if( pxBlock->xBlockSize >= [1] ) {
        if( pxBlock->xBlockSize < xBestSize ) {
            pxBestBlock = pxBlock;
            xBestSize = pxBlock->xBlockSize;
        }
    }
    pxBlock = pxBlock->pxNextFreeBlock;
}

if( pxBestBlock != NULL ) {
    /* Allocate from pxBestBlock */
    /* ... */
}
Drag options to blanks, or click blank then click option'
AxWantedSize
BxBlockSize
CxSize
DconfigTOTAL_HEAP_SIZE
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing to the wrong size variable.
Not updating the best block when a smaller suitable block is found.
5fill in blank
hard

Fill all three blanks to complete the heap_5 allocation function that uses a first-fit algorithm and coalesces adjacent free blocks.

FreeRTOS
BlockLink_t *pxPreviousBlock = &xStart;
BlockLink_t *pxBlock = xStart.pxNextFreeBlock;

while( pxBlock != pxEnd ) {
    if( pxBlock->xBlockSize >= [1] ) {
        /* Remove block from free list */
        pxPreviousBlock->pxNextFreeBlock = pxBlock->pxNextFreeBlock;

        /* Split block if large enough */
        if( pxBlock->xBlockSize - [2] > [3] ) {
            /* Create new free block with remaining size */
            /* ... */
        }
        return ( void * )( pxBlock + 1 );
    }
    pxPreviousBlock = pxBlock;
    pxBlock = pxBlock->pxNextFreeBlock;
}
Drag options to blanks, or click blank then click option'
AxWantedSize
Csizeof( BlockLink_t )
DconfigMINIMAL_STACK_SIZE
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect sizes for splitting condition.
Not updating the free list pointers correctly.