alloca.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 or clang, this file is not needed. */
  34. #if !(defined __GNUC__ || defined __clang__)
  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. /* Define STACK_DIRECTION if you know the direction of stack
  54. growth for your system; otherwise it will be automatically
  55. deduced at run-time.
  56. STACK_DIRECTION > 0 => grows toward higher addresses
  57. STACK_DIRECTION < 0 => grows toward lower addresses
  58. STACK_DIRECTION = 0 => direction of growth unknown */
  59. # ifndef STACK_DIRECTION
  60. # define STACK_DIRECTION 0 /* Direction unknown. */
  61. # endif
  62. # if STACK_DIRECTION != 0
  63. # define STACK_DIR STACK_DIRECTION /* Known at compile-time. */
  64. # else /* STACK_DIRECTION == 0; need run-time code. */
  65. static int stack_dir; /* 1 or -1 once known. */
  66. # define STACK_DIR stack_dir
  67. static int
  68. find_stack_direction (int *addr, int depth)
  69. {
  70. int dir, dummy = 0;
  71. if (! addr)
  72. addr = &dummy;
  73. *addr = addr < &dummy ? 1 : addr == &dummy ? 0 : -1;
  74. dir = depth ? find_stack_direction (addr, depth - 1) : 0;
  75. return dir + dummy;
  76. }
  77. # endif /* STACK_DIRECTION == 0 */
  78. /* An "alloca header" is used to:
  79. (a) chain together all alloca'ed blocks;
  80. (b) keep track of stack depth.
  81. It is very important that sizeof(header) agree with malloc
  82. alignment chunk size. The following default should work okay. */
  83. # ifndef ALIGN_SIZE
  84. # define ALIGN_SIZE sizeof(double)
  85. # endif
  86. typedef union hdr
  87. {
  88. char align[ALIGN_SIZE]; /* To force sizeof(header). */
  89. struct
  90. {
  91. union hdr *next; /* For chaining headers. */
  92. char *deep; /* For stack depth measure. */
  93. } h;
  94. } header;
  95. static header *last_alloca_header = NULL; /* -> last alloca header. */
  96. /* Return a pointer to at least SIZE bytes of storage,
  97. which will be automatically reclaimed upon exit from
  98. the procedure that called alloca. Originally, this space
  99. was supposed to be taken from the current stack frame of the
  100. caller, but that method cannot be made to work for some
  101. implementations of C, for example under Gould's UTX/32. */
  102. void *
  103. alloca (size_t size)
  104. {
  105. auto char probe; /* Probes stack depth: */
  106. register char *depth = &probe;
  107. # if STACK_DIRECTION == 0
  108. if (STACK_DIR == 0) /* Unknown growth direction. */
  109. STACK_DIR = find_stack_direction (NULL, (size & 1) + 20);
  110. # endif
  111. /* Reclaim garbage, defined as all alloca'd storage that
  112. was allocated from deeper in the stack than currently. */
  113. {
  114. register header *hp; /* Traverses linked list. */
  115. # ifdef emacs
  116. BLOCK_INPUT;
  117. # endif
  118. for (hp = last_alloca_header; hp != NULL;)
  119. if ((STACK_DIR > 0 && hp->h.deep > depth)
  120. || (STACK_DIR < 0 && hp->h.deep < depth))
  121. {
  122. register header *np = hp->h.next;
  123. free (hp); /* Collect garbage. */
  124. hp = np; /* -> next header. */
  125. }
  126. else
  127. break; /* Rest are not deeper. */
  128. last_alloca_header = hp; /* -> last valid storage. */
  129. # ifdef emacs
  130. UNBLOCK_INPUT;
  131. # endif
  132. }
  133. if (size == 0)
  134. return NULL; /* No allocation required. */
  135. /* Allocate combined header + user data storage. */
  136. {
  137. /* Address of header. */
  138. register header *new;
  139. size_t combined_size = sizeof (header) + size;
  140. if (combined_size < sizeof (header))
  141. memory_full ();
  142. new = malloc (combined_size);
  143. if (! new)
  144. memory_full ();
  145. new->h.next = last_alloca_header;
  146. new->h.deep = depth;
  147. last_alloca_header = new;
  148. /* User storage begins just after header. */
  149. return (void *) (new + 1);
  150. }
  151. }
  152. # endif /* no alloca */
  153. #endif /* not GCC || clang */