- extern "C" void *malloc(unsigned nbyte)
- {
- static int* begin = (int *)Image$$RW$$Limit; //堆首地址
- static int* end = (int *)(HEAPEND); //堆大小
- static int* top = begin; //堆顶
- int* cur = begin;
- int size = ((nbyte%4 == 0) ? nbyte/4 : nbyte/4+1); //尺寸对齐
- while(cur < top) //查找空闲块
- {
- if(*cur == 0 && *(cur+1) == size) //如果找到同样尺寸的空闲块
- {
- *cur = 1; //置占用位
- return (cur+2); //返回地址
- }
- else
- {
- cur += 2 + *(cur+1); //遍历至下一个有效位
- }
- }
- *top++ = 1; //分配新块
- *top++ = size;
- cur = top;
- top += size;
- if(top >= end)
- {
- top -= size+2;
- return NULL;
- }
- else
- {
- return cur;
- }
- }
- //-------------------------------------------------------------------
- extern "C" void free(void *pt)
- {
- int *cur = (int *)pt;
- *(cur-2) = 0;
- }
最简单的malloc free函数实现(用于arms3c2440)
最新推荐文章于 2024-02-27 20:45:28 发布