home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 4 / DATAFILE_PDCD4.iso / unix / unixtools / util / c / alloca next >
Text File  |  1992-07-21  |  5KB  |  167 lines

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