alloca.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* alloca.c -- allocate automatically reclaimed memory
  2. (Mostly) portable public-domain implementation -- D A Gwyn
  3. Taken from GNU binutils 2.10.1.
  4. Minor changes
  5. Copyright © 2018,2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  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. There are some preprocessor constants that can
  11. be defined when compiling for your specific system, for
  12. improved efficiency; however, the defaults should be okay.
  13. The general concept of this implementation is to keep
  14. track of all alloca-allocated blocks, and reclaim any
  15. that are found to be deeper in the stack than the current
  16. invocation. This heuristic does not reclaim storage as
  17. soon as it becomes invalid, but it will do so eventually.
  18. As a special case, alloca(0) reclaims storage without
  19. allocating any. It is a good idea to use alloca(0) in
  20. your main control loop, etc. to force garbage collection. */
  21. #include <stdlib.h>
  22. #include <unistd.h>
  23. #define ALIGN_SIZE 4
  24. #define ADDRESS_FUNCTION(arg) &(arg)
  25. #define STACK_DIR -1
  26. union alloca_header
  27. {
  28. char align[ALIGN_SIZE]; /* To force sizeof(union alloca_header). */
  29. struct
  30. {
  31. union alloca_header *next; /* For chaining headers. */
  32. char *deep; /* For stack depth measure. */
  33. } h;
  34. };
  35. static union alloca_header *last_alloca_header = NULL; /* -> last alloca header. */
  36. /* Return a void * to at least SIZE bytes of storage,
  37. which will be automatically reclaimed upon exit from
  38. the procedure that called alloca. Originally, this space
  39. was supposed to be taken from the current stack frame of the
  40. caller, but that method cannot be made to work for some
  41. implementations of C, for example under Gould's UTX/32. */
  42. void *
  43. alloca (size_t size)
  44. {
  45. char probe; /* Probes stack depth: */
  46. char *depth = ADDRESS_FUNCTION (probe);
  47. /* Reclaim garbage, defined as all alloca'd storage that
  48. was allocated from deeper in the stack than currently. */
  49. {
  50. union alloca_header *hp; /* Traverses linked list. */
  51. for (hp = last_alloca_header; hp != NULL;)
  52. if ((STACK_DIR > 0 && hp->h.deep > depth) || (STACK_DIR < 0 && hp->h.deep < depth))
  53. {
  54. union alloca_header *np = hp->h.next;
  55. free ((void *) hp); /* Collect garbage. */
  56. hp = np; /* -> next header. */
  57. }
  58. else
  59. break; /* Rest are not deeper. */
  60. last_alloca_header = hp; /* -> last valid storage. */
  61. }
  62. if (size == 0)
  63. return NULL; /* No allocation required. */
  64. /* Allocate combined header + user data storage. */
  65. {
  66. void *new = malloc (sizeof (union alloca_header) + size);
  67. /* Address of header. */
  68. if (new == 0)
  69. abort ();
  70. ((union alloca_header *) new)->h.next = last_alloca_header;
  71. ((union alloca_header *) new)->h.deep = depth;
  72. last_alloca_header = (union alloca_header *) new;
  73. /* User storage begins just after header. */
  74. return (void *) ((char *) new + sizeof (union alloca_header));
  75. }
  76. }