[net.unix-wizards] stack allocator

eric%cit-vax@sri-unix.UUCP (07/08/83)

BINGO!!

    From thompson.umn-cs@Rand-Relay Thu Jul  7 23:35:19 1983
    Date: 7 Jul 1983 12:16:34-PDT
    From: thompson.umn-cs@Rand-Relay
    Return-Path: <thompson.UMN-CS@Rand-Relay>
    Subject: Stack allocation in C
    To: eric@cit-vax
    Via:  UMN-CS; 7 Jul 83 23:14-PDT
	
    We have been using a stack allocation routine in our image processing
    software for a year and a half without difficulties.  (This of course does
    not exactly answer your question.)  The automatic stack manipulation on
    calls and returns makes this somewhat difficult to do.  Here is our code:
	
    /*
     *      getspace(nbytes)
     *
     *      VAX-11 version.
     *
     *      Dynamically allocates storage space on run time stack.  Returns
     *      a pointer to a 'nbytes' byte area.  Space is freed when routine
     *      which called 'getspace' returns.
     */
	
	    .align  1
	    .text
	    .globl  _getspace
    _getspace:
	    .word   0x0             /* no need to save registers */
	    movl    4(ap),r0        /* temporarily save 'nbytes' */
	    movl    16(fp),r1       /* save address of calling routine */
	    movl    $__fake,16(fp)  /* insert a fake return address */
	    ret                     /* jump to fake return address */
    __fake:
	    bicl2   $03,sp          /* align stack to double word boundary */
				    /* (almost certainly not necessary) */
	    subl2   r0,sp           /* get extra space */
	    bicl2   $03,sp          /* align stack (again) */
	    movl    sp,r0           /* return start of storage area */
	    jmp     (r1)            /* go back to calling program */
				    /* (this one's for real!) */
	
Guess it can be done after all. Since this is a function, you
could melt yourself by doing something like somefunc(1,getspace(10),2)
and having a big hole between the 1 argument and the 2 argument to
getfunc.

				    * Eric Holstege
				    * Caltech, Pasadena, CA.
				    * (eric@cit-vax)
				    * (...!ucbvax!cithep!citcsv!eric)
				    * (...!research!citcsv!eric)

dbj.rice%rand-relay@sri-unix.UUCP (07/09/83)

From:  Dave Johnson <dbj.rice@rand-relay>

Why not just use the libc routine "alloca", which is distributed by Berkeley
(at least with 4.1)?   I've never used it, but from the source, it looks like
what you want.  Take a look at /usr/src/libc/sys/alloca.s...

                                        Dave Johnson
                                        Dept. of Math Science
                                        Rice University
                                        dbj.rice@Rand-Relay