alloca.c 5.2 KB

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