You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
3.0 KiB
89 lines
3.0 KiB
2 years ago
|
#ifndef __MALLOC_H
|
||
|
#define __MALLOC_H
|
||
|
|
||
|
#include "stm32f4xx.h"
|
||
|
|
||
|
#ifndef NULL
|
||
|
#define NULL 0
|
||
|
#endif
|
||
|
|
||
|
#ifndef __MEMORY_AT
|
||
|
#define __MEMORY_AT(x) __attribute__((section(".#x")))
|
||
|
#endif
|
||
|
|
||
|
|
||
|
//定义两个内存池
|
||
|
#define SRAMIN 0 //内部内存池
|
||
|
#define SRAMEX 1 //外部内存池
|
||
|
// #define SRAMDISP 2 //外部内存池
|
||
|
|
||
|
#define SRAMBANK 2 //定义支持的SRAM块数.
|
||
|
|
||
|
//mem1内存参数设定.mem1完全处于内部SRAM里面.
|
||
|
#define MEM1_BLOCK_SIZE 32 //内存块大小为32字节
|
||
|
#define MEM1_MAX_SIZE 20*1024 //最大管理内存 40K
|
||
|
#define MEM1_ALLOC_TABLE_SIZE MEM1_MAX_SIZE/MEM1_BLOCK_SIZE //内存表大小
|
||
|
|
||
|
|
||
|
//mem2内存参数设定.mem2的内存池处于外部SRAM里面
|
||
|
#define MEM2_BLOCK_SIZE 32 //内存块大小为32字节
|
||
|
#define MEM2_MAX_SIZE 15*1024*1024 // 16M malloc不能分配
|
||
|
#define MEM2_ALLOC_TABLE_SIZE MEM2_MAX_SIZE/MEM2_BLOCK_SIZE //内存表大小
|
||
|
|
||
|
//mem3内存参数设定.mem2的内存池处于外部SRAM里面
|
||
|
// #define MEM3_BLOCK_SIZE 32 //内存块大小为32字节
|
||
|
// #define MEM3_MAX_SIZE 1*1024*1024 // 16M malloc不能分配
|
||
|
// #define MEM3_ALLOC_TABLE_SIZE MEM3_MAX_SIZE/MEM3_BLOCK_SIZE //内存表大小
|
||
|
|
||
|
//内存管理控制器
|
||
|
struct _m_mallco_dev
|
||
|
{
|
||
|
void ( * init ) ( uint8_t ); //初始化
|
||
|
uint8_t ( * perused ) ( uint8_t ); //内存使用率
|
||
|
uint8_t * membase [ SRAMBANK ]; //内存池 管理SRAMBANK个区域的内存
|
||
|
uint16_t * memmap [ SRAMBANK ]; //内存管理状态表
|
||
|
uint8_t memrdy [ SRAMBANK ]; //内存管理是否就绪
|
||
|
};
|
||
|
extern struct _m_mallco_dev mallco_dev; //在mallco.c里面定义
|
||
|
|
||
|
|
||
|
void mymemset(void *s,uint8_t c,uint32_t count); //设置内存
|
||
|
void mymemcpy(void *des,void *src,uint32_t n); //复制内存
|
||
|
void my_mem_init(uint8_t memx); //内存管理初始化函数(外/内部调用)
|
||
|
uint32_t my_mem_malloc(uint8_t memx,uint32_t size); //内存分配(内部调用)
|
||
|
uint8_t my_mem_free(uint8_t memx,uint32_t offset); //内存释放(内部调用)
|
||
|
uint8_t my_mem_perused(uint8_t memx); //获得内存使用率(外/内部调用)
|
||
|
|
||
|
|
||
|
//用户调用函数
|
||
|
void myfree(uint8_t memx,void *ptr); //内存释放(外部调用)
|
||
|
void *mymalloc(uint8_t memx,uint32_t size); //内存分配(外部调用)
|
||
|
void *myrealloc(uint8_t memx,void *ptr,uint32_t size); //重新分配内存(外部调用)
|
||
|
|
||
|
#endif
|
||
|
|
||
|
|
||
|
/*
|
||
|
// // 初始化外部内存池
|
||
|
mallco_dev.init( SRAMEX );
|
||
|
uint8_t *malloc_ex_buf;
|
||
|
uint8_t *malloc_in_buf;
|
||
|
malloc_ex_buf=mymalloc(SRAMEX,512); //申请一个扇区的缓存
|
||
|
malloc_in_buf=mymalloc(SRAMIN,512); //申请一个扇区的缓存
|
||
|
if (malloc_ex_buf != NULL)
|
||
|
log_i("Malloc SRAMEX, Test OK ...");
|
||
|
if (malloc_in_buf != NULL)
|
||
|
log_i("Malloc SRAMIN, Test OK ...");
|
||
|
myfree(SRAMEX, malloc_ex_buf );
|
||
|
myfree(SRAMIN, malloc_in_buf );
|
||
|
log_i("Free SRAMEX SRAMIN, Test OK ...");
|
||
|
*/
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|