alloca.c 4.8 KB

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