FreeRTOS --(3)内存管理 heap2
在《FreeRTOS --(2)内存管理 heap1》知道 heap 1 的内存管理其实只是简单的实现了内存对齐的分配策略,heap 2 的实现策略相比 heap 1 稍微复杂一点,不仅仅是提供了分配内存的接口,同时也提供了释放内存的接口;
但是 heap 2 的内存分配策略中,并没有提供空闲内存的合并策略,对内存碎片没有处理;换句话来说,如果有多次的,大小各异的内存申请和释放的场景下,很可能导致很多内存碎片;
1、内存大小
和 heap 1 一样,用于内存管理的内存大小来自于一个大数组,数组的下标就是整个需要被管理的内存的大小,这个是和具体芯片所支持的 RAM 大小相关:
configTOTAL_HEAP_SIZE
被管理的内存定义为:
static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
ucHeap 就是管理的对象;
2、对齐
有的处理器是对内存对齐有要求的,比如 ARM-CM3 等,AAPCS规则要求堆栈保持8字节对齐。给任务分配栈时需要保证栈是8字节对齐的。所以这里 FreeRTOS 就需要涉及到对齐操作;针对 ARM-CM3 这类处理器来说,在portmacro.h 文件中,定义了对齐的字节数:
/* Hardware specifics. */
#define portBYTE_ALIGNMENT 8
而在 portable.h 中,定义了对应的 Mask(8字节对齐,那么都要是 8 的倍数,也就是二进制的 4'b1000,所以 MASK 是 4'b0111 也就是 0x07):
#if portBYTE_ALIGNMENT == 8
#define portBYTE_ALIGNMENT_MASK ( 0x0007 )
#endif
和 heap 1 一样,在处理对齐的时候,由于可能 ucHeap 初始的地址就没对齐,所以这里真正可以对齐分配的内存的 SIZE 就要做一些调整和妥协,由于是 8 字节对齐,所以最多妥协的大小就是 8 字节,也就是真正被管理的内存大小只有 configADJUSTED_HEAP_SIZE,这里可能造成几个字节的浪费(浪费多少,取决于ucHeap 初始地址 ),不过为了对齐,也就忽略了;
/* A few bytes might be lost to byte aligning the heap start address. */
#define configADJUSTED_HEAP_SIZE ( configTOTAL_HEAP_SIZE - portBYTE_ALIGNMENT )
3、内存块
与 heap 1 不同,heap 2 可以支持分配和释放,那么管理内存的手段势必比 heap 1 复杂一些,heap 2 对内存进行分块管理,将每块内存通过一个表征该内存块的的数据结构表示,以单向链表串在一起;
3.1、数据结构
表达一个内存块的数据结构是 BlockLink_t,它的定义是:
/* Define the linked list structure. This is used to link free blocks in order
of their size. */
typedef struct A_BLOCK_LINK
{
struct A_BLOCK_LINK *pxNextFreeBlock; /*<< The next free block in the list. */
size_t xBlockSize; /*<< The size of the free block. */
} BlockLink_t;
pxNextFreeBlock 指向下一个内存块的 BlockLink_t 结构;
xBlockSize 代表本内存块的大小;
3.2、数据结构对齐
当然内存块也需要对齐:
static const uint16_t heapSTRUCT_SIZE = ( ( sizeof ( BlockLink_t ) + ( portBYTE_ALIGNMENT - 1 ) ) & ~portBYTE_ALIGNMENT_MASK );
3.3、内存块 Marker
FreeRTOS 为内存管理,定义了两个 BlockLink_t 结构体,xStart 和 xEnd:
/* Create a couple of list links to mark the start and end of the list. */
static BlockLink_t xStart, xEnd;
xStart 和 xEnd 仅仅作为 mark,标记内存块的起始和结束;
3.4、可用内存
在 heap2 中定义了 xFreeBytesRemaining 来代表当前可用于分配的内存,每当内存被分配出去,这个值会减,内存被free 后,该值增加:
/* Keeps track of the number of free bytes remaining, but says nothing about
fragmentation. */
static size_t xFreeBytesRemaining = configADJUSTED_HEAP_SIZE;
4、分配内存
和 heap 1 一样,内存分配使用 pvPortMalloc 函数,传入的是希望拿到的内存,返回值拿到的内存起始地址,如果分配失败返回 NULL;
/*-----------------------------------------------------------*/ void *pvPortMalloc( size_t xWantedSize )
{
BlockLink_t *pxBlock, *pxPreviousBlock, *pxNewBlockLink;
static BaseType_t xHeapHasBeenInitialised = pdFALSE;
void *pvReturn = NULL; vTaskSuspendAll();
{
/* If this is the first call to malloc then the heap will require
initialisation to setup the list of free blocks. */
if( xHeapHasBeenInitialised == pdFALSE )
{
prvHeapInit();
xHeapHasBeenInitialised = pdTRUE;
} /* The wanted size is increased so it can contain a BlockLink_t
structure in addition to the requested amount of bytes. */
if( xWantedSize > 0 )
{
xWantedSize += heapSTRUCT_SIZE; /* Ensure that blocks are always aligned to the required number of bytes. */
if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0 )
{
/* Byte alignment required. */
xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
}
} if( ( xWantedSize > 0 ) && ( xWantedSize < configADJUSTED_HEAP_SIZE ) )
{
/* Blocks are stored in byte order - traverse the list from the start
(smallest) block until one of adequate size is found. */
pxPreviousBlock = &xStart;
pxBlock = xStart.pxNextFreeBlock;
while( ( pxBlock->xBlockSize < xWantedSize ) && ( pxBlock->pxNextFreeBlock != NULL ) )
{
pxPreviousBlock = pxBlock;
pxBlock = pxBlock->pxNextFreeBlock;
} /* If we found the end marker then a block of adequate size was not found. */
if( pxBlock != &xEnd )
{
/* Return the memory space - jumping over the BlockLink_t structure
at its start. */
pvReturn = ( void * ) ( ( ( uint8_t * ) pxPreviousBlock->pxNextFreeBlock ) + heapSTRUCT_SIZE ); /* This block is being returned for use so must be taken out of the
list of free blocks. */
pxPreviousBlock->pxNextFreeBlock = pxBlock->pxNextFreeBlock; /* If the block is larger than required it can be split into two. */
if( ( pxBlock->xBlockSize - xWantedSize ) > heapMINIMUM_BLOCK_SIZE )
{
/* This block is to be split into two. Create a new block
following the number of bytes requested. The void cast is
used to prevent byte alignment warnings from the compiler. */
pxNewBlockLink = ( void * ) ( ( ( uint8_t * ) pxBlock ) + xWantedSize ); /* Calculate the sizes of two blocks split from the single
block. */
pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize;
pxBlock->xBlockSize = xWantedSize; /* Insert the new block into the list of free blocks. */
prvInsertBlockIntoFreeList( ( pxNewBlockLink ) );
} xFreeBytesRemaining -= pxBlock->xBlockSize;
}
} traceMALLOC( pvReturn, xWantedSize );
}
( void ) xTaskResumeAll(); #if( configUSE_MALLOC_FAILED_HOOK == 1 )
{
if( pvReturn == NULL )
{
extern void vApplicationMallocFailedHook( void );
vApplicationMallocFailedHook();
}
}
#endif return pvReturn;
}
/*-----------------------------------------------------------*/
首先调用 vTaskSuspendAll(); 来挂起所有任务,不允许进程调度;
接着调用 prvHeapInit(); 来初始化相关的内存管理的链表结构:
static void prvHeapInit( void )
{
BlockLink_t *pxFirstFreeBlock;
uint8_t *pucAlignedHeap; /* Ensure the heap starts on a correctly aligned boundary. */
pucAlignedHeap = ( uint8_t * ) ( ( ( portPOINTER_SIZE_TYPE ) &ucHeap[ portBYTE_ALIGNMENT ] ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) ); /* xStart is used to hold a pointer to the first item in the list of free
blocks. The void cast is used to prevent compiler warnings. */
xStart.pxNextFreeBlock = ( void * ) pucAlignedHeap;
xStart.xBlockSize = ( size_t ) 0; /* xEnd is used to mark the end of the list of free blocks. */
xEnd.xBlockSize = configADJUSTED_HEAP_SIZE;
xEnd.pxNextFreeBlock = NULL; /* To start with there is a single free block that is sized to take up the
entire heap space. */
pxFirstFreeBlock = ( void * ) pucAlignedHeap;
pxFirstFreeBlock->xBlockSize = configADJUSTED_HEAP_SIZE;
pxFirstFreeBlock->pxNextFreeBlock = &xEnd;
}
在初始化内存相关的结构的时候,首先将 ucHeap 的地址进行对齐操作,得到可以对齐后用于真实的内存管理的起始地址为:
pucAlignedHeap

然后初始化 xStart 和 xEnd,这两个 marker,然后将整个可用的内存视为一块,可用的内存的开始地方,放置了一个 BlockLink_t 结构体并初始化它的 xBlockSize 为之前调整过的 configADJUSTED_HEAP_SIZE;

我们在回到 pvPortMalloc 的地方,继续分析;
prvHeapInit() 初始化完成后,便可用分配内存了;分配内存的时候,需要对每一个内存块分配一个标志它的描述符,也就是 BlockLink_t 结构体,所以如果要分配 xWantedSize,那么就要分配 :
xWantedSize += heapSTRUCT_SIZE;
然后,对 xWantedSize 进行字节对齐操作;

接下来便进行链表搜寻,找到 Size 合适的地方,将其分配出来;
值得注意的是,内存块链表是有排序的,开始是 xStart 后面跟的内存块,内存块由小到大,最后是 xEnd;
/*
* Insert a block into the list of free blocks - which is ordered by size of
* the block. Small blocks at the start of the list and large blocks at the end
* of the list.
*/
#define prvInsertBlockIntoFreeList( pxBlockToInsert ) \
{ \
BlockLink_t *pxIterator; \
size_t xBlockSize; \
\
xBlockSize = pxBlockToInsert->xBlockSize; \
\
/* Iterate through the list until a block is found that has a larger size */ \
/* than the block we are inserting. */ \
for( pxIterator = &xStart; pxIterator->pxNextFreeBlock->xBlockSize < xBlockSize; pxIterator = pxIterator->pxNextFreeBlock ) \
{ \
/* There is nothing to do here - just iterate to the correct position. */ \
} \
\
/* Update the list to include the block being inserted in the correct */ \
/* position. */ \
pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock; \
pxIterator->pxNextFreeBlock = pxBlockToInsert; \
}
继续看代码;
如果 pxBlock 不是 xEnd 的话,那么说明找到有 Size 大于期望分配的 Size 的 Block 了;
那么就将返回值:
/* Return the memory space - jumping over the BlockLink_t structure at its start. */
pvReturn = ( void * ) ( ( ( uint8_t * ) pxPreviousBlock->pxNextFreeBlock ) + heapSTRUCT_SIZE );
这里,分配内存,能够实际给调用这个 API 接口使用的内存要从起始的 Block 地址加上 heapSTRUCT_SIZE 开始算,因为 heapSTRUCT_SIZE 已经用来表示这个 Block 的信息了;
然后判断剩余的 SIZE 是否大于最小的可用的空间分配的阈值 heapMINIMUM_BLOCK_SIZE :
#define heapMINIMUM_BLOCK_SIZE ( ( size_t ) ( heapSTRUCT_SIZE * 2 ) )
如果剩余的内存空间还足够那么:
/* If the block is larger than required it can be split into two. */
if( ( pxBlock->xBlockSize - xWantedSize ) > heapMINIMUM_BLOCK_SIZE )
{
/* This block is to be split into two. Create a new block
following the number of bytes requested. The void cast is
used to prevent byte alignment warnings from the compiler. */
pxNewBlockLink = ( void * ) ( ( ( uint8_t * ) pxBlock ) + xWantedSize ); /* Calculate the sizes of two blocks split from the single block. */
pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize;
pxBlock->xBlockSize = xWantedSize; /* Insert the new block into the list of free blocks. */
prvInsertBlockIntoFreeList( ( pxNewBlockLink ) );
}
使用新的 pxNewBlockLink 结构表示摘除 pxBlock 内存块后的下一个内存块,并将其初始化,然后按照排序(从小到大的顺序)插入到以 xStart 开始的地方;
所以,被初始化后的内存

分配一次的结果是:

5、释放内存
heap2 支持释放内存:
void vPortFree( void *pv )
{
uint8_t *puc = ( uint8_t * ) pv;
BlockLink_t *pxLink; if( pv != NULL )
{
/* The memory being freed will have an BlockLink_t structure immediately
before it. */
puc -= heapSTRUCT_SIZE; /* This unexpected casting is to keep some compilers from issuing
byte alignment warnings. */
pxLink = ( void * ) puc; vTaskSuspendAll();
{
/* Add this block to the list of free blocks. */
prvInsertBlockIntoFreeList( ( ( BlockLink_t * ) pxLink ) );
xFreeBytesRemaining += pxLink->xBlockSize;
traceFREE( pv, pxLink->xBlockSize );
}
( void ) xTaskResumeAll();
}
}
来自用户释放的指针 pv 是实际的数据指针,代表这个内存的结构体在他前面 heapSTRUCT_SIZE 的位置,所以该 pv 的 BlockLink_t 结构体指针 pxLink = ( void * )(puc - heapSTRUCT_SIZE);
调用 prvInsertBlockIntoFreeList 将其插入到链表中;并且更新当前剩余的内存量;
释放后的内存如下所示:

FreeRTOS --(3)内存管理 heap2的更多相关文章
- 轻量级操作系统FreeRTOS的内存管理机制(一)
本文由嵌入式企鹅圈原创团队成员朱衡德(Hunter_Zhu)供稿. 近几年来,FreeRTOS在嵌入式操作系统排行榜中一直位居前列,作为开源的嵌入式操作系统之一,它支持许多不同架构的处理器以及多种编译 ...
- FreeRTOS 动态内存管理
以下转载自安富莱电子: http://forum.armfly.com/forum.php 本章节为大家讲解 FreeRTOS 动态内存管理,动态内存管理是 FreeRTOS 非常重要的一项功能,前面 ...
- FreeRTOS的内存管理
FreeRTOS提供了几个内存堆管理方案,有复杂的也有简单的.其中最简单的管理策略也能满足很多应用的要求,比如对安全要求高的应用,这些应用根本不允许动态内存分配的. FreeRTOS也允许你自己实现内 ...
- freertos之内存管理
任务.信号量.邮箱才调度器开始调度之前就应该创建,所以它不可能像裸奔程序那样的函数调用能确定需要多少内存资源,RTOS提供了3种内存管理的方法: 1 方法一:确定性好适合于任务.信号量.队列都不被删除 ...
- FreeRTOS --(5)内存管理 heap4
FreeRTOS 中的 heap 4 内存管理,可以算是 heap 2 的增强版本,在 <FreeRTOS --(3)内存管理 heap2>中,我们可以看到,每次内存分配后都会产生一个内存 ...
- FreeRTOS内存管理
简介 Freertos的内存管理分别在heap_1.c,heap_2.c,heap_3.c,heap_4.c,heap_5.c个文件中,选择合适的一种应用于嵌入式项目中即可. 本文的图片中 红色部分B ...
- FreeRTOS --(2)内存管理 heap1
转载自https://blog.csdn.net/zhoutaopower/article/details/106631237 FreeRTOS 提供了5种内存堆管理方案,分别对应heap1/heap ...
- FreeRTOS --(6)内存管理 heap5
转载自https://blog.csdn.net/zhoutaopower/article/details/106748308 FreeRTOS 中的 heap 5 内存管理,相对于 heap 4&l ...
- FreeRTOS--堆内存管理
因为项目需要,最近开始学习FreeRTOS,一开始有些紧张,因为两个星期之前对于FreeRTOS的熟悉度几乎为零,经过对FreeRTOS官网的例子程序的摸索,和项目中问题的解决,遇到了很多熟悉的身影, ...
随机推荐
- Kafka 缺点?
由于是批量发送,数据并非真正的实时: 对于mqtt协议不支持: 不支持物联网传感数据直接接入: 仅支持统一分区内消息有序,无法实现全局消息有序: 监控不完善,需要安装插件: 依赖zookeeper进行 ...
- docker安装部署、fastDFS文件服务器搭建与springboot项目接口
一.docker安装部署 1.更新yum包:sudo yum update 2.安装需要的软件包,yum-util 提供yum-config-manager功能,另外两个是devicemapper驱动 ...
- spring-boot-learning 缓存之redis
什么是BSD协议: BSD是"Berkeley Software Distribution"的缩写,意思是"伯克利软件发行版". BSD开源协议是一个给于使用者 ...
- 面试问题之操作系统:Linux下进程的内存结构
转载于:http://www.hqj.com/news/emb184.htm Linux操作系统采用虚拟内存管理技术,使得每个进程都有各自互不干涉的进程地址空间.该地址空间是大小为4GB的线性虚拟空间 ...
- 【转】ng-class的用法
原文出处:https://segmentfault.com/a/11... 在开发中我们通常会遇到一种需求:一个元素在不同的状态需要展现不同的样子. 而在这所谓的样子当然就是改变其css的属性,而实现 ...
- 从零开始:微信小程序新手入门宝典《一》
为了方便大家了解并入门微信小程序,我将一些可能会需要的知识,列在这里,让大家方便的从零开始学习: 一:微信小程序的特点 张小龙:张小龙全面阐述小程序,推荐通读此文: 小程序是一种不需要下载.安装即可使 ...
- win10 Celery异步任务报错: Task handler raised error: ValueError('not enough values to unpack (expected 3, got 0)
示例代码如下: from celery import Celery app = Celery('tasks', backend='redis://×××:6379/1', broker='redis: ...
- github账号&文章选题
----------------------------------------------------------- https://github.com/yanpanjiao github ...
- vs技巧 - 调试asp.net core源码
学习asp.net core的方式除了看官方文档,看源码是也是一种很好的方式.本文介绍一种方法,简单配置vs,无需第三方插件就可以将asp.net core的源码链接自己的项目,随时穿梭于core的源 ...
- 探索Django验证码功能的实现 - DjangoStarter项目模板里的封装
前言 依然是最近在做的这个项目,用Django做后端,App上提交信息的时候需要一个验证码来防止用户乱提交,正好我的「DjangoStarter」项目脚手架也有封装了验证码功能,不过我发现好像里面只是 ...