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. */
33 # include "blockinput.h"
36 # define free EMACS_FREE
39 # define memory_full() abort ()
42 /* If compiling with GCC or clang, this file is not needed. */
43 #if !(defined __GNUC__ || defined __clang__)
45 /* If someone has defined alloca as a macro,
46 there must be some other way alloca is supposed to work. */
51 /* actually, only want this if static is defined as ""
52 -- this is for usg, in which emacs must undefine static
53 in order to make unexec workable
55 # ifndef STACK_DIRECTION
58 -- must know STACK_DIRECTION at compile
-time
59 /* Using #error here is not wise since this file should work for
60 old and obscure compilers. */
61 # endif /* STACK_DIRECTION undefined */
65 /* Define STACK_DIRECTION if you know the direction of stack
66 growth for your system; otherwise it will be automatically
69 STACK_DIRECTION > 0 => grows toward higher addresses
70 STACK_DIRECTION < 0 => grows toward lower addresses
71 STACK_DIRECTION = 0 => direction of growth unknown */
73 # ifndef STACK_DIRECTION
74 # define STACK_DIRECTION 0 /* Direction unknown. */
77 # if STACK_DIRECTION != 0
79 # define STACK_DIR STACK_DIRECTION /* Known at compile-time. */
81 # else /* STACK_DIRECTION == 0; need run-time code. */
83 static int stack_dir
; /* 1 or -1 once known. */
84 # define STACK_DIR stack_dir
87 find_stack_direction (int *addr
, int depth
)
92 *addr
= addr
< &dummy
? 1 : addr
== &dummy
? 0 : -1;
93 dir
= depth
? find_stack_direction (addr
, depth
- 1) : 0;
97 # endif /* STACK_DIRECTION == 0 */
99 /* An "alloca header" is used to:
100 (a) chain together all alloca'ed blocks;
101 (b) keep track of stack depth.
103 It is very important that sizeof(header) agree with malloc
104 alignment chunk size. The following default should work okay. */
107 # define ALIGN_SIZE sizeof(double)
112 char align
[ALIGN_SIZE
]; /* To force sizeof(header). */
115 union hdr
*next
; /* For chaining headers. */
116 char *deep
; /* For stack depth measure. */
120 static header
*last_alloca_header
= NULL
; /* -> last alloca header. */
122 /* Return a pointer to at least SIZE bytes of storage,
123 which will be automatically reclaimed upon exit from
124 the procedure that called alloca. Originally, this space
125 was supposed to be taken from the current stack frame of the
126 caller, but that method cannot be made to work for some
127 implementations of C, for example under Gould's UTX/32. */
132 auto char probe
; /* Probes stack depth: */
133 register char *depth
= &probe
;
135 # if STACK_DIRECTION == 0
136 if (STACK_DIR
== 0) /* Unknown growth direction. */
137 STACK_DIR
= find_stack_direction (NULL
, (size
& 1) + 20);
140 /* Reclaim garbage, defined as all alloca'd storage that
141 was allocated from deeper in the stack than currently. */
144 register header
*hp
; /* Traverses linked list. */
150 for (hp
= last_alloca_header
; hp
!= NULL
;)
151 if ((STACK_DIR
> 0 && hp
->h
.deep
> depth
)
152 || (STACK_DIR
< 0 && hp
->h
.deep
< depth
))
154 register header
*np
= hp
->h
.next
;
156 free (hp
); /* Collect garbage. */
158 hp
= np
; /* -> next header. */
161 break; /* Rest are not deeper. */
163 last_alloca_header
= hp
; /* -> last valid storage. */
171 return NULL
; /* No allocation required. */
173 /* Allocate combined header + user data storage. */
176 /* Address of header. */
177 register header
*new;
179 size_t combined_size
= sizeof (header
) + size
;
180 if (combined_size
< sizeof (header
))
183 new = malloc (combined_size
);
188 new->h
.next
= last_alloca_header
;
191 last_alloca_header
= new;
193 /* User storage begins just after header. */
195 return (void *) (new + 1);
199 # endif /* no alloca */
200 #endif /* not GCC || clang */