Heap_Size EQU 0x00000800
AREA HEAP, NOINIT, READWRITE, ALIGN=3
__heap_base
Heap_Mem SPACE Heap_Size
__heap_limit
unsigned char Heap_Mem[Heap_Size] = {0};
__user_initial_stackheap
LDR R0, = Heap_Mem ; 返回系统中堆内存起始地址
LDR R1, =(Stack_Mem + Stack_Size)
LDR R2, = (Heap_Mem + Heap_Size); 返回系统中堆内存的结束地址
LDR R3, = Stack_Mem
BX LR
unsigned char mem_buffer[512];
unsigned char *mem_offset = & mem_buffer;
void *malloc(int size)
{
unsigned char *tmp = mem_offset;
mem_offset += size;
return (void *)tmp;
}
void free(void *mem)
{
mem_offset = mem;
}
//内存池的描述,使用联合体,体现穷人的智慧。就如,我一同学说的:一个字节,恨不得掰成8个字节来用。
typedef union mem {
struct list_head list;
unsigned char buffer[MEM_BUFFER_SIZE];
}mem_t;
static union mem gmem[MEM_BUFFER_LEN];
LIST_HEAD(mem_pool);
//分配内存
void *mem_pop()
{
union mem *ret = NULL;
psr_t psr;
psr = ENTER_CRITICAL();
if(!list_empty(&mem_pool)) { //有可用的内存池
ret = list_first_entry(&mem_pool, union mem, list);
//printf("mem_pool = 0x%p ret = 0x%p\n", &mem_pool, &ret->list);
list_del(&ret->list);
}
EXIT_CRITICAL(psr);
return ret;//->buffer;
}
//回收内存
void mem_push(void *mem)
{
union mem *tmp = NULL;
psr_t psr;
tmp = (void *)mem;//container_of(mem, struct mem, buffer);
psr = ENTER_CRITICAL();
list_add(&tmp->list, &mem_pool);
//printf("free = 0x%p\n", &tmp->list);
EXIT_CRITICAL(psr);
}
//初始化内存池
void mem_pool_init()
{
int i;
psr_t psr;
psr = ENTER_CRITICAL();
for(i=0; i<MEM_BUFFER_LEN; i++) {
list_add(&(gmem[i].list), &mem_pool);
//printf("add mem 0x%p\n", &(gmem[i].list));
}
EXIT_CRITICAL(psr);
}
1.外媒谈英伟达-Arm并购案~
2.这里聚焦了全球嵌入式技术风景~
3.航天器、导弹喜欢用单片机?
4.工程师的硬核单片机编程思想~
5.嵌入式开发需要架构设计吗?
6.【技术转管理必备】嵌入式产品的研发流程
免责声明:本文系网络转载,版权归原作者所有。如涉及作品版权问题,请与我们联系,我们将根据您提供的版权证明材料确认版权并支付稿酬或者删除内容。