上一篇文章还是两三年前,没想到弹指一挥间这么长时间过去了,对象也在这一弹间弹没了0.0,Android开发工作已经不再从事,现在搞嵌入式都快三年了(没毕业就在这个公司)。最近在开发中需要使用内存管理方面的代码,使用c库自带的malloc和free函数的话在嵌入式上跑总有些担心,所以自己根据网上的一些经验复写了这两个函数。希望有这方面需要的同仁不再走些弯路。
//stm32库中对一些数据类型进行的规定
#define uint16_t unsigned short
#define uint8_t unsigned char
#define uint32_t unsigned int
constexpr auto _NUM_MALLOCLEN_SYS = 256; //vs中新的常量表示方法,直接拿来用了 相当于一个预定义
struct T_MemCell_s
{
uint16_t use; //使用单元个数
uint16_t free; //本单元口空闲单元个数
T_MemCell_s* next; //下一个使用的区域
};
static T_MemCell_s inf_Malloc[_NUM_MALLOCLEN_SYS]; //声明预制缓冲区
static T_MemCell_s* ptEndCell = 0; //声明尾指针的位置,方便分配空间
//分配
void* malloc_h(uint32_t len)
{
uint16_t free_Cells = 0;
uint16_t num_Cells = 0; /