home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C++ / Snippets / GNU String⁄Regex / alloca.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-06  |  5.4 KB  |  204 lines  |  [TEXT/CWIE]

  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.      ...hack for MWC6 6jul95ww
  30.  
  31.  
  32. */
  33. #define xmalloc malloc
  34.  
  35. #ifndef lint
  36. static char    SCCSid[] = "@(#)alloca.c    1.1";    /* for the "what" utility */
  37. #endif
  38.  
  39. #ifdef emacs
  40. #include "config.h"
  41. #ifdef static
  42. /* actually, only want this if static is defined as ""
  43.    -- this is for usg, in which emacs must undefine static
  44.    in order to make unexec workable
  45.    */
  46. #ifndef STACK_DIRECTION
  47. you
  48. lose
  49. -- must know STACK_DIRECTION at compile-time
  50. #endif /* STACK_DIRECTION undefined */
  51. #endif /* static */
  52. #endif /* emacs */
  53.  
  54. #ifndef ADDRESS_FUNCTION
  55. #define ADDRESS_FUNCTION(arg) &arg
  56. #endif
  57.  
  58. #ifndef alloca  /* If compiling with GCC, this file's not needed.  */
  59.  
  60. #ifdef __STDC__
  61. typedef void    *pointer;        /* generic pointer type */
  62. #else
  63. typedef char    *pointer;        /* generic pointer type */
  64. #endif
  65.  
  66. #define    NULL    0            /* null pointer constant */
  67.  
  68. extern "C" void    free( pointer );
  69. extern "C" pointer    xmalloc( unsigned long );
  70.  
  71. /*
  72.     Define STACK_DIRECTION if you know the direction of stack
  73.     growth for your system; otherwise it will be automatically
  74.     deduced at run-time.
  75.  
  76.     STACK_DIRECTION > 0 => grows toward higher addresses
  77.     STACK_DIRECTION < 0 => grows toward lower addresses
  78.     STACK_DIRECTION = 0 => direction of growth unknown
  79. */
  80.  
  81. #ifndef STACK_DIRECTION
  82. #define    STACK_DIRECTION    -1        /* down ww 6jul95ww */
  83. #endif
  84.  
  85. #if STACK_DIRECTION != 0
  86.  
  87. #define    STACK_DIR    STACK_DIRECTION    /* known at compile-time */
  88.  
  89. #else    /* STACK_DIRECTION == 0; need run-time code */
  90.  
  91. static int    stack_dir;        /* 1 or -1 once known */
  92. #define    STACK_DIR    stack_dir
  93.  
  94. static void
  95. find_stack_direction (/* void */)
  96. {
  97.   static char    *addr = NULL;    /* address of first
  98.                    `dummy', once known */
  99.   auto char    dummy;        /* to get stack address */
  100.  
  101.   if (addr == NULL)
  102.     {                /* initial entry */
  103.       addr = ADDRESS_FUNCTION(dummy);
  104.  
  105.       find_stack_direction ();    /* recurse once */
  106.     }
  107.   else                /* second entry */
  108.     if (ADDRESS_FUNCTION(dummy) > addr)
  109.       stack_dir = 1;        /* stack grew upward */
  110.     else
  111.       stack_dir = -1;        /* stack grew downward */
  112. }
  113.  
  114. #endif    /* STACK_DIRECTION == 0 */
  115.  
  116. /*
  117.     An "alloca header" is used to:
  118.     (a) chain together all alloca()ed blocks;
  119.     (b) keep track of stack depth.
  120.  
  121.     It is very important that sizeof(header) agree with malloc()
  122.     alignment chunk size.  The following default should work okay.
  123. */
  124.  
  125. #ifndef    ALIGN_SIZE
  126. #define    ALIGN_SIZE    sizeof(double)
  127. #endif
  128.  
  129. typedef union hdr
  130. {
  131.   char    align[ALIGN_SIZE];    /* to force sizeof(header) */
  132.   struct
  133.     {
  134.       union hdr *next;        /* for chaining headers */
  135.       char *deep;        /* for stack depth measure */
  136.     } h;
  137. } header;
  138.  
  139. /*
  140.     alloca( size ) returns a pointer to at least `size' bytes of
  141.     storage which will be automatically reclaimed upon exit from
  142.     the procedure that called alloca().  Originally, this space
  143.     was supposed to be taken from the current stack frame of the
  144.     caller, but that method cannot be made to work for some
  145.     implementations of C, for example under Gould's UTX/32.
  146. */
  147.  
  148. static header *last_alloca_header = NULL; /* -> last alloca header */
  149.  
  150. extern "C" pointer alloca( unsigned size );
  151. extern "C" pointer alloca( unsigned size )
  152. {
  153.   auto char    probe;        /* probes stack depth: */
  154.   register char    *depth = (char *) ADDRESS_FUNCTION(probe);
  155.  
  156. #if STACK_DIRECTION == 0
  157.   if (STACK_DIR == 0)        /* unknown growth direction */
  158.     find_stack_direction ();
  159. #endif
  160.  
  161.                 /* Reclaim garbage, defined as all alloca()ed storage that
  162.                    was allocated from deeper in the stack than currently. */
  163.  
  164.   {
  165.     register header    *hp;    /* traverses linked list */
  166.  
  167.     for (hp = last_alloca_header; hp != NULL;)
  168.       if (   (STACK_DIR > 0 && hp->h.deep > depth)
  169.       || (STACK_DIR < 0 && hp->h.deep < depth) )
  170.     {
  171.       register header    *np = hp->h.next;
  172.  
  173.       free ( (pointer) hp);    /* collect garbage */
  174.  
  175.       hp = np;        /* -> next header */
  176.     }
  177.       else
  178.     break;            /* rest are not deeper */
  179.  
  180.     last_alloca_header = hp;    /* -> last valid storage */
  181.   }
  182.  
  183.   if (size == 0)
  184.     return NULL;        /* no allocation required */
  185.  
  186.   /* Allocate combined header + user data storage. */
  187.  
  188.   {
  189.     register pointer    New = xmalloc (sizeof (header) + size);
  190.     /* address of header */
  191.  
  192.     ((header *)New)->h.next = last_alloca_header;
  193.     ((header *)New)->h.deep = depth;
  194.  
  195.     last_alloca_header = (header *)New;
  196.  
  197.     /* User storage begins just after header. */
  198.  
  199.     return (pointer)((char *)New + sizeof(header));
  200.   }
  201. }
  202.  
  203. #endif /* no alloca */
  204.