Static Memory allocation

michaeln32 wrote on Sunday, December 24, 2017:

Hi,
Why should I call the callback vApplicationGetIdleTaskMemory() when I use static memory allocation ?

Thank you

Michael

rtel wrote on Sunday, December 24, 2017:

You don’t call it, FreeRTOS calls it, you just have to provide it.

The Idle task (and potentially the timer service/RTOS daemon task) is
created internally by FreeRTOS when the scheduler is started. The tasks
need a task control block and a stack, just like any other task. If you
are using dynamic memory allocation then FreeRTOS can just allocate the
required memory from the heap. If you have opted to use static memory
allocation then FreeRTOS cannot obtain the memory itself so you must
provide it. That is what the callback/hook function does:
https://www.freertos.org/a00110.html#configSUPPORT_STATIC_ALLOCATION

I want to use FreeRTOS streams with static allocation. To do this I need to set “Enable Static memory allocation” to 1. This seems to cause all memory needed by FreeRTOS to be obtained statically.

So I guess this is a feature request, I think it would be an improvement to be able to have some allocations be static and others dynamic. Perhaps the enable static configuration could be a bit mask instead of a single number.

I do not know how difficult this would be to support, however.

There are two defines that control how things are allocated:
configSUPPORT_STATIC_ALLOCATION
and
configSUPPORT_DYNAMIC_ALLOCATION

They both can be set to 1 if you want to be able to use both static and dynamic allocations.
The one issue I can think of is that the built in tasks will decide they should be created statically if static allocation is enabled, and you can’t force them to be dynamically created if you want any static allocations, but the callbacks to get the stack/object allocations can call pvPortMalloc to get the memory for them if you want.

Right, I saw that both can be turned on and I tried doing that. This seems to be OK with stream_buffer.c, if both are defined, both versions of CREATE are defined. However other parts of the code do not work the same way, for example the idle task will use static if both are defined. There is no option to use dynamic memory allocation if static is configured.

It looks like I can setup both options in streams_buffer.c

change line 281: #if( configSUPPORT_STATIC_ALLOCATION == 1 )
to this: if( 1 )

Or just comment it out, as well as line 356

BTW, why are those functions marked PRIVILEGED

Just took a look at this and see that the function to create a static stream buffer is provided if configSUPPORT_STATIC_ALLOCATION is 1 here: https://sourceforge.net/p/freertos/code/HEAD/tree/trunk/FreeRTOS/Source/stream_buffer.c#l281

Likewise the function to create a dynamically allocated stream buffer is provided if configSUPPORT_DYNAMIC_ALLOCATION is 1 here: https://sourceforge.net/p/freertos/code/HEAD/tree/trunk/FreeRTOS/Source/stream_buffer.c#l217

So you can have either one or both enabled at any given time. So I’m not sure I understand what the issue is, or am I looking in the wrong place?

There is a space disadvantage if both Static and Dynamic allocation is allowed in that then each object needs to record if it was created staticly or dynamically so the destroy function knows what to do. There is also the issue that some compiler/linkers have problems removing unused functions from a file, so if all allocation will be of the same type, you can save space by indicating this.

The PRIVILEGED mark makes a difference if you are using the MPU version, in which case these functions in non-privileged tasks get call through a gateway to move into privileged mode so they can do their job.