annotate src/alloca.c @ 30273:dad74ad87ac2

(eshell-smart): Replace links to eshell.info with links to eshell, to avoid problems on systems where the manual is installed as `eshell'.
author Eli Zaretskii <eliz@gnu.org>
date Tue, 18 Jul 2000 11:50:42 +0000
parents 1e2f2a050a93
children 60a78156cc87
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2746
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
1 /* alloca.c -- allocate automatically reclaimed memory
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
2 (Mostly) portable public-domain implementation -- D A Gwyn
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
3
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
4 This implementation of the PWB library alloca function,
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
5 which is used to allocate space off the run-time stack so
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
6 that it is automatically reclaimed upon procedure exit,
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
7 was inspired by discussions with J. Q. Johnson of Cornell.
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
8 J.Otto Tennant <jot@cray.com> contributed the Cray support.
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
9
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
10 There are some preprocessor constants that can
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
11 be defined when compiling for your specific system, for
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
12 improved efficiency; however, the defaults should be okay.
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
13
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
14 The general concept of this implementation is to keep
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
15 track of all alloca-allocated blocks, and reclaim any
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
16 that are found to be deeper in the stack than the current
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
17 invocation. This heuristic does not reclaim storage as
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
18 soon as it becomes invalid, but it will do so eventually.
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
19
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
20 As a special case, alloca(0) reclaims storage without
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
21 allocating any. It is a good idea to use alloca(0) in
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
22 your main control loop, etc. to force garbage collection. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
23
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
24 #ifdef HAVE_CONFIG_H
4686
3ddc8e2be5d7 *** empty log message ***
Roland McGrath <roland@gnu.org>
parents: 4646
diff changeset
25 #include <config.h>
2746
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
26 #endif
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
27
14844
885968b5998e [HAVE_STRING_H]: Include string.h.
Roland McGrath <roland@gnu.org>
parents: 14050
diff changeset
28 #ifdef HAVE_STRING_H
885968b5998e [HAVE_STRING_H]: Include string.h.
Roland McGrath <roland@gnu.org>
parents: 14050
diff changeset
29 #include <string.h>
885968b5998e [HAVE_STRING_H]: Include string.h.
Roland McGrath <roland@gnu.org>
parents: 14050
diff changeset
30 #endif
885968b5998e [HAVE_STRING_H]: Include string.h.
Roland McGrath <roland@gnu.org>
parents: 14050
diff changeset
31 #ifdef HAVE_STDLIB_H
885968b5998e [HAVE_STRING_H]: Include string.h.
Roland McGrath <roland@gnu.org>
parents: 14050
diff changeset
32 #include <stdlib.h>
885968b5998e [HAVE_STRING_H]: Include string.h.
Roland McGrath <roland@gnu.org>
parents: 14050
diff changeset
33 #endif
885968b5998e [HAVE_STRING_H]: Include string.h.
Roland McGrath <roland@gnu.org>
parents: 14050
diff changeset
34
7767
5be3332469e7 [emacs]: Block input around the garbage reclamation.
Richard M. Stallman <rms@gnu.org>
parents: 4753
diff changeset
35 #ifdef emacs
30032
53eb5de09ac4 [emacs]: Include lisp.h (needed by atimer.h included
Andrew Innes <andrewi@gnu.org>
parents: 15593
diff changeset
36 #include "lisp.h"
7767
5be3332469e7 [emacs]: Block input around the garbage reclamation.
Richard M. Stallman <rms@gnu.org>
parents: 4753
diff changeset
37 #include "blockinput.h"
5be3332469e7 [emacs]: Block input around the garbage reclamation.
Richard M. Stallman <rms@gnu.org>
parents: 4753
diff changeset
38 #endif
5be3332469e7 [emacs]: Block input around the garbage reclamation.
Richard M. Stallman <rms@gnu.org>
parents: 4753
diff changeset
39
4557
bc281eb57c68 *** empty log message ***
Roland McGrath <roland@gnu.org>
parents: 4541
diff changeset
40 /* If compiling with GCC 2, this file's not needed. */
4646
bdc64f022625 *** empty log message ***
Roland McGrath <roland@gnu.org>
parents: 4557
diff changeset
41 #if !defined (__GNUC__) || __GNUC__ < 2
bdc64f022625 *** empty log message ***
Roland McGrath <roland@gnu.org>
parents: 4557
diff changeset
42
4557
bc281eb57c68 *** empty log message ***
Roland McGrath <roland@gnu.org>
parents: 4541
diff changeset
43 /* If someone has defined alloca as a macro,
bc281eb57c68 *** empty log message ***
Roland McGrath <roland@gnu.org>
parents: 4541
diff changeset
44 there must be some other way alloca is supposed to work. */
2746
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
45 #ifndef alloca
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
46
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
47 #ifdef emacs
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
48 #ifdef static
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
49 /* actually, only want this if static is defined as ""
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
50 -- this is for usg, in which emacs must undefine static
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
51 in order to make unexec workable
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
52 */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
53 #ifndef STACK_DIRECTION
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
54 you
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
55 lose
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
56 -- must know STACK_DIRECTION at compile-time
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
57 #endif /* STACK_DIRECTION undefined */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
58 #endif /* static */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
59 #endif /* emacs */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
60
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
61 /* If your stack is a linked list of frames, you have to
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
62 provide an "address metric" ADDRESS_FUNCTION macro. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
63
4073
c476d93af94b *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 3044
diff changeset
64 #if defined (CRAY) && defined (CRAY_STACKSEG_END)
2746
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
65 long i00afunc ();
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
66 #define ADDRESS_FUNCTION(arg) (char *) i00afunc (&(arg))
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
67 #else
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
68 #define ADDRESS_FUNCTION(arg) &(arg)
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
69 #endif
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
70
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
71 #if __STDC__
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
72 typedef void *pointer;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
73 #else
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
74 typedef char *pointer;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
75 #endif
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
76
14845
fce6bf9ce9b0 (NULL): Define only if not already defined.
Roland McGrath <roland@gnu.org>
parents: 14844
diff changeset
77 #ifndef NULL
2746
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
78 #define NULL 0
14845
fce6bf9ce9b0 (NULL): Define only if not already defined.
Roland McGrath <roland@gnu.org>
parents: 14844
diff changeset
79 #endif
2746
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
80
3019
6261c371091e *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 2941
diff changeset
81 /* Different portions of Emacs need to call different versions of
6261c371091e *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 2941
diff changeset
82 malloc. The Emacs executable needs alloca to call xmalloc, because
6261c371091e *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 2941
diff changeset
83 ordinary malloc isn't protected from input signals. On the other
6261c371091e *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 2941
diff changeset
84 hand, the utilities in lib-src need alloca to call malloc; some of
6261c371091e *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 2941
diff changeset
85 them are very simple, and don't have an xmalloc routine.
6261c371091e *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 2941
diff changeset
86
3044
81c1ecaafd88 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 3019
diff changeset
87 Non-Emacs programs expect this to call use xmalloc.
81c1ecaafd88 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 3019
diff changeset
88
81c1ecaafd88 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 3019
diff changeset
89 Callers below should use malloc. */
81c1ecaafd88 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 3019
diff changeset
90
30233
1e2f2a050a93 (malloc) [emacs]: Define as xmalloc.
Gerd Moellmann <gerd@gnu.org>
parents: 30032
diff changeset
91 #ifdef emacs
3044
81c1ecaafd88 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 3019
diff changeset
92 #define malloc xmalloc
3019
6261c371091e *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 2941
diff changeset
93 #endif
4541
8c4835a24aca *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 4073
diff changeset
94 extern pointer malloc ();
3019
6261c371091e *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 2941
diff changeset
95
2746
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
96 /* Define STACK_DIRECTION if you know the direction of stack
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
97 growth for your system; otherwise it will be automatically
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
98 deduced at run-time.
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
99
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
100 STACK_DIRECTION > 0 => grows toward higher addresses
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
101 STACK_DIRECTION < 0 => grows toward lower addresses
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
102 STACK_DIRECTION = 0 => direction of growth unknown */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
103
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
104 #ifndef STACK_DIRECTION
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
105 #define STACK_DIRECTION 0 /* Direction unknown. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
106 #endif
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
107
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
108 #if STACK_DIRECTION != 0
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
109
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
110 #define STACK_DIR STACK_DIRECTION /* Known at compile-time. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
111
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
112 #else /* STACK_DIRECTION == 0; need run-time code. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
113
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
114 static int stack_dir; /* 1 or -1 once known. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
115 #define STACK_DIR stack_dir
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
116
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
117 static void
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
118 find_stack_direction ()
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
119 {
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
120 static char *addr = NULL; /* Address of first `dummy', once known. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
121 auto char dummy; /* To get stack address. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
122
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
123 if (addr == NULL)
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
124 { /* Initial entry. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
125 addr = ADDRESS_FUNCTION (dummy);
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
126
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
127 find_stack_direction (); /* Recurse once. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
128 }
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
129 else
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
130 {
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
131 /* Second entry. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
132 if (ADDRESS_FUNCTION (dummy) > addr)
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
133 stack_dir = 1; /* Stack grew upward. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
134 else
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
135 stack_dir = -1; /* Stack grew downward. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
136 }
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
137 }
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
138
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
139 #endif /* STACK_DIRECTION == 0 */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
140
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
141 /* An "alloca header" is used to:
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
142 (a) chain together all alloca'ed blocks;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
143 (b) keep track of stack depth.
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
144
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
145 It is very important that sizeof(header) agree with malloc
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
146 alignment chunk size. The following default should work okay. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
147
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
148 #ifndef ALIGN_SIZE
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
149 #define ALIGN_SIZE sizeof(double)
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
150 #endif
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
151
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
152 typedef union hdr
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
153 {
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
154 char align[ALIGN_SIZE]; /* To force sizeof(header). */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
155 struct
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
156 {
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
157 union hdr *next; /* For chaining headers. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
158 char *deep; /* For stack depth measure. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
159 } h;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
160 } header;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
161
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
162 static header *last_alloca_header = NULL; /* -> last alloca header. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
163
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
164 /* Return a pointer to at least SIZE bytes of storage,
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
165 which will be automatically reclaimed upon exit from
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
166 the procedure that called alloca. Originally, this space
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
167 was supposed to be taken from the current stack frame of the
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
168 caller, but that method cannot be made to work for some
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
169 implementations of C, for example under Gould's UTX/32. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
170
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
171 pointer
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
172 alloca (size)
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
173 unsigned size;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
174 {
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
175 auto char probe; /* Probes stack depth: */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
176 register char *depth = ADDRESS_FUNCTION (probe);
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
177
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
178 #if STACK_DIRECTION == 0
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
179 if (STACK_DIR == 0) /* Unknown growth direction. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
180 find_stack_direction ();
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
181 #endif
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
182
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
183 /* Reclaim garbage, defined as all alloca'd storage that
15593
88900acd9f99 formatting tweaks
Mike Stump <mrs@apple.com>
parents: 14845
diff changeset
184 was allocated from deeper in the stack than currently. */
2746
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
185
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
186 {
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
187 register header *hp; /* Traverses linked list. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
188
7767
5be3332469e7 [emacs]: Block input around the garbage reclamation.
Richard M. Stallman <rms@gnu.org>
parents: 4753
diff changeset
189 #ifdef emacs
5be3332469e7 [emacs]: Block input around the garbage reclamation.
Richard M. Stallman <rms@gnu.org>
parents: 4753
diff changeset
190 BLOCK_INPUT;
5be3332469e7 [emacs]: Block input around the garbage reclamation.
Richard M. Stallman <rms@gnu.org>
parents: 4753
diff changeset
191 #endif
5be3332469e7 [emacs]: Block input around the garbage reclamation.
Richard M. Stallman <rms@gnu.org>
parents: 4753
diff changeset
192
2746
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
193 for (hp = last_alloca_header; hp != NULL;)
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
194 if ((STACK_DIR > 0 && hp->h.deep > depth)
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
195 || (STACK_DIR < 0 && hp->h.deep < depth))
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
196 {
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
197 register header *np = hp->h.next;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
198
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
199 free ((pointer) hp); /* Collect garbage. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
200
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
201 hp = np; /* -> next header. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
202 }
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
203 else
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
204 break; /* Rest are not deeper. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
205
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
206 last_alloca_header = hp; /* -> last valid storage. */
7767
5be3332469e7 [emacs]: Block input around the garbage reclamation.
Richard M. Stallman <rms@gnu.org>
parents: 4753
diff changeset
207
5be3332469e7 [emacs]: Block input around the garbage reclamation.
Richard M. Stallman <rms@gnu.org>
parents: 4753
diff changeset
208 #ifdef emacs
5be3332469e7 [emacs]: Block input around the garbage reclamation.
Richard M. Stallman <rms@gnu.org>
parents: 4753
diff changeset
209 UNBLOCK_INPUT;
5be3332469e7 [emacs]: Block input around the garbage reclamation.
Richard M. Stallman <rms@gnu.org>
parents: 4753
diff changeset
210 #endif
2746
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
211 }
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
212
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
213 if (size == 0)
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
214 return NULL; /* No allocation required. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
215
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
216 /* Allocate combined header + user data storage. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
217
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
218 {
2941
59bbdf423db8 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 2746
diff changeset
219 register pointer new = malloc (sizeof (header) + size);
2746
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
220 /* Address of header. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
221
14050
28df14e7a281 (alloca): If malloc fails, just abort.
Karl Heuer <kwzh@gnu.org>
parents: 9097
diff changeset
222 if (new == 0)
28df14e7a281 (alloca): If malloc fails, just abort.
Karl Heuer <kwzh@gnu.org>
parents: 9097
diff changeset
223 abort();
28df14e7a281 (alloca): If malloc fails, just abort.
Karl Heuer <kwzh@gnu.org>
parents: 9097
diff changeset
224
2746
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
225 ((header *) new)->h.next = last_alloca_header;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
226 ((header *) new)->h.deep = depth;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
227
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
228 last_alloca_header = (header *) new;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
229
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
230 /* User storage begins just after header. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
231
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
232 return (pointer) ((char *) new + sizeof (header));
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
233 }
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
234 }
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
235
4073
c476d93af94b *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 3044
diff changeset
236 #if defined (CRAY) && defined (CRAY_STACKSEG_END)
2746
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
237
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
238 #ifdef DEBUG_I00AFUNC
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
239 #include <stdio.h>
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
240 #endif
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
241
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
242 #ifndef CRAY_STACK
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
243 #define CRAY_STACK
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
244 #ifndef CRAY2
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
245 /* Stack structures for CRAY-1, CRAY X-MP, and CRAY Y-MP */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
246 struct stack_control_header
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
247 {
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
248 long shgrow:32; /* Number of times stack has grown. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
249 long shaseg:32; /* Size of increments to stack. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
250 long shhwm:32; /* High water mark of stack. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
251 long shsize:32; /* Current size of stack (all segments). */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
252 };
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
253
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
254 /* The stack segment linkage control information occurs at
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
255 the high-address end of a stack segment. (The stack
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
256 grows from low addresses to high addresses.) The initial
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
257 part of the stack segment linkage control information is
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
258 0200 (octal) words. This provides for register storage
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
259 for the routine which overflows the stack. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
260
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
261 struct stack_segment_linkage
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
262 {
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
263 long ss[0200]; /* 0200 overflow words. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
264 long sssize:32; /* Number of words in this segment. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
265 long ssbase:32; /* Offset to stack base. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
266 long:32;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
267 long sspseg:32; /* Offset to linkage control of previous
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
268 segment of stack. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
269 long:32;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
270 long sstcpt:32; /* Pointer to task common address block. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
271 long sscsnm; /* Private control structure number for
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
272 microtasking. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
273 long ssusr1; /* Reserved for user. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
274 long ssusr2; /* Reserved for user. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
275 long sstpid; /* Process ID for pid based multi-tasking. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
276 long ssgvup; /* Pointer to multitasking thread giveup. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
277 long sscray[7]; /* Reserved for Cray Research. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
278 long ssa0;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
279 long ssa1;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
280 long ssa2;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
281 long ssa3;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
282 long ssa4;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
283 long ssa5;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
284 long ssa6;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
285 long ssa7;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
286 long sss0;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
287 long sss1;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
288 long sss2;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
289 long sss3;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
290 long sss4;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
291 long sss5;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
292 long sss6;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
293 long sss7;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
294 };
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
295
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
296 #else /* CRAY2 */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
297 /* The following structure defines the vector of words
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
298 returned by the STKSTAT library routine. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
299 struct stk_stat
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
300 {
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
301 long now; /* Current total stack size. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
302 long maxc; /* Amount of contiguous space which would
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
303 be required to satisfy the maximum
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
304 stack demand to date. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
305 long high_water; /* Stack high-water mark. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
306 long overflows; /* Number of stack overflow ($STKOFEN) calls. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
307 long hits; /* Number of internal buffer hits. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
308 long extends; /* Number of block extensions. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
309 long stko_mallocs; /* Block allocations by $STKOFEN. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
310 long underflows; /* Number of stack underflow calls ($STKRETN). */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
311 long stko_free; /* Number of deallocations by $STKRETN. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
312 long stkm_free; /* Number of deallocations by $STKMRET. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
313 long segments; /* Current number of stack segments. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
314 long maxs; /* Maximum number of stack segments so far. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
315 long pad_size; /* Stack pad size. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
316 long current_address; /* Current stack segment address. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
317 long current_size; /* Current stack segment size. This
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
318 number is actually corrupted by STKSTAT to
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
319 include the fifteen word trailer area. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
320 long initial_address; /* Address of initial segment. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
321 long initial_size; /* Size of initial segment. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
322 };
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
323
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
324 /* The following structure describes the data structure which trails
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
325 any stack segment. I think that the description in 'asdef' is
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
326 out of date. I only describe the parts that I am sure about. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
327
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
328 struct stk_trailer
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
329 {
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
330 long this_address; /* Address of this block. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
331 long this_size; /* Size of this block (does not include
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
332 this trailer). */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
333 long unknown2;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
334 long unknown3;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
335 long link; /* Address of trailer block of previous
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
336 segment. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
337 long unknown5;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
338 long unknown6;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
339 long unknown7;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
340 long unknown8;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
341 long unknown9;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
342 long unknown10;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
343 long unknown11;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
344 long unknown12;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
345 long unknown13;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
346 long unknown14;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
347 };
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
348
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
349 #endif /* CRAY2 */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
350 #endif /* not CRAY_STACK */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
351
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
352 #ifdef CRAY2
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
353 /* Determine a "stack measure" for an arbitrary ADDRESS.
15593
88900acd9f99 formatting tweaks
Mike Stump <mrs@apple.com>
parents: 14845
diff changeset
354 I doubt that "lint" will like this much. */
2746
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
355
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
356 static long
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
357 i00afunc (long *address)
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
358 {
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
359 struct stk_stat status;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
360 struct stk_trailer *trailer;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
361 long *block, size;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
362 long result = 0;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
363
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
364 /* We want to iterate through all of the segments. The first
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
365 step is to get the stack status structure. We could do this
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
366 more quickly and more directly, perhaps, by referencing the
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
367 $LM00 common block, but I know that this works. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
368
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
369 STKSTAT (&status);
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
370
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
371 /* Set up the iteration. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
372
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
373 trailer = (struct stk_trailer *) (status.current_address
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
374 + status.current_size
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
375 - 15);
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
376
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
377 /* There must be at least one stack segment. Therefore it is
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
378 a fatal error if "trailer" is null. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
379
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
380 if (trailer == 0)
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
381 abort ();
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
382
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
383 /* Discard segments that do not contain our argument address. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
384
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
385 while (trailer != 0)
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
386 {
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
387 block = (long *) trailer->this_address;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
388 size = trailer->this_size;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
389 if (block == 0 || size == 0)
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
390 abort ();
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
391 trailer = (struct stk_trailer *) trailer->link;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
392 if ((block <= address) && (address < (block + size)))
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
393 break;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
394 }
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
395
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
396 /* Set the result to the offset in this segment and add the sizes
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
397 of all predecessor segments. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
398
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
399 result = address - block;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
400
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
401 if (trailer == 0)
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
402 {
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
403 return result;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
404 }
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
405
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
406 do
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
407 {
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
408 if (trailer->this_size <= 0)
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
409 abort ();
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
410 result += trailer->this_size;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
411 trailer = (struct stk_trailer *) trailer->link;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
412 }
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
413 while (trailer != 0);
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
414
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
415 /* We are done. Note that if you present a bogus address (one
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
416 not in any segment), you will get a different number back, formed
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
417 from subtracting the address of the first block. This is probably
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
418 not what you want. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
419
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
420 return (result);
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
421 }
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
422
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
423 #else /* not CRAY2 */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
424 /* Stack address function for a CRAY-1, CRAY X-MP, or CRAY Y-MP.
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
425 Determine the number of the cell within the stack,
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
426 given the address of the cell. The purpose of this
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
427 routine is to linearize, in some sense, stack addresses
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
428 for alloca. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
429
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
430 static long
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
431 i00afunc (long address)
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
432 {
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
433 long stkl = 0;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
434
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
435 long size, pseg, this_segment, stack;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
436 long result = 0;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
437
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
438 struct stack_segment_linkage *ssptr;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
439
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
440 /* Register B67 contains the address of the end of the
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
441 current stack segment. If you (as a subprogram) store
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
442 your registers on the stack and find that you are past
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
443 the contents of B67, you have overflowed the segment.
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
444
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
445 B67 also points to the stack segment linkage control
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
446 area, which is what we are really interested in. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
447
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
448 stkl = CRAY_STACKSEG_END ();
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
449 ssptr = (struct stack_segment_linkage *) stkl;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
450
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
451 /* If one subtracts 'size' from the end of the segment,
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
452 one has the address of the first word of the segment.
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
453
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
454 If this is not the first segment, 'pseg' will be
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
455 nonzero. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
456
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
457 pseg = ssptr->sspseg;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
458 size = ssptr->sssize;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
459
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
460 this_segment = stkl - size;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
461
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
462 /* It is possible that calling this routine itself caused
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
463 a stack overflow. Discard stack segments which do not
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
464 contain the target address. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
465
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
466 while (!(this_segment <= address && address <= stkl))
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
467 {
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
468 #ifdef DEBUG_I00AFUNC
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
469 fprintf (stderr, "%011o %011o %011o\n", this_segment, address, stkl);
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
470 #endif
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
471 if (pseg == 0)
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
472 break;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
473 stkl = stkl - pseg;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
474 ssptr = (struct stack_segment_linkage *) stkl;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
475 size = ssptr->sssize;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
476 pseg = ssptr->sspseg;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
477 this_segment = stkl - size;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
478 }
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
479
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
480 result = address - this_segment;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
481
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
482 /* If you subtract pseg from the current end of the stack,
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
483 you get the address of the previous stack segment's end.
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
484 This seems a little convoluted to me, but I'll bet you save
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
485 a cycle somewhere. */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
486
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
487 while (pseg != 0)
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
488 {
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
489 #ifdef DEBUG_I00AFUNC
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
490 fprintf (stderr, "%011o %011o\n", pseg, size);
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
491 #endif
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
492 stkl = stkl - pseg;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
493 ssptr = (struct stack_segment_linkage *) stkl;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
494 size = ssptr->sssize;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
495 pseg = ssptr->sspseg;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
496 result += size;
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
497 }
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
498 return (result);
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
499 }
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
500
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
501 #endif /* not CRAY2 */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
502 #endif /* CRAY */
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
503
e4595f258ddd Initial revision
Jim Meyering <jim@meyering.net>
parents:
diff changeset
504 #endif /* no alloca */
4557
bc281eb57c68 *** empty log message ***
Roland McGrath <roland@gnu.org>
parents: 4541
diff changeset
505 #endif /* not GCC version 2 */