home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / TOP / USR / SRC / gawk2.0.t.Z / gawk2.0.t / alloca.c next >
Text File  |  1988-05-29  |  5KB  |  193 lines

  1. /*
  2.     alloca -- (mostly) portable public-domain implementation -- D A Gwyn
  3.  
  4.     last edit:    86/05/30    rms
  5.        include config.h, since on VMS it renames some symbols.
  6.        Use xmalloc instead of malloc.
  7.  
  8.     This implementation of the PWB library alloca() function,
  9.     which is used to allocate space off the run-time stack so
  10.     that it is automatically reclaimed upon procedure exit, 
  11.     was inspired by discussions with J. Q. Johnson of Cornell.
  12.  
  13.     It should work under any C implementation that uses an
  14.     actual procedure stack (as opposed to a linked list of
  15.     frames).  There are some preprocessor constants that can
  16.     be defined when compiling for your specific system, for
  17.     improved efficiency; however, the defaults should be okay.
  18.  
  19.     The general concept of this implementation is to keep
  20.     track of all alloca()-allocated blocks, and reclaim any
  21.     that are found to be deeper in the stack than the current
  22.     invocation.  This heuristic does not reclaim storage as
  23.     soon as it becomes invalid, but it will do so eventually.
  24.  
  25.     As a special case, alloca(0) reclaims storage without
  26.     allocating any.  It is a good idea to use alloca(0) in
  27.     your main control loop, etc. to force garbage collection.
  28. */
  29.  
  30. #ifdef emacs
  31. #include "config.h"
  32. #ifdef static
  33. /* actually, only want this if static is defined as ""
  34.    -- this is for usg, in which emacs must undefine static
  35.    in order to make unexec workable
  36.    */
  37. #ifndef STACK_DIRECTION
  38. you
  39. lose
  40. -- must know STACK_DIRECTION at compile-time
  41. #endif /* STACK_DIRECTION undefined */
  42. #endif static
  43. #endif emacs
  44.  
  45. #ifdef X3J11
  46. typedef void    *pointer;        /* generic pointer type */
  47. #else
  48. typedef char    *pointer;        /* generic pointer type */
  49. #endif
  50.  
  51. #define    NULL    0            /* null pointer constant */
  52.  
  53. extern void    free();
  54. extern pointer    malloc();
  55.  
  56. /*
  57.     Define STACK_DIRECTION if you know the direction of stack
  58.     growth for your system; otherwise it will be automatically
  59.     deduced at run-time.
  60.  
  61.     STACK_DIRECTION > 0 => grows toward higher addresses
  62.     STACK_DIRECTION < 0 => grows toward lower addresses
  63.     STACK_DIRECTION = 0 => direction of growth unknown
  64. */
  65.  
  66. #define STACK_DIRECTION -1
  67.  
  68. #ifndef STACK_DIRECTION
  69. #define    STACK_DIRECTION    0        /* direction unknown */
  70. #endif
  71.  
  72. #if STACK_DIRECTION != 0
  73.  
  74. #define    STACK_DIR    STACK_DIRECTION    /* known at compile-time */
  75.  
  76. #else    /* STACK_DIRECTION == 0; need run-time code */
  77.  
  78. static int    stack_dir;        /* 1 or -1 once known */
  79. #define    STACK_DIR    stack_dir
  80.  
  81. static void
  82. find_stack_direction (/* void */)
  83. {
  84.   static char    *addr = NULL;    /* address of first
  85.                    `dummy', once known */
  86.   auto char    dummy;        /* to get stack address */
  87.  
  88.   if (addr == NULL)
  89.     {                /* initial entry */
  90.       addr = &dummy;
  91.  
  92.       find_stack_direction ();    /* recurse once */
  93.     }
  94.   else                /* second entry */
  95.     if (&dummy > addr)
  96.       stack_dir = 1;        /* stack grew upward */
  97.     else
  98.       stack_dir = -1;        /* stack grew downward */
  99. }
  100.  
  101. #endif    /* STACK_DIRECTION == 0 */
  102.  
  103. /*
  104.     An "alloca header" is used to:
  105.     (a) chain together all alloca()ed blocks;
  106.     (b) keep track of stack depth.
  107.  
  108.     It is very important that sizeof(header) agree with malloc()
  109.     alignment chunk size.  The following default should work okay.
  110. */
  111.  
  112. #define ALIGN_SIZE    4
  113.  
  114. #ifndef    ALIGN_SIZE
  115. #define    ALIGN_SIZE    sizeof(double)
  116. #endif
  117.  
  118. typedef union hdr
  119. {
  120.   char    align[ALIGN_SIZE];    /* to force sizeof(header) */
  121.   struct
  122.     {
  123.       union hdr *next;        /* for chaining headers */
  124.       char *deep;        /* for stack depth measure */
  125.     } h;
  126. } header;
  127.  
  128. /*
  129.     alloca( size ) returns a pointer to at least `size' bytes of
  130.     storage which will be automatically reclaimed upon exit from
  131.     the procedure that called alloca().  Originally, this space
  132.     was supposed to be taken from the current stack frame of the
  133.     caller, but that method cannot be made to work for some
  134.     implementations of C, for example under Gould's UTX/32.
  135. */
  136.  
  137. static header *last_alloca_header = NULL; /* -> last alloca header */
  138.  
  139. pointer
  140. alloca (size)            /* returns pointer to storage */
  141.      unsigned    size;        /* # bytes to allocate */
  142. {
  143.   auto char    probe;        /* probes stack depth: */
  144.   register char    *depth = &probe;
  145.  
  146. #if STACK_DIRECTION == 0
  147.   if (STACK_DIR == 0)        /* unknown growth direction */
  148.     find_stack_direction ();
  149. #endif
  150.  
  151.                 /* Reclaim garbage, defined as all alloca()ed storage that
  152.                    was allocated from deeper in the stack than currently. */
  153.  
  154.   {
  155.     register header    *hp;    /* traverses linked list */
  156.  
  157.     for (hp = last_alloca_header; hp != NULL;)
  158.       if (STACK_DIR > 0 && hp->h.deep > depth
  159.       || STACK_DIR < 0 && hp->h.deep < depth)
  160.     {
  161.       register header    *np = hp->h.next;
  162.  
  163.       free ((pointer) hp);    /* collect garbage */
  164.  
  165.       hp = np;        /* -> next header */
  166.     }
  167.       else
  168.     break;            /* rest are not deeper */
  169.  
  170.     last_alloca_header = hp;    /* -> last valid storage */
  171.   }
  172.  
  173.   if (size == 0)
  174.     return NULL;        /* no allocation required */
  175.  
  176.   /* Allocate combined header + user data storage. */
  177.  
  178.   {
  179.     register pointer    new = malloc (sizeof (header) + size);
  180.     /* address of header */
  181.  
  182.     ((header *)new)->h.next = last_alloca_header;
  183.     ((header *)new)->h.deep = depth;
  184.  
  185.     last_alloca_header = (header *)new;
  186.  
  187.     /* User storage begins just after header. */
  188.  
  189.     return (pointer)((char *)new + sizeof(header));
  190.   }
  191. }
  192.  
  193.