home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / g__src1 / alloca.c next >
C/C++ Source or Header  |  1993-07-23  |  5KB  |  178 lines

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