Subj : Re: where can I find a malloc for standalone programs ? To : comp.programming From : Ed Prochak Date : Fri Aug 19 2005 12:49 pm 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 > -- > DEMAINE Benoit-Pierre (aka DoubleHP ) http://www.demaine.info/ > \_o< If computing were an exact science, IT engineers would not have work >o_/ Do you have any functions in the boot ROM for memory allocation? For your environment, that's where the foundation of malloc should be. you can write some simple algorithms in assembler to handle this, given that you have one application. I don't quite see your concern about endianness (do you have two processors of different endianess?) or alignment (memory allocation should always match the most restricted alignment for all datatypes). I'm curious though why you have NO operating system. I've done enough embedded programming to say that the overhead of a kernel is much lower than the warts produced in the main program logic to deal with the equivalent of task switching. For memory allocation, it can be customized to a great extent. (e.g., you allocate either for string data or certain structures which are fixed size, then designing a memory pool that allocates strings from the top and structs from the bottom might be better than a generic malloc(). Yes that means possibly another parameter to your version of malloc() to indicate which one. One critical tip: ALWAYS check the return value from malloc() and deal with the case that it could NOT get the memory you requested. In testing, if nothing else, you should be able to dump memory in that event and debug by checking malloc's data structures. HTH, Ed (drop me a line sometime. I'd love to hear more about your project.) edprochak at netscape dot net .