source: trunk/binutils/libiberty/alloca.c@ 2446

Last change on this file since 2446 was 610, checked in by bird, 22 years ago

This commit was generated by cvs2svn to compensate for changes in r609,
which included commits to RCS files with non-trunk default branches.

  • Property cvs2svn:cvs-rev set to 1.1.1.2
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 13.6 KB
Line 
1/* alloca.c -- allocate automatically reclaimed memory
2 (Mostly) portable public-domain implementation -- D A Gwyn
3
4 This implementation of the PWB library alloca function,
5 which is used to allocate space off the run-time stack so
6 that it is automatically reclaimed upon procedure exit,
7 was inspired by discussions with J. Q. Johnson of Cornell.
8 J.Otto Tennant <[email protected]> contributed the Cray support.
9
10 There are some preprocessor constants that can
11 be defined when compiling for your specific system, for
12 improved efficiency; however, the defaults should be okay.
13
14 The general concept of this implementation is to keep
15 track of all alloca-allocated blocks, and reclaim any
16 that are found to be deeper in the stack than the current
17 invocation. This heuristic does not reclaim storage as
18 soon as it becomes invalid, but it will do so eventually.
19
20 As a special case, alloca(0) reclaims storage without
21 allocating any. It is a good idea to use alloca(0) in
22 your main control loop, etc. to force garbage collection. */
23
24/*
25
26@deftypefn Replacement void* alloca (size_t @var{size})
27
28This function allocates memory which will be automatically reclaimed
29after the procedure exits. The @libib{} implementation does not free
30the memory immediately but will do so eventually during subsequent
31calls to this function. Memory is allocated using @code{xmalloc} under
32normal circumstances.
33
34The header file @file{alloca-conf.h} can be used in conjunction with the
35GNU Autoconf test @code{AC_FUNC_ALLOCA} to test for and properly make
36available this function. The @code{AC_FUNC_ALLOCA} test requires that
37client code use a block of preprocessor code to be safe (see the Autoconf
38manual for more); this header incorporates that logic and more, including
39the possibility of a GCC built-in function.
40
41@end deftypefn
42
43*/
44
45#ifdef HAVE_CONFIG_H
46#include <config.h>
47#endif
48
49#include <libiberty.h>
50
51#ifdef HAVE_STRING_H
52#include <string.h>
53#endif
54#ifdef HAVE_STDLIB_H
55#include <stdlib.h>
56#endif
57
58/* These variables are used by the ASTRDUP implementation that relies
59 on C_alloca. */
60const char *libiberty_optr;
61char *libiberty_nptr;
62unsigned long libiberty_len;
63
64/* If your stack is a linked list of frames, you have to
65 provide an "address metric" ADDRESS_FUNCTION macro. */
66
67#if defined (CRAY) && defined (CRAY_STACKSEG_END)
68static long i00afunc ();
69#define ADDRESS_FUNCTION(arg) (char *) i00afunc (&(arg))
70#else
71#define ADDRESS_FUNCTION(arg) &(arg)
72#endif
73
74#ifndef NULL
75#define NULL 0
76#endif
77
78/* Define STACK_DIRECTION if you know the direction of stack
79 growth for your system; otherwise it will be automatically
80 deduced at run-time.
81
82 STACK_DIRECTION > 0 => grows toward higher addresses
83 STACK_DIRECTION < 0 => grows toward lower addresses
84 STACK_DIRECTION = 0 => direction of growth unknown */
85
86#ifndef STACK_DIRECTION
87#define STACK_DIRECTION 0 /* Direction unknown. */
88#endif
89
90#if STACK_DIRECTION != 0
91
92#define STACK_DIR STACK_DIRECTION /* Known at compile-time. */
93
94#else /* STACK_DIRECTION == 0; need run-time code. */
95
96static int stack_dir; /* 1 or -1 once known. */
97#define STACK_DIR stack_dir
98
99static void
100find_stack_direction ()
101{
102 static char *addr = NULL; /* Address of first `dummy', once known. */
103 auto char dummy; /* To get stack address. */