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 04:58 AM
Atollic provided these :
/* Functions */ void initialise_monitor_handles() int _getpid( void ) int _kill( int pid, int sig ) void _exit( int status ) int _write( int file, char *ptr, int len ) caddr_t _sbrk( int incr ) int _close( int file ) int _fstat( int file, struct stat *st ) int _isatty( int file ) int _lseek( int file, int ptr, int dir ) int _read( int file, char *ptr, int len ) int _open( char *path, int flags, ... ) int _wait( int *status ) int _unlink( char *name ) int _times( struct tms *buf ) int _stat( char *file, struct stat *st ) int _link( char *old, char *new ) int _fork( void ) int _execve( char *name, char **argv, char **env )2014-03-31 04:59 AM
Angelo,
I implemented the most trivial _sbrk as possible, and malloc magically worked out-of-the-box without headaches. However it came to my mind that you may miss one of these: - calling __libc_init_array as early as possible - make sure to zero bss area from __bss_start__ to __bss_end__. Hope, it can help.2014-03-31 06:13 AM
Dear Gonzalez and all,
first a big thank for the kind support from all. This is not trivial, seems on many forums newby gets often brutal answers. I realized from your answers above that since i choosed to avoid an OS (-nostartfiles, etc) maybe it has not sense now try to enabling malloc. Actually it simply cannot work. I have -nostartfiles, and i also have -Wl,--gc-sections enabled, and a quite minimal startup.s so to modify all for malloc to work i probably have to go through several things. My idea is to go for static allocation that seems to be enough for my app. Best Regards, Angelo2014-03-31 06:57 AM
Hi
''I realized from your answers above that since i choosed to avoid an OS (-nostartfiles, etc) maybe it has not sense now try to enabling malloc. Actually it simply cannot work.'' The main problem is that you are trying to get malloc() working without an OS. The implementation of malloc() from gnu assumes that there will be some kind of infrastructure from a OS. You could try a version of malloc() from an embedded system. Have a look a the malloc() implementations from FreeRTOS (there are 4, one of them is simply calling the gnu one).