Is FreeRTOS Dynamically Allocated Memory?

Follow+Star PublicNumber, don’t miss the wonderful content

Is FreeRTOS Dynamically Allocated Memory?

Author | strongerHuang

WeChat Public Account | Embedded Column

Some readers might ask: Can FreeRTOS create tasks and delete tasks? Is it dynamically allocated memory?
xTaskCreate(LED_Task, "LED_Task", 128, NULL, 6, NULL);

If we want to elaborate on this question, there are many related knowledge points involved. Below are a few important related contents 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?

Simply put, 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 a few situations, such as:

Startup allocation stack:

Stack_Size    EQU     0x400

Defining a static variable is the easiest example 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 memory is allocated at an uncertain moment.
The most common case is when a function defines a local variable. If this function is called, a temporary memory space is allocated for this variable, and once the function is executed, this memory is released.
void UART_Send(char *p){  char buf[10];  //......}
Another classic example is the malloc() function for dynamic memory allocation, which is generally not recommended for ordinary developers due to some “disadvantages”.
For example: forgetting to use free() to release memory, and it is easy to produce “memory fragmentation”, etc.

Sorting Out FreeRTOS Memory Allocation

1. Global Array (Stack)

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

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

Is FreeRTOS Dynamically Allocated Memory?

Array (Stack) ucHeap:

Is FreeRTOS Dynamically Allocated Memory?

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

2. Creating Tasks

FreeRTOS creates tasks, allocating 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 the “create task” function calls the [pvPortMalloc] function to allocate memory.

Is FreeRTOS Dynamically Allocated Memory?

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 more details on how it is “divided”, you can refer to the “heap_4.c” source code(by using the “heap_4.c” memory allocation method, if others can refer to the corresponding source code).
Other creations of semaphores and queues follow a similar principle.

3. Deleting Tasks

FreeRTOS deletes tasks, calling the “vPortFree()” function to release the corresponding memory.

Is FreeRTOS Dynamically Allocated Memory?

This involves TCB (Task Control Block), which is the data related to the task. (Details will be discussed later when there is a chance).

Is FreeRTOS Dynamically Allocated Memory?

Having read this, can you answer the initial question?

The answer is: FreeRTOS is not dynamically allocated 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, and reply “1024” to see more content.

Is FreeRTOS Dynamically Allocated Memory?

Is FreeRTOS Dynamically Allocated Memory?

Click “Read Original” to see more shares.

Leave a Comment

Your email address will not be published. Required fields are marked *