/*
* FreeRTOS Kernel V10.4.3
* Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* https://www.FreeRTOS.org
* https://github.com/FreeRTOS
*
*/
/*
* A sample implementation of pvPortMalloc() and vPortFree() that combines
* (coalescences) adjacent memory blocks as they are freed, and in so doing
* limits memory fragmentation.
*
* See heap_1.c, heap_2.c and heap_3.c for alternative implementations, and the
* memory management pages of https://www.FreeRTOS.org for more information.
*/
#include <stdlib.h>
#include "heap_4.h"
/********************************只需要修改这里*************************************/
/*内存大小*/
#define configTOTAL_HEAP_SIZE ( ( size_t ) ( 10 * 1024 ) )
/***********************************************************************************/
/*字节对齐*/
#define portBYTE_ALIGNMENT 8
#define portBYTE_ALIGNMENT_MASK ( 0x0007 )
#define portMAX_DELAY ( size_t ) 0xffffffffUL
/* Block sizes must not get too small. */
#define heapMINIMUM_BLOCK_SIZE ( ( size_t ) ( xHeapStructSize << 1 ) )
/* Assumes 8bit bytes! */
#define heapBITS_PER_BYTE ( ( size_t ) 8 )
/* Allocate the memory for the heap. */
static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
/* Define the linked list structure. This is used to link free blocks in order
* of their memory address. */
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;
/*-----------------------------------------------------------*/
/*
* Inserts a block of memory that is being freed into the correct position in
* the list of free memory blocks. The block being freed will be merged with
* the block in front it and/or the block behind it if the memory blocks are
* adjacent to each other.
*/
static void prvInsertBlockIntoFreeList( BlockLink_t * pxBlockToInsert );
/*
* Called automatically to setup the required heap structures the first time
* pvPortMalloc() is called.
*/
static void prvHeapInit( void );
/*-----------------------------------------------------------*/
/* The size of the structure placed at the beginning of each allocated memory
* block must by correctly byte aligned. */
static const size_t xHeapStructSize = ( sizeof( BlockLink_t ) + ( ( size_t ) ( portBYTE_ALIGNMENT - 1 ) ) ) & ~( ( size_t ) portBYTE_ALIGNMENT_MASK );
/* Create a couple of list links to mark the start and end of the list. */
static BlockLink_t xStart, * pxEnd = NULL;
/* Keeps track of the number of calls to allocate and free memory as well as the
* number of free bytes remaining, but says nothing about fragmentation. */
static size_t xFreeBytesRemaining = 0U;
static size_t xMinimumEverFreeBytesRemaining = 0U;
static size_t xNumberOfSuccessfulAllocations = 0;
static size_t xNumberOfSuccessfulFrees = 0;
/* Gets set to the top bit of an size_t type. When this bit in the xBlockSize
* member of an BlockLink_t structure is set then the block belongs to the
* application. When the bit is free the block is still part of the free heap
* space. */
static size_t xBlockAllocatedBit = 0;
/*-----------------------------------------------------------*/
void * pvPortMalloc( size_t xWantedSize )
{
BlockLink_t * pxBlock, * pxPreviousBlock, * pxNewBlockLink;
void * pvReturn = NULL;
/* If this is the first call to malloc then the heap will require
* initialisation to setup the list of free blocks. */
if( pxEnd == NULL )
{
prvHeapInit();
}
else
{
}
/* Check the requested block size is not so large that the top bit is
* set. The top bit of the block size member of the BlockLink_t structure
* is used to determine who owns the block - the application or the
* kernel, so it must be free. */
if( ( xWantedSize & xBlockAllocatedBit ) == 0 )
{
/* The wanted size must be increased so it can contain a BlockLink_t
* structure in addition to the requested amount of bytes. */
if( ( xWantedSize > 0 ) &&
( ( xWantedSize + xHeapStructSize ) > xWantedSize ) ) /* Overflow check */
{
xWantedSize += xHeapStructSize;
/* Ensure that blocks are always aligned. */
if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0x00 )
{
/* Byte alignment required. Check for overflow. */
if( ( xWantedSize + ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) ) )
> xWantedSize )
{
xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
}
else
{
xWantedSize = 0;
}
}
else
{
}
}
else
{
xWantedSize = 0;
}
if( ( xWantedSize > 0 ) && ( xWantedSize <= xFreeBytesRemaining ) )
{
/* Traverse the list from the start (lowest address) 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 the end marker was reached then a block of adequate size
* was not found. */
if( pxBlock != pxEnd )
{
/* Return the memory space pointed to - jumping over the
* BlockLink_t structure at its start. */
pvReturn = ( void * ) ( ( ( uint8_t * ) pxPreviousBlock->pxNextFreeBlock ) + xHeapStructSize );
/* 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 );
}
else
{
}
xFreeBytesRemaining -= pxBlock->xBlockSize;
if( xFreeBytesRemaining < xMinimumEverFreeBytesRemaining )
{
xMinimumEverFreeBytesRemaining = xFreeBytesRemaining;
}
else
{
}
/* The block is being returned - it is allocated and owned
* by the application and has no "next" block. */
pxBlock->xBlockSize |= xBlockAlloc
FreeRTOS里的heap-4文件
需积分: 0 51 浏览量
更新于2023-09-13
收藏 5KB ZIP 举报
FreeRTOS是一个广泛使用的实时操作系统(RTOS),它为嵌入式系统提供了一个轻量级的调度器,用于管理和协调多个并发任务。在FreeRTOS中,内存管理是系统运行的关键部分,因为它确保了各个任务能有效地分配和释放内存。heap_4是FreeRTOS中的一种内存管理策略,称为"内存池"或"通用堆内存管理"。
heap_4.c和heap_4.h是实现这个内存管理策略的源代码文件。heap_4.c包含了具体的内存分配和释放函数的实现,而heap_4.h则定义了相关的数据结构和函数接口。下面将详细解释heap_4的核心概念和功能:
1. **内存分配策略**:heap_4使用了内存池的概念,预先定义了一块连续的内存区域作为“池”,然后从中动态地分配和释放内存块。这种方法比简单的堆管理更可控,尤其是在资源有限的嵌入式环境中。
2. **内存分配函数**:heap_4提供了`pvPortMalloc()`和`vPortFree()`函数,分别用于申请和释放内存。这两个函数是FreeRTOS内存管理的核心,它们确保了内存的安全分配和释放。
3. **内存碎片管理**:heap_4通过合并相邻的空闲内存块来减少内存碎片。当一个内存块被释放后,heap_4会尝试将其与相邻的空闲块合并,从而保持内存的有效利用率。
4. **数据结构**:heap_4使用链表数据结构来跟踪已分配和未分配的内存块。每个内存块都有一个头部,包含关于该内存块的信息,如大小和状态。
5. **内存溢出检测**:heap_4还提供了可选的内存溢出检测机制,通过在分配的内存前后添加边界标记,可以在程序运行时检测到非法越界访问。
6. **内存统计**:heap_4允许开发者获取当前系统的内存使用情况,如总内存大小、已分配内存、空闲内存等,这对于调试和性能优化非常有用。
7. **兼容性**:heap_4设计为裸机环境直接使用,这意味着它不依赖于操作系统服务,适用于那些没有高级库支持的简单嵌入式系统。
8. **配置选项**:FreeRTOS的内存管理策略是可以配置的,heap_4也不例外。开发者可以根据需求调整内存池的大小、是否启用内存检查等功能。
在实际应用中,理解heap_4的工作原理并正确配置其参数,对于提高FreeRTOS系统的稳定性和效率至关重要。开发人员需要根据硬件资源和应用程序的需求,合理地设置内存池大小,并且在调试过程中密切关注内存使用情况,以避免内存泄漏和其他相关问题。同时,由于heap_4是开源的,开发者可以深入研究其源代码,以适应特定项目的需求,甚至对其进行定制和优化。
内向的新之助
- 粉丝: 3
- 资源: 4
最新资源
- CCF-大数据竞赛-基金间的相关性预测-复赛19名.zip
- MR深度报告三部曲之内容应用:3D内容深度报告:空间互联网与未来应用探讨
- PMSM永磁同步电机最大转矩电流比MTPA控制仿真,弱磁控制仿真,前馈补偿仿真程序,详细解析教程文档 这是一份非常完美的仿真文件及详细教程,从仿真效果图看转速、电流及转矩跟随非常稳定 该算法架构包
- CesiumJS 的扩展库,提供事件订阅、大数据geojson加载、geojson样式、tooltip、popup、缩放控制、绘图工具、测量工具、双屏联动、指南针、热力图等功能.zip
- Collection of China illegal cases about web crawler 本项目用来整理所有中国大陆爬虫开发者涉诉与违规相关的新闻、资料与法律法规 致力于帮助在中.zip
- LLC谐振变器变频与移相混合控制 仿真模型采用混合控制,控制策略为:当输入电压较低时,采用变频控制,变器满占空比工作,通过改变开关频率来调节输出电压,称此时变器工作在变频(Variable-Frequ
- 2024最新聚合免签约云支付系统平台整站源码+更新N套首页模板
- 三相车载充电机充电桩PWM整流+全桥LLC Simlink仿真模型 前级三相PWM整流,单位功率因数运行,AC输入176~264V,中间级直流母线750V,一定范围内母线电压可调 采用七段式SVPW
- eletronicMallcssadasdas
- hadoop-cos(CosN文件系统)为Apache Hadoop、Spark以及Tez等大数据计算框架集成提供支持,可以像访问HDFS一样读写存储在腾讯云COS上的数据 同时也支持作为Dr.zip
- 超级记事本-仅个人学习使用
- HMX-Server分步式服务器框架,主要分为网关、登录、世界、场景、数据服务器,适用于大中小型项目.zip
- JavaScript 版本的数据结构,提供常用的数据结构封装,基于清华大学邓俊辉老师的数据结构课程.zip
- Java版的GBRT及RF,曾在天池大数据竞赛中获得2次TOP10.zip
- keras+tensorflow+python3下的中文分词, 大数据可训练,解决内存不够用问题.zip
- 基于张正友标定法的相机标定实验