alloca.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. alloca -- (mostly) portable public-domain implementation -- D A Gwyn
  3. 93/3/31 jimb
  4. Use xfree instead of free.
  5. 86/05/30 rms
  6. include config.h, since on VMS it renames some symbols.
  7. Use xmalloc instead of malloc.
  8. This implementation of the PWB library alloca() function,
  9. which is used to allocate space off the run-time stack so
  10. that it is automatically reclaimed upon procedure exit,
  11. was inspired by discussions with J. Q. Johnson of Cornell.
  12. It should work under any C implementation that uses an
  13. actual procedure stack (as opposed to a linked list of
  14. frames). There are some preprocessor constants that can
  15. be defined when compiling for your specific system, for
  16. improved efficiency; however, the defaults should be okay.
  17. The general concept of this implementation is to keep
  18. track of all alloca()-allocated blocks, and reclaim any
  19. that are found to be deeper in the stack than the current
  20. invocation. This heuristic does not reclaim storage as
  21. soon as it becomes invalid, but it will do so eventually.
  22. As a special case, alloca(0) reclaims storage without
  23. allocating any. It is a good idea to use alloca(0) in
  24. your main control loop, etc. to force garbage collection.
  25. */
  26. #ifndef lint
  27. static char SCCSid[] = "@(#)alloca.c 1.1"; /* for the "what" utility */
  28. #endif
  29. #ifdef HAVE_CONFIG_H
  30. #include "config.h"
  31. #endif
  32. #ifdef emacs
  33. #ifdef static
  34. /* actually, only want this if static is defined as ""
  35. -- this is for usg, in which emacs must undefine static
  36. in order to make unexec workable
  37. */
  38. #ifndef STACK_DIRECTION
  39. you
  40. lose
  41. -- must know STACK_DIRECTION at compile-time
  42. #endif /* STACK_DIRECTION undefined */
  43. #endif /* static */
  44. #endif /* emacs */
  45. #ifdef emacs
  46. #define free xfree
  47. #endif
  48. #ifndef alloca /* If compiling with GCC, this file's not needed. */
  49. #ifdef __STDC__
  50. typedef void *pointer; /* generic pointer type */
  51. #else
  52. typedef char *pointer; /* generic pointer type */
  53. #endif
  54. #define NULL 0 /* null pointer constant */
  55. extern pointer xmalloc ();
  56. /*
  57. Define STACK_DIRECTION if you know the direction of stack
  58. growth for your system; otherwise it will be automatically
  59. deduced at run-time.
  60. STACK_DIRECTION > 0 => grows toward higher addresses
  61. STACK_DIRECTION < 0 => grows toward lower addresses
  62. STACK_DIRECTION = 0 => direction of growth unknown
  63. */
  64. #ifndef STACK_DIRECTION
  65. #define STACK_DIRECTION 0 /* direction unknown */
  66. #endif
  67. #if STACK_DIRECTION != 0
  68. #define STACK_DIR STACK_DIRECTION /* known at compile-time */
  69. #else /* STACK_DIRECTION == 0; need run-time code */
  70. static int stack_dir; /* 1 or -1 once known */
  71. #define STACK_DIR stack_dir
  72. static void
  73. find_stack_direction (/* void */)
  74. {
  75. static char *addr = NULL; /* address of first
  76. `dummy', once known */
  77. auto char dummy; /* to get stack address */
  78. if (addr == NULL)
  79. { /* initial entry */
  80. addr = &dummy;
  81. find_stack_direction (); /* recurse once */
  82. }
  83. else /* second entry */
  84. if (&dummy > addr)
  85. stack_dir = 1; /* stack grew upward */
  86. else
  87. stack_dir = -1; /* stack grew downward */
  88. }
  89. #endif /* STACK_DIRECTION == 0 */
  90. /*
  91. An "alloca header" is used to:
  92. (a) chain together all alloca'ed blocks;
  93. (b) keep track of stack depth.
  94. It is very important that sizeof (header) agree with malloc
  95. alignment chunk size. The following default should work okay.
  96. */
  97. #ifndef ALIGN_SIZE
  98. #define ALIGN_SIZE sizeof (double)
  99. #endif
  100. typedef union hdr
  101. {
  102. char align[ALIGN_SIZE]; /* to force sizeof (header) */
  103. struct
  104. {
  105. union hdr *next; /* for chaining headers */
  106. char *deep; /* for stack depth measure */
  107. } h;
  108. } header;
  109. /*
  110. alloca ( size ) returns a pointer to at least `size' bytes of
  111. storage which will be automatically reclaimed upon exit from
  112. the procedure that called alloca. Originally, this space
  113. was supposed to be taken from the current stack frame of the
  114. caller, but that method cannot be made to work for some
  115. implementations of C, for example under Gould's UTX/32.
  116. */
  117. static header *last_alloca_header = NULL; /* -> last alloca header */
  118. pointer
  119. alloca (size) /* returns pointer to storage */
  120. unsigned size; /* # bytes to allocate */
  121. {
  122. auto char probe; /* probes stack depth: */
  123. register char *depth = &probe;
  124. #if STACK_DIRECTION == 0
  125. if (STACK_DIR == 0) /* unknown growth direction */
  126. find_stack_direction ();
  127. #endif
  128. /* Reclaim garbage, defined as all alloca'ed storage that
  129. was allocated from deeper in the stack than currently. */
  130. {
  131. register header *hp; /* traverses linked list */
  132. for (hp = last_alloca_header; hp != NULL;)
  133. if ((STACK_DIR > 0 && hp->h.deep > depth)
  134. || (STACK_DIR < 0 && hp->h.deep < depth))
  135. {
  136. register header *np = hp->h.next;
  137. free ((pointer) hp); /* collect garbage */
  138. hp = np; /* -> next header */
  139. }
  140. else
  141. break; /* rest are not deeper */
  142. last_alloca_header = hp; /* -> last valid storage */
  143. }
  144. if (size == 0)
  145. return NULL; /* no allocation required */
  146. /* Allocate combined header + user data storage. */
  147. {
  148. register pointer new = xmalloc (sizeof (header) + size);
  149. /* address of header */
  150. ((header *)new)->h.next = last_alloca_header;
  151. ((header *)new)->h.deep = depth;
  152. last_alloca_header = (header *)new;
  153. /* User storage begins just after header. */
  154. return (pointer)((char *)new + sizeof (header));
  155. }
  156. }
  157. #endif /* no alloca */