#include "malloc.h"
/**
******************************************************************************
* @file STM32F4xx_IAP/src/malloc.h
* @author MCD Application Team
* @version V1.0.0
* @date 10-October-2015
* @brief Main program body
******************************************************************************
* @attention
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
*
© COPYRIGHT 2011 STMicroelectronics
******************************************************************************
*/
//Memory pool (4-byte alignment)
//__align(4) u8 mem1base[MEM1_MAX_SIZE];
__align(4) u8 mem3base[MEM3_MAX_SIZE] __attribute__((at(0x10000000))); //CMM within the memory pool
//Memory management tables
//u16 mem1mapbase[MEM1_ALLOC_TABLE_SIZE]; //The internal SRAM memory pool MAP
u16 mem3mapbase[MEM3_ALLOC_TABLE_SIZE] __attribute__((at(0X10000000 + MEM3_MAX_SIZE))); //Internal CCM pools MAP
//Memory management parameters
const u32 memtblsize[SRAMBANK] = {MEM3_ALLOC_TABLE_SIZE}; //Memory table size
const u32 memblksize[SRAMBANK] = {MEM3_BLOCK_SIZE}; //Memory block size
const u32 memsize [SRAMBANK] = {MEM3_MAX_SIZE}; //Total memory size
//Memory management controller
struct _m_mallco_dev mallco_dev=
{
mymem_init, //Memory initialization
mem_perused, //Memory usage
mem3base, //Memory pool
mem3mapbase, //Memory management tables
0, //Memory management is not ready
};
//Copy memory
//*des:Destination address
//*src:Source address
//n:Need to copy memory length (bytes)
void mymemcpy(void *des,void *src,u32 n)
{
u8 *xdes = des;
u8 *xsrc = src;
while(n--) *xdes++ = *xsrc++;
}
//Setting memory
//*s:Memory address
//c :The value to be set
//count:Need to set the memory size(Bytes)
void mymemset(void*s,u8 c,u32 count)
{
u8 *xs = s;
while(count--) *xs++=c;
}
//Memory management class
//memx:Belongs to a block of memory
void mymem_init(u8 memx)
{
mymemset(mallco_dev.memmap [memx], 0, memtblsize[memx] * 2); //Memory status table is cleared to zero
mymemset(mallco_dev.membase[memx], 0, memsize [memx]); //Memory pool all data to zero
mallco_dev.memrdy[memx] = 1; //Memory initialized OK
}
//Get memory usage
//memx:Belongs to a block of memory
//Return value: usage (0~100)
u8 mem_perused(u8 memx)
{
u32 used = 0;
u32 i;
for (i = 0; i < memtblsize[memx]; i++)
{
if (mallco_dev.memmap[memx][i]) used++;
}
return (used * 100) / (memtblsize[memx]);
}
//Memory allocation (internal call)
//memx:Belongs to a block of memory
//size:Amount of memory to allocate (in bytes)
//The return value:0XFFFFFFFF,To indicate an error; the other, the memory offset
u32 _mem_malloc(u8 memx,u32 size)
{
signed long offset=0;
u16 nmemb; //Required number of memory blocks
u16 cmemb=0;//Number of contiguous blocks of free memory
u32 i;
if(!mallco_dev.memrdy[memx])mallco_dev.init(memx);//Is not initialized, perform the initialization
if(size==0)return 0XFFFFFFFF;//Do not need to allocate
nmemb=size/memblksize[memx]; //Gets the required number of contiguous blocks of memory allocated
if(size%memblksize[memx])nmemb++;
for(offset=memtblsize[memx]-1;offset>=0;offset--)//Search the entire memory control
{
if(!mallco_dev.memmap[memx][offset])cmemb++;//Increase the number of contiguous blocks of free memory
else cmemb=0; //Contiguous blocks of memory to zero
if(cmemb==nmemb) //Find nmemb consecutive empty memory blocks
{
for(i=0;i