X-Git-Url: https://code.delx.au/gnu-emacs/blobdiff_plain/94f6013a0038c21c3ec13d13c07b328b159ef348..03da5d089a8ed035cec443a27259e7d21487a22e:/src/alloca.c diff --git a/src/alloca.c b/src/alloca.c index 282600340d..e8c8319adc 100644 --- a/src/alloca.c +++ b/src/alloca.c @@ -1,6 +1,9 @@ /* alloca.c -- allocate automatically reclaimed memory (Mostly) portable public-domain implementation -- D A Gwyn + NOTE: The canonical source of this file is maintained with gnulib. + Bugs can be reported to bug-gnulib@gnu.org. + This implementation of the PWB library alloca function, which is used to allocate space off the run-time stack so that it is automatically reclaimed upon procedure exit, @@ -22,7 +25,18 @@ your main control loop, etc. to force garbage collection. */ #ifdef HAVE_CONFIG_H -#include +# include +#endif + +#ifdef HAVE_STRING_H +# include +#endif +#ifdef HAVE_STDLIB_H +# include +#endif + +#ifdef DO_BLOCK_INPUT +# include "blockinput.h" #endif /* If compiling with GCC 2, this file's not needed. */ @@ -30,54 +44,63 @@ /* If someone has defined alloca as a macro, there must be some other way alloca is supposed to work. */ -#ifndef alloca +# ifndef alloca -#ifdef emacs -#ifdef static +# ifdef emacs +# ifdef static /* actually, only want this if static is defined as "" -- this is for usg, in which emacs must undefine static in order to make unexec workable */ -#ifndef STACK_DIRECTION +# ifndef STACK_DIRECTION you lose -- must know STACK_DIRECTION at compile-time -#endif /* STACK_DIRECTION undefined */ -#endif /* static */ -#endif /* emacs */ +/* Using #error here is not wise since this file should work for + old and obscure compilers. + + As far as I know, using it is OK if it's indented -- at least for + pcc-based processors. -- fx */ +# endif /* STACK_DIRECTION undefined */ +# endif /* static */ +# endif /* emacs */ /* If your stack is a linked list of frames, you have to provide an "address metric" ADDRESS_FUNCTION macro. */ -#if defined (CRAY) && defined (CRAY_STACKSEG_END) +# if defined (CRAY) && defined (CRAY_STACKSEG_END) long i00afunc (); -#define ADDRESS_FUNCTION(arg) (char *) i00afunc (&(arg)) -#else -#define ADDRESS_FUNCTION(arg) &(arg) -#endif - -#if __STDC__ -typedef void *pointer; -#else -typedef char *pointer; -#endif - -#define NULL 0 - -/* Different portions of Emacs need to call different versions of - malloc. The Emacs executable needs alloca to call xmalloc, because - ordinary malloc isn't protected from input signals. On the other - hand, the utilities in lib-src need alloca to call malloc; some of - them are very simple, and don't have an xmalloc routine. - - Non-Emacs programs expect this to call use xmalloc. +# define ADDRESS_FUNCTION(arg) (char *) i00afunc (&(arg)) +# else +# define ADDRESS_FUNCTION(arg) &(arg) +# endif + +# ifndef POINTER_TYPE +# ifdef __STDC__ +# define POINTER_TYPE void +# else +# define POINTER_TYPE char +# endif +# endif +typedef POINTER_TYPE *pointer; + +# ifndef NULL +# define NULL 0 +# endif + +/* The Emacs executable needs alloca to call xmalloc, because ordinary + malloc isn't protected from input signals. xmalloc also checks for + out-of-memory errors, so we should use it generally. Callers below should use malloc. */ -#ifndef emacs -#define malloc xmalloc -#endif -extern pointer malloc (); +# undef malloc +# define malloc xmalloc +# undef free +# define free xfree + +void *xmalloc _P ((size_t)); +void xfree _P ((void *)); /* Define STACK_DIRECTION if you know the direction of stack growth for your system; otherwise it will be automatically @@ -87,18 +110,18 @@ extern pointer malloc (); STACK_DIRECTION < 0 => grows toward lower addresses STACK_DIRECTION = 0 => direction of growth unknown */ -#ifndef STACK_DIRECTION -#define STACK_DIRECTION 0 /* Direction unknown. */ -#endif +# ifndef STACK_DIRECTION +# define STACK_DIRECTION 0 /* Direction unknown. */ +# endif -#if STACK_DIRECTION != 0 +# if STACK_DIRECTION != 0 -#define STACK_DIR STACK_DIRECTION /* Known at compile-time. */ +# define STACK_DIR STACK_DIRECTION /* Known at compile-time. */ -#else /* STACK_DIRECTION == 0; need run-time code. */ +# else /* STACK_DIRECTION == 0; need run-time code. */ static int stack_dir; /* 1 or -1 once known. */ -#define STACK_DIR stack_dir +# define STACK_DIR stack_dir static void find_stack_direction () @@ -122,7 +145,7 @@ find_stack_direction () } } -#endif /* STACK_DIRECTION == 0 */ +# endif /* STACK_DIRECTION == 0 */ /* An "alloca header" is used to: (a) chain together all alloca'ed blocks; @@ -131,9 +154,9 @@ find_stack_direction () It is very important that sizeof(header) agree with malloc alignment chunk size. The following default should work okay. */ -#ifndef ALIGN_SIZE -#define ALIGN_SIZE sizeof(double) -#endif +# ifndef ALIGN_SIZE +# define ALIGN_SIZE sizeof(double) +# endif typedef union hdr { @@ -156,22 +179,26 @@ static header *last_alloca_header = NULL; /* -> last alloca header. */ pointer alloca (size) - unsigned size; + size_t size; { auto char probe; /* Probes stack depth: */ register char *depth = ADDRESS_FUNCTION (probe); -#if STACK_DIRECTION == 0 +# if STACK_DIRECTION == 0 if (STACK_DIR == 0) /* Unknown growth direction. */ find_stack_direction (); -#endif +# endif /* Reclaim garbage, defined as all alloca'd storage that - was allocated from deeper in the stack than currently. */ + was allocated from deeper in the stack than currently. */ { register header *hp; /* Traverses linked list. */ +# ifdef DO_BLOCK_INPUT + BLOCK_INPUT; +# endif + for (hp = last_alloca_header; hp != NULL;) if ((STACK_DIR > 0 && hp->h.deep > depth) || (STACK_DIR < 0 && hp->h.deep < depth)) @@ -186,6 +213,10 @@ alloca (size) break; /* Rest are not deeper. */ last_alloca_header = hp; /* -> last valid storage. */ + +# ifdef DO_BLOCK_INPUT + UNBLOCK_INPUT; +# endif } if (size == 0) @@ -194,8 +225,11 @@ alloca (size) /* Allocate combined header + user data storage. */ { - register pointer new = malloc (sizeof (header) + size); /* Address of header. */ + register pointer new = malloc (sizeof (header) + size); + + if (new == 0) + abort(); ((header *) new)->h.next = last_alloca_header; ((header *) new)->h.deep = depth; @@ -208,15 +242,15 @@ alloca (size) } } -#if defined (CRAY) && defined (CRAY_STACKSEG_END) +# if defined (CRAY) && defined (CRAY_STACKSEG_END) -#ifdef DEBUG_I00AFUNC -#include -#endif +# ifdef DEBUG_I00AFUNC +# include +# endif -#ifndef CRAY_STACK -#define CRAY_STACK -#ifndef CRAY2 +# ifndef CRAY_STACK +# define CRAY_STACK +# ifndef CRAY2 /* Stack structures for CRAY-1, CRAY X-MP, and CRAY Y-MP */ struct stack_control_header { @@ -268,7 +302,7 @@ struct stack_segment_linkage long sss7; }; -#else /* CRAY2 */ +# else /* CRAY2 */ /* The following structure defines the vector of words returned by the STKSTAT library routine. */ struct stk_stat @@ -321,12 +355,12 @@ struct stk_trailer long unknown14; }; -#endif /* CRAY2 */ -#endif /* not CRAY_STACK */ +# endif /* CRAY2 */ +# endif /* not CRAY_STACK */ -#ifdef CRAY2 +# ifdef CRAY2 /* Determine a "stack measure" for an arbitrary ADDRESS. - I doubt that "lint" will like this much. */ + I doubt that "lint" will like this much. */ static long i00afunc (long *address) @@ -395,7 +429,7 @@ i00afunc (long *address) return (result); } -#else /* not CRAY2 */ +# else /* not CRAY2 */ /* Stack address function for a CRAY-1, CRAY X-MP, or CRAY Y-MP. Determine the number of the cell within the stack, given the address of the cell. The purpose of this @@ -440,9 +474,9 @@ i00afunc (long address) while (!(this_segment <= address && address <= stkl)) { -#ifdef DEBUG_I00AFUNC +# ifdef DEBUG_I00AFUNC fprintf (stderr, "%011o %011o %011o\n", this_segment, address, stkl); -#endif +# endif if (pseg == 0) break; stkl = stkl - pseg; @@ -461,9 +495,9 @@ i00afunc (long address) while (pseg != 0) { -#ifdef DEBUG_I00AFUNC +# ifdef DEBUG_I00AFUNC fprintf (stderr, "%011o %011o\n", pseg, size); -#endif +# endif stkl = stkl - pseg; ssptr = (struct stack_segment_linkage *) stkl; size = ssptr->sssize; @@ -473,8 +507,11 @@ i00afunc (long address) return (result); } -#endif /* not CRAY2 */ -#endif /* CRAY */ +# endif /* not CRAY2 */ +# endif /* CRAY */ -#endif /* no alloca */ +# endif /* no alloca */ #endif /* not GCC version 2 */ + +/* arch-tag: 5c9901c8-3cd4-453e-bd66-d9035a175ee3 + (do not change this comment) */