home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / f2c / src / alloca.c next >
C/C++ Source or Header  |  2000-05-07  |  5KB  |  196 lines

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