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