alloca.c revision 60484
133965Sjdp/* alloca.c -- allocate automatically reclaimed memory
233965Sjdp   (Mostly) portable public-domain implementation -- D A Gwyn
333965Sjdp
433965Sjdp   This implementation of the PWB library alloca function,
533965Sjdp   which is used to allocate space off the run-time stack so
633965Sjdp   that it is automatically reclaimed upon procedure exit,
733965Sjdp   was inspired by discussions with J. Q. Johnson of Cornell.
833965Sjdp   J.Otto Tennant <jot@cray.com> contributed the Cray support.
933965Sjdp
1033965Sjdp   There are some preprocessor constants that can
1133965Sjdp   be defined when compiling for your specific system, for
1233965Sjdp   improved efficiency; however, the defaults should be okay.
1333965Sjdp
1433965Sjdp   The general concept of this implementation is to keep
1533965Sjdp   track of all alloca-allocated blocks, and reclaim any
1633965Sjdp   that are found to be deeper in the stack than the current
1733965Sjdp   invocation.  This heuristic does not reclaim storage as
1833965Sjdp   soon as it becomes invalid, but it will do so eventually.
1933965Sjdp
2033965Sjdp   As a special case, alloca(0) reclaims storage without
2133965Sjdp   allocating any.  It is a good idea to use alloca(0) in
2233965Sjdp   your main control loop, etc. to force garbage collection.  */
2333965Sjdp
2433965Sjdp#ifdef HAVE_CONFIG_H
2560484Sobrien#include <config.h>
2633965Sjdp#endif
2733965Sjdp
2860484Sobrien#ifdef HAVE_STRING_H
2960484Sobrien#include <string.h>
3060484Sobrien#endif
3160484Sobrien#ifdef HAVE_STDLIB_H
3260484Sobrien#include <stdlib.h>
3360484Sobrien#endif
3460484Sobrien
3560484Sobrien#ifdef emacs
3660484Sobrien#include "blockinput.h"
3760484Sobrien#endif
3860484Sobrien
3960484Sobrien/* If compiling with GCC 2, this file's not needed.  Except of course if
4060484Sobrien   the C alloca is explicitly requested.  */
4160484Sobrien#if defined (USE_C_ALLOCA) || !defined (__GNUC__) || __GNUC__ < 2
4260484Sobrien
4360484Sobrien/* If someone has defined alloca as a macro,
4460484Sobrien   there must be some other way alloca is supposed to work.  */
4533965Sjdp#ifndef alloca
4633965Sjdp
4733965Sjdp#ifdef emacs
4833965Sjdp#ifdef static
4933965Sjdp/* actually, only want this if static is defined as ""
5033965Sjdp   -- this is for usg, in which emacs must undefine static
5133965Sjdp   in order to make unexec workable
5233965Sjdp   */
5333965Sjdp#ifndef STACK_DIRECTION
5433965Sjdpyou
5533965Sjdplose
5633965Sjdp-- must know STACK_DIRECTION at compile-time
5733965Sjdp#endif /* STACK_DIRECTION undefined */
5833965Sjdp#endif /* static */
5933965Sjdp#endif /* emacs */
6033965Sjdp
6133965Sjdp/* If your stack is a linked list of frames, you have to
6233965Sjdp   provide an "address metric" ADDRESS_FUNCTION macro.  */
6333965Sjdp
6433965Sjdp#if defined (CRAY) && defined (CRAY_STACKSEG_END)
6533965Sjdplong i00afunc ();
6633965Sjdp#define ADDRESS_FUNCTION(arg) (char *) i00afunc (&(arg))
6733965Sjdp#else
6833965Sjdp#define ADDRESS_FUNCTION(arg) &(arg)
6933965Sjdp#endif
7033965Sjdp
7133965Sjdp#if __STDC__
7233965Sjdptypedef void *pointer;
7333965Sjdp#else
7433965Sjdptypedef char *pointer;
7533965Sjdp#endif
7633965Sjdp
7733965Sjdp#ifndef NULL
7833965Sjdp#define	NULL	0
7933965Sjdp#endif
8033965Sjdp
8133965Sjdp/* Different portions of Emacs need to call different versions of
8233965Sjdp   malloc.  The Emacs executable needs alloca to call xmalloc, because
8333965Sjdp   ordinary malloc isn't protected from input signals.  On the other
8433965Sjdp   hand, the utilities in lib-src need alloca to call malloc; some of
8533965Sjdp   them are very simple, and don't have an xmalloc routine.
8633965Sjdp
8733965Sjdp   Non-Emacs programs expect this to call use xmalloc.
8833965Sjdp
8933965Sjdp   Callers below should use malloc.  */
9033965Sjdp
9133965Sjdp#ifndef emacs
9233965Sjdp#define malloc xmalloc
9333965Sjdp#endif
9460484Sobrienextern pointer malloc ();
9533965Sjdp
9633965Sjdp/* Define STACK_DIRECTION if you know the direction of stack
9733965Sjdp   growth for your system; otherwise it will be automatically
9833965Sjdp   deduced at run-time.
9933965Sjdp
10033965Sjdp   STACK_DIRECTION > 0 => grows toward higher addresses
10133965Sjdp   STACK_DIRECTION < 0 => grows toward lower addresses
10233965Sjdp   STACK_DIRECTION = 0 => direction of growth unknown  */
10333965Sjdp
10433965Sjdp#ifndef STACK_DIRECTION
10533965Sjdp#define	STACK_DIRECTION	0	/* Direction unknown.  */
10633965Sjdp#endif
10733965Sjdp
10833965Sjdp#if STACK_DIRECTION != 0
10933965Sjdp
11033965Sjdp#define	STACK_DIR	STACK_DIRECTION	/* Known at compile-time.  */
11133965Sjdp
11233965Sjdp#else /* STACK_DIRECTION == 0; need run-time code.  */
11333965Sjdp
11433965Sjdpstatic int stack_dir;		/* 1 or -1 once known.  */
11533965Sjdp#define	STACK_DIR	stack_dir
11633965Sjdp
11733965Sjdpstatic void
11833965Sjdpfind_stack_direction ()
11933965Sjdp{
12033965Sjdp  static char *addr = NULL;	/* Address of first `dummy', once known.  */
12133965Sjdp  auto char dummy;		/* To get stack address.  */
12233965Sjdp
12333965Sjdp  if (addr == NULL)
12433965Sjdp    {				/* Initial entry.  */
12533965Sjdp      addr = ADDRESS_FUNCTION (dummy);
12633965Sjdp
12733965Sjdp      find_stack_direction ();	/* Recurse once.  */
12833965Sjdp    }
12933965Sjdp  else
13033965Sjdp    {
13133965Sjdp      /* Second entry.  */
13233965Sjdp      if (ADDRESS_FUNCTION (dummy) > addr)
13333965Sjdp	stack_dir = 1;		/* Stack grew upward.  */
13433965Sjdp      else
13533965Sjdp	stack_dir = -1;		/* Stack grew downward.  */
13633965Sjdp    }
13733965Sjdp}
13833965Sjdp
13933965Sjdp#endif /* STACK_DIRECTION == 0 */
14033965Sjdp
14133965Sjdp/* An "alloca header" is used to:
14233965Sjdp   (a) chain together all alloca'ed blocks;
14333965Sjdp   (b) keep track of stack depth.
14433965Sjdp
14533965Sjdp   It is very important that sizeof(header) agree with malloc
14633965Sjdp   alignment chunk size.  The following default should work okay.  */
14733965Sjdp
14833965Sjdp#ifndef	ALIGN_SIZE
14933965Sjdp#define	ALIGN_SIZE	sizeof(double)
15033965Sjdp#endif
15133965Sjdp
15233965Sjdptypedef union hdr
15333965Sjdp{
15433965Sjdp  char align[ALIGN_SIZE];	/* To force sizeof(header).  */
15533965Sjdp  struct
15633965Sjdp    {
15733965Sjdp      union hdr *next;		/* For chaining headers.  */
15833965Sjdp      char *deep;		/* For stack depth measure.  */
15933965Sjdp    } h;
16033965Sjdp} header;
16133965Sjdp
16233965Sjdpstatic header *last_alloca_header = NULL;	/* -> last alloca header.  */
16333965Sjdp
16433965Sjdp/* Return a pointer to at least SIZE bytes of storage,
16533965Sjdp   which will be automatically reclaimed upon exit from
16633965Sjdp   the procedure that called alloca.  Originally, this space
16733965Sjdp   was supposed to be taken from the current stack frame of the
16833965Sjdp   caller, but that method cannot be made to work for some
16933965Sjdp   implementations of C, for example under Gould's UTX/32.  */
17033965Sjdp
17133965Sjdppointer
17233965Sjdpalloca (size)
17360484Sobrien     unsigned size;
17433965Sjdp{
17533965Sjdp  auto char probe;		/* Probes stack depth: */
17633965Sjdp  register char *depth = ADDRESS_FUNCTION (probe);
17733965Sjdp
17833965Sjdp#if STACK_DIRECTION == 0
17933965Sjdp  if (STACK_DIR == 0)		/* Unknown growth direction.  */
18033965Sjdp    find_stack_direction ();
18133965Sjdp#endif
18233965Sjdp
18333965Sjdp  /* Reclaim garbage, defined as all alloca'd storage that
18460484Sobrien     was allocated from deeper in the stack than currently.  */
18533965Sjdp
18633965Sjdp  {
18733965Sjdp    register header *hp;	/* Traverses linked list.  */
18833965Sjdp
18960484Sobrien#ifdef emacs
19060484Sobrien    BLOCK_INPUT;
19160484Sobrien#endif
19260484Sobrien
19333965Sjdp    for (hp = last_alloca_header; hp != NULL;)
19433965Sjdp      if ((STACK_DIR > 0 && hp->h.deep > depth)
19533965Sjdp	  || (STACK_DIR < 0 && hp->h.deep < depth))
19633965Sjdp	{
19733965Sjdp	  register header *np = hp->h.next;
19833965Sjdp
19933965Sjdp	  free ((pointer) hp);	/* Collect garbage.  */
20033965Sjdp
20133965Sjdp	  hp = np;		/* -> next header.  */
20233965Sjdp	}
20333965Sjdp      else
20433965Sjdp	break;			/* Rest are not deeper.  */
20533965Sjdp
20633965Sjdp    last_alloca_header = hp;	/* -> last valid storage.  */
20760484Sobrien
20860484Sobrien#ifdef emacs
20960484Sobrien    UNBLOCK_INPUT;
21060484Sobrien#endif
21133965Sjdp  }
21233965Sjdp
21333965Sjdp  if (size == 0)
21433965Sjdp    return NULL;		/* No allocation required.  */
21533965Sjdp
21633965Sjdp  /* Allocate combined header + user data storage.  */
21733965Sjdp
21833965Sjdp  {
21933965Sjdp    register pointer new = malloc (sizeof (header) + size);
22033965Sjdp    /* Address of header.  */
22133965Sjdp
22260484Sobrien    if (new == 0)
22360484Sobrien      abort();
22460484Sobrien
22533965Sjdp    ((header *) new)->h.next = last_alloca_header;
22633965Sjdp    ((header *) new)->h.deep = depth;
22733965Sjdp
22833965Sjdp    last_alloca_header = (header *) new;
22933965Sjdp
23033965Sjdp    /* User storage begins just after header.  */
23133965Sjdp
23233965Sjdp    return (pointer) ((char *) new + sizeof (header));
23333965Sjdp  }
23433965Sjdp}
23533965Sjdp
23633965Sjdp#if defined (CRAY) && defined (CRAY_STACKSEG_END)
23733965Sjdp
23833965Sjdp#ifdef DEBUG_I00AFUNC
23933965Sjdp#include <stdio.h>
24033965Sjdp#endif
24133965Sjdp
24233965Sjdp#ifndef CRAY_STACK
24333965Sjdp#define CRAY_STACK
24433965Sjdp#ifndef CRAY2
24533965Sjdp/* Stack structures for CRAY-1, CRAY X-MP, and CRAY Y-MP */
24633965Sjdpstruct stack_control_header
24733965Sjdp  {
24833965Sjdp    long shgrow:32;		/* Number of times stack has grown.  */
24933965Sjdp    long shaseg:32;		/* Size of increments to stack.  */
25033965Sjdp    long shhwm:32;		/* High water mark of stack.  */
25133965Sjdp    long shsize:32;		/* Current size of stack (all segments).  */
25233965Sjdp  };
25333965Sjdp
25433965Sjdp/* The stack segment linkage control information occurs at
25533965Sjdp   the high-address end of a stack segment.  (The stack
25633965Sjdp   grows from low addresses to high addresses.)  The initial
25733965Sjdp   part of the stack segment linkage control information is
25833965Sjdp   0200 (octal) words.  This provides for register storage
25933965Sjdp   for the routine which overflows the stack.  */
26033965Sjdp
26133965Sjdpstruct stack_segment_linkage
26233965Sjdp  {
26333965Sjdp    long ss[0200];		/* 0200 overflow words.  */
26433965Sjdp    long sssize:32;		/* Number of words in this segment.  */
26533965Sjdp    long ssbase:32;		/* Offset to stack base.  */
26633965Sjdp    long:32;
26733965Sjdp    long sspseg:32;		/* Offset to linkage control of previous
26833965Sjdp				   segment of stack.  */
26933965Sjdp    long:32;
27033965Sjdp    long sstcpt:32;		/* Pointer to task common address block.  */
27133965Sjdp    long sscsnm;		/* Private control structure number for
27233965Sjdp				   microtasking.  */
27333965Sjdp    long ssusr1;		/* Reserved for user.  */
27433965Sjdp    long ssusr2;		/* Reserved for user.  */
27533965Sjdp    long sstpid;		/* Process ID for pid based multi-tasking.  */
27633965Sjdp    long ssgvup;		/* Pointer to multitasking thread giveup.  */
27733965Sjdp    long sscray[7];		/* Reserved for Cray Research.  */
27833965Sjdp    long ssa0;
27933965Sjdp    long ssa1;
28033965Sjdp    long ssa2;
28133965Sjdp    long ssa3;
28233965Sjdp    long ssa4;
28333965Sjdp    long ssa5;
28433965Sjdp    long ssa6;
28533965Sjdp    long ssa7;
28633965Sjdp    long sss0;
28733965Sjdp    long sss1;
28833965Sjdp    long sss2;
28933965Sjdp    long sss3;
29033965Sjdp    long sss4;
29133965Sjdp    long sss5;
29233965Sjdp    long sss6;
29333965Sjdp    long sss7;
29433965Sjdp  };
29533965Sjdp
29633965Sjdp#else /* CRAY2 */
29733965Sjdp/* The following structure defines the vector of words
29833965Sjdp   returned by the STKSTAT library routine.  */
29933965Sjdpstruct stk_stat
30033965Sjdp  {
30133965Sjdp    long now;			/* Current total stack size.  */
30233965Sjdp    long maxc;			/* Amount of contiguous space which would
30333965Sjdp				   be required to satisfy the maximum
30433965Sjdp				   stack demand to date.  */
30533965Sjdp    long high_water;		/* Stack high-water mark.  */
30633965Sjdp    long overflows;		/* Number of stack overflow ($STKOFEN) calls.  */
30733965Sjdp    long hits;			/* Number of internal buffer hits.  */
30833965Sjdp    long extends;		/* Number of block extensions.  */
30933965Sjdp    long stko_mallocs;		/* Block allocations by $STKOFEN.  */
31033965Sjdp    long underflows;		/* Number of stack underflow calls ($STKRETN).  */
31133965Sjdp    long stko_free;		/* Number of deallocations by $STKRETN.  */
31233965Sjdp    long stkm_free;		/* Number of deallocations by $STKMRET.  */
31333965Sjdp    long segments;		/* Current number of stack segments.  */
31433965Sjdp    long maxs;			/* Maximum number of stack segments so far.  */
31533965Sjdp    long pad_size;		/* Stack pad size.  */
31633965Sjdp    long current_address;	/* Current stack segment address.  */
31733965Sjdp    long current_size;		/* Current stack segment size.  This
31833965Sjdp				   number is actually corrupted by STKSTAT to
31933965Sjdp				   include the fifteen word trailer area.  */
32033965Sjdp    long initial_address;	/* Address of initial segment.  */
32133965Sjdp    long initial_size;		/* Size of initial segment.  */
32233965Sjdp  };
32333965Sjdp
32433965Sjdp/* The following structure describes the data structure which trails
32533965Sjdp   any stack segment.  I think that the description in 'asdef' is
32633965Sjdp   out of date.  I only describe the parts that I am sure about.  */
32733965Sjdp
32833965Sjdpstruct stk_trailer
32933965Sjdp  {
33033965Sjdp    long this_address;		/* Address of this block.  */
33133965Sjdp    long this_size;		/* Size of this block (does not include
33233965Sjdp				   this trailer).  */
33333965Sjdp    long unknown2;
33433965Sjdp    long unknown3;
33533965Sjdp    long link;			/* Address of trailer block of previous
33633965Sjdp				   segment.  */
33733965Sjdp    long unknown5;
33833965Sjdp    long unknown6;
33933965Sjdp    long unknown7;
34033965Sjdp    long unknown8;
34133965Sjdp    long unknown9;
34233965Sjdp    long unknown10;
34333965Sjdp    long unknown11;
34433965Sjdp    long unknown12;
34533965Sjdp    long unknown13;
34633965Sjdp    long unknown14;
34733965Sjdp  };
34833965Sjdp
34933965Sjdp#endif /* CRAY2 */
35033965Sjdp#endif /* not CRAY_STACK */
35133965Sjdp
35233965Sjdp#ifdef CRAY2
35333965Sjdp/* Determine a "stack measure" for an arbitrary ADDRESS.
35460484Sobrien   I doubt that "lint" will like this much.  */
35533965Sjdp
35633965Sjdpstatic long
35733965Sjdpi00afunc (long *address)
35833965Sjdp{
35933965Sjdp  struct stk_stat status;
36033965Sjdp  struct stk_trailer *trailer;
36133965Sjdp  long *block, size;
36233965Sjdp  long result = 0;
36333965Sjdp
36433965Sjdp  /* We want to iterate through all of the segments.  The first
36533965Sjdp     step is to get the stack status structure.  We could do this
36633965Sjdp     more quickly and more directly, perhaps, by referencing the
36733965Sjdp     $LM00 common block, but I know that this works.  */
36833965Sjdp
36933965Sjdp  STKSTAT (&status);
37033965Sjdp
37133965Sjdp  /* Set up the iteration.  */
37233965Sjdp
37333965Sjdp  trailer = (struct stk_trailer *) (status.current_address
37433965Sjdp				    + status.current_size
37533965Sjdp				    - 15);
37633965Sjdp
37733965Sjdp  /* There must be at least one stack segment.  Therefore it is
37833965Sjdp     a fatal error if "trailer" is null.  */
37933965Sjdp
38033965Sjdp  if (trailer == 0)
38133965Sjdp    abort ();
38233965Sjdp
38333965Sjdp  /* Discard segments that do not contain our argument address.  */
38433965Sjdp
38533965Sjdp  while (trailer != 0)
38633965Sjdp    {
38733965Sjdp      block = (long *) trailer->this_address;
38833965Sjdp      size = trailer->this_size;
38933965Sjdp      if (block == 0 || size == 0)
39033965Sjdp	abort ();
39133965Sjdp      trailer = (struct stk_trailer *) trailer->link;
39233965Sjdp      if ((block <= address) && (address < (block + size)))
39333965Sjdp	break;
39433965Sjdp    }
39533965Sjdp
39633965Sjdp  /* Set the result to the offset in this segment and add the sizes
39733965Sjdp     of all predecessor segments.  */
39833965Sjdp
39933965Sjdp  result = address - block;
40033965Sjdp
40133965Sjdp  if (trailer == 0)
40233965Sjdp    {
40333965Sjdp      return result;
40433965Sjdp    }
40533965Sjdp
40633965Sjdp  do
40733965Sjdp    {
40833965Sjdp      if (trailer->this_size <= 0)
40933965Sjdp	abort ();
41033965Sjdp      result += trailer->this_size;
41133965Sjdp      trailer = (struct stk_trailer *) trailer->link;
41233965Sjdp    }
41333965Sjdp  while (trailer != 0);
41433965Sjdp
41533965Sjdp  /* We are done.  Note that if you present a bogus address (one
41633965Sjdp     not in any segment), you will get a different number back, formed
41733965Sjdp     from subtracting the address of the first block.  This is probably
41833965Sjdp     not what you want.  */
41933965Sjdp
42033965Sjdp  return (result);
42133965Sjdp}
42233965Sjdp
42333965Sjdp#else /* not CRAY2 */
42433965Sjdp/* Stack address function for a CRAY-1, CRAY X-MP, or CRAY Y-MP.
42533965Sjdp   Determine the number of the cell within the stack,
42633965Sjdp   given the address of the cell.  The purpose of this
42733965Sjdp   routine is to linearize, in some sense, stack addresses
42833965Sjdp   for alloca.  */
42933965Sjdp
43033965Sjdpstatic long
43133965Sjdpi00afunc (long address)
43233965Sjdp{
43333965Sjdp  long stkl = 0;
43433965Sjdp
43533965Sjdp  long size, pseg, this_segment, stack;
43633965Sjdp  long result = 0;
43733965Sjdp
43833965Sjdp  struct stack_segment_linkage *ssptr;
43933965Sjdp
44033965Sjdp  /* Register B67 contains the address of the end of the
44133965Sjdp     current stack segment.  If you (as a subprogram) store
44233965Sjdp     your registers on the stack and find that you are past
44333965Sjdp     the contents of B67, you have overflowed the segment.
44433965Sjdp
44533965Sjdp     B67 also points to the stack segment linkage control
44633965Sjdp     area, which is what we are really interested in.  */
44733965Sjdp
44833965Sjdp  stkl = CRAY_STACKSEG_END ();
44933965Sjdp  ssptr = (struct stack_segment_linkage *) stkl;
45033965Sjdp
45133965Sjdp  /* If one subtracts 'size' from the end of the segment,
45233965Sjdp     one has the address of the first word of the segment.
45333965Sjdp
45433965Sjdp     If this is not the first segment, 'pseg' will be
45533965Sjdp     nonzero.  */
45633965Sjdp
45733965Sjdp  pseg = ssptr->sspseg;
45833965Sjdp  size = ssptr->sssize;
45933965Sjdp
46033965Sjdp  this_segment = stkl - size;
46133965Sjdp
46233965Sjdp  /* It is possible that calling this routine itself caused
46333965Sjdp     a stack overflow.  Discard stack segments which do not
46433965Sjdp     contain the target address.  */
46533965Sjdp
46633965Sjdp  while (!(this_segment <= address && address <= stkl))
46733965Sjdp    {
46833965Sjdp#ifdef DEBUG_I00AFUNC
46933965Sjdp      fprintf (stderr, "%011o %011o %011o\n", this_segment, address, stkl);
47033965Sjdp#endif
47133965Sjdp      if (pseg == 0)
47233965Sjdp	break;
47333965Sjdp      stkl = stkl - pseg;
47433965Sjdp      ssptr = (struct stack_segment_linkage *) stkl;
47533965Sjdp      size = ssptr->sssize;
47633965Sjdp      pseg = ssptr->sspseg;
47733965Sjdp      this_segment = stkl - size;
47833965Sjdp    }
47933965Sjdp
48033965Sjdp  result = address - this_segment;
48133965Sjdp
48233965Sjdp  /* If you subtract pseg from the current end of the stack,
48333965Sjdp     you get the address of the previous stack segment's end.
48433965Sjdp     This seems a little convoluted to me, but I'll bet you save
48533965Sjdp     a cycle somewhere.  */
48633965Sjdp
48733965Sjdp  while (pseg != 0)
48833965Sjdp    {
48933965Sjdp#ifdef DEBUG_I00AFUNC
49033965Sjdp      fprintf (stderr, "%011o %011o\n", pseg, size);
49133965Sjdp#endif
49233965Sjdp      stkl = stkl - pseg;
49333965Sjdp      ssptr = (struct stack_segment_linkage *) stkl;
49433965Sjdp      size = ssptr->sssize;
49533965Sjdp      pseg = ssptr->sspseg;
49633965Sjdp      result += size;
49733965Sjdp    }
49833965Sjdp  return (result);
49933965Sjdp}
50033965Sjdp
50133965Sjdp#endif /* not CRAY2 */
50233965Sjdp#endif /* CRAY */
50333965Sjdp
50433965Sjdp#endif /* no alloca */
50560484Sobrien#endif /* not GCC version 2 */
506