Understanding Dynamic Memory Allocation in FreeRTOS

Follow+Star Public Account Number, don’t miss wonderful content

Understanding Dynamic Memory Allocation in FreeRTOS

Author | strongerHuang

WeChat Public Account | Embedded Column

You may wonder: Can FreeRTOS create tasks and delete tasks, is it dynamically allocated memory?
xTaskCreate(LED_Task, "LED_Task", 128, NULL, 6, NULL);

If this question is to be elaborated, it involves many knowledge points, below I will pick a few important related content to answer this question.

Static and Dynamic Memory Allocation

Memory allocation is generally divided into: static and dynamic allocation.

1.Static Memory Allocation

What is static memory allocation?

In simple terms, it is memory allocated at compile time. You can understand it as when the chip is powered on, a specified area (address) of memory is occupied.

There are generally several situations, such as:

Startup allocation stack:

Stack_Size    EQU     0x400

Defining a static variable, this is the easiest to understand:

static int a;

Defining a global variable/array, etc.:

int char;

2.Dynamic Memory Allocation

What is dynamic memory allocation?
In contrast to static allocation, dynamic means that the memory allocated is uncertain at a certain moment.
The most common is when a function defines a local variable, if this function is called, a temporary memory space will be allocated for this variable, and after the function executes, this memory will be released.
void UART_Send(char *p){  char buf[10];  //......}
Another classic is the malloc() dynamic memory allocation function, which is generally not recommended for ordinary developers because of some “disadvantages”.
For example: forgetting to use free() to release memory, and it is easy to produce “memory fragmentation” and so on.

Sorting out FreeRTOS Memory Allocation

1.Global Array (Stack)

The memory of FreeRTOS is allocated in the FreeRTOSConfig.h file as a global array, the size of this memory is allocated by the user according to the situation, for example:

#define configTOTAL_HEAP_SIZE     ((size_t)(10 * 1024))

Understanding Dynamic Memory Allocation in FreeRTOS

Array (Stack) ucHeap:

Understanding Dynamic Memory Allocation in FreeRTOS

This array is the “stack” of the FreeRTOS system, creating tasks, semaphores, queues, etc. will call this stack.

2.Creating Tasks

FreeRTOS creates tasks and allocates stack size, for example: 128 “words”

xTaskCreate(LED_Task, "LED_Task", 128, NULL, 6, NULL);

Reminder:Here “word” is the unit, for example: uint32_t

By further tracing the code, you will find that in the “create task” function, the [pvPortMalloc] function is called to allocate memory.

Understanding Dynamic Memory Allocation in FreeRTOS

This is a user-defined function, not the standard malloc function.
It seems to be “dynamically allocating memory”, but in fact it is “dividing” the global array (stack) defined above.
For specific how to “divide”, you can refer to the “heap_4.c” source code (you can refer to the corresponding source code for other memory allocation methods).
Other created semaphores and queues are similar in principle.

3.Deleting Tasks

FreeRTOS deletes tasks and calls the “vPortFree()” function to release the corresponding memory.

Understanding Dynamic Memory Allocation in FreeRTOS

This involves TCB (Task Control Block), which is the data related to the task. (Not going into detail here, will discuss later if there is an opportunity).

Is FreeRTOS dynamically allocated memory?

Having seen this, can you answer the question at the beginning?

The answer is: FreeRTOS does not dynamically allocate memory, it only simulates the way of dynamic allocation, and the actual memory is statically allocated.

———— END ————
Follow the public account and reply Embedded DevelopmentMicrocontrollerRTOS to read more related articles.
Reply “Join Group” to join the technical exchange group according to the rules, reply “1024” to see more content.

Understanding Dynamic Memory Allocation in FreeRTOS

Understanding Dynamic Memory Allocation in FreeRTOS

Click “Read the original text” to see more shares.

Leave a Comment

×