alloca.c 14 KB

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