home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d156 / flex.lha / Flex / Flex2 / gnu.lib.src / gnulib.c < prev    next >
C/C++ Source or Header  |  1988-10-02  |  7KB  |  262 lines

  1. /* This software is covered by the GNU copyright */
  2.  
  3. #include <stdio.h>
  4.  
  5. void
  6. bzero (b, length)
  7.      register char *b;
  8.      register int length;
  9. {
  10.    memset(b,'\0',length);
  11. }
  12.  
  13. void
  14. bcopy (b1, b2, length)
  15.      register char *b1;
  16.      register char *b2;
  17.      register int length;
  18. {
  19.    memcpy(b1,b2,length);
  20. }
  21.  
  22. int
  23. bcmp (b1, b2, length)
  24.      register char *b1;
  25.      register char *b2;
  26.       register int length;
  27. {
  28.    return memcmp(b1,b2,length);
  29. }
  30.  
  31. void
  32. memory_full ()
  33. {
  34.   printf ("Memory exhausted.\n");
  35.   exit(1);
  36. }
  37.  
  38.  
  39. char *
  40. xmalloc (size)
  41.      int size;
  42. {
  43.   extern char *malloc ();
  44.   register char *ptr = malloc (size);
  45.   if (ptr != 0) return (ptr);
  46.   memory_full ();
  47.   /*NOTREACHED*/
  48. }
  49.  
  50. char *
  51. xrealloc (old, size)
  52.      char *old;
  53.      int size;
  54. {
  55.   extern char *realloc ();
  56.   register char *ptr = realloc (old, size);
  57.   if (ptr != 0) return (ptr);
  58.   memory_full ();
  59.   /*NOTREACHED*/
  60. }
  61.  
  62. char *
  63. xcalloc (number, size)
  64.      int number, size;
  65. {
  66.   extern char *malloc ();
  67.   register int total = number * size;
  68.   register char *ptr = malloc (total);
  69.   if (ptr != 0)
  70.     {
  71.       if (total > 100)
  72.         bzero (ptr, total);
  73.       else {
  74.         /* It's not too long, so loop, zeroing by longs.
  75.            It must be safe because malloc values are always well aligned.  */
  76.         register long *zp = (long *) ptr;
  77.         register long *zl = (long *) (ptr + total - 4);
  78.         register int i = total - 4;
  79.         while (zp < zl)
  80.           *zp++ = 0;
  81.         if (i < 0)
  82.           i = 0;
  83.         while (i < total)
  84.           ptr[i++] = 0;
  85.       }
  86.       return ptr;
  87.     }
  88.   memory_full ();
  89.   /*NOTREACHED*/
  90. }
  91.  
  92. /*
  93.         alloca -- (mostly) portable public-domain implementation -- D A Gwyn
  94.  
  95.         last edit:      86/05/30        rms
  96.            include config.h, since on VMS it renames some symbols.
  97.            Use xmalloc instead of malloc.
  98.  
  99.         This implementation of the PWB library alloca() function,
  100.         which is used to allocate space off the run-time stack so
  101.         that it is automatically reclaimed upon procedure exit,
  102.         was inspired by discussions with J. Q. Johnson of Cornell.
  103.  
  104.         It should work under any C implementation that uses an
  105.         actual procedure stack (as opposed to a linked list of
  106.         frames).  There are some preprocessor constants that can
  107.         be defined when compiling for your specific system, for
  108.         improved efficiency; however, the defaults should be okay.
  109.  
  110.         The general concept of this implementation is to keep
  111.         track of all alloca()-allocated blocks, and reclaim any
  112.         that are found to be deeper in the stack than the current
  113.         invocation.  This heuristic does not reclaim storage as
  114.         soon as it becomes invalid, but it will do so eventually.
  115.  
  116.         As a special case, alloca(0) reclaims storage without
  117.         allocating any.  It is a good idea to use alloca(0) in
  118.         your main control loop, etc. to force garbage collection.
  119. */
  120.  
  121. typedef char    *pointer;               /* generic pointer type */
  122.  
  123. extern void     free();
  124. extern pointer  xmalloc();
  125.  
  126. /*
  127.         Define STACK_DIRECTION if you know the direction of stack
  128.         growth for your system; otherwise it will be automatically
  129.         deduced at run-time.
  130.  
  131.         STACK_DIRECTION > 0 => grows toward higher addresses
  132.         STACK_DIRECTION < 0 => grows toward lower addresses
  133.         STACK_DIRECTION = 0 => direction of growth unknown
  134. */
  135.  
  136. #define STACK_DIRECTION -1
  137.  
  138. #ifndef STACK_DIRECTION
  139. #define STACK_DIRECTION 0               /* direction unknown */
  140. #endif
  141.  
  142. #if STACK_DIRECTION != 0
  143.  
  144. #define STACK_DIR       STACK_DIRECTION /* known at compile-time */
  145.  
  146. #else   /* STACK_DIRECTION == 0; need run-time code */
  147.  
  148. static int      stack_dir;              /* 1 or -1 once known */
  149. #define STACK_DIR       stack_dir
  150.  
  151. static void
  152. find_stack_direction (/* void */)
  153. {
  154.   static char   *addr = NULL;   /* address of first
  155.                                    `dummy', once known */
  156.   auto char     dummy;          /* to get stack address */
  157.  
  158.   if (addr == NULL)
  159.     {                           /* initial entry */
  160.       addr = &dummy;
  161.  
  162.       find_stack_direction ();  /* recurse once */
  163.     }
  164.   else                          /* second entry */
  165.     if (&dummy > addr)
  166.       stack_dir = 1;            /* stack grew upward */
  167.     else
  168.       stack_dir = -1;           /* stack grew downward */
  169. }
  170.  
  171. #endif  /* STACK_DIRECTION == 0 */
  172.  
  173. /*
  174.         An "alloca header" is used to:
  175.         (a) chain together all alloca()ed blocks;
  176.         (b) keep track of stack depth.
  177.  
  178.         It is very important that sizeof(header) agree with malloc()
  179.         alignment chunk size.  The following default should work okay.
  180. */
  181.  
  182. #ifndef ALIGN_SIZE
  183. #define ALIGN_SIZE      sizeof(double)
  184. #endif
  185.  
  186. typedef union hdr
  187. {
  188.   char  align[ALIGN_SIZE];      /* to force sizeof(header) */
  189.   struct
  190.     {
  191.       union hdr *next;          /* for chaining headers */
  192.       char *deep;               /* for stack depth measure */
  193.     } h;
  194. } header;
  195.  
  196. /*
  197.         alloca( size ) returns a pointer to at least `size' bytes of
  198.         storage which will be automatically reclaimed upon exit from
  199.         the procedure that called alloca().  Originally, this space
  200.         was supposed to be taken from the current stack frame of the
  201.         caller, but that method cannot be made to work for some
  202.         implementations of C, for example under Gould's UTX/32.
  203. */
  204.  
  205. static header *last_alloca_header = NULL; /* -> last alloca header */
  206.  
  207. pointer
  208. alloca (size)                   /* returns pointer to storage */
  209.      unsigned   size;           /* # bytes to allocate */
  210. {
  211.   auto char     probe;          /* probes stack depth: */
  212.   register char *depth = &probe;
  213.  
  214. #if STACK_DIRECTION == 0
  215.   if (STACK_DIR == 0)           /* unknown growth direction */
  216.     find_stack_direction ();
  217. #endif
  218.  
  219.                                 /* Reclaim garbage, defined as all alloca()ed storage that
  220.                                    was allocated from deeper in the stack than currently. */
  221.  
  222.   {
  223.     register header     *hp;    /* traverses linked list */
  224.  
  225.     for (hp = last_alloca_header; hp != NULL;)
  226.       if (STACK_DIR > 0 && hp->h.deep > depth
  227.           || STACK_DIR < 0 && hp->h.deep < depth)
  228.         {
  229.           register header       *np = hp->h.next;
  230.  
  231.           free ((pointer) hp);  /* collect garbage */
  232.  
  233.           hp = np;              /* -> next header */
  234.         }
  235.       else
  236.         break;                  /* rest are not deeper */
  237.  
  238.     last_alloca_header = hp;    /* -> last valid storage */
  239.   }
  240.  
  241.   if (size == 0)
  242.     return NULL;                /* no allocation required */
  243.  
  244.   /* Allocate combined header + user data storage. */
  245.  
  246.   {
  247.     register pointer    new = xmalloc (sizeof (header) + size);
  248.     /* address of header */
  249.  
  250.     ((header *)new)->h.next = last_alloca_header;
  251.     ((header *)new)->h.deep = depth;
  252.  
  253.     last_alloca_header = (header *)new;
  254.  
  255.     /* User storage begins just after header. */
  256.  
  257.     return (pointer)((char *)new + sizeof(header));
  258.   }
  259. }
  260.  
  261.  
  262.