home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 2 / RISC_DISC_2.iso / pd_share / comms / encoders / uuenc / c / alloca next >
Encoding:
Text File  |  1995-03-10  |  12.9 KB  |  463 lines

  1. /* alloca.c -- allocate automatically reclaimed memory
  2.    (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.    J.Otto Tennant <jot@cray.com> contributed the Cray support.
  9.  
  10.    There are some preprocessor constants that can
  11.    be defined when compiling for your specific system, for
  12.    improved efficiency; however, the defaults should be okay.
  13.  
  14.    The general concept of this implementation is to keep
  15.    track of all alloca-allocated blocks, and reclaim any
  16.    that are found to be deeper in the stack than the current
  17.    invocation.  This heuristic does not reclaim storage as
  18.    soon as it becomes invalid, but it will do so eventually.
  19.  
  20.    As a special case, alloca(0) reclaims storage without
  21.    allocating any.  It is a good idea to use alloca(0) in
  22.    your main control loop, etc. to force garbage collection.  */
  23.  
  24. #ifdef HAVE_CONFIG_H
  25. #include "config.h"
  26. #endif
  27.  
  28. #include <stdlib.h>
  29.  
  30. /* If compiling with GCC, this file's not needed.  */
  31. #ifndef alloca
  32.  
  33. #ifdef emacs
  34. #ifdef static
  35. /* actually, only want this if static is defined as ""
  36.    -- this is for usg, in which emacs must undefine static
  37.    in order to make unexec workable
  38.    */
  39. #ifndef STACK_DIRECTION
  40. you
  41. lose
  42. -- must know STACK_DIRECTION at compile-time
  43. #endif /* STACK_DIRECTION undefined */
  44. #endif /* static */
  45. #endif /* emacs */
  46.  
  47. /* If your stack is a linked list of frames, you have to
  48.    provide an "address metric" ADDRESS_FUNCTION macro.  */
  49.  
  50. #ifdef CRAY
  51. long i00afunc ();
  52. #define ADDRESS_FUNCTION(arg) (char *) i00afunc (&(arg))
  53. #else
  54. #define ADDRESS_FUNCTION(arg) &(arg)
  55. #endif
  56.  
  57. #if __STDC__
  58. typedef void *pointer;
  59. #else
  60. typedef char *pointer;
  61. #endif
  62.  
  63. #define    NULL    0
  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. #ifndef STACK_DIRECTION
  74. #define    STACK_DIRECTION    0    /* Direction unknown.  */
  75. #endif
  76.  
  77. #if STACK_DIRECTION != 0
  78.  
  79. #define    STACK_DIR    STACK_DIRECTION    /* Known at compile-time.  */
  80.  
  81. #else /* STACK_DIRECTION == 0; need run-time code.  */
  82.  
  83. static int stack_dir;        /* 1 or -1 once known.  */
  84. #define    STACK_DIR    stack_dir
  85.  
  86. static void
  87. find_stack_direction ()
  88. {
  89.   static char *addr = NULL;    /* Address of first `dummy', once known.  */
  90.   auto char dummy;        /* To get stack address.  */
  91.  
  92.   if (addr == NULL)
  93.     {                /* Initial entry.  */
  94.       addr = ADDRESS_FUNCTION (dummy);
  95.  
  96.       find_stack_direction ();    /* Recurse once.  */
  97.     }
  98.   else
  99.     {
  100.       /* Second entry.  */
  101.       if (ADDRESS_FUNCTION (dummy) > addr)
  102.     stack_dir = 1;        /* Stack grew upward.  */
  103.       else
  104.     stack_dir = -1;        /* Stack grew downward.  */
  105.     }
  106. }
  107.  
  108. #endif /* STACK_DIRECTION == 0 */
  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. #ifndef    ALIGN_SIZE
  118. #define    ALIGN_SIZE    sizeof(double)
  119. #endif
  120.  
  121. typedef union hdr
  122. {
  123.   char align[ALIGN_SIZE];    /* To force sizeof(header).  */
  124.   struct
  125.     {
  126.       union hdr *next;        /* For chaining headers.  */
  127.       char *deep;        /* For stack depth measure.  */
  128.     } h;
  129. } header;
  130.  
  131. static header *last_alloca_header = NULL;    /* -> last alloca header.  */
  132.  
  133. /* Return a pointer to at least SIZE bytes of storage,
  134.    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. pointer
  141. alloca (size)
  142.      unsigned size;
  143. {
  144.   auto char probe;        /* Probes stack depth: */
  145.   register char *depth = ADDRESS_FUNCTION (probe);
  146.  
  147. #if STACK_DIRECTION == 0
  148.   if (STACK_DIR == 0)        /* Unknown growth direction.  */
  149.     find_stack_direction ();
  150. #endif
  151.  
  152.   /* Reclaim garbage, defined as all alloca'd storage that
  153.      was allocated from deeper in the stack than currently. */
  154.  
  155.   {
  156.     register header *hp;    /* Traverses linked list.  */
  157.  
  158.     for (hp = last_alloca_header; hp != NULL;)
  159.       if ((STACK_DIR > 0 && hp->h.deep > depth)
  160.       || (STACK_DIR < 0 && hp->h.deep < depth))
  161.     {
  162.       register header *np = hp->h.next;
  163.  
  164.       free ((pointer) hp);    /* Collect garbage.  */
  165.  
  166.       hp = np;        /* -> next header.  */
  167.     }
  168.       else
  169.     break;            /* Rest are not deeper.  */
  170.  
  171.     last_alloca_header = hp;    /* -> last valid storage.  */
  172.   }
  173.  
  174.   if (size == 0)
  175.     return NULL;        /* No allocation required.  */
  176.  
  177.   /* Allocate combined header + user data storage.  */
  178.  
  179.   {
  180.     register pointer new = malloc (sizeof (header) + size);
  181.     /* Address of header.  */
  182.  
  183.     ((header *) new)->h.next = last_alloca_header;
  184.     ((header *) new)->h.deep = depth;
  185.  
  186.     last_alloca_header = (header *) new;
  187.  
  188.     /* User storage begins just after header.  */
  189.  
  190.     return (pointer) ((char *) new + sizeof (header));
  191.   }
  192. }
  193.  
  194. #ifdef CRAY
  195.  
  196. #ifdef DEBUG_I00AFUNC
  197. #include <stdio.h>
  198. #endif
  199.  
  200. #ifndef CRAY_STACK
  201. #define CRAY_STACK
  202. #ifndef CRAY2
  203. /* Stack structures for CRAY-1, CRAY X-MP, and CRAY Y-MP */
  204. struct stack_control_header
  205.   {
  206.     long shgrow:32;        /* Number of times stack has grown.  */
  207.     long shaseg:32;        /* Size of increments to stack.  */
  208.     long shhwm:32;        /* High water mark of stack.  */
  209.     long shsize:32;        /* Current size of stack (all segments).  */
  210.   };
  211.  
  212. /* The stack segment linkage control information occurs at
  213.    the high-address end of a stack segment.  (The stack
  214.    grows from low addresses to high addresses.)  The initial
  215.    part of the stack segment linkage control information is
  216.    0200 (octal) words.  This provides for register storage
  217.    for the routine which overflows the stack.  */
  218.  
  219. struct stack_segment_linkage
  220.   {
  221.     long ss[0200];        /* 0200 overflow words.  */
  222.     long sssize:32;        /* Number of words in this segment.  */
  223.     long ssbase:32;        /* Offset to stack base.  */
  224.     long:32;
  225.     long sspseg:32;        /* Offset to linkage control of previous
  226.                    segment of stack.  */
  227.     long:32;
  228.     long sstcpt:32;        /* Pointer to task common address block.  */
  229.     long sscsnm;        /* Private control structure number for
  230.                    microtasking.  */
  231.     long ssusr1;        /* Reserved for user.  */
  232.     long ssusr2;        /* Reserved for user.  */
  233.     long sstpid;        /* Process ID for pid based multi-tasking.  */
  234.     long ssgvup;        /* Pointer to multitasking thread giveup.  */
  235.     long sscray[7];        /* Reserved for Cray Research.  */
  236.     long ssa0;
  237.     long ssa1;
  238.     long ssa2;
  239.     long ssa3;
  240.     long ssa4;
  241.     long ssa5;
  242.     long ssa6;
  243.     long ssa7;
  244.     long sss0;
  245.     long sss1;
  246.     long sss2;
  247.     long sss3;
  248.     long sss4;
  249.     long sss5;
  250.     long sss6;
  251.     long sss7;
  252.   };
  253.  
  254. #else /* CRAY2 */
  255. /* The following structure defines the vector of words
  256.    returned by the STKSTAT library routine.  */
  257. struct stk_stat
  258.   {
  259.     long now;            /* Current total stack size.  */
  260.     long maxc;            /* Amount of contiguous space which would
  261.                    be required to satisfy the maximum
  262.                    stack demand to date.  */
  263.     long high_water;        /* Stack high-water mark.  */
  264.     long overflows;        /* Number of stack overflow ($STKOFEN) calls.  */
  265.     long hits;            /* Number of internal buffer hits.  */
  266.     long extends;        /* Number of block extensions.  */
  267.     long stko_mallocs;        /* Block allocations by $STKOFEN.  */
  268.     long underflows;        /* Number of stack underflow calls ($STKRETN).  */
  269.     long stko_free;        /* Number of deallocations by $STKRETN.  */
  270.     long stkm_free;        /* Number of deallocations by $STKMRET.  */
  271.     long segments;        /* Current number of stack segments.  */
  272.     long maxs;            /* Maximum number of stack segments so far.  */
  273.     long pad_size;        /* Stack pad size.  */
  274.     long current_address;    /* Current stack segment address.  */
  275.     long current_size;        /* Current stack segment size.  This
  276.                    number is actually corrupted by STKSTAT to
  277.                    include the fifteen word trailer area.  */
  278.     long initial_address;    /* Address of initial segment.  */
  279.     long initial_size;        /* Size of initial segment.  */
  280.   };
  281.  
  282. /* The following structure describes the data structure which trails
  283.    any stack segment.  I think that the description in 'asdef' is
  284.    out of date.  I only describe the parts that I am sure about.  */
  285.  
  286. struct stk_trailer
  287.   {
  288.     long this_address;        /* Address of this block.  */
  289.     long this_size;        /* Size of this block (does not include
  290.                    this trailer).  */
  291.     long unknown2;
  292.     long unknown3;
  293.     long link;            /* Address of trailer block of previous
  294.                    segment.  */
  295.     long unknown5;
  296.     long unknown6;
  297.     long unknown7;
  298.     long unknown8;
  299.     long unknown9;
  300.     long unknown10;
  301.     long unknown11;
  302.     long unknown12;
  303.     long unknown13;
  304.     long unknown14;
  305.   };
  306.  
  307. #endif /* CRAY2 */
  308. #endif /* not CRAY_STACK */
  309.  
  310. #ifdef CRAY2
  311. /* Determine a "stack measure" for an arbitrary ADDRESS.
  312.    I doubt that "lint" will like this much. */
  313.  
  314. static long
  315. i00afunc (long *address)
  316. {
  317.   struct stk_stat status;
  318.   struct stk_trailer *trailer;
  319.   long *block, size;
  320.   long result = 0;
  321.  
  322.   /* We want to iterate through all of the segments.  The first
  323.      step is to get the stack status structure.  We could do this
  324.      more quickly and more directly, perhaps, by referencing the
  325.      $LM00 common block, but I know that this works.  */
  326.  
  327.   STKSTAT (&status);
  328.  
  329.   /* Set up the iteration.  */
  330.  
  331.   trailer = (struct stk_trailer *) (status.current_address
  332.                     + status.current_size
  333.                     - 15);
  334.  
  335.   /* There must be at least one stack segment.  Therefore it is
  336.      a fatal error if "trailer" is null.  */
  337.  
  338.   if (trailer == 0)
  339.     abort ();
  340.  
  341.   /* Discard segments that do not contain our argument address.  */
  342.  
  343.   while (trailer != 0)
  344.     {
  345.       block = (long *) trailer->this_address;
  346.       size = trailer->this_size;
  347.       if (block == 0 || size == 0)
  348.     abort ();
  349.       trailer = (struct stk_trailer *) trailer->link;
  350.       if ((block <= address) && (address < (block + size)))
  351.     break;
  352.     }
  353.  
  354.   /* Set the result to the offset in this segment and add the sizes
  355.      of all predecessor segments.  */
  356.  
  357.   result = address - block;
  358.  
  359.   if (trailer == 0)
  360.     {
  361.       return result;
  362.     }
  363.  
  364.   do
  365.     {
  366.       if (trailer->this_size <= 0)
  367.     abort ();
  368.       result += trailer->this_size;
  369.       trailer = (struct stk_trailer *) trailer->link;
  370.     }
  371.   while (trailer != 0);
  372.  
  373.   /* We are done.  Note that if you present a bogus address (one
  374.      not in any segment), you will get a different number back, formed
  375.      from subtracting the address of the first block.  This is probably
  376.      not what you want.  */
  377.  
  378.   return (result);
  379. }
  380.  
  381. #else /* not CRAY2 */
  382. /* Stack address function for a CRAY-1, CRAY X-MP, or CRAY Y-MP.
  383.    Determine the number of the cell within the stack,
  384.    given the address of the cell.  The purpose of this
  385.    routine is to linearize, in some sense, stack addresses
  386.    for alloca.  */
  387.  
  388. static long
  389. i00afunc (long address)
  390. {
  391.   long stkl = 0;
  392.  
  393.   long size, pseg, this_segment, stack;
  394.   long result = 0;
  395.  
  396.   struct stack_segment_linkage *ssptr;
  397.  
  398.   /* Register B67 contains the address of the end of the
  399.      current stack segment.  If you (as a subprogram) store
  400.      your registers on the stack and find that you are past
  401.      the contents of B67, you have overflowed the segment.
  402.  
  403.      B67 also points to the stack segment linkage control
  404.      area, which is what we are really interested in.  */
  405.  
  406.   stkl = CRAY_STACKSEG_END ();
  407.   ssptr = (struct stack_segment_linkage *) stkl;
  408.  
  409.   /* If one subtracts 'size' from the end of the segment,
  410.      one has the address of the first word of the segment.
  411.  
  412.      If this is not the first segment, 'pseg' will be
  413.      nonzero.  */
  414.  
  415.   pseg = ssptr->sspseg;
  416.   size = ssptr->sssize;
  417.  
  418.   this_segment = stkl - size;
  419.  
  420.   /* It is possible that calling this routine itself caused
  421.      a stack overflow.  Discard stack segments which do not
  422.      contain the target address.  */
  423.  
  424.   while (!(this_segment <= address && address <= stkl))
  425.     {
  426. #ifdef DEBUG_I00AFUNC
  427.       fprintf (stderr, "%011o %011o %011o\n", this_segment, address, stkl);
  428. #endif
  429.       if (pseg == 0)
  430.     break;
  431.       stkl = stkl - pseg;
  432.       ssptr = (struct stack_segment_linkage *) stkl;
  433.       size = ssptr->sssize;
  434.       pseg = ssptr->sspseg;
  435.       this_segment = stkl - size;
  436.     }
  437.  
  438.   result = address - this_segment;
  439.  
  440.   /* If you subtract pseg from the current end of the stack,
  441.      you get the address of the previous stack segment's end.
  442.      This seems a little convoluted to me, but I'll bet you save
  443.      a cycle somewhere.  */
  444.  
  445.   while (pseg != 0)
  446.     {
  447. #ifdef DEBUG_I00AFUNC
  448.       fprintf (stderr, "%011o %011o\n", pseg, size);
  449. #endif
  450.       stkl = stkl - pseg;
  451.       ssptr = (struct stack_segment_linkage *) stkl;
  452.       size = ssptr->sssize;
  453.       pseg = ssptr->sspseg;
  454.       result += size;
  455.     }
  456.   return (result);
  457. }
  458.  
  459. #endif /* not CRAY2 */
  460. #endif /* CRAY */
  461.  
  462. #endif /* no alloca */
  463.