硬汉嵌入式论坛

 找回密码
 立即注册
查看: 6170|回复: 3
收起左侧

[FreeRTOS] FreeRTOS动态内存管理和静态内存管理两种方式的异同

[复制链接]

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106708
QQ
发表于 2016-6-7 02:54:08 | 显示全部楼层 |阅读模式
静态内存管理是在V9.0.0版本才新增的一种方式,介绍看官方的这个帖子即可:
http://www.freertos.org/Static_Vs_Dynamic_Memory_Allocation.html

=======================================================


Static Vs Dynamic Memory Allocation



Introduction

FreeRTOS versions prior to V9.0.0 allocate the memory used by the RTOS objects listed below from thespecial FreeRTOS heap. FreeRTOS V9.0.0 and onwards gives the application writer the ability to instead provide the memory themselves, allowing the following objects to optionally be created without any memory being allocated dynamically:
    [li]Tasks[/li][li]Software Timers[/li][li]Queues[/li][li]Event Groups[/li][li]Binary Semaphores[/li][li]Counting Semaphores[/li][li]Recursive Semaphores[/li][li]Mutexes[/li]
Whether it is preferable to use static or dynamic memory allocation is dependent on the application, and the preference of the application writer. Both methods have pros and cons, and both methods can be used within the same RTOS application.The simple Win32 example located in the FreeRTOS/Source/WIN32-MSVC-Static-Allocation-Only directory of the main FreeRTOS download demonstrates how a FreeRTOS application can be created without including any of the FreeRTOS heap implementations in a project.




Creating an RTOS Object Using Dynamically Allocated RAM

Creating RTOS objects dynamically has the benefit of greater simplicity, and the potential to minimise the application's maximum RAM usage:
    [li]Fewer function parameters are required when an object is created.
    [/li][li]The memory allocation occurs automatically, within the RTOS API functions.
    [/li][li]The application writer does not need to concern themselves with allocating memory themselves.
    [/li][li]The RAM used by an RTOS object can be re-used if the object is deleted, potentially reducing the application's maximum RAM footprint.
    [/li][li]RTOS API functions are provided to return information on heap usage, allowing the heap size to be optimised.
    [/li][li]The memory allocation scheme used can be chosen to best suite the application, be that heap_1.c for simplicity and determinism often necessary for safety critical applications, heap_4.c for fragmentation protection, heap_5.c to split the heap across multiple RAM regions, or an allocation scheme provided by the application writer themselves.
    [/li]
The following API functions, which are available if configSUPPORT_DYNAMIC_ALLOCATION is set to 1 or left undefined, create RTOS objects using dynamically allocated RAM:




Creating an RTOS Object Using Statically Allocated RAM

Creating RTOS objects using statically allocated RAM has the benefit of providing the application writer with more control:
    [li]RTOS objects can be placed at specific memory locations.
    [/li][li]The maximum RAM footprint can be determined at link time, rather than run time.
    [/li][li]The application writer does not need to concern themselves with graceful handling of memory allocation failures.
    [/li][li]It allows the RTOS to be used in applications that simply don't allow any dynamic memory allocation (although FreeRTOS includes allocation schemes that can overcome most objections).
    [/li]
The following API functions, which are available if configSUPPORT_STATIC_ALLOCATION is set to 1, allow RTOS objects to be created using memory provided by the application writer. To provide memory the application writer simply needs to declare a variable of the appropriate object type, then pass the address of the variable into the RTOS API function. The StaticAllocation.c standard demo/test task is provided to demonstrate how the functions are used:
回复

使用道具 举报

1

主题

70

回帖

73

积分

初级会员

积分
73
发表于 2021-6-12 16:49:17 | 显示全部楼层
硬汉哥,想问下,FreeRTOS中动态任务创建和静态任务创建啥区别,咱们手册里面也没提到静态任务创建。
回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106708
QQ
 楼主| 发表于 2021-6-13 10:00:22 | 显示全部楼层
星辰大海 发表于 2021-6-12 16:49
硬汉哥,想问下,FreeRTOS中动态任务创建和静态任务创建啥区别,咱们手册里面也没提到静态任务创建。

制作我们的那个手册那会FreeRTOS还没有静态方式。

静态方式是后来出的,之前仅有动态方式。

静态方式就是定义各一个数组,动态方式就是类似malloc动态申请和释放。
回复

使用道具 举报

6

主题

231

回帖

249

积分

高级会员

积分
249
发表于 2021-6-15 15:41:53 | 显示全部楼层
增加静态创建API好,比较灵活。有了静态版本后就可以灵活放到自己的结构里面,不需要再用FreeRTOS本身的内存分配模块了。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2024-4-30 10:25 , Processed in 0.263366 second(s), 25 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表