alloca.c 14 KB

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