Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!cornell!uw-beaver!uw-june!ka From: ka@june.cs.washington.edu (Kenneth Almquist) Newsgroups: comp.unix.wizards Subject: Re: alloca (was: libpw.a) Message-ID: <7963@june.cs.washington.edu> Date: 24 Apr 89 14:27:11 GMT References: <157@dftsrv.gsfc.nasa.gov> <10013@smoke.BRL.MIL> <8261@chinet.chi.il.us> Organization: U of Washington, Computer Science, Seattle Lines: 29 les@chinet.chi.il.us (Leslie Mikesell) writes: > There are two situations which are helped by the use of alloca(): > 1) dividing the dynamically allocated space into reasonably sized chunks > to reduce the time malloc() and free() need to search their lists, and > 2) providing the ability to longjmp() out of a function (or just return) > with the no longer needed memory automatically released (just like local > variables). Ash uses a memory allocator with stack-like semantics: struct stackmark smark; /* stack mark saves stack location */ setstackmark(&smark); /* smark = stack pointer */ for (;;) { ... p = stalloc(n); /* allocate space off stack */ ... } /* now free everything allocated by the stalloc calls */ popstackmark(&smark); /* stack pointer = smark */ This is more flexible than alloca because routines can return blocks allocated via stalloc. It solves the problems you mention. (It deals with problem 1 by allocating reasonably large blocks from malloc and doling them out in smaller pieces.) Its main advantage is that it can be implemented reliably on any machine that can support C. Ash will appear in comp.sources.unix next week if you want to steal the code. Kenneth Almquist