Tearsback's picture
download
raw
11.4 kB
/*********************************************************************************************************
* �����: Malloc.c
* ժ Ҫ: Mallocģ��
* ��ǰ�汾: 1.0.0
* �� ��: SZ-BMQ(COPYRIGHT 2018 SZ-BMQ. All rights reserved.)
* �������: 2018��01��01��
* �� ��:
* ע ��: none
**********************************************************************************************************
* ȡ���汾:
* �� ��:
* �������:
* �޸�����:
* �޸��ļ�:
*********************************************************************************************************/
/*********************************************************************************************************
* ����ͷ�ļ�
*********************************************************************************************************/
#include "Malloc.h"
#include "gd32f470x_conf.h"
/*********************************************************************************************************
* �궨��
*********************************************************************************************************/
/*********************************************************************************************************
* ö�ٽṹ��
*********************************************************************************************************/
/*********************************************************************************************************
* �ڲ���������
*********************************************************************************************************/
//�ڴ��(32�ֽڶ���)
__align(32) u8 Memory0Base[MEM0_MAX_SIZE]; //__attribute__((at((u32)0x10000000))); //TCMSRAM�ڴ��
__align(32) u8 Memory1Base[MEM1_MAX_SIZE]; //�ڲ�SRAM�ڴ��
__align(32) u8 Memory2Base[MEM2_MAX_SIZE] __attribute__((at((u32)0xD0000000 + 4 * 801 * 480))); //SDRAM�ڴ��
//�ڴ������
u32 Memory0MapBase[MEM0_ALLOC_TABLE_SIZE];// __attribute__((at((u32)0x10000000 + MEM0_MAX_SIZE))); //TCMSRAM�ڴ��MAP
u32 Memory1MapBase[MEM1_ALLOC_TABLE_SIZE]; //�ڲ�SRAM�ڴ��MAP
u32 Memory2MapBase[MEM2_ALLOC_TABLE_SIZE] __attribute__((at((u32)0xD0000000 + 4 * 801 * 480 + MEM2_MAX_SIZE))); //SDRAM�ڴ��MAP
//�ڴ��������
const u32 c_iMemoryTblSize[SRAMBANK] = {MEM0_ALLOC_TABLE_SIZE, MEM1_ALLOC_TABLE_SIZE, MEM2_ALLOC_TABLE_SIZE}; //�ڴ����С
const u32 c_iMemoryBlkSize[SRAMBANK] = {MEM0_BLOCK_SIZE, MEM1_BLOCK_SIZE, MEM2_BLOCK_SIZE}; //�ڴ�ֿ��С
const u32 c_iMemorySize[SRAMBANK] = {MEM0_MAX_SIZE, MEM1_MAX_SIZE, MEM2_MAX_SIZE}; //�ڴ��ܴ�С
//�ڴ����������
static StructMallocDeviceDef s_structMallocDev =
{
InitMemory, //�ڴ��ʼ��
MemoryPerused, //�ڴ�ʹ����
Memory0Base, //�ڴ��
Memory1Base, //�ڴ��
Memory2Base, //�ڴ��
Memory0MapBase, //�ڴ����״̬��
Memory1MapBase, //�ڴ����״̬��
Memory2MapBase, //�ڴ����״̬��
0, //�ڴ����δ����
0, //�ڴ����δ����
0, //�ڴ����δ����
};
/*********************************************************************************************************
* �ڲ���������
*********************************************************************************************************/
static void SetMemory(void* s, u8 c, u32 count); //�����ڴ�
static void CopyMemory(void* des, void* src, u32 n); //�����ڴ�
static u32 MallocMemory(u8 memx, u32 size); //�ڴ����
static u8 FreeMemory(u8 memx, u32 offset); //�ڴ��ͷ�
/*********************************************************************************************************
* �ڲ�����ʵ��
*********************************************************************************************************/
/*********************************************************************************************************
* ��������: SetMemory
* ��������: �����ڴ�
* �������: *s���ڴ��׵�ַ��c��Ҫ���õ�ֵ��count����Ҫ���õ��ڴ��С���ֽ�Ϊ��λ��
* �������: void
* �� �� ֵ: void
* ��������: 2018��01��01��
* ע ��:
*********************************************************************************************************/
static void SetMemory(void* s, u8 c, u32 count)
{
u8 *xs = s;
while(count--)
{
*xs++ = c;
}
}
/*********************************************************************************************************
* ��������: CopyMemory
* ��������: �����ڴ�
* �������: *des��Ŀ�ĵ�ַ��*src��Դ��ַ��n����Ҫ���Ƶ��ڴ泤�ȣ��ֽ�Ϊ��λ��
* �������: void
* �� �� ֵ: void
* ��������: 2018��01��01��
* ע ��:
*********************************************************************************************************/
static void CopyMemory(void* des, void* src, u32 n)
{
u8 *xdes = des;
u8 *xsrc = src;
while(n--)
{
*xdes++ = *xsrc++;
}
}
/*********************************************************************************************************
* ��������: MallocMemory
* ��������: �ڴ����
* �������: memx�������ڴ�飻size��Ҫ������ڴ��С���ֽڣ�
* �������: void
* �� �� ֵ: void
* ��������: 2018��01��01��
* ע ��:
*********************************************************************************************************/
static u32 MallocMemory(u8 memx, u32 size)
{
signed long offset = 0;
u32 nmemb; //��Ҫ���ڴ����
u32 cmemb = 0; //�������ڴ����
u32 i;
if(!s_structMallocDev.memoryRdy[memx])
{
s_structMallocDev.init(memx);//δ��ʼ��,��ִ�г�ʼ��
}
if(size == 0)
{
return 0XFFFFFFFF;//����Ҫ����
}
nmemb = size / c_iMemoryBlkSize[memx]; //��ȡ��Ҫ����������ڴ����
if(size % c_iMemoryBlkSize[memx])
{
nmemb++;
}
for(offset = c_iMemoryTblSize[memx] - 1; offset >= 0; offset--)//���������ڴ������
{
if(!s_structMallocDev.memoryMap[memx][offset])
{
cmemb++;//�������ڴ��������
}
else
{
cmemb = 0; //�����ڴ������
}
if(cmemb == nmemb) //�ҵ�������nmemb�����ڴ��
{
for(i = 0; i < nmemb; i++) //��ע�ڴ��ǿ�
{
s_structMallocDev.memoryMap[memx][offset + i] = nmemb;
}
return (offset * c_iMemoryBlkSize[memx]);//����ƫ�Ƶ�ַ
}
}
return 0XFFFFFFFF;//δ�ҵ����Ϸ����������ڴ��
}
/*********************************************************************************************************
* ��������: FreeMemory
* ��������: �ڴ��ͷ�
* �������: memx�������ڴ�飻size���ڴ��ַƫ��
* �������: void
* �� �� ֵ: ����ֵ��0���ͷųɹ���1���ͷ�ʧ�ܣ�2��ƫ�Ƴ���
* ��������: 2018��01��01��
* ע ��:
*********************************************************************************************************/
static u8 FreeMemory(u8 memx, u32 offset)
{
int i;
int index;
int nmemb;
if(!s_structMallocDev.memoryRdy[memx])//δ��ʼ��,��ִ�г�ʼ��
{
s_structMallocDev.init(memx);
return 1;//δ��ʼ��
}
if(offset < c_iMemorySize[memx])//ƫ�����ڴ����
{
index = offset / c_iMemoryBlkSize[memx]; //ƫ�������ڴ�����
nmemb = s_structMallocDev.memoryMap[memx][index]; //�ڴ������
for(i = 0; i < nmemb; i++) //�ڴ������
{
s_structMallocDev.memoryMap[memx][index+i] = 0;
}
return 0;
}
else
{
return 2;//ƫ�Ƴ�����
}
}
/*********************************************************************************************************
* API����ʵ��
*********************************************************************************************************/
/*********************************************************************************************************
* ��������: InitMemory
* ��������: ��ʼ��Memoryģ��
* �������: void
* �������: void
* �� �� ֵ: void
* ��������: 2018��01��01��
* ע ��:
*********************************************************************************************************/
void InitMemory(u8 memx)
{
//ʹ��TCMSRAMʱ��
if(SRAMTCM == memx)
{
rcu_periph_clock_enable(RCU_TCMSRAM);
}
//�ڴ�״̬����������
SetMemory(s_structMallocDev.memoryMap[memx], 0, c_iMemoryTblSize[memx] * 4);
//�ڴ������ʼ��OK
s_structMallocDev.memoryRdy[memx] = 1; //�ڴ������ʼ��OK
}
/*********************************************************************************************************
* ��������: MemoryPerused
* ��������: ����ڴ�ʹ����
* �������: memx�������ڴ��
* �������: void
* �� �� ֵ: ʹ���ʣ�������10����0~1000������0.0%~100.0%��
* ��������: 2018��01��01��
* ע ��:
*********************************************************************************************************/
u16 MemoryPerused(u8 memx)
{
u32 used = 0;
u32 i;
for(i = 0; i < c_iMemoryTblSize[memx]; i++)
{
if(s_structMallocDev.memoryMap[memx][i])
{
used++;
}
}
return (used * 1000) / (c_iMemoryTblSize[memx]);
}
/*********************************************************************************************************
* ��������: MyFree
* ��������: �ڴ��ͷ�
* �������: memx�������ڴ�飻ptr���ڴ��׵�ַ
* �������: void
* �� �� ֵ: void
* ��������: 2018��01��01��
* ע ��:
*********************************************************************************************************/
void MyFree(u8 memx, void* ptr)
{
u32 offset;
if(ptr == NULL)
{
return;//��ַΪ0
}
offset = (u32)ptr - (u32)s_structMallocDev.memoryBase[memx];
FreeMemory(memx, offset); //�ͷ��ڴ�
}
/*********************************************************************************************************
* ��������: MyMalloc
* ��������: �ڴ����
* �������: memx�������ڴ�飻size���ڴ��С���ֽڣ�
* �������: void
* �� �� ֵ: ����ֵ�����䵽���ڴ��׵�ַ
* ��������: 2018��01��01��
* ע ��:
*********************************************************************************************************/
void* MyMalloc(u8 memx, u32 size)
{
u32 offset;
offset = MallocMemory(memx, size);
if(offset == 0XFFFFFFFF)
{
return NULL;
}
else
{
return (void*)((u32)s_structMallocDev.memoryBase[memx] + offset);
}
}
/*********************************************************************************************************
* ��������: MyRealloc
* ��������: ���·����ڴ�
* �������: memx�������ڴ�飻ptr���ڴ��׵�ַ��size���ڴ��С���ֽڣ�
* �������: void
* �� �� ֵ: ����ֵ�����䵽���ڴ��׵�ַ
* ��������: 2018��01��01��
* ע ��:
*********************************************************************************************************/
void* MyRealloc(u8 memx, void* ptr, u32 size)
{
u32 offset;
offset = MallocMemory(memx, size);
if(offset == 0XFFFFFFFF)
{
return NULL;
}
else
{
//�������ڴ����ݵ����ڴ�
CopyMemory((void*)((u32)s_structMallocDev.memoryBase[memx] + offset), ptr, size);
//�ͷž��ڴ�
MyFree(memx, ptr);
//�������ڴ��׵�ַ
return (void*)((u32)s_structMallocDev.memoryBase[memx] + offset);
}
}

Xet Storage Details

Size:
11.4 kB
·
Xet hash:
1d1c99c69f60c0b90e47645a00f68f01b524ee902f420bfecf6f4221e90966ed

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.