alloca (was: libpw.a)

Kenneth Almquist ka at june.cs.washington.edu
Tue Apr 25 00:27:11 AEST 1989


les at 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



More information about the Comp.unix.wizards mailing list