home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v941.tgz / icon.v941src.tar / icon.v941src / src / common / alloc.c next >
C/C++ Source or Header  |  2001-12-12  |  1KB  |  66 lines

  1. /*
  2.  * alloc.c -- allocation routines for the Icon compiler
  3.  */
  4.  
  5. #include "../h/gsupport.h"
  6.  
  7. #ifdef TypTrc
  8.    int typealloc = 0;        /* type allocation switch */
  9.    long typespace = 0;        /* type allocation amount */
  10. #endif                    /* TypTrc */
  11.  
  12. /*
  13.  * salloc - allocate and initialize string
  14.  */
  15.  
  16. char *salloc(s)
  17. char *s;
  18.    {
  19.    register char *s1;
  20.  
  21.    s1 = (char *)malloc(strlen(s) + 1);
  22.    if (s1 == NULL) {
  23.       fprintf(stderr, "salloc(%d): out of memory\n", (int)strlen(s) + 1);
  24.       exit(EXIT_FAILURE);
  25.       }
  26.    return strcpy(s1, s);
  27.    }
  28.  
  29. /*
  30.  * alloc - allocate n bytes
  31.  */
  32.  
  33. pointer alloc(n)
  34. unsigned int n;
  35.    {
  36.    register pointer a;
  37.  
  38. #ifdef AllocTrace
  39.    static int sum = 0;
  40. #endif                    /* AllocTrace */
  41.  
  42. #ifdef TypTrc
  43.    if (typealloc)
  44.       typespace += (long)n;
  45. #endif                    /* TypTrc */
  46.  
  47. #ifdef AllocTrace
  48.    sum = sum + n;
  49.    if (sum > 5000) {
  50.       fprintf(stderr, ".");
  51.       fflush(stderr);
  52.       sum = 0;
  53.       };
  54. #endif                    /* AllocTrace */
  55.  
  56.    if (n == 0)                /* Work-around for 0 allocation */
  57.       n = 1;
  58.  
  59.    a = calloc(n, sizeof(char));
  60.    if (a == NULL) {
  61.       fprintf(stderr, "alloc(%d): out of memory\n", (int)n);
  62.       exit(EXIT_FAILURE);
  63.       }
  64.    return a;
  65.    }
  66.