Subj : Re: where can I find a malloc for standalone programs ? To : comp.programming From : Giannis Papadopoulos Date : Sat Aug 20 2005 07:08 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 Since you only have one program and specific memory on your device, why don't you create a simple array that is as big as the rest of your memory (allocated in static space) and create an malloc "emulation"... If you have 64K of memory and your program is 32K, use the remaining 32K for creating this malloc emulation and the array that represents heap space.. I have tried this a year or so ago (out of curiosity) and it was very nice ;) but it could only allocate up to 127 bytes for every object (I used only 7bits for storing the length of the allocated space and 1 bit to declare it free or not). -- one's freedom stops where others' begin Giannis Papadopoulos http://dop.users.uth.gr/ University of Thessaly Computer & Communications Engineering dept. .