Memory management experiment notes

define memory pool##

//The 32-byte alignment of the internal SRAM memory pool indicates that it is efficient to take out 32 bytes at a time
__align(32) u8 mem1base[MEM1_MAX_SIZE];
_**_align(32)** u8 mem2base[MEM2_MAX_SIZE] attribute ((at(0X68000000)) );

Define the table that manages the memory pool

**虽然起到管理内存的作用  但是同时也带来了内存开销(浪费内存)**

u16 mem1mapbase[MEM1_ALLOC_TABLE_SIZE]; //Internal SRAM memory pool MAP
u16 mem2mapbase[MEM2_ALLOC_TABLE_SIZE] attribute ((at(0X68000000+MEM2_MAX_SIZE))); //External SRAM memory pool MAP

//Memory management parameters/ /The size of the memory table and memory pool are stored in these arrays

const u32 memtblsize[SRAMBANK]={MEM1_ALLOC_TABLE_SIZE,MEM2_ALLOC_TABLE_SIZE}; //memory table size
const u32 memblksize[SRAMBANK]={MEM1_BLOCK_SIZE,MEM2_BLOCK_SIZE}; //memory block size
const u32 memsize[SRAMBANK]={MEM1_MAX_SIZE,MEM2_MAX_SIZE};

//The memory management controller defines a structure

struct _m_mallco_dev
{
    void (*init)(u8);                   //**函数指针**   用于调用初始化函数
    u8 (*perused)(u8);                  //   同样是函数指针 用于调用使用率函数
    u8  *membase[SRAMBANK];     //内存池 管理SRAMBANK个区域的内存   
    u16 *memmap[SRAMBANK];              //内存管理状态表
    u8  memrdy[SRAMBANK];               //内存管理是否就绪
};

Define a global variable of the above structure and initialize it accordingly

struct _m_mallco_dev mallco_dev=
{
    my_mem_init,                //内存初始化
    my_mem_perused,             //内存使用率
    mem1base,mem2base,          //内存池
    mem1mapbase,mem2mapbase,    //内存管理状态表
    0,0,                        //内存管理未就绪   1表示就绪
};

Initialize zeroing of memory tables and memory pools

//内存管理初始化  
//memx:所属内存块
void my_mem_init(u8 memx)  
{  
    mymemset(mallco_dev.memmap[memx], 0,memtblsize[memx]*2);//内存状态表数据清零  
    mymemset(mallco_dev.membase[memx], 0,memsize[memx]);    //内存池所有数据清零  
    mallco_dev.memrdy[memx]=1;                              //内存管理初始化OK  
}
void mymemset(void *s,u8 c,u32 count)  
{  
    u8 *xs = s;  
    while(count--)*xs++=c;  
}

The memory allocation function malloc (mem_addr, size)
The malloc function returns a specific address
, so it is necessary to find a continuous size of memory and return the first address of this continuous memory

How to find it?

Method: Start from the tail to find the 32-byte block and move forward one by one until you find the end of a row.
So first, get size/32+size%32 to get how many blocks are needed. Start
from the tail of the memory management table and manage the memory management table. The number of blocks is given to the offset offset and then offset – until a continuous block is found to jump out of the loop. After
finding it, don’t forget to set the corresponding block in the management table to 1 to mark that it is already in use

What is the basis for finding?

The management table is set to 0 to indicate that it is not used

The specific code is as follows:

//内存分配(内部调用)
//memx:所属内存块
//size:要分配的内存大小(字节)
//返回值:0XFFFFFFFF,代表错误;其他,内存偏移地址 
u32 my_mem_malloc(u8 memx,u32 size)  
{  
    signed long offset=0;  
    u32 nmemb;  //需要的内存块数  
    u32 cmemb=0;//连续空内存块数
    u32 i;  
    if(!mallco_dev.memrdy[memx])mallco_dev.init(memx);//未初始化,先执行初始化 
    if(size==0)return 0XFFFFFFFF;//不需要分配
    nmemb=size/memblksize[memx];    //获取需要分配的连续内存块数
    if(size%memblksize[memx])nmemb++;  
    for(offset=memtblsize[memx]-1(其实这个就是管理的块数量);offset>=0;offset--)//搜索整个内存控制区  
    {     
        if(!mallco_dev.memmap[memx][offset])cmemb++;//连续快计数
        else cmemb=0;                               //如果中间是断开的计数清零  需要重新计数
        if(cmemb==nmemb)                            //找到了连续nmemb个空内存块
        {
            for(i=0;i<nmemb;i++)                    //标注内存块非空   别忘记标记找的块
            {  
                mallco_dev.memmap[memx][offset+i]=nmemb;  
            }  //这不是二维数组 因为memmap[memx]=数组首地址
            return (offset*memblksize[memx]);//返回偏移地址  
        }
    }  
    return 0XFFFFFFFF;//未找到符合分配条件的内存块  
}  

“`

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325770609&siteId=291194637