在 arm7 问题中使用 gcc 实现 malloc : malloc return NULL [英] Implementing malloc with gcc in an arm7 problems : malloc return NULL

查看:38
本文介绍了在 arm7 问题中使用 gcc 实现 malloc : malloc return NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的固件中添加 malloc 支持,但我想我遗漏了一些东西!

I am adding malloc support in my firmware and I think I'm missing something!

我为 arm7tdmi 处理器使用代码源 g++ lite 库,我的代码基于此链接中的示例:http://e2e.ti.com/support/microcontrollers/stellaris_arm_cortex-m3_microcontroller/f/473/t/44452.aspx#539503

I use code sourcery g++ lite library for an arm7tdmi processor and my code is based on the example found in this link : http://e2e.ti.com/support/microcontrollers/stellaris_arm_cortex-m3_microcontroller/f/473/t/44452.aspx#539503

我添加了我的 _sbrk 版本:

I added my version of _sbrk :

char * _sbrk(int incr)
{
    //extern char _end; /* Defined by the linker */
    static char *heap_end;
    char *prev_heap_end;
    register char* stackPtr;

    if (heap_end == 0)
    {
        // first allocation
        heap_end =HEAP_END;
    }

    prev_heap_end = heap_end;

    // get current stack pointer
    asm ("mov %0, sp\n\t" : "=r" (stackPtr) );



    if (heap_end + incr > stackPtr) {
        return NULL;// error - no more memory
        //write (1, "Heap and stack collision\n", 25);
        //abort ();
    }
    heap_end += incr;
    return (char*) prev_heap_end;
}

sbrk 使用的一些定义:

Some defines used by sbrk :

#define SDRAM_SIZE 16*1024*1024        
#define HEAP_BASE  _ebss
#define HEAP_END ((_stext + SDRAM_SIZE) -1)
#define HEAP_SIZE HEAP_END - HEAP_BASE

(_ebss 和 _stext 来自我的链接器文件)

(_ebss and _stext come from my linker file)

这是我的主要内容,我做了一个简单的 malloc/free 调用:

Here's my main where I did a simple malloc/free call :

void C_main ( void)
{
  char * testmalloc=0;
  /* Initialize "Heap Descriptor" pointer */
  pHeapDescriptor =  __rt_embeddedalloc_init ((void*)HEAP_BASE,HEAP_SIZE);
  testmalloc = malloc(2048);
  free(testmalloc);
}

我以步进模式运行这个程序.当我调用 malloc 时,它最终调用了我的 _sbrk 实现,返回值(prev_heap_end)有一个预期值,但是当程序返回 main 时,testmalloc 值为 NULL(在 gcc 库的某个地方,prev_heap_end 丢失了).

I run this program in step mode. When I call malloc, it eventually call my _sbrk implementation, the return value (prev_heap_end) have an expected value, but when the program return to the main, the testmalloc value is NULL (somewhere in the gcc library, the prev_heap_end is lost).

有人知道我做错了什么吗?

Does someone have an idea of what I'm doing wrong?

不知道这是否有帮助,但这是我的 gcc 编译参数:

Don't know if that will help, but this is my gcc compilation parameter :

arm-none-eabi-gcc  -march=armv4t -mcpu=arm7tdmi -dp -c 
-Wa,-adhlns="../../Base/Lib/Pa/main.o.lst" -fmessage-length=0 
-fno-zero-initialized-in-bss -MMD -MP -MF"../../Base/Lib/Pa/main.d" 
-MT"../../Base/Lib/Pa/main.d" -fpic -mlittle-endian -Wall -g3 -gdwarf-2  
../../Base/Hardintrf/Mezzanine/main.c -o"../../Base/Lib/Pa/main.o"

在此先感谢您的帮助!

推荐答案

if (heap_end == 0)
{
    // first allocation
    heap_end = HEAP_END;
}

这应该是:

if (heap_end == 0)
{
    // first allocation
    heap_end = HEAP_BASE;
}

所以你不要在堆的末尾开始你的堆...你可能想为那个变量想一个更好的名字,然后是 heap_end 以避免这种混淆.

So you don't start your heap at the end of your heap... You might want to think up a better name for that variable then heap_end to avoid this confusion in the first place.

此外,您不需要使用寄存器修饰符来使内联汇编正常工作.编译器足够聪明,可以为您做到这一点.

Also you don't need to use the register modifier to make your inline assembly work correctly. The compiler is smart enough to do that for you.

这篇关于在 arm7 问题中使用 gcc 实现 malloc : malloc return NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆