2014-03-30 04:11 PM
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-ever2014-03-31 12:24 AM
This stuff is specific to the particular toolchain you're using - which you omitted to mention!
What does your toolchain documentation say?2014-03-31 12:24 AM
(forum playing-up again)
2014-03-31 02:03 AM
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?2014-03-31 02:31 AM
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 thanks2014-03-31 02:32 AM
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 thanks2014-03-31 02:50 AM
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?2014-03-31 04:18 AM
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.2014-03-31 04:21 AM
2014-03-31 04:55 AM
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.