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