home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / D / CLISP / CLISPSRC.TAR / clisp-1995-01-01 / src / readline / alloca.c next >
Encoding:
C/C++ Source or Header  |  1993-03-16  |  4.7 KB  |  174 lines

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