alloca.c 14 KB

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