Subj : Re: where can I find a malloc for standalone programs ? To : comp.programming From : Rob Thorpe Date : Fri Aug 19 2005 11:55 am DEMAINE Benoit-Pierre wrote: > I am writing a program for standalone use; the circuit in configured with some > bootstrap, then my code is called. > > There is no Linux, nor filesyem, nor libraries around. > > my code is statically compiled and linked with gcc. > > problem occurs about malloc: since there is no kernel, nor system, nor stdlib, there > is no malloc defined. I can not either link my application against a stdlib stolen > to any system, since I have no kernel, and that stdlib would not know the addresses > of memory pages available. > > Thus, I need a new malloc that is initialised by a special initialisation call, with > begin and end adresses of memory space, and implements malloc() and free(), and is > aware of endianness, bus width, alignment problems ... > > It would be a plus if some internal function could return the amount of alocated > zones, so that I can track memory consumption, and expect/track leakages. > > The memory pages available are not all memory locations wired on the board. the > bootstrap requires about 50% for internal use, and interrupt management. I have the > upper 50% for me. > > The CPU stack is stored somewhere else, not sure exactly where in fact :S > > Problem is that my code requires some bits of dynamically allocated variables and > arrays, so that I really need malloc; I hope I spend less time implementing malloc > than trying to port the code to use only fixed allocations. > > Inside my zone, I need to allocate for personnal use some pages (about 200kB, to > store constants, and exchange values with interrupts). These pages shall NOT be > managed by malloc; they are managed manually when I write my source. > > I am using at the moment GCC-bare-metal from > http://www.codesourcery.com/gnu_toolchains/arm/, with Gentoo-x86 as host, and a > Cogent CSB336 MC9328MXL as target: ARM920T with RAM, FLASH, and various devices. > > I can only use GPL and public programs, softwares, and libraries. > > Thanks for any help, URL, books, sneekers, check, tip ... :p There are several small libraries made for this task. For example, search for "diet-libc", it provides a whole C library. You could also get a version of Malloc from somewhere and use that. There is an example implementation of malloc in K&R's "The C programming language", I think, though its not very good. Otherwise search the web for: * Doug Lea Malloc * BSD Malloc (this is in the perl source somewhere) * GNU Malloc (somewhere on their ftp server) Doug Lea's malloc and GNU malloc are probably overkill for something small. BSD malloc may be OK, since it's fairly easy to understand. You will have to write the function "sbrk" that malloc relies on yourself for your environment. .