裸机malloc实现
来源:互联网 发布:java 7 64位 解压包 编辑:程序博客网 时间:2024/06/05 01:10
曾在arm裸机程序或者RTOS移植时,用到malloc,参考某位网友修改。
在6410跑过,所以设置6410的一块固定地址为堆,提供给malloc。
#define UNUSED 0#define USED 1#define HEAP_NOTFIND 0#define HEAP_FOUND 1static unsigned long HEAP_START = 0x54000000;static unsigned long HEAP_END = 0x58000000;typedef struct heap{int size;int start;int end;int used;struct heap *next;struct heap *prv;}heap;static heap *heap_head;static heap *heap_crrunt;static heap *heap_mark;static int heapnum=0;//初始化堆void init_heap(){heap_head = (heap *)HEAP_START;heap_head->size = (HEAP_END-HEAP_START)-sizeof(heap);heap_head->start = HEAP_START+sizeof(heap);heap_head->end = HEAP_END;heap_head->used = UNUSED;heap_head->next = NULL;heap_head->prv = NULL;heap_crrunt = heap_head;heapnum=0;printf("HEAP_START=%08x\n",HEAP_START);printf("HEAP_END=%08x\n",HEAP_END);printf("heap_head=0x%08x\n",heap_crrunt);printf("heap_head->start=%08x\n",heap_head->start);printf("heap_head->size=0x%08x\n",heap_head->size);}//分配内存函数void * xmalloc(unsigned int size){heap *newblock;heap *prtblock;int stat=HEAP_NOTFIND;int num = 0;size = (size+7)&~7;//printf("xmalloc:%d\n",heapnum);heap_mark=heap_crrunt;//printf("heap_crrunt:0x%08x\n",heap_crrunt);while(heap_crrunt->next != heap_mark) { if(heap_crrunt == NULL) heap_crrunt = heap_head; if(heap_crrunt->used == UNUSED) { if(heap_crrunt->size > size) { newblock = (heap*)((unsigned char *)heap_crrunt+(size+sizeof(heap))); newblock->size = heap_crrunt->size-(size+sizeof(heap)); newblock->start = heap_crrunt->start+(size+sizeof(heap)); newblock->end = heap_crrunt->end; newblock->used = UNUSED; newblock->next = heap_crrunt->next; newblock->prv = heap_crrunt; heap_crrunt->used = USED; heap_crrunt->size = size; heap_crrunt->end = heap_crrunt->start+size; heap_crrunt->next = newblock; stat = HEAP_FOUND;heapnum++; printf("heapnum:%d,heap:0x%08x,heapsize:%d\n",heapnum,heap_crrunt->start,size); break; } } num++; delay(1);if(num > 1000){printf("alloc error\n");return NULL;} heap_crrunt = heap_crrunt->next; } if(stat == HEAP_NOTFIND) { printf("malloc fail\n"); return NULL; } prtblock = heap_crrunt; heap_crrunt = newblock;//printf("newblock=0x%08x\n",newblock);//printf("newblock start=0x%08x\n",newblock->start); return ((heap *)((u32)prtblock + sizeof(heap))); } //释放void xfree(void *block){heap * ptrblock;heap * prvblock;heap * nextblock; if(block==NULL) return ; ptrblock = (heap *)((unsigned char *)block-sizeof(heap)); ptrblock->used = UNUSED; prvblock = ptrblock->prv; nextblock = ptrblock->next; printf("xfree\n"); if(prvblock != NULL) { if(prvblock->used==UNUSED) { prvblock->size = prvblock->size+ptrblock->size+sizeof(heap); prvblock->end = ptrblock->end; prvblock->next = ptrblock->next; ptrblock = prvblock;heapnum--; } } if(nextblock != NULL) { if(nextblock->used == UNUSED) { ptrblock->size = ptrblock->size+nextblock->size+sizeof(heap); ptrblock->end = nextblock->end; ptrblock->next = ptrblock->next;heapnum--; } } heap_crrunt = ptrblock;}
1 0
- 裸机malloc实现
- malloc 实现
- malloc实现
- malloc实现
- OV9650 裸机实现
- 裸机LCD汉字显示实现
- 裸机S3C6410显示控制器(4)- 裸机程序实现显示图片
- 使用多链表实现malloc
- 用哈希表加链表实现动态malloc
- malloc的实现模拟
- malloc实现原理
- malloc的实现机制
- malloc()实现原理1
- malloc() 函数实现
- malloc实现源码
- malloc函数的实现
- malloc的实现
- malloc 的实现
- 父母不同意结婚肯定有他们的理由,我现在也总算是明白了,一切也是他们看透了!
- hdu 4780 Candy Factory(费用流)
- 【Linux学习】Linux的文件权限(一)
- BZOJ2440(完全平方数)二分+莫比乌斯容斥
- cygwin 字符显示
- 裸机malloc实现
- PostGIS整合Hibernate4.0+Spring(Maven,Kepler)
- 弹出不可移动遮罩层
- 夏天小女孩子光着屁股很雅观吗?
- C99的指定初始化转化为C89?用(a|e)?bnf实现吧,写个玩玩
- 《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅰ
- nodejs命令用法
- iframe自适应浏览器的高度
- 模块管理常规功能自定义系统的设计与实现(53--演示程序和视频讲解 )