当前位置:网站首页>[FreeRTOS] 13 Dynamic Memory Management
[FreeRTOS] 13 Dynamic Memory Management
2022-08-10 16:03:00 【xiaobaibai_2021】
本节我们来学习FreeRTOSThe dynamic memory management functions.
在FreeRTOS中,Dynamic memory management is a very important function,Before the said task creation、信号量创建、The create of a message queue, etc,Have created dynamically and static create two ways.静态创建时,Requires the user to define a first memory area;And dynamically create,Don't need to specify the memory,System automatically in the heap(heap)In applying for a piece of memory;Dynamic creation time here,With the function of dynamic memory management;此外,User applications can also apply for dynamic memory use.
1)FreeRTOS动态内存管理
When it comes to dynamic memory management,我们最容易想到的就是CDynamic memory allocation function of our languagemalloc(),This function can apply for a specified size memory area,With returns pointer to the memory address.Users can use the memory,After finished to usefree()函数来释放,否则会造成内存泄漏.
在嵌入式系统中,为了提高代码的运行效率,Generally do not use the standardC的malloc和free函数,Usually with a relatively simple method to realize the distribution of memory,Encapsulated into and againmalloc和free相同的函数接口.
FreeRTOS中,Application memory function prototype如下:
void *pvPortMalloc( size_t xWantedSize );//Parameters for the need to apply for the number of bytes,返回地址指针;
释放内存的函数原型如下:
void vPortFree( void *pv ); //Parameter is a pointer to the memory;
虽然FreeRTOSProvides a unified interface function corresponds to the standardC语言中的malloc和free,But there are several ways to their internal implementation.FreeRTOS实际上有5Realization of dynamic memory allocation ways,Can be used by the user to select which one.Below to introduce respectively their similarities and differences between.
Heap_1方式:
它是5The most simple ways,运行效率最高;Only supports the application of memory,不允许释放;具有时间确定性,不会产生内存碎片;Although it does not support release,But under the embedded system programming in general also enough,Because both task creation、Semaphore creation and so on,After creating the general rarely need to delete,Also don't release the memory.
Heap_2方式:
支持内存申请、内存释放;But does not support memory defragmentation(After the release of free small memory blocks will not be merged into big blocks of memory).
Heap_2Method is applied to dynamically allocated memory block size is the same case,Such as each time you create a queue,长度一样,It won't produce pieces of memory.使用时先用pvPortMalloc函数申请内存,After get the memory block pointer,With a static way to create the resources,使用完之后用vPortFree函数释放内存.
Heap_3方式:
Provided by the compilermalloc和free进行了封装,需要编译器提供malloc和free的实现.
Heap_4方式:
支持内存申请、内存释放;And support the memory defragmentation.
Heap_5方式:
这种方式是在heap_4的基础上,Added support for discrete memory of,Namely heap space can be discrete space;Such as single chip microcomputer internal we haveSRAM,External can mountSRAM或SDRAM,If you want to put this two pieces of discontinuous address space management,可以用heap_5Way for dynamic memory management.
使用heap_5Approach to dynamic memory management,需调用vPortDefineHeapRegions函数进行初始化,Before they can call application memory、动态创建任务、Dynamically create a semaphore application memory operations such as.
FreeRTOSThe heap size by theconfigTOTAL_HEAP_SIZE宏来确定;除了heap_3方式,Other memory allocation ways,Are in this heap space distribution of.
使用FreeRTOSHeap space as a dynamic memory(也就是除heap_3之外),可以用xPortGetFreeHeapSize()The size of the function to get the rest of the available heap space.使用heap_4或heap_5时,可以用xPortGetMinimumEverFreeHeapSize()History function to get the remaining space available minimum.
2)编程试验
Let's write a program testfreeRTOSThe dynamic memory allocation function of.
首先创建工程,默认情况下,cubemx中freeRTOSThe heap size is defined as3072字节,使用heap_4方式,我们不修改它,生成keil工程:
生成keil工程后,Can see in the source file have aheap_4.c文件;而且FreeRTOSConfig.h文件中的configTOTAL_HEAP_SIZE宏定义为3072:
In the task to write the following code,申请、释放内存,And print the remaining memory size:
程序运行的结果如下图所示:
初始时,Memory in the heap has1216字节的可用空间;
执行申请10字节空间后,减小到了1192字节;This is not reduce10字节,一方面是因为freeRTOSApplication memory of8字节对齐,On the other hand is because the management has applied for the block of memory also needs certain space;
Implement dynamically create a semaphore after,减小到了1112字节;
After release of application space,Available space back to1216字节;
The last print history available space minimum,1112字节.
可以看到,Dynamic application memory space,The available space in the heap is also a corresponding change of.
好了,本节的内容就到这里了.
如果觉得有用可以关注作者微信号“小白白学电子”,在公众号可以找到代码和资料下载地址
边栏推荐
猜你喜欢
随机推荐
Detailed understanding of anonymous functions and all built-in functions (Part 2)
华为云DevCloud获信通院首批云原生技术架构成熟度评估的最高级认证
NFT digital collection development issue - digital collection platform
Reids 源码导读
IPC:Interrupts and Signals
fuse简介
【芯片】人人皆可免费造芯?谷歌开源芯片计划已释放90nm、130nm和180nm工艺设计套件
智为链接,慧享生活,荣耀智慧服务,只为 “懂” 你
【21天学习挑战赛】折半查找
scala 10种函数高级应用
LeetCode-922. Sort Array By Parity II
Introduction to the functional logic of metaForce Fosage 2.0 system development
数据治理项目成功的要点,企业培养数据要把握好关键环节
并发容器线程安全应对之道
JVM学习——2——内存加载过程(类加载器)
哈希表应用:只出现一次的数字
Oracle数据库备份dmp文件太大,有什么办法可以在备份的时候拆分成多个dmp吗?
Colocate Join :ClickHouse的一种高性能分布式join查询模型
26、压缩及解压缩命令
MySQL batch update and batch update method of different values of multiple records