/[gnats]/gnats/libiberty/alloca.c
ViewVC logotype

Diff of /gnats/libiberty/alloca.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.2 by jsm, Tue Oct 26 07:10:16 1999 UTC revision 1.3 by chewie, Sat Nov 13 05:14:17 2004 UTC
# Line 21  Line 21 
21     allocating any.  It is a good idea to use alloca(0) in     allocating any.  It is a good idea to use alloca(0) in
22     your main control loop, etc. to force garbage collection.  */     your main control loop, etc. to force garbage collection.  */
23    
24    /*
25    
26    @deftypefn Replacement void* alloca (size_t @var{size})
27    
28    This function allocates memory which will be automatically reclaimed
29    after the procedure exits.  The @libib{} implementation does not free
30    the memory immediately but will do so eventually during subsequent
31    calls to this function.  Memory is allocated using @code{xmalloc} under
32    normal circumstances.
33    
34    The header file @file{alloca-conf.h} can be used in conjunction with the
35    GNU Autoconf test @code{AC_FUNC_ALLOCA} to test for and properly make
36    available this function.  The @code{AC_FUNC_ALLOCA} test requires that
37    client code use a block of preprocessor code to be safe (see the Autoconf
38    manual for more); this header incorporates that logic and more, including
39    the possibility of a GCC built-in function.
40    
41    @end deftypefn
42    
43    */
44    
45  #ifdef HAVE_CONFIG_H  #ifdef HAVE_CONFIG_H
46  #include <config.h>  #include <config.h>
47  #endif  #endif
48    
49    #include <libiberty.h>
50    
51  #ifdef HAVE_STRING_H  #ifdef HAVE_STRING_H
52  #include <string.h>  #include <string.h>
53  #endif  #endif
# Line 32  Line 55 
55  #include <stdlib.h>  #include <stdlib.h>
56  #endif  #endif
57    
58  #ifdef emacs  /* These variables are used by the ASTRDUP implementation that relies
59  #include "blockinput.h"     on C_alloca.  */
60  #endif  const char *libiberty_optr;
61    char *libiberty_nptr;
62  /* If compiling with GCC 2, this file's not needed.  Except of course if  unsigned long libiberty_len;
    the C alloca is explicitly requested.  */  
 #if defined (USE_C_ALLOCA) || !defined (__GNUC__) || __GNUC__ < 2  
   
 /* If someone has defined alloca as a macro,  
    there must be some other way alloca is supposed to work.  */  
 #ifndef alloca  
   
 #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  
 you  
 lose  
 -- must know STACK_DIRECTION at compile-time  
 #endif /* STACK_DIRECTION undefined */  
 #endif /* static */  
 #endif /* emacs */  
63    
64  /* If your stack is a linked list of frames, you have to  /* If your stack is a linked list of frames, you have to
65     provide an "address metric" ADDRESS_FUNCTION macro.  */     provide an "address metric" ADDRESS_FUNCTION macro.  */
66    
67  #if defined (CRAY) && defined (CRAY_STACKSEG_END)  #if defined (CRAY) && defined (CRAY_STACKSEG_END)
68  long i00afunc ();  static long i00afunc ();
69  #define ADDRESS_FUNCTION(arg) (char *) i00afunc (&(arg))  #define ADDRESS_FUNCTION(arg) (char *) i00afunc (&(arg))
70  #else  #else
71  #define ADDRESS_FUNCTION(arg) &(arg)  #define ADDRESS_FUNCTION(arg) &(arg)
72  #endif  #endif
73    
 #if __STDC__  
 typedef void *pointer;  
 #else  
 typedef char *pointer;  
 #endif  
   
74  #ifndef NULL  #ifndef NULL
75  #define NULL    0  #define NULL    0
76  #endif  #endif
77    
 /* 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.  
   
    Callers below should use malloc.  */  
   
 #ifndef emacs  
 #define malloc xmalloc  
 #endif  
 extern pointer malloc ();  
   
78  /* Define STACK_DIRECTION if you know the direction of stack  /* Define STACK_DIRECTION if you know the direction of stack
79     growth for your system; otherwise it will be automatically     growth for your system; otherwise it will be automatically
80     deduced at run-time.     deduced at run-time.
# Line 168  static header *last_alloca_header = NULL Line 150  static header *last_alloca_header = NULL
150     caller, but that method cannot be made to work for some     caller, but that method cannot be made to work for some
151     implementations of C, for example under Gould's UTX/32.  */     implementations of C, for example under Gould's UTX/32.  */
152    
153  pointer  /* @undocumented C_alloca */
154  alloca (size)  
155       unsigned size;  PTR
156    C_alloca (size)
157         size_t size;
158  {  {
159    auto char probe;              /* Probes stack depth: */    auto char probe;              /* Probes stack depth: */
160    register char *depth = ADDRESS_FUNCTION (probe);    register char *depth = ADDRESS_FUNCTION (probe);
# Line 186  alloca (size) Line 170  alloca (size)
170    {    {
171      register header *hp;        /* Traverses linked list.  */      register header *hp;        /* Traverses linked list.  */
172    
 #ifdef emacs  
     BLOCK_INPUT;  
 #endif  
   
173      for (hp = last_alloca_header; hp != NULL;)      for (hp = last_alloca_header; hp != NULL;)
174        if ((STACK_DIR > 0 && hp->h.deep > depth)        if ((STACK_DIR > 0 && hp->h.deep > depth)
175            || (STACK_DIR < 0 && hp->h.deep < depth))            || (STACK_DIR < 0 && hp->h.deep < depth))
176          {          {
177            register header *np = hp->h.next;            register header *np = hp->h.next;
178    
179            free ((pointer) hp);  /* Collect garbage.  */            free ((PTR) hp);      /* Collect garbage.  */
180    
181            hp = np;              /* -> next header.  */            hp = np;              /* -> next header.  */
182          }          }
# Line 204  alloca (size) Line 184  alloca (size)
184          break;                  /* Rest are not deeper.  */          break;                  /* Rest are not deeper.  */
185    
186      last_alloca_header = hp;    /* -> last valid storage.  */      last_alloca_header = hp;    /* -> last valid storage.  */
   
 #ifdef emacs  
     UNBLOCK_INPUT;  
 #endif  
187    }    }
188    
189    if (size == 0)    if (size == 0)
# Line 216  alloca (size) Line 192  alloca (size)
192    /* Allocate combined header + user data storage.  */    /* Allocate combined header + user data storage.  */
193    
194    {    {
195      register pointer new = malloc (sizeof (header) + size);      register PTR new = xmalloc (sizeof (header) + size);
196      /* Address of header.  */      /* Address of header.  */
197    
198      if (new == 0)      if (new == 0)
# Line 229  alloca (size) Line 205  alloca (size)
205    
206      /* User storage begins just after header.  */      /* User storage begins just after header.  */
207    
208      return (pointer) ((char *) new + sizeof (header));      return (PTR) ((char *) new + sizeof (header));
209    }    }
210  }  }
211    
# Line 500  i00afunc (long address) Line 476  i00afunc (long address)
476    
477  #endif /* not CRAY2 */  #endif /* not CRAY2 */
478  #endif /* CRAY */  #endif /* CRAY */
   
 #endif /* no alloca */  
 #endif /* not GCC version 2 */  

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.3

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26