Skip to content

Commit 2a604f4

Browse files
authored
Support allocating stack from separate heap (#267)
The change adds support for allocating task stacks from separate heap. When configSTACK_ALLOCATION_FROM_SEPARATE_HEAP is defined as 1 in FreeRTOSConfig.h, task stacks are allocated and freed using pvPortMallocStack and vPortFreeStack functions. This allows the application writer to provide a separate allocator for task stacks. When configSTACK_ALLOCATION_FROM_SEPARATE_HEAP is defined as 0, task stacks are allocated and freed using FreeRTOS heap functions pvPortMalloc and vPortFree. For backward compatibility, configSTACK_ALLOCATION_FROM_SEPARATE_HEAP defaults to 0. Signed-off-by: Gaurav Aggarwal <[email protected]>
1 parent 81f5892 commit 2a604f4

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-5
lines changed

.github/lexicon.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,6 +1617,7 @@ pvowner
16171617
pvparameter
16181618
pvparameters
16191619
pvportmalloc
1620+
pvportmallocstack
16201621
pvportrealloc
16211622
pvreg
16221623
pvrxdata

include/FreeRTOS.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,11 @@
896896
#define configSUPPORT_DYNAMIC_ALLOCATION 1
897897
#endif
898898

899+
#ifndef configSTACK_ALLOCATION_FROM_SEPARATE_HEAP
900+
/* Defaults to 0 for backward compatibility. */
901+
#define configSTACK_ALLOCATION_FROM_SEPARATE_HEAP 0
902+
#endif
903+
899904
#ifndef configSTACK_DEPTH_TYPE
900905

901906
/* Defaults to uint16_t for backward compatibility, but can be overridden

include/portable.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,14 @@ void vPortInitialiseBlocks( void ) PRIVILEGED_FUNCTION;
179179
size_t xPortGetFreeHeapSize( void ) PRIVILEGED_FUNCTION;
180180
size_t xPortGetMinimumEverFreeHeapSize( void ) PRIVILEGED_FUNCTION;
181181

182+
#if( configSTACK_ALLOCATION_FROM_SEPARATE_HEAP == 1 )
183+
void *pvPortMallocStack( size_t xSize ) PRIVILEGED_FUNCTION;
184+
void vPortFreeStack( void *pv ) PRIVILEGED_FUNCTION;
185+
#else
186+
#define pvPortMallocStack pvPortMalloc
187+
#define vPortFreeStack vPortFree
188+
#endif
189+
182190
/*
183191
* Setup the hardware ready for the scheduler to take control. This generally
184192
* sets up a tick interrupt and sets timers for the correct tick frequency.

tasks.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,7 @@ static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION;
748748
/* Allocate space for the stack used by the task being created.
749749
* The base of the stack memory stored in the TCB so the task can
750750
* be deleted later if required. */
751-
pxNewTCB->pxStack = ( StackType_t * ) pvPortMalloc( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
751+
pxNewTCB->pxStack = ( StackType_t * ) pvPortMallocStack( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
752752

753753
if( pxNewTCB->pxStack == NULL )
754754
{
@@ -763,7 +763,7 @@ static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION;
763763
StackType_t * pxStack;
764764

765765
/* Allocate space for the stack used by the task being created. */
766-
pxStack = pvPortMalloc( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ) ); /*lint !e9079 All values returned by pvPortMalloc() have at least the alignment required by the MCU's stack and this allocation is the stack. */
766+
pxStack = pvPortMallocStack( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ) ); /*lint !e9079 All values returned by pvPortMalloc() have at least the alignment required by the MCU's stack and this allocation is the stack. */
767767

768768
if( pxStack != NULL )
769769
{
@@ -779,7 +779,7 @@ static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION;
779779
{
780780
/* The stack cannot be used as the TCB was not created. Free
781781
* it again. */
782-
vPortFree( pxStack );
782+
vPortFreeStack( pxStack );
783783
}
784784
}
785785
else
@@ -3950,7 +3950,7 @@ static void prvCheckTasksWaitingTermination( void )
39503950
{
39513951
/* The task can only have been allocated dynamically - free both
39523952
* the stack and TCB. */
3953-
vPortFree( pxTCB->pxStack );
3953+
vPortFreeStack( pxTCB->pxStack );
39543954
vPortFree( pxTCB );
39553955
}
39563956
#elif ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
@@ -3962,7 +3962,7 @@ static void prvCheckTasksWaitingTermination( void )
39623962
{
39633963
/* Both the stack and TCB were allocated dynamically, so both
39643964
* must be freed. */
3965-
vPortFree( pxTCB->pxStack );
3965+
vPortFreeStack( pxTCB->pxStack );
39663966
vPortFree( pxTCB );
39673967
}
39683968
else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )

0 commit comments

Comments
 (0)