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