Blame gl/alloca.c

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