annotate lib/alloca.c @ 425:67815606c5d9 3.0-beta2

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