Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Real time embedded FreeRTOS mailing list 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem TCP & FAT Training




Loading

Reducing amount of RAM used by Task

Posted by zltigo on August 25, 2007
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
xListItemxGenericListItem;//< List item used to place the TCB in ready and blocked queues.
xListItemxEventListItem;//< List item used to place the TCB in event lists.
unsigned portBASE_TYPEuxPriority;//< 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_ALIGNMENTportBYTE_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 80x86) 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().

RE: Reducing amount of RAM used by Task

Posted by Dave on August 26, 2007
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?

RE: Reducing amount of RAM used by Task

Posted by zltigo on August 26, 2007
Right you are, but there is also excluded unnecessary pointer from TCB. And added feature for independed alingments of the stack


[ Back to the top ]    [ About FreeRTOS ]    [ Sitemap ]    [ ]




Copyright (C) 2004-2010 Richard Barry. Copyright (C) 2010-2016 Real Time Engineers Ltd.
Any and all data, files, source code, html content and documentation included in the FreeRTOSTM distribution or available on this site are the exclusive property of Real Time Engineers Ltd.. See the files license.txt (included in the distribution) and this copyright notice for more information. FreeRTOSTM and FreeRTOS.orgTM are trade marks of Real Time Engineers Ltd.

Latest News:

FreeRTOS V9.0.0 is now available for download.


Free TCP/IP and file system demos for the RTOS


Sponsored Links

⇓ Now With No Code Size Limit! ⇓
⇑ Free Download Without Registering ⇑


FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Renesas Electronics Gold Alliance RTOS Partner.jpg

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Atmel RTOS partner supporting ARM Cortex-M3 and AVR32 microcontrollers

STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Xilinx Microblaze and Zynq partner

Silicon Labs low power RTOS partner

Altera RTOS partner for Nios II and Cortex-A9 SoC

Freescale Alliance RTOS Member supporting ARM and ColdFire microcontrollers

Infineon ARM Cortex-M microcontrollers

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

Cypress RTOS partner supporting ARM Cortex-M3

Fujitsu RTOS partner supporting ARM Cortex-M3 and FM3

Microsemi (previously Actel) RTOS partner supporting ARM Cortex-M3

Atollic Partner

IAR Partner

Keil ARM Partner

Embedded Artists