关于freescale的k60芯片使用malloc的问题
时间:10-02
整理:3721RD
点击:
问题描述: 使用拉普兰德的k60固件库的malloc函数申请堆存,我想要一个8字节的空间(对齐以后的struct),可是每次malloc都给我分配了两个block(一个block 16字节),以至于使用链表的时候总是堆存不够,如何解决?代码如下
- /*
- * This struct forms the minimum block size which is allocated, and
- * also forms the linked list for the memory space used with alloc()
- * and free(). It is padded so that on a 32-bit machine, all malloc'ed
- * pointers are 16-byte aligned.
- */
- typedef struct ALLOC_HDR
- {
- struct
- {
- struct ALLOC_HDR *ptr;
- unsigned int size;
- } s;
- unsigned int align;
- unsigned int pad;
- } ALLOC_HDR;
- static ALLOC_HDR base;
- static ALLOC_HDR *freep = NULL;
- void * malloc (unsigned nbytes)
- {
- /* Get addresses for the HEAP start and end */
- #if defined(__IAR_SYSTEMS_ICC__)
- char* __HEAP_START = __section_begin("HEAP");
- char* __HEAP_END = __section_end("HEAP");
- #else
- #warning 非IAR编译器需确定HEAP起始结束地址
- extern char __HEAP_START;
- extern char __HEAP_END[];
- #endif
-
- ALLOC_HDR *p, *prevp;
- unsigned nunits;
- nunits = ((nbytes+sizeof(ALLOC_HDR)-1) / sizeof(ALLOC_HDR)) + 1;
-
- if ((prevp = freep) == NULL)
- {
- p = (ALLOC_HDR *)__HEAP_START;
- p->s.size = ( ((uint32)__HEAP_END - (uint32)__HEAP_START)
- / sizeof(ALLOC_HDR) );
- p->s.ptr = &base;
- base.s.ptr = p;
- base.s.size = 0;
- prevp = freep = &base;
- }
- for (p = prevp->s.ptr; ; prevp = p, p = p->s.ptr)
- {
- if (p->s.size >= nunits)
- {
- if (p->s.size == nunits)
- {
- prevp->s.ptr = p->s.ptr;
- }
- else
- {
- p->s.size -= nunits;
- p += p->s.size;
- p->s.size = nunits;
- }
- freep = prevp;
- OLED_ShowNum(0, 5, p -> s.size, 18);
- return (void *)(p + 1);
- }
- if (p == freep)
- return NULL;
- }
- }
已退回3积分