Reducing amount of RAM used by Task
Minimum two RAM blocks (TCB and Stack) for every Task are used in the current version of FreeRTOS. Such decision requires the additional expenseses for the second Memory Control Block and for its pointer pxStack in TCB. Besides, justification of the Stack depends on realization of Memory Manager.
I have contributed the following changes (marked as zlt[….] / configUSE_COMMON_TCB_STK ) for use single Memory Block. Under my Memory Manager (ARM port) this decision saves 20 bytes for every Task.
Changes in task.c:
-—————————————————————
typedef struct tskTaskControlBlock
{
volatile portSTACK_TYPE *pxTopOfStack; //< Points to the location of the last item placed on the tasks stack. THIS MUST BE THE FIRST MEMBER OF THE STRUCT
xListItem xGenericListItem; //< List item used to place the TCB in ready and blocked queues.
xListItem xEventListItem; //< List item used to place the TCB in event lists.
unsigned portBASE_TYPE uxPriority; //< The priority of the task where 0 is the lowest priority.
#if( configUSE_COMMON_TCB_STK == 0 ) //zlt[]
portSTACK_TYPE *pxStack; //< Points to the start of the stack.
#endif //zlt[]
….
//zlt[
#if( configUSE_COMMON_TCB_STK )
#ifndef portSTACK_ALIGNMENT
#define portSTACK_ALIGNMENT portBYTE_ALIGNMENT
#endif
#define TCB_SIZE ((sizeof(tskTCB) + (portSTACK_ALIGNMENT-1) )&( ~(portSTACK_ALIGNMENT-1 ) ))
#endif
//]
…..
static tskTCB *prvAllocateTCBAndStack( unsigned portSHORT usStackDepth )
{
tskTCB *pxNewTCB;
/* Allocate space for the TCB. Where the memory comes from depends on
the implementation of the port malloc function. */
//zlt[
#if( configUSE_COMMON_TCB_STK )
/* Common memory block for TCB & Stack */
pxNewTCB = ( tskTCB * ) pvPortMalloc( TCB_SIZE + ( size_t )usStackDepth * sizeof( portSTACK_TYPE ), OWNER_TCB );
#else
//]zlt
pxNewTCB = ( tskTCB * ) pvPortMalloc( sizeof( tskTCB ), OWNER_TCB );
#endif
if( pxNewTCB != NULL )
{
#if( configUSE_COMMON_TCB_STK == 0 ) //zlt[]
/* Allocate space for the stack used by the task being created.
The base of the stack memory stored in the TCB so the task can
be deleted later if required. */
pxNewTCB->pxStack = ( portSTACK_TYPE * ) pvPortMalloc( ( ( size_t )usStackDepth ) * sizeof( portSTACK_TYPE ),
OWNER_STACK ); //zlt[]
if( pxNewTCB->pxStack == NULL )
{
// Could not allocate the stack. Delete the allocated TCB.
vPortFree( pxNewTCB );
pxNewTCB = NULL;
}
else
#endif //zlt[]
{
/* Just to help debugging. */
//zlt[
// memset( pxNewTCB->pxStack, tskSTACK_FILL_BYTE, usStackDepth * sizeof( portSTACK_TYPE ) );
int i;
#if( configUSE_COMMON_TCB_STK )
for( i = TCB_SIZE/sizeof( portSTACK_TYPE ); i < usStackDepth; i++ )
*((portSTACK_TYPE *)pxNewTCB + i) = (portSTACK_TYPE)tskSTACK_FILL;
#else
for( i = 0; i < usStackDepth; i++ )
*(pxNewTCB->pxStack + i) = (portSTACK_TYPE)tskSTACK_FILL;
#endif
//]zlt
}
}
return pxNewTCB;
}
static void prvDeleteTCB( tskTCB *pxTCB )
{
/* Free up the memory allocated by the scheduler for the task. It is up to
the task to free any memory allocated at the application level. */
//zlt[
#if( configUSE_COMMON_TCB_STK == 0 )
vPortFree( pxTCB->pxStack );
#endif
//]zlt
vPortFree( pxTCB );
}
signed portBASE_TYPE xTaskCreate( pdTASK_CODE pvTaskCode, const signed portCHAR * const pcName, unsigned portSHORT usStackDepth, void *pvParameters, unsigned portBASE_TYPE uxPriority, xTaskHandle *pxCreatedTask )
{
…….
/* Calculate the top of stack address. This depends on whether the
stack grows from high memory to low (as per the 80×86) or visa versa.
portSTACK_GROWTH is used to make the result positive or negative as
required by the port. */
#if portSTACK_GROWTH < 0
{
//zlt[
#if( configUSE_COMMON_TCB_STK )
/* Common memory block for TCB & Stack */
pxTopOfStack = (portSTACK_TYPE *)pxNewTCB + TCB_SIZE/sizeof(portSTACK_TYPE) + ( usStackDepth – 1 );
#else
//]
pxTopOfStack = pxNewTCB->pxStack + ( usStackDepth – 1 );
#endif
}
#else
{
//zlt[
#if( configUSE_COMMON_TCB_STK )
pxTopOfStack = (portSTACK_TYPE *)pxNewTCB + TCB_SIZE/sizeof(portSTACK_TYPE);
#else
//]
pxTopOfStack = pxNewTCB->pxStack;
#endif
}
#endif
…….
Some changes are also required in prvListTaskWithinSingleList() and usTaskCheckFreeStackSpace().
Reducing amount of RAM used by Task
It took me a while to understand this, but I think what you are doing here is malloc’ing a single block rather than two separate blocks, and everything else is the same. Right?
Reducing amount of RAM used by Task
Right you are, but there is also excluded unnecessary pointer from TCB. And added feature for independed alingments of the stack