本帖最后由 mrmzyking 于 2017-11-11 11:17 编辑
头文件
#ifndef __MEM_H__
#define __MEM_H__
#include "stdlib.h"
#include "stdio.h"
#include "stm32f4xx_hal.h"
#define Mem_Total_Size (32*1024*1024L) //需要用户填写
#define Mem_Block_Size (2*64*1024L) //需要用户填写
#define Mem_Start_Addr (0xC0000000L) //需要用户填写
#define Mem_Manage_Size (sizeof(struct Mem_Info) * Mem_Block_Num)
#define Mem_Block_Num (Mem_Total_Size/(sizeof(struct Mem_Info)+ Mem_Block_Size ))
struct Memery_Info
{
unsigned long TotalSizes ;
unsigned long TotalBlock ;
unsigned long Bytes_Block ;
unsigned long FreeSizeBytes ;
unsigned long FreeBlock ;
unsigned int Mem_Start ;
unsigned int Mem_End ;
} ;
struct Mem_Info
{
unsigned char Flag ; //0:free;1:used
unsigned long BlkNum ;
unsigned int Start_Addr ;
unsigned int End_Addr ;
unsigned char *Buff ;
} ;
extern unsigned char Init_Memery( void ) ;
extern void *Get_Mem( unsigned long Size) ;
extern unsigned char Free_Mem( void *p ) ;
#endif
实现文件
#include "mem.h"
struct Memery_Info Memry ;
struct Mem_Info Mem[Mem_Block_Num] __attribute__ ((at(Mem_Start_Addr)));;
unsigned char Mem_Buff[Mem_Block_Num][Mem_Block_Size] __attribute__ ((at(Mem_Start_Addr + Mem_Manage_Size)));
unsigned char Init_Memery( void )
{
unsigned long i ;
volatile unsigned int tmpreg ;
tmpreg = Mem_Manage_Size + Mem_Block_Num * Mem_Block_Size ;
if( tmpreg > Mem_Total_Size )
{
tmpreg = Mem_Total_Size/(sizeof(struct Mem_Info)+Mem_Block_Size) ;
return 0 ;
}
Memry.TotalSizes = Mem_Total_Size ;
Memry.TotalBlock = Mem_Block_Num ;
Memry.Bytes_Block = Mem_Block_Size ;
Memry.FreeSizeBytes = Mem_Total_Size ;
Memry.Mem_Start = Mem_Start_Addr ;
Memry.FreeBlock = Mem_Block_Num ;
Memry.Mem_End = Mem_Start_Addr + Mem_Total_Size ;
for(i = 0 ; i < Mem_Block_Num ; i++ )
{
Mem.Flag = 0 ;
Mem.BlkNum = 0 ;
Mem.Start_Addr = Memry.Mem_Start + i * Mem_Block_Size ;
Mem.End_Addr = Memry.Mem_Start + ( i + 1 ) * Mem_Block_Size ;
Mem.Buff = ( unsigned char *)&Mem_Buff[0] ;
}
return 1 ;
}
void *Get_Mem( unsigned long Size)
{
volatile unsigned long i ,j ;
volatile unsigned long NeedBlk ;
volatile unsigned long Blktmp ;
volatile unsigned char Flag ;
if(Size > Memry.FreeSizeBytes)
{
return NULL ;
}
NeedBlk = ( unsigned long )( Size / Mem_Block_Size ) ;
if( ( Size % Mem_Block_Size ) != 0 )
{
NeedBlk++ ;
}
if( NeedBlk > Memry.FreeBlock )
{
return NULL ;
}
Blktmp = 0 ;
Flag = 0 ;
for( i = 0 ; i < Mem_Block_Num ; i++ )
{
if( Mem.Flag == 0 )
{
for( j = 0 ; j < NeedBlk ; j++ )
{
if( Mem[ i + j ].Flag == 0 )
{
Blktmp++ ;
if( Blktmp == NeedBlk )
{
Flag = 1 ;
break ;
}
}
else
{
break ;
}
}
}
if( Flag == 1 )
{
break ;
}
}
if( Flag == 1 )
{
for( j = 0 ; j < NeedBlk ; j++ )
{
Mem[i+j].Flag = 1 ;
}
Mem.BlkNum = NeedBlk ;
Memry.FreeSizeBytes = Memry.TotalSizes - Size ;
Memry.FreeBlock = Memry.FreeBlock - NeedBlk ;
return (void *)Mem.Buff ;
}
else
{
return NULL ;
}
}
unsigned char Free_Mem( void *p )
{
unsigned char *q ;
unsigned long i ;
unsigned long j ;
unsigned long tmpreg ;
if( p==NULL )
{
return 0 ;
}
q = ( unsigned char *)p ;
for( i = 0 ; i < Mem_Block_Num ; i++ )
{
if( Mem.Start_Addr == ( unsigned int )( q ) )
{
if( Mem.Flag == 1 )
{
if( Mem.BlkNum == 0 )
{
Mem.Flag = 0 ;
Memry.FreeBlock++ ;
}
else
{
tmpreg = Mem.BlkNum ;
for( j = 0 ; j < tmpreg ; j++ )
{
Mem[i+j].Flag = 0 ;
Mem[i+j].BlkNum = 0 ;
Memry.FreeBlock++ ;
}
break ;
}
}
else
{
return 0 ;
}
}
}
p = (unsigned char *)0 ;
return 1 ;
}
请大家批评指正
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>