224 lines
7.6 KiB
C
224 lines
7.6 KiB
C
#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.
|
|
*
|
|
* <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
|
|
******************************************************************************
|
|
*/
|
|
|
|
|
|
//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<nmemb;i++) //Mark a block of memory is not empty
|
|
{
|
|
mallco_dev.memmap[memx][offset+i]=nmemb;
|
|
}
|
|
return (offset*memblksize[memx]);//Returns the offset address
|
|
}
|
|
}
|
|
return 0XFFFFFFFF;//Not found consistent with allocated memory block
|
|
}
|
|
|
|
//Free memory (internal call)
|
|
//memx:Belongs to a block of memory
|
|
//offset:Memory address of offset
|
|
//Return values: 0, release success; 1, release failed;
|
|
u8 _mem_free(u8 memx,u32 offset)
|
|
{
|
|
int i;
|
|
if(!mallco_dev.memrdy[memx])//Is not initialized, perform the initialization
|
|
{
|
|
mallco_dev.init(memx);
|
|
return 1;//Not initialized
|
|
}
|
|
if(offset<memsize[memx])//Offset within the memory pool.
|
|
{
|
|
int index=offset/memblksize[memx]; //Offset where the memory block number
|
|
int nmemb=mallco_dev.memmap[memx][index]; //Number of memory blocks
|
|
for(i=0;i<nmemb;i++) //Zero out memory blocks
|
|
{
|
|
mallco_dev.memmap[memx][index+i]=0;
|
|
}
|
|
return 0;
|
|
}else return 2;//Offset Super.
|
|
}
|
|
|
|
//Free memory (external call)
|
|
//memx:Belongs to a block of memory
|
|
//ptr:Memory address
|
|
void myfreeOrg(u8 memx,void *ptr)
|
|
{
|
|
u32 offset;
|
|
if(ptr==NULL)return;//Address 0.
|
|
offset=(u32)ptr-(u32)mallco_dev.membase[memx];
|
|
_mem_free(memx,offset);//Free up memory
|
|
}
|
|
|
|
//Free memory (external call)
|
|
//memx:Belongs to a block of memory
|
|
//ptr:Memory address
|
|
void myfree(void *ptr)
|
|
{
|
|
u32 offset;
|
|
if (ptr == NULL) return; // Address 0.
|
|
offset = (u32)ptr - (u32)mallco_dev.membase[SRAMCCM];
|
|
_mem_free(SRAMCCM, offset); // Free up memory
|
|
}
|
|
|
|
//Allocating memory(External call)
|
|
//memx:Belongs to a block of memory
|
|
//size:Memory size (in bytes)
|
|
//Return values: the first address of the allocated memory.
|
|
void *mymallocOrg(u8 memx,u32 size)
|
|
{
|
|
u32 offset;
|
|
offset=_mem_malloc(memx,size);
|
|
if(offset==0XFFFFFFFF)return NULL;
|
|
else return (void*)((u32)mallco_dev.membase[memx]+offset);
|
|
}
|
|
|
|
//Allocating memory(External call)
|
|
//memx:Belongs to a block of memory
|
|
//size:Memory size (in bytes)
|
|
//Return values: the first address of the allocated memory.
|
|
void *mymalloc(u32 size)
|
|
{
|
|
u32 offset;
|
|
offset=_mem_malloc(SRAMCCM, size);
|
|
if (offset == 0XFFFFFFFF) return NULL;
|
|
else return (void*)((u32)mallco_dev.membase[SRAMCCM] + offset);
|
|
}
|
|
|
|
//Reallocating memory (external call)
|
|
//memx:Belongs to a block of memory
|
|
//*ptr:The old memory address
|
|
//size:Amount of memory to allocate (in bytes)
|
|
//Return values: the first address of the newly allocated memory.
|
|
void *myreallocOrg(u8 memx,void *ptr,u32 size)
|
|
{
|
|
u32 offset;
|
|
offset=_mem_malloc(memx,size);
|
|
if(offset==0XFFFFFFFF)return NULL;
|
|
else
|
|
{
|
|
mymemcpy((void*)((u32)mallco_dev.membase[memx]+offset),ptr,size); //Copy the old memory contents to the new memory
|
|
myfreeOrg(memx,ptr); //Free old memory
|
|
return (void*)((u32)mallco_dev.membase[memx]+offset); //Returns a new memory address
|
|
}
|
|
}
|
|
|
|
//Reallocating memory (external call)
|
|
//memx:Belongs to a block of memory
|
|
//*ptr:The old memory address
|
|
//size:Amount of memory to allocate (in bytes)
|
|
//Return values: the first address of the newly allocated memory.
|
|
void *myrealloc(void *ptr, u32 size)
|
|
{
|
|
u32 offset;
|
|
offset = _mem_malloc(SRAMCCM, size);
|
|
if (offset == 0XFFFFFFFF) return NULL;
|
|
else
|
|
{
|
|
mymemcpy((void*)((u32)mallco_dev.membase[SRAMCCM] + offset), ptr, size); //Copy the old memory contents to the new memory
|
|
myfree(ptr); //Free old memory
|
|
return (void*)((u32)mallco_dev.membase[SRAMCCM] + offset); //Returns a new memory address
|
|
}
|
|
}
|