home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c100 / 4.ddi / COM.ZIP / ALLOCA.CPP next >
Encoding:
C/C++ Source or Header  |  1990-08-02  |  5.5 KB  |  201 lines

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