home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / mntlib32.zoo / lattice / alloca.c < prev    next >
C/C++ Source or Header  |  1992-09-03  |  4KB  |  116 lines

  1. /*
  2.         This implementation of the PWB library alloca() function,
  3.         which is used to allocate space off the run-time stack so
  4.         that it is automatically reclaimed upon procedure exit,
  5.         was inspired by discussions with J. Q. Johnson of Cornell.
  6.  
  7.         It should work under any C implementation that uses an
  8.         actual procedure stack (as opposed to a linked list of
  9.         frames).  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.  
  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.  
  19.         As a special case, alloca(0) reclaims storage without
  20.         allocating any.  It is a good idea to use alloca(0) in
  21.         your main control loop, etc. to force garbage collection.
  22. */
  23.  
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26.  
  27. typedef void        *pointer;                /* generic pointer type */
  28.  
  29. void *xmalloc(size_t n)
  30. {
  31.   void *block;
  32.  
  33.   block = calloc(n,1);
  34.   if (block == NULL)
  35.     {
  36.       fprintf(stderr, "alloca: memory exhausted\n");
  37.       exit(1);
  38.     }
  39.  
  40.   return (block);
  41. }
  42.  
  43. void *mallocate(unsigned n)
  44. {
  45.   return xmalloc(n);
  46. }
  47.  
  48.  
  49. static int stack_dir=-1;
  50.  
  51. /*
  52.         An "alloca header" is used to:
  53.         (a) chain together all alloca()ed blocks;
  54.         (b) keep track of stack depth.
  55.  
  56.         It is very important that sizeof(header) agree with malloc()
  57.         alignment chunk size.  The following default should work okay.
  58. */
  59.  
  60. #ifndef        ALIGN_SIZE
  61. #define        ALIGN_SIZE        sizeof(double)
  62. #endif
  63.  
  64. typedef union hdr
  65. {
  66.   char        align[ALIGN_SIZE];        /* to force sizeof(header) */
  67.   struct
  68.     {
  69.       union hdr *next;                /* for chaining headers */
  70.       char *deep;                /* for stack depth measure */
  71.     } h;
  72. } header;
  73.  
  74. /*
  75.         alloca( size ) returns a pointer to at least `size' bytes of
  76.         storage which will be automatically reclaimed upon exit from
  77.         the procedure that called alloca().  Originally, this space
  78.         was supposed to be taken from the current stack frame of the
  79.         caller, but that method cannot be made to work for some
  80.         implementations of C, for example under Gould's UTX/32.
  81. */
  82.  
  83. static header *last_alloca_header = NULL; /* -> last alloca header */
  84.  
  85. pointer alloca(size_t size)
  86. {
  87.   auto char        probe;                /* probes stack depth: */
  88.   register char        *depth = &probe;
  89.   /* Reclaim garbage, defined as all alloca()ed storage that
  90.      was allocated from deeper in the stack than currently. */
  91.   {
  92.     register header        *hp;        /* traverses linked list */
  93.  
  94.     for (hp = last_alloca_header; hp != NULL;)
  95.       if (stack_dir > 0 && hp->h.deep > depth
  96.           || stack_dir < 0 && hp->h.deep < depth) {
  97.           register header        *np = hp->h.next;
  98.           free ((pointer) hp);        /* collect garbage */
  99.           hp = np;                /* -> next header */
  100.         } else break;                /* rest are not deeper */
  101.     last_alloca_header = hp;        /* -> last valid storage */
  102.   }
  103.   if (size == 0) return NULL;        /* no allocation required */
  104.   /* Allocate combined header + user data storage. */
  105.   {
  106.     register pointer        new = xmalloc (sizeof (header) + size);
  107.     /* address of header */
  108.     ((header *)new)->h.next = last_alloca_header;
  109.     ((header *)new)->h.deep = depth;
  110.     last_alloca_header = (header *)new;
  111.     /* User storage begins just after header. */
  112.     return (pointer)((char *)new + sizeof(header));
  113.   }
  114. }
  115.  
  116.