alloca.c 14 KB

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