stack allocator

eric%cit-vax at sri-unix.UUCP eric%cit-vax at sri-unix.UUCP
Fri Jul 8 17:39:36 AEST 1983


BINGO!!

    From thompson.umn-cs at Rand-Relay Thu Jul  7 23:35:19 1983
    Date: 7 Jul 1983 12:16:34-PDT
    From: thompson.umn-cs at Rand-Relay
    Return-Path: <thompson.UMN-CS at Rand-Relay>
    Subject: Stack allocation in C
    To: eric at 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 at cit-vax)
				    * (...!ucbvax!cithep!citcsv!eric)
				    * (...!research!citcsv!eric)



More information about the Comp.unix.wizards mailing list