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