alloca.c 14 KB

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