alloca.c 5.5 KB

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