1 /* alloca.c -- allocate automatically reclaimed memory
2 (Mostly) portable public-domain implementation -- D A Gwyn
4 This implementation of the PWB library alloca function,
5 which is used to allocate space off the run-time stack so
6 that it is automatically reclaimed upon procedure exit,
7 was inspired by discussions with J. Q. Johnson of Cornell.
8 J.Otto Tennant <jot@cray.com> contributed the Cray support.
10 There are some preprocessor constants that can
11 be defined when compiling for your specific system, for
12 improved efficiency; however, the defaults should be okay.
14 The general concept of this implementation is to keep
15 track of all alloca-allocated blocks, and reclaim any
16 that are found to be deeper in the stack than the current
17 invocation. This heuristic does not reclaim storage as
18 soon as it becomes invalid, but it will do so eventually.
20 As a special case, alloca(0) reclaims storage without
21 allocating any. It is a good idea to use alloca(0) in
22 your main control loop, etc. to force garbage collection. */
28 #include <libiberty.h>
37 /* If your stack is a linked list of frames, you have to
38 provide an "address metric" ADDRESS_FUNCTION macro. */
40 #if defined (CRAY) && defined (CRAY_STACKSEG_END)
41 static long i00afunc ();
42 #define ADDRESS_FUNCTION(arg) (char *) i00afunc (&(arg))
44 #define ADDRESS_FUNCTION(arg) &(arg)
51 /* Define STACK_DIRECTION if you know the direction of stack
52 growth for your system; otherwise it will be automatically
55 STACK_DIRECTION > 0 => grows toward higher addresses
56 STACK_DIRECTION < 0 => grows toward lower addresses
57 STACK_DIRECTION = 0 => direction of growth unknown */
59 #ifndef STACK_DIRECTION
60 #define STACK_DIRECTION 0 /* Direction unknown. */
63 #if STACK_DIRECTION != 0
65 #define STACK_DIR STACK_DIRECTION /* Known at compile-time. */
67 #else /* STACK_DIRECTION == 0; need run-time code. */
69 static int stack_dir; /* 1 or -1 once known. */
70 #define STACK_DIR stack_dir
73 find_stack_direction ()
75 static char *addr = NULL; /* Address of first `dummy', once known. */
76 auto char dummy; /* To get stack address. */
79 { /* Initial entry. */
80 addr = ADDRESS_FUNCTION (dummy);
82 find_stack_direction (); /* Recurse once. */
87 if (ADDRESS_FUNCTION (dummy) > addr)
88 stack_dir = 1; /* Stack grew upward. */
90 stack_dir = -1; /* Stack grew downward. */
94 #endif /* STACK_DIRECTION == 0 */
96 /* An "alloca header" is used to:
97 (a) chain together all alloca'ed blocks;
98 (b) keep track of stack depth.
100 It is very important that sizeof(header) agree with malloc
101 alignment chunk size. The following default should work okay. */
104 #define ALIGN_SIZE sizeof(double)
109 char align[ALIGN_SIZE]; /* To force sizeof(header). */
112 union hdr *next; /* For chaining headers. */
113 char *deep; /* For stack depth measure. */
117 static header *last_alloca_header = NULL; /* -> last alloca header. */
119 /* Return a pointer to at least SIZE bytes of storage,
120 which will be automatically reclaimed upon exit from
121 the procedure that called alloca. Originally, this space
122 was supposed to be taken from the current stack frame of the
123 caller, but that method cannot be made to work for some
124 implementations of C, for example under Gould's UTX/32. */
130 auto char probe; /* Probes stack depth: */
131 register char *depth = ADDRESS_FUNCTION (probe);
133 #if STACK_DIRECTION == 0
134 if (STACK_DIR == 0) /* Unknown growth direction. */
135 find_stack_direction ();
138 /* Reclaim garbage, defined as all alloca'd storage that
139 was allocated from deeper in the stack than currently. */
142 register header *hp; /* Traverses linked list. */
144 for (hp = last_alloca_header; hp != NULL;)
145 if ((STACK_DIR > 0 && hp->h.deep > depth)
146 || (STACK_DIR < 0 && hp->h.deep < depth))
148 register header *np = hp->h.next;
150 free ((PTR) hp); /* Collect garbage. */
152 hp = np; /* -> next header. */
155 break; /* Rest are not deeper. */
157 last_alloca_header = hp; /* -> last valid storage. */
161 return NULL; /* No allocation required. */
163 /* Allocate combined header + user data storage. */
166 register PTR new = xmalloc (sizeof (header) + size);
167 /* Address of header. */
172 ((header *) new)->h.next = last_alloca_header;
173 ((header *) new)->h.deep = depth;
175 last_alloca_header = (header *) new;
177 /* User storage begins just after header. */
179 return (PTR) ((char *) new + sizeof (header));
183 #if defined (CRAY) && defined (CRAY_STACKSEG_END)
185 #ifdef DEBUG_I00AFUNC
192 /* Stack structures for CRAY-1, CRAY X-MP, and CRAY Y-MP */
193 struct stack_control_header
195 long shgrow:32; /* Number of times stack has grown. */
196 long shaseg:32; /* Size of increments to stack. */
197 long shhwm:32; /* High water mark of stack. */
198 long shsize:32; /* Current size of stack (all segments). */
201 /* The stack segment linkage control information occurs at
202 the high-address end of a stack segment. (The stack
203 grows from low addresses to high addresses.) The initial
204 part of the stack segment linkage control information is
205 0200 (octal) words. This provides for register storage
206 for the routine which overflows the stack. */
208 struct stack_segment_linkage
210 long ss[0200]; /* 0200 overflow words. */
211 long sssize:32; /* Number of words in this segment. */
212 long ssbase:32; /* Offset to stack base. */
214 long sspseg:32; /* Offset to linkage control of previous
217 long sstcpt:32; /* Pointer to task common address block. */
218 long sscsnm; /* Private control structure number for
220 long ssusr1; /* Reserved for user. */
221 long ssusr2; /* Reserved for user. */
222 long sstpid; /* Process ID for pid based multi-tasking. */
223 long ssgvup; /* Pointer to multitasking thread giveup. */
224 long sscray[7]; /* Reserved for Cray Research. */
244 /* The following structure defines the vector of words
245 returned by the STKSTAT library routine. */
248 long now; /* Current total stack size. */
249 long maxc; /* Amount of contiguous space which would
250 be required to satisfy the maximum
251 stack demand to date. */
252 long high_water; /* Stack high-water mark. */
253 long overflows; /* Number of stack overflow ($STKOFEN) calls. */
254 long hits; /* Number of internal buffer hits. */
255 long extends; /* Number of block extensions. */
256 long stko_mallocs; /* Block allocations by $STKOFEN. */
257 long underflows; /* Number of stack underflow calls ($STKRETN). */
258 long stko_free; /* Number of deallocations by $STKRETN. */
259 long stkm_free; /* Number of deallocations by $STKMRET. */
260 long segments; /* Current number of stack segments. */
261 long maxs; /* Maximum number of stack segments so far. */
262 long pad_size; /* Stack pad size. */
263 long current_address; /* Current stack segment address. */
264 long current_size; /* Current stack segment size. This
265 number is actually corrupted by STKSTAT to
266 include the fifteen word trailer area. */
267 long initial_address; /* Address of initial segment. */
268 long initial_size; /* Size of initial segment. */
271 /* The following structure describes the data structure which trails
272 any stack segment. I think that the description in 'asdef' is
273 out of date. I only describe the parts that I am sure about. */
277 long this_address; /* Address of this block. */
278 long this_size; /* Size of this block (does not include
282 long link; /* Address of trailer block of previous
297 #endif /* not CRAY_STACK */
300 /* Determine a "stack measure" for an arbitrary ADDRESS.
301 I doubt that "lint" will like this much. */
304 i00afunc (long *address)
306 struct stk_stat status;
307 struct stk_trailer *trailer;
311 /* We want to iterate through all of the segments. The first
312 step is to get the stack status structure. We could do this
313 more quickly and more directly, perhaps, by referencing the
314 $LM00 common block, but I know that this works. */
318 /* Set up the iteration. */
320 trailer = (struct stk_trailer *) (status.current_address
321 + status.current_size
324 /* There must be at least one stack segment. Therefore it is
325 a fatal error if "trailer" is null. */
330 /* Discard segments that do not contain our argument address. */
334 block = (long *) trailer->this_address;
335 size = trailer->this_size;
336 if (block == 0 || size == 0)
338 trailer = (struct stk_trailer *) trailer->link;
339 if ((block <= address) && (address < (block + size)))
343 /* Set the result to the offset in this segment and add the sizes
344 of all predecessor segments. */
346 result = address - block;
355 if (trailer->this_size <= 0)
357 result += trailer->this_size;
358 trailer = (struct stk_trailer *) trailer->link;
360 while (trailer != 0);
362 /* We are done. Note that if you present a bogus address (one
363 not in any segment), you will get a different number back, formed
364 from subtracting the address of the first block. This is probably
365 not what you want. */
370 #else /* not CRAY2 */
371 /* Stack address function for a CRAY-1, CRAY X-MP, or CRAY Y-MP.
372 Determine the number of the cell within the stack,
373 given the address of the cell. The purpose of this
374 routine is to linearize, in some sense, stack addresses
378 i00afunc (long address)
382 long size, pseg, this_segment, stack;
385 struct stack_segment_linkage *ssptr;
387 /* Register B67 contains the address of the end of the
388 current stack segment. If you (as a subprogram) store
389 your registers on the stack and find that you are past
390 the contents of B67, you have overflowed the segment.
392 B67 also points to the stack segment linkage control
393 area, which is what we are really interested in. */
395 stkl = CRAY_STACKSEG_END ();
396 ssptr = (struct stack_segment_linkage *) stkl;
398 /* If one subtracts 'size' from the end of the segment,
399 one has the address of the first word of the segment.
401 If this is not the first segment, 'pseg' will be
404 pseg = ssptr->sspseg;
405 size = ssptr->sssize;
407 this_segment = stkl - size;
409 /* It is possible that calling this routine itself caused
410 a stack overflow. Discard stack segments which do not
411 contain the target address. */
413 while (!(this_segment <= address && address <= stkl))
415 #ifdef DEBUG_I00AFUNC
416 fprintf (stderr, "%011o %011o %011o\n", this_segment, address, stkl);
421 ssptr = (struct stack_segment_linkage *) stkl;
422 size = ssptr->sssize;
423 pseg = ssptr->sspseg;
424 this_segment = stkl - size;
427 result = address - this_segment;
429 /* If you subtract pseg from the current end of the stack,
430 you get the address of the previous stack segment's end.
431 This seems a little convoluted to me, but I'll bet you save
432 a cycle somewhere. */
436 #ifdef DEBUG_I00AFUNC
437 fprintf (stderr, "%011o %011o\n", pseg, size);
440 ssptr = (struct stack_segment_linkage *) stkl;
441 size = ssptr->sssize;
442 pseg = ssptr->sspseg;
448 #endif /* not CRAY2 */