FreeRTOS代码阅读笔记:heap_5.c

  • Post author:
  • Post category:其他


FreeRTOS中对于内存的管理当前一共有5种实现方式(作者当前的版本是10.1.1),均在【 \Source\portable\MemMang 】下面,这里笔记下。

heap_5.c和heap_4.c实现方法比较相似。在调用API之前需要想调用 vPortDefineHeapRegions()来初始化内存堆。

heap5 允许内存堆跨越多个连续的内存段。比如STM32内部RAM做内存堆,但是需要大内存时候,可外接SRAM甚至SDRAM。

首先定义内存段:

HeapRegion_t xHeapRegions[]=
{
    {(uint8_t *)0x200001C0UL,0x10000}, //内部 RAM,内存起始地址 0x200001C0 大小64KB
		{(uint8_t *)0x68000000UL,0x100000},//外部SRAM,内存起始地址 0x68000000 大小1MB
    {NULL,0}                           //数组结尾 
};

在一开始就要调用函数vPortDefineHeapRegions完成内存堆的初始化!然后才能创建任务和堆栈。。。

heap5和heap4内容基本一样,请参照。

/*
heap5.c和heap4.c类似,但是heap5.c的内存是由外部定义的,因此在使用前必须调用函数vPortDefineHeapRegions()来说明要使用的内存。
vPortDefineHeapRegions()的入参可以参考下面的英文注释。
*/
/*
 * A sample implementation of pvPortMalloc() that allows the heap to be defined
 * across multiple non-contigous blocks and combines (coalescences) adjacent
 * memory blocks as they are freed.
 *
 * See heap_1.c, heap_2.c, heap_3.c and heap_4.c for alternative
 * implementations, and the memory management pages of http://www.FreeRTOS.org
 * for more information.
 *
 * Usage notes:
 *
 * vPortDefineHeapRegions() ***must*** be called before pvPortMalloc().
 * pvPortMalloc() will be called if any task objects (tasks, queues, event
 * groups, etc.) are created, therefore vPortDefineHeapRegions() ***must*** be
 * called before any other objects are defined.
 *
 * vPortDefineHeapRegions() takes a single parameter.  The parameter is an array
 * of HeapRegion_t structures.  HeapRegion_t is defined in portable.h as
 *
 * typedef struct HeapRegion
 * {
 *  uint8_t *pucStartAddress; << Start address of a block of memory that will be part of the heap.
 *  size_t xSizeInBytes;      << Size of the block of memory.
 * } HeapRegion_t;
 *
 * The array is terminated using a NULL zero sized region definition, and the
 * memory regions defined in the array ***must*** appear in address order from
 * low address to high address.  So the following is a valid example of how
 * to use the function.
 *
 * HeapRegion_t xHeapRegions[] =
 * {
 *  { ( uint8_t * ) 0x80000000UL, 0x10000 }, << Defines a block of 0x10000 bytes starting at address 0x80000000
 *  { ( uint8_t * ) 0x90000000UL, 0xa0000 }, << Defines a block of 0xa0000 bytes starting at address of 0x90000000
 *  { NULL, 0 }                << Terminates the array.
 * };
 *
 * vPortDefineHeapRegions( xHeapRegions ); << Pass the array into vPortDefineHeapRegions().
 *
 * Note 0x80000000 is the lower address so appears in the array first.
 *
 */
#include <stdlib.h>
 
/* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
all the API functions to use the MPU wrappers.  That should only be done when
task.h is included from an application file. */
#define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
 
#include "FreeRTOS.h"
#include "task.h"
 
#undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
 
/* Block sizes must not get too small. */
#define heapMINIMUM_BLOCK_SIZE  ( ( size_t ) ( uxHeapStructSize << 1 ) )
 
/*
1个byte有8个bits
*/
/* Assumes 8bit bytes! */
#define heapBITS_PER_BYTE       ( ( size_t ) 8 )
 
/* 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其实也是放在它对应的空余内存的头部的,但是空闲内存的大小并没有考虑放入的BlockLink_t */
} BlockLink_t;
//空闲内存管理结构体,通过它来管理释放回来的内存
 
/*-----------------------------------------------------------*/
 
/*
 * Inserts a block of memory that is being freed 



版权声明:本文为ZenNaiHeQiao原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。