Blame lib/alloca.c

Packit 709fb3
/* alloca.c -- allocate automatically reclaimed memory
Packit 709fb3
   (Mostly) portable public-domain implementation -- D A Gwyn
Packit 709fb3
Packit 709fb3
   This implementation of the PWB library alloca function,
Packit 709fb3
   which is used to allocate space off the run-time stack so
Packit 709fb3
   that it is automatically reclaimed upon procedure exit,
Packit 709fb3
   was inspired by discussions with J. Q. Johnson of Cornell.
Packit 709fb3
   J.Otto Tennant <jot@cray.com> contributed the Cray support.
Packit 709fb3
Packit 709fb3
   There are some preprocessor constants that can
Packit 709fb3
   be defined when compiling for your specific system, for
Packit 709fb3
   improved efficiency; however, the defaults should be okay.
Packit 709fb3
Packit 709fb3
   The general concept of this implementation is to keep
Packit 709fb3
   track of all alloca-allocated blocks, and reclaim any
Packit 709fb3
   that are found to be deeper in the stack than the current
Packit 709fb3
   invocation.  This heuristic does not reclaim storage as
Packit 709fb3
   soon as it becomes invalid, but it will do so eventually.
Packit 709fb3
Packit 709fb3
   As a special case, alloca(0) reclaims storage without
Packit 709fb3
   allocating any.  It is a good idea to use alloca(0) in
Packit 709fb3
   your main control loop, etc. to force garbage collection.  */
Packit 709fb3
Packit 709fb3
#include <config.h>
Packit 709fb3
Packit 709fb3
#include <alloca.h>
Packit 709fb3
Packit 709fb3
#include <string.h>
Packit 709fb3
#include <stdlib.h>
Packit 709fb3
Packit 709fb3
#ifdef emacs
Packit 709fb3
# include "lisp.h"
Packit 709fb3
# include "blockinput.h"
Packit 709fb3
# ifdef EMACS_FREE
Packit 709fb3
#  undef free
Packit 709fb3
#  define free EMACS_FREE
Packit 709fb3
# endif
Packit 709fb3
#else
Packit 709fb3
# define memory_full() abort ()
Packit 709fb3
#endif
Packit 709fb3
Packit 709fb3
/* If compiling with GCC 2, this file's not needed.  */
Packit 709fb3
#if !defined (__GNUC__) || __GNUC__ < 2
Packit 709fb3
Packit 709fb3
/* If someone has defined alloca as a macro,
Packit 709fb3
   there must be some other way alloca is supposed to work.  */
Packit 709fb3
# ifndef alloca
Packit 709fb3
Packit 709fb3
#  ifdef emacs
Packit 709fb3
#   ifdef static
Packit 709fb3
/* actually, only want this if static is defined as ""
Packit 709fb3
   -- this is for usg, in which emacs must undefine static
Packit 709fb3
   in order to make unexec workable
Packit 709fb3
   */
Packit 709fb3
#    ifndef STACK_DIRECTION
Packit 709fb3
you
Packit 709fb3
lose
Packit 709fb3
-- must know STACK_DIRECTION at compile-time
Packit 709fb3
/* Using #error here is not wise since this file should work for
Packit 709fb3
   old and obscure compilers.  */
Packit 709fb3
#    endif /* STACK_DIRECTION undefined */
Packit 709fb3
#   endif /* static */
Packit 709fb3
#  endif /* emacs */
Packit 709fb3
Packit 709fb3
/* If your stack is a linked list of frames, you have to
Packit 709fb3
   provide an "address metric" ADDRESS_FUNCTION macro.  */
Packit 709fb3
Packit 709fb3
#  if defined (CRAY) && defined (CRAY_STACKSEG_END)
Packit 709fb3
long i00afunc ();
Packit 709fb3
#   define ADDRESS_FUNCTION(arg) (char *) i00afunc (&(arg))
Packit 709fb3
#  else
Packit 709fb3
#   define ADDRESS_FUNCTION(arg) &(arg)
Packit 709fb3
#  endif
Packit 709fb3
Packit 709fb3
/* Define STACK_DIRECTION if you know the direction of stack
Packit 709fb3
   growth for your system; otherwise it will be automatically
Packit 709fb3
   deduced at run-time.
Packit 709fb3
Packit 709fb3
   STACK_DIRECTION > 0 => grows toward higher addresses
Packit 709fb3
   STACK_DIRECTION < 0 => grows toward lower addresses
Packit 709fb3
   STACK_DIRECTION = 0 => direction of growth unknown  */
Packit 709fb3
Packit 709fb3
#  ifndef STACK_DIRECTION
Packit 709fb3
#   define STACK_DIRECTION      0       /* Direction unknown.  */
Packit 709fb3
#  endif
Packit 709fb3
Packit 709fb3
#  if STACK_DIRECTION != 0
Packit 709fb3
Packit 709fb3
#   define STACK_DIR    STACK_DIRECTION /* Known at compile-time.  */
Packit 709fb3
Packit 709fb3
#  else /* STACK_DIRECTION == 0; need run-time code.  */
Packit 709fb3
Packit 709fb3
static int stack_dir;           /* 1 or -1 once known.  */
Packit 709fb3
#   define STACK_DIR    stack_dir
Packit 709fb3
Packit 709fb3
static int
Packit 709fb3
find_stack_direction (int *addr, int depth)
Packit 709fb3
{
Packit 709fb3
  int dir, dummy = 0;
Packit 709fb3
  if (! addr)
Packit 709fb3
    addr = &dummy;
Packit 709fb3
  *addr = addr < &dummy ? 1 : addr == &dummy ? 0 : -1;
Packit 709fb3
  dir = depth ? find_stack_direction (addr, depth - 1) : 0;
Packit 709fb3
  return dir + dummy;
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
#  endif /* STACK_DIRECTION == 0 */
Packit 709fb3
Packit 709fb3
/* An "alloca header" is used to:
Packit 709fb3
   (a) chain together all alloca'ed blocks;
Packit 709fb3
   (b) keep track of stack depth.
Packit 709fb3
Packit 709fb3
   It is very important that sizeof(header) agree with malloc
Packit 709fb3
   alignment chunk size.  The following default should work okay.  */
Packit 709fb3
Packit 709fb3
#  ifndef       ALIGN_SIZE
Packit 709fb3
#   define ALIGN_SIZE   sizeof(double)
Packit 709fb3
#  endif
Packit 709fb3
Packit 709fb3
typedef union hdr
Packit 709fb3
{
Packit 709fb3
  char align[ALIGN_SIZE];       /* To force sizeof(header).  */
Packit 709fb3
  struct
Packit 709fb3
    {
Packit 709fb3
      union hdr *next;          /* For chaining headers.  */
Packit 709fb3
      char *deep;               /* For stack depth measure.  */
Packit 709fb3
    } h;
Packit 709fb3
} header;
Packit 709fb3
Packit 709fb3
static header *last_alloca_header = NULL;       /* -> last alloca header.  */
Packit 709fb3
Packit 709fb3
/* Return a pointer to at least SIZE bytes of storage,
Packit 709fb3
   which will be automatically reclaimed upon exit from
Packit 709fb3
   the procedure that called alloca.  Originally, this space
Packit 709fb3
   was supposed to be taken from the current stack frame of the
Packit 709fb3
   caller, but that method cannot be made to work for some
Packit 709fb3
   implementations of C, for example under Gould's UTX/32.  */
Packit 709fb3
Packit 709fb3
void *
Packit 709fb3
alloca (size_t size)
Packit 709fb3
{
Packit 709fb3
  auto char probe;              /* Probes stack depth: */
Packit 709fb3
  register char *depth = ADDRESS_FUNCTION (probe);
Packit 709fb3
Packit 709fb3
#  if STACK_DIRECTION == 0
Packit 709fb3
  if (STACK_DIR == 0)           /* Unknown growth direction.  */
Packit 709fb3
    STACK_DIR = find_stack_direction (NULL, (size & 1) + 20);
Packit 709fb3
#  endif
Packit 709fb3
Packit 709fb3
  /* Reclaim garbage, defined as all alloca'd storage that
Packit 709fb3
     was allocated from deeper in the stack than currently.  */
Packit 709fb3
Packit 709fb3
  {
Packit 709fb3
    register header *hp;        /* Traverses linked list.  */
Packit 709fb3
Packit 709fb3
#  ifdef emacs
Packit 709fb3
    BLOCK_INPUT;
Packit 709fb3
#  endif
Packit 709fb3
Packit 709fb3
    for (hp = last_alloca_header; hp != NULL;)
Packit 709fb3
      if ((STACK_DIR > 0 && hp->h.deep > depth)
Packit 709fb3
          || (STACK_DIR < 0 && hp->h.deep < depth))
Packit 709fb3
        {
Packit 709fb3
          register header *np = hp->h.next;
Packit 709fb3
Packit 709fb3
          free (hp);            /* Collect garbage.  */
Packit 709fb3
Packit 709fb3
          hp = np;              /* -> next header.  */
Packit 709fb3
        }
Packit 709fb3
      else
Packit 709fb3
        break;                  /* Rest are not deeper.  */
Packit 709fb3
Packit 709fb3
    last_alloca_header = hp;    /* -> last valid storage.  */
Packit 709fb3
Packit 709fb3
#  ifdef emacs
Packit 709fb3
    UNBLOCK_INPUT;
Packit 709fb3
#  endif
Packit 709fb3
  }
Packit 709fb3
Packit 709fb3
  if (size == 0)
Packit 709fb3
    return NULL;                /* No allocation required.  */
Packit 709fb3
Packit 709fb3
  /* Allocate combined header + user data storage.  */
Packit 709fb3
Packit 709fb3
  {
Packit 709fb3
    /* Address of header.  */
Packit 709fb3
    register header *new;
Packit 709fb3
Packit 709fb3
    size_t combined_size = sizeof (header) + size;
Packit 709fb3
    if (combined_size < sizeof (header))
Packit 709fb3
      memory_full ();
Packit 709fb3
Packit 709fb3
    new = malloc (combined_size);
Packit 709fb3
Packit 709fb3
    if (! new)
Packit 709fb3
      memory_full ();
Packit 709fb3
Packit 709fb3
    new->h.next = last_alloca_header;
Packit 709fb3
    new->h.deep = depth;
Packit 709fb3
Packit 709fb3
    last_alloca_header = new;
Packit 709fb3
Packit 709fb3
    /* User storage begins just after header.  */
Packit 709fb3
Packit 709fb3
    return (void *) (new + 1);
Packit 709fb3
  }
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
#  if defined (CRAY) && defined (CRAY_STACKSEG_END)
Packit 709fb3
Packit 709fb3
#   ifdef DEBUG_I00AFUNC
Packit 709fb3
#    include <stdio.h>
Packit 709fb3
#   endif
Packit 709fb3
Packit 709fb3
#   ifndef CRAY_STACK
Packit 709fb3
#    define CRAY_STACK
Packit 709fb3
#    ifndef CRAY2
Packit 709fb3
/* Stack structures for CRAY-1, CRAY X-MP, and CRAY Y-MP */
Packit 709fb3
struct stack_control_header
Packit 709fb3
  {
Packit 709fb3
    long shgrow:32;             /* Number of times stack has grown.  */
Packit 709fb3
    long shaseg:32;             /* Size of increments to stack.  */
Packit 709fb3
    long shhwm:32;              /* High water mark of stack.  */
Packit 709fb3
    long shsize:32;             /* Current size of stack (all segments).  */
Packit 709fb3
  };
Packit 709fb3
Packit 709fb3
/* The stack segment linkage control information occurs at
Packit 709fb3
   the high-address end of a stack segment.  (The stack
Packit 709fb3
   grows from low addresses to high addresses.)  The initial
Packit 709fb3
   part of the stack segment linkage control information is
Packit 709fb3
   0200 (octal) words.  This provides for register storage
Packit 709fb3
   for the routine which overflows the stack.  */
Packit 709fb3
Packit 709fb3
struct stack_segment_linkage
Packit 709fb3
  {
Packit 709fb3
    long ss[0200];              /* 0200 overflow words.  */
Packit 709fb3
    long sssize:32;             /* Number of words in this segment.  */
Packit 709fb3
    long ssbase:32;             /* Offset to stack base.  */
Packit 709fb3
    long:32;
Packit 709fb3
    long sspseg:32;             /* Offset to linkage control of previous
Packit 709fb3
                                   segment of stack.  */
Packit 709fb3
    long:32;
Packit 709fb3
    long sstcpt:32;             /* Pointer to task common address block.  */
Packit 709fb3
    long sscsnm;                /* Private control structure number for
Packit 709fb3
                                   microtasking.  */
Packit 709fb3
    long ssusr1;                /* Reserved for user.  */
Packit 709fb3
    long ssusr2;                /* Reserved for user.  */
Packit 709fb3
    long sstpid;                /* Process ID for pid based multi-tasking.  */
Packit 709fb3
    long ssgvup;                /* Pointer to multitasking thread giveup.  */
Packit 709fb3
    long sscray[7];             /* Reserved for Cray Research.  */
Packit 709fb3
    long ssa0;
Packit 709fb3
    long ssa1;
Packit 709fb3
    long ssa2;
Packit 709fb3
    long ssa3;
Packit 709fb3
    long ssa4;
Packit 709fb3
    long ssa5;
Packit 709fb3
    long ssa6;
Packit 709fb3
    long ssa7;
Packit 709fb3
    long sss0;
Packit 709fb3
    long sss1;
Packit 709fb3
    long sss2;
Packit 709fb3
    long sss3;
Packit 709fb3
    long sss4;
Packit 709fb3
    long sss5;
Packit 709fb3
    long sss6;
Packit 709fb3
    long sss7;
Packit 709fb3
  };
Packit 709fb3
Packit 709fb3
#    else /* CRAY2 */
Packit 709fb3
/* The following structure defines the vector of words
Packit 709fb3
   returned by the STKSTAT library routine.  */
Packit 709fb3
struct stk_stat
Packit 709fb3
  {
Packit 709fb3
    long now;                   /* Current total stack size.  */
Packit 709fb3
    long maxc;                  /* Amount of contiguous space which would
Packit 709fb3
                                   be required to satisfy the maximum
Packit 709fb3
                                   stack demand to date.  */
Packit 709fb3
    long high_water;            /* Stack high-water mark.  */
Packit 709fb3
    long overflows;             /* Number of stack overflow ($STKOFEN) calls.  */
Packit 709fb3
    long hits;                  /* Number of internal buffer hits.  */
Packit 709fb3
    long extends;               /* Number of block extensions.  */
Packit 709fb3
    long stko_mallocs;          /* Block allocations by $STKOFEN.  */
Packit 709fb3
    long underflows;            /* Number of stack underflow calls ($STKRETN).  */
Packit 709fb3
    long stko_free;             /* Number of deallocations by $STKRETN.  */
Packit 709fb3
    long stkm_free;             /* Number of deallocations by $STKMRET.  */
Packit 709fb3
    long segments;              /* Current number of stack segments.  */
Packit 709fb3
    long maxs;                  /* Maximum number of stack segments so far.  */
Packit 709fb3
    long pad_size;              /* Stack pad size.  */
Packit 709fb3
    long current_address;       /* Current stack segment address.  */
Packit 709fb3
    long current_size;          /* Current stack segment size.  This
Packit 709fb3
                                   number is actually corrupted by STKSTAT to
Packit 709fb3
                                   include the fifteen word trailer area.  */
Packit 709fb3
    long initial_address;       /* Address of initial segment.  */
Packit 709fb3
    long initial_size;          /* Size of initial segment.  */
Packit 709fb3
  };
Packit 709fb3
Packit 709fb3
/* The following structure describes the data structure which trails
Packit 709fb3
   any stack segment.  I think that the description in 'asdef' is
Packit 709fb3
   out of date.  I only describe the parts that I am sure about.  */
Packit 709fb3
Packit 709fb3
struct stk_trailer
Packit 709fb3
  {
Packit 709fb3
    long this_address;          /* Address of this block.  */
Packit 709fb3
    long this_size;             /* Size of this block (does not include
Packit 709fb3
                                   this trailer).  */
Packit 709fb3
    long unknown2;
Packit 709fb3
    long unknown3;
Packit 709fb3
    long link;                  /* Address of trailer block of previous
Packit 709fb3
                                   segment.  */
Packit 709fb3
    long unknown5;
Packit 709fb3
    long unknown6;
Packit 709fb3
    long unknown7;
Packit 709fb3
    long unknown8;
Packit 709fb3
    long unknown9;
Packit 709fb3
    long unknown10;
Packit 709fb3
    long unknown11;
Packit 709fb3
    long unknown12;
Packit 709fb3
    long unknown13;
Packit 709fb3
    long unknown14;
Packit 709fb3
  };
Packit 709fb3
Packit 709fb3
#    endif /* CRAY2 */
Packit 709fb3
#   endif /* not CRAY_STACK */
Packit 709fb3
Packit 709fb3
#   ifdef CRAY2
Packit 709fb3
/* Determine a "stack measure" for an arbitrary ADDRESS.
Packit 709fb3
   I doubt that "lint" will like this much.  */
Packit 709fb3
Packit 709fb3
static long
Packit 709fb3
i00afunc (long *address)
Packit 709fb3
{
Packit 709fb3
  struct stk_stat status;
Packit 709fb3
  struct stk_trailer *trailer;
Packit 709fb3
  long *block, size;
Packit 709fb3
  long result = 0;
Packit 709fb3
Packit 709fb3
  /* We want to iterate through all of the segments.  The first
Packit 709fb3
     step is to get the stack status structure.  We could do this
Packit 709fb3
     more quickly and more directly, perhaps, by referencing the
Packit 709fb3
     $LM00 common block, but I know that this works.  */
Packit 709fb3
Packit 709fb3
  STKSTAT (&status);
Packit 709fb3
Packit 709fb3
  /* Set up the iteration.  */
Packit 709fb3
Packit 709fb3
  trailer = (struct stk_trailer *) (status.current_address
Packit 709fb3
                                    + status.current_size
Packit 709fb3
                                    - 15);
Packit 709fb3
Packit 709fb3
  /* There must be at least one stack segment.  Therefore it is
Packit 709fb3
     a fatal error if "trailer" is null.  */
Packit 709fb3
Packit 709fb3
  if (trailer == 0)
Packit 709fb3
    abort ();
Packit 709fb3
Packit 709fb3
  /* Discard segments that do not contain our argument address.  */
Packit 709fb3
Packit 709fb3
  while (trailer != 0)
Packit 709fb3
    {
Packit 709fb3
      block = (long *) trailer->this_address;
Packit 709fb3
      size = trailer->this_size;
Packit 709fb3
      if (block == 0 || size == 0)
Packit 709fb3
        abort ();
Packit 709fb3
      trailer = (struct stk_trailer *) trailer->link;
Packit 709fb3
      if ((block <= address) && (address < (block + size)))
Packit 709fb3
        break;
Packit 709fb3
    }
Packit 709fb3
Packit 709fb3
  /* Set the result to the offset in this segment and add the sizes
Packit 709fb3
     of all predecessor segments.  */
Packit 709fb3
Packit 709fb3
  result = address - block;
Packit 709fb3
Packit 709fb3
  if (trailer == 0)
Packit 709fb3
    {
Packit 709fb3
      return result;
Packit 709fb3
    }
Packit 709fb3
Packit 709fb3
  do
Packit 709fb3
    {
Packit 709fb3
      if (trailer->this_size <= 0)
Packit 709fb3
        abort ();
Packit 709fb3
      result += trailer->this_size;
Packit 709fb3
      trailer = (struct stk_trailer *) trailer->link;
Packit 709fb3
    }
Packit 709fb3
  while (trailer != 0);
Packit 709fb3
Packit 709fb3
  /* We are done.  Note that if you present a bogus address (one
Packit 709fb3
     not in any segment), you will get a different number back, formed
Packit 709fb3
     from subtracting the address of the first block.  This is probably
Packit 709fb3
     not what you want.  */
Packit 709fb3
Packit 709fb3
  return (result);
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
#   else /* not CRAY2 */
Packit 709fb3
/* Stack address function for a CRAY-1, CRAY X-MP, or CRAY Y-MP.
Packit 709fb3
   Determine the number of the cell within the stack,
Packit 709fb3
   given the address of the cell.  The purpose of this
Packit 709fb3
   routine is to linearize, in some sense, stack addresses
Packit 709fb3
   for alloca.  */
Packit 709fb3
Packit 709fb3
static long
Packit 709fb3
i00afunc (long address)
Packit 709fb3
{
Packit 709fb3
  long stkl = 0;
Packit 709fb3
Packit 709fb3
  long size, pseg, this_segment, stack;
Packit 709fb3
  long result = 0;
Packit 709fb3
Packit 709fb3
  struct stack_segment_linkage *ssptr;
Packit 709fb3
Packit 709fb3
  /* Register B67 contains the address of the end of the
Packit 709fb3
     current stack segment.  If you (as a subprogram) store
Packit 709fb3
     your registers on the stack and find that you are past
Packit 709fb3
     the contents of B67, you have overflowed the segment.
Packit 709fb3
Packit 709fb3
     B67 also points to the stack segment linkage control
Packit 709fb3
     area, which is what we are really interested in.  */
Packit 709fb3
Packit 709fb3
  stkl = CRAY_STACKSEG_END ();
Packit 709fb3
  ssptr = (struct stack_segment_linkage *) stkl;
Packit 709fb3
Packit 709fb3
  /* If one subtracts 'size' from the end of the segment,
Packit 709fb3
     one has the address of the first word of the segment.
Packit 709fb3
Packit 709fb3
     If this is not the first segment, 'pseg' will be
Packit 709fb3
     nonzero.  */
Packit 709fb3
Packit 709fb3
  pseg = ssptr->sspseg;
Packit 709fb3
  size = ssptr->sssize;
Packit 709fb3
Packit 709fb3
  this_segment = stkl - size;
Packit 709fb3
Packit 709fb3
  /* It is possible that calling this routine itself caused
Packit 709fb3
     a stack overflow.  Discard stack segments which do not
Packit 709fb3
     contain the target address.  */
Packit 709fb3
Packit 709fb3
  while (!(this_segment <= address && address <= stkl))
Packit 709fb3
    {
Packit 709fb3
#    ifdef DEBUG_I00AFUNC
Packit 709fb3
      fprintf (stderr, "%011o %011o %011o\n", this_segment, address, stkl);
Packit 709fb3
#    endif
Packit 709fb3
      if (pseg == 0)
Packit 709fb3
        break;
Packit 709fb3
      stkl = stkl - pseg;
Packit 709fb3
      ssptr = (struct stack_segment_linkage *) stkl;
Packit 709fb3
      size = ssptr->sssize;
Packit 709fb3
      pseg = ssptr->sspseg;
Packit 709fb3
      this_segment = stkl - size;
Packit 709fb3
    }
Packit 709fb3
Packit 709fb3
  result = address - this_segment;
Packit 709fb3
Packit 709fb3
  /* If you subtract pseg from the current end of the stack,
Packit 709fb3
     you get the address of the previous stack segment's end.
Packit 709fb3
     This seems a little convoluted to me, but I'll bet you save
Packit 709fb3
     a cycle somewhere.  */
Packit 709fb3
Packit 709fb3
  while (pseg != 0)
Packit 709fb3
    {
Packit 709fb3
#    ifdef DEBUG_I00AFUNC
Packit 709fb3
      fprintf (stderr, "%011o %011o\n", pseg, size);
Packit 709fb3
#    endif
Packit 709fb3
      stkl = stkl - pseg;
Packit 709fb3
      ssptr = (struct stack_segment_linkage *) stkl;
Packit 709fb3
      size = ssptr->sssize;
Packit 709fb3
      pseg = ssptr->sspseg;
Packit 709fb3
      result += size;
Packit 709fb3
    }
Packit 709fb3
  return (result);
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
#   endif /* not CRAY2 */
Packit 709fb3
#  endif /* CRAY */
Packit 709fb3
Packit 709fb3
# endif /* no alloca */
Packit 709fb3
#endif /* not GCC 2 */