cancel
Showing results for 
Search instead for 
Did you mean: 

stm32f405 malloc issue

Angelo1
Associate II
Posted on March 31, 2014 at 01:11

Dear,

as in several examples, i implemented my _sbrk function to be able to use malloc, as (copied from examples):

extern int  _end;

/*

 * _sbrk needed for malloc

 */

caddr_t _sbrk ( int incr ) {

        static unsigned char *heap = NULL;

        unsigned char *prev_heap;

        if (heap == NULL) {

                heap = (unsigned char *)&_end;

        }

        prev_heap = heap;

        /* check removed to show basic approach */

        heap += incr;

        return (caddr_t) prev_heap;

}

Program compiles fine, no warnings, if i call directly _sbrk to allocate memory (instead of malloc) it works perfect, if i instead call ''malloc'', i see that _sbrk is called, but the return value of malloc is an null pointer (0).

Probably, malloc after calling sbrk do some other checks, and returns 0.

Every help is appreciated.

Thanks

#worst-forum-software-ever
13 REPLIES 13
Andrew Neil
Evangelist
Posted on March 31, 2014 at 09:24

This stuff is specific to the particular toolchain you're using - which you omitted to mention!

What does your toolchain documentation say?

Andrew Neil
Evangelist
Posted on March 31, 2014 at 09:24

(forum playing-up again)

chen
Associate II
Posted on March 31, 2014 at 11:03

Hi

Would you happen to be using Atollic and FreeRTOS?

When you called _sbrk() - was this before the scheduler started?

When you called malloc() - was this after the scheduler started?

Angelo1
Associate II
Posted on March 31, 2014 at 11:31

Dear

thanks,

i am using

/opt/gcc-arm-none-eabi-4_8-2013q4/bin/arm-none-eabi-gcc

compile options are currently:

OPTIMIZATION=-fdata-sections -ffunction-sections -fstrict-aliasing -O2 -mfpu=fpv4-sp-d16$

CFLAGS  =-mthumb -mcpu=cortex-m4 -Wall $(OPTIMIZATION) -c -Iinclude -I$(INCSTM) -I$(INCC$

ASFLAGS =-mthumb -mcpu=cortex-m4 -Wall $(OPTIMIZATION) -c -D__ASSEMBLY__

LDFLAGS =-mthumb -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard -nostartfiles -T stm$

My guess is that ''malloc'' is doing some additional checks after calling _sbrk (maybe taking references from the linker file, i can attach it eventually), and returning 0.

_sbrk is called from malloc since if i put a while(1) inside the program block.

If i call the _sbrk function, it works perfect and returns a valid pointer.

Many thanks

Angelo1
Associate II
Posted on March 31, 2014 at 11:32

Dear

thanks,

i am using

/opt/gcc-arm-none-eabi-4_8-2013q4/bin/arm-none-eabi-gcc

compile options are currently:

OPTIMIZATION=-fdata-sections -ffunction-sections -fstrict-aliasing -O2 -mfpu=fpv4-sp-d16$

CFLAGS  =-mthumb -mcpu=cortex-m4 -Wall $(OPTIMIZATION) -c -Iinclude -I$(INCSTM) -I$(INCC$

ASFLAGS =-mthumb -mcpu=cortex-m4 -Wall $(OPTIMIZATION) -c -D__ASSEMBLY__

LDFLAGS =-mthumb -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard -nostartfiles -T stm$

My guess is that ''malloc'' is doing some additional checks after calling _sbrk (maybe taking references from the linker file, i can attach it eventually), and returning 0.

_sbrk is called from malloc since if i put a while(1) inside the function the program blocks.

If i call the _sbrk function, it works perfect and returns a valid pointer.

-- sory for double posting, i get an error from the web at first ''OK'' and i tried again --

Many thanks

chen
Associate II
Posted on March 31, 2014 at 11:50

Hi

Just examined the code for _sbrk() in detail.

Curious - Where does _end get assigned to?

Does the heap grow upwards or downwards?

What happens when it reaches end of memory?

chen
Associate II
Posted on March 31, 2014 at 13:18

Hi

''My guess is that ''malloc'' is doing some additional checks after calling _sbrk (maybe taking references from the linker file, i can attach it eventually), and returning 0.''

Yes. Depends on the particular implementation of  malloc()

When I last looked at it - I found the linux implementation.

It creates pools of used memory - linking them into linked lists, so that they can be got to by the free.

The _sbrk() will be asked to get a larger size than was asked for in malloc().

The larger size should be a multiple of power of 2 (ie 16, 32, 64 etc)

The larger size is needed so that a linked list header can be applied and wraps the memory that was asked for.

Must be failing somewhere in this routine.

Angelo1
Associate II
Posted on March 31, 2014 at 13:21

Hi chen,

thanks,

i just copied that _sbrk function i found in several places.

that ''end'' is the end of the ebss and the start (in memory as lower address) of the space reserved of the heap. Yes, that ''end'' is a bit misleading wordings, but i kept them from linker files and _sbrk examples. So, once the pointer is returned , memory address (kn heap) is increased of the object amount / memory size.

It works great if i call _sbrk directly. There is something with gcc, malloc, and final checks of malloc, that makes returning a ''0'' pointer.

chen
Associate II
Posted on March 31, 2014 at 13:55

Hi

malloc() from gcc will assume you are working in some kind of OS.

I assumed that you were using Atollic and FreeRTOS BUT I see you are using raw gcc.

I think you may be missing some OS functions which malloc() is calling.

Atollic provides a file called syscalls.c which provided enough 'OS calls' for gcc to work.

I think you will have to figure out what else is needed.