在ARM嵌入式系统实现malloc内存分配机制。
在ARM cpu上验证通过。
ps:也可在pc ubuntu linux进行验证
#include "include/lib_replace.h"
#include "usb.h"
//#define TEST_MALLOC y
#ifdef TEST_MALLOC
#include
#define PRINT printf
#else
#define PRINT print
#endif
#define MEM_SIZE 0x1500 //5k
char __attribute((aligned (16))) mem[MEM_SIZE]; //16 byte aligned
#define MEM_START &mem[0]
#define MEM_END &mem[MEM_SIZE]
// #define MEM_START 0x400012fb0
// #define MEM_END 0x400012fb0 + MEM_SIZE
// #define mem ((void *) MEM_START)
#define UNUSED 0
#define USED 1
#define PACKED_SIZE 16
#pragma pack(8)
typedef struct mem_block{ /*定义内存管理块的数据结构*/
void *mem_ptr; /*当前内存块的内存地址*/
struct mem_block *nxt_ptr; /*下一个内存管理块的地址*/
u64 mem_size; /*当前内存块的大小*/
u64 mem_sta; /*当前内存块的状态*/
}mem_block;
#pragma pack()
#define BLK_SIZE sizeof(mem_block) /*内存管理块的大小*/
#define HEAD_NODE (MEM_END - BLK_SIZE)/*头内存管理块的地址*/
static signed char mem_init_flag = -1; /*内存分配系统初始化的标志(-1 未初始化),(1 已初始化)*/
static int total_size = 0;
int get_total_size(void)
{
return total_size;
}
void *memset_pro(void *addr, char c, unsigned int size)
{
int i;
unsigned long long *p = (unsigned long long *)addr;
unsigned int cnt = size / sizeof(unsigned long long);
//unsigned int cnt2 = size % sizeof(unsigned long long);
if(p == NULL)
return NULL;
for(i = 0; i < cnt; i ++)
{
*(p + i) = c;
}
return addr;
}
void mem_init_ex(void)
{
PRINT("mem_init_ex: MEM_START:%p, MEM_END:%p, HEAD_NODE:%p, BLK_SIZE:%d\r\n", mem, (void *)MEM_END, (void *)HEAD_NODE, BLK_SIZE);
mem_block *node;
memset_pro(mem, 0, sizeof(mem));
node = (mem_block *)HEAD_NODE;
node->mem_ptr = MEM_START;
node->nxt_ptr = (mem_block *)HEAD_NODE;
node->mem_size = MEM_SIZE - BLK_SIZE;
node->mem_sta = UNUSED;
mem_init_flag = 1;
}
void *malloc_ex(unsigned int nbytes)
{
u64 suit_size = 0xFFFFFFFFFFFFUL;
mem_block *head_node=NULL, *tmp_node=NULL, *suit_node=NULL;
if(nbytes % PACKED_SIZE)
nbytes = (nbytes/PACKED_SIZE + 1) * PACKED_SIZE;
//PRINT("malloc_ex size:%d\r\n", nbytes);
if(nbytes == 0)
{
return NULL;
}
if(mem_init_flag < 0)
{
mem_init_ex();
}
head_node = tmp_node = (mem_block *)HEAD_NODE;
while(1)
{
if(tmp_node->mem_sta == UNUSED)
{
if(nbytes <= tmp_node->mem_size && tmp_node->mem_size < suit_size)
{
suit_node = tmp_node;
suit_size = suit_node->mem_size;
}
}
tmp_node = tmp_node->nxt_ptr;
if(tmp_node == head_node)
{
if(suit_node == NULL)
{
PRINT("NULL\r\n");
return NULL;
}
break;
}
}
if(nbytes <= suit_node->mem_size && (nbytes + BLK_SIZE) >= suit_node->mem_size)
{
suit_node->mem_sta = USED;
total_size += nbytes;
PRINT("malloc_ex: addr:%p, size:%d\r\n", suit_node->mem_ptr, nbytes);
return suit_node->mem_ptr;
}
else if(suit_node->mem_size > (nbytes + BLK_SIZE))
{
tmp_node = suit_node->mem_ptr;
tmp_node = (mem_block *)((unsigned char *)tmp_node + nbytes);
tmp_node->mem_ptr = suit_node->mem_ptr;
tmp_node->nxt_ptr = suit_node->nxt_ptr;
tmp_node->mem_size = nbytes;
tmp_node->mem_sta = USED;
//PRINT("tmp_node->mem_ptr:%p\r\n", tmp_node->mem_ptr);
suit_node->mem_ptr = (mem_block *)((unsigned char *)tmp_node + BLK_SIZE);
//PRINT("suit_node->mem_ptr:%p\r\n", suit_node->mem_ptr);
suit_node->nxt_ptr = tmp_node;
suit_node->mem_size -= (nbytes + BLK_SIZE);
suit_node->mem_sta = UNUSED;
total_size += nbytes;
PRINT("malloc_ex: addr:%p, size:%d\r\n", tmp_node->mem_ptr, nbytes);
return tmp_node->mem_ptr;
}
else
{
PRINT("%s,size err!\r\n",__FUNCTION__);
}
return NULL;
}
void free_ex(void *ap)
{
mem_block *head_node, *tmp_node, *nxt_node;
if(ap == NULL)
return;
if(mem_init_flag < 0)
{
return;
}
head_node = tmp_node = (mem_block *)HEAD_NODE;
while(1)
{
//PRINT("head_node:%p, tmp_node->mem_ptr:%p\r\n", head_node, tmp_node->mem_ptr);
if(tmp_node->mem_ptr == ap)
{
if(tmp_node->mem_sta != UNUSED)
{
PRINT("free_ex: p=%p, size:%ld\r\n", ap, tmp_node->mem_size);
total_size = total_size - tmp_node->mem_size;
tmp_node->mem_sta = UNUSED;
break;
}
else
{
return;
}
}
tmp_node = tmp_node->nxt_ptr;
if(tmp_node == head_node)
{
PRINT("%s,can not found ap!\r\n",__FUNCTION__);
return ;
}
}
AGAIN:
head_node = tmp_node = (mem_block *)HEAD_NODE;
while(1)
{
nxt_node = tmp_node->nxt_ptr;
if(nxt_node == head_node)
{
break;
}
if(tmp_node->mem_sta == UNUSED && nxt_node->mem_sta == UNUSED)
{
tmp_node->mem_ptr = nxt_node->mem_ptr;
tmp_node->nxt_ptr = nxt_node->nxt_ptr;
tmp_node->mem_size += nxt_node->mem_size + BLK_SIZE;
tmp_node->mem_sta = UNUSED;
goto AGAIN;
}
tmp_node = nxt_node;
}
}
#ifdef TEST_MALLOC
int main(void)
{
char *p, *p1, *p3, *p4;
PRINT("start:%p\r\n", mem);
p = malloc_ex(8);
memset_ex(p, 'a', 9);
PRINT("p:%p, p=%s\r\n", p, p);
free_ex(p);
p1 = malloc_ex(10000);
memset_ex(p1, 'b', 9);
PRINT("p1:%p, p1=%s\r\n", p1, p1);
free_ex(p1);
p3 = malloc_ex(16);
memset_ex(p3, 'c', 9);
PRINT("p3:%p, p3=%s\r\n", p3, p3);
//free_ex(p3);
p4 = malloc_ex(5);
memset_ex(p4, 'd', 9);
PRINT("p4:%p, p4=%s\r\n", p4, p4);
free_ex(p4);
while(1);
}
#endif