#include "includes.h"
#ifdef USE_OS_MEM
#if 1
#define MEMPrintf vgDebugPrintf
#else
void MEMPrintf(char*s,...){}
#endif
typedef struct SS_MYMEM{
struct SS_MYMEM *next;
OS_MEM *pMem;
}S_MYMEM;
S_MYMEM MyMemTbl[30],*pUseMyMem=NULL,*pFreeMyMem;
void malloc_error(INT32U t_len,INT8U i,INT8U err)
{
MEMPrintf("MEM %d %d %ld error",i,err,t_len);
asm("clr 0xD");
while (1) ;
}
//申请缓存
void *Jmalloc(unsigned long t_len)
{
INT8U err;
void *p_des=NULL;
S_MYMEM *pt;
pt=pUseMyMem;
while(pt!=NULL)
{
if(t_len<=pt->pMem->OSMemBlkSize)
{
p_des=OSMemGet(pt->pMem, &err);
if(err!=OS_NO_ERR) malloc_error(t_len,0,err);
else MEMPrintf("GEM:%ld OK\r\n",t_len);
return p_des;
}
pt=pt->next;
}
malloc_error(t_len,8,0xff);
return NULL;
}
//释放缓存
void Jfree(void *pblk)
{
S_MYMEM *pt;
pt=pUseMyMem;
while(pt!=NULL)
{
if( (INT32U)(pblk)>=(INT32U)pt->pMem->OSMemAddr &&
(INT32U)(pblk)<( (INT32U)pt->pMem->OSMemAddr+pt->pMem->OSMemNBlks*pt->pMem->OSMemBlkSize ) )
{
MEMPrintf("REM:%ld\r\n",pt->pMem->OSMemBlkSize);OSMemPut(pt->pMem, pblk);return;
}
pt=pt->next;
}
return;
}
#define CREATEMEM(NUM,SIZE) do{\
INT8U err; \
S_MYMEM *pt; \
static INT32U BUF[NUM*SIZE/4]; \
pt=pFreeMyMem;pFreeMyMem=pFreeMyMem->next;pt->next=pUseMyMem;pUseMyMem=pt;\
pUseMyMem->pMem=OSMemCreate(BUF, NUM, SIZE, &err);MEMPrintf("初始化申请内存%d %lX\r\n",err,(INT32U)BUF);}while(0)
//初始化缓存 在UCOS初始化之后,可以放在OSInitHookEnd里面
void InitMem(void)
{
//OS init 之后
INT8U i;
for(i=0;i<GetItemSum(MyMemTbl);i++)
{
if(i!=GetItemSum(MyMemTbl)-1) MyMemTbl[i].next=&MyMemTbl[i+1];
else MyMemTbl[i].next=NULL;
MyMemTbl[i].pMem=NULL;
}
pUseMyMem=NULL;pFreeMyMem=&MyMemTbl[0];
//由大到小的载入 必须的
CREATEMEM(2,1024*2046);
CREATEMEM(2,1024*512);
CREATEMEM(4,1024*128);
CREATEMEM(4,1024*32);
CREATEMEM(4,1024*16);
CREATEMEM(16,1024*8);
CREATEMEM(32,1024*4);
CREATEMEM(128,512);
CREATEMEM(1024,80);
}
#endif |