home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_progs / fileutil / sed.lzh / SED / ALLOCA.C < prev    next >
C/C++ Source or Header  |  1991-08-16  |  6KB  |  191 lines

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