home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / textprocess / grep_2 / release / c / alloca next >
Text File  |  1998-09-23  |  6KB  |  188 lines

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