home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / MPW / fgrep 1.1 / Library stuff / alloca.c next >
Encoding:
C/C++ Source or Header  |  1993-03-14  |  6.4 KB  |  226 lines  |  [TEXT/MPS ]

  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.    MPW C changes
  8.      Copyright (C) 1989, 1990 Apple Computer, Inc.
  9.     Sun, Mar 14, 1993    Franklin Chen changed defined MPW_C here upon
  10.         macintosh, and made a few other changes as well (not that they really
  11.         matter).
  12.         
  13.     This implementation of the PWB library alloca() function,
  14.     which is used to allocate space off the run-time stack so
  15.     that it is automatically reclaimed upon procedure exit, 
  16.     was inspired by discussions with J. Q. Johnson of Cornell.
  17.  
  18.     It should work under any C implementation that uses an
  19.     actual procedure stack (as opposed to a linked list of
  20.     frames).  There are some preprocessor constants that can
  21.     be defined when compiling for your specific system, for
  22.     improved efficiency; however, the defaults should be okay.
  23.  
  24.     The general concept of this implementation is to keep
  25.     track of all alloca()-allocated blocks, and reclaim any
  26.     that are found to be deeper in the stack than the current
  27.     invocation.  This heuristic does not reclaim storage as
  28.     soon as it becomes invalid, but it will do so eventually.
  29.  
  30.     As a special case, alloca(0) reclaims storage without
  31.     allocating any.  It is a good idea to use alloca(0) in
  32.     your main control loop, etc. to force garbage collection.
  33. */
  34. #ifndef lint
  35. static char    SCCSid[] = "@(#)alloca.c    1.1";    /* for the "what" utility */
  36. #endif
  37.  
  38. #ifdef emacs
  39. #include "config.h"
  40. #ifdef static
  41. /* actually, only want this if static is defined as ""
  42.    -- this is for usg, in which emacs must undefine static
  43.    in order to make unexec workable
  44.    */
  45. #ifndef STACK_DIRECTION
  46. /*"you lose -- must know STACK_DIRECTION at compile-time"*/
  47. #endif /* STACK_DIRECTION undefined */
  48. #endif static
  49. #endif emacs
  50.  
  51. #ifndef alloca  /* If compiling with GCC, this file's not needed.  */
  52.  
  53. #if defined(__STDC__) || defined(macintosh)
  54. typedef void    *pointer;        /* generic pointer type */
  55. #else
  56. typedef char    *pointer;        /* generic pointer type */
  57. #endif
  58.  
  59. #define    NULL    0            /* null pointer constant */
  60.  
  61. extern void    free();
  62. extern pointer    xmalloc();
  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. #ifdef macintosh
  75. #define MPW_C
  76. #define STACK_DIRECTION    -1
  77. #endif
  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
  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. #ifdef MPW_C
  148. /* MPW C plays games with the stack pointer, in much the same way that GCC
  149.    does, but unfortunately it can't be told to be careful around alloca.
  150.    So instead of looking at sp, we look at the frame pointer of alloca's
  151.    caller, which is known to always be defined and not to change within
  152.    its scope of definition.  Doing this requires a direct function that
  153.    picks up the caller's a6, using the single instruction "move.l (a6),d0". */
  154.    
  155. static char *frame_pointer_value() = 0x2016;
  156.  
  157. /* This variable just keeps track of much alloca is used. */
  158.  
  159. int totalloca = 0;
  160. #endif /* MPW_C */
  161.  
  162. pointer
  163. alloca (size)            /* returns pointer to storage */
  164.      unsigned    size;        /* # bytes to allocate */
  165. {
  166. #ifdef MPW_C
  167.   /* Use the caller's frame pointer to estimate depth in the stack. */
  168.   register char *depth = frame_pointer_value();
  169. #else
  170.   auto char    probe;        /* probes stack depth: */
  171.   register char    *depth = &probe;
  172. #endif /* MPW_C */
  173. #ifdef MPW_C
  174.   /* Count this call against the total allocation. */
  175.   totalloca += size;
  176. #endif /* MPW_C */
  177.  
  178. #if STACK_DIRECTION == 0
  179.   if (STACK_DIR == 0)        /* unknown growth direction */
  180.     find_stack_direction ();
  181. #endif
  182.  
  183.                 /* Reclaim garbage, defined as all alloca()ed storage that
  184.                    was allocated from deeper in the stack than currently. */
  185.  
  186.   {
  187.     register header    *hp;    /* traverses linked list */
  188.  
  189.     for (hp = last_alloca_header; hp != NULL;)
  190.       if ((STACK_DIR > 0 && hp->h.deep > depth)
  191.       || (STACK_DIR < 0 && hp->h.deep < depth))
  192.     {
  193.       register header    *np = hp->h.next;
  194.  
  195.       free ((pointer) hp);    /* collect garbage */
  196.  
  197.       hp = np;        /* -> next header */
  198.     }
  199.       else
  200.     break;            /* rest are not deeper */
  201.  
  202.     last_alloca_header = hp;    /* -> last valid storage */
  203.   }
  204.  
  205.   if (size == 0)
  206.     return NULL;        /* no allocation required */
  207.  
  208.   /* Allocate combined header + user data storage. */
  209.  
  210.   {
  211.     register pointer    new = xmalloc (sizeof (header) + size);
  212.     /* address of header */
  213.  
  214.     ((header *)new)->h.next = last_alloca_header;
  215.     ((header *)new)->h.deep = depth;
  216.  
  217.     last_alloca_header = (header *)new;
  218.  
  219.     /* User storage begins just after header. */
  220.  
  221.     return (pointer)((char *)new + sizeof(header));
  222.   }
  223. }
  224.  
  225. #endif /* no alloca */
  226.