home *** CD-ROM | disk | FTP | other *** search
/ Encyclopedia of Graphics File Formats Companion / GFF_CD.ISO / formats / ttddd / spec / t3d_doc / igensurf.zoo / src / ealloc.c < prev    next >
C/C++ Source or Header  |  1991-09-25  |  1KB  |  85 lines

  1. /* Copyright 1988 Regents of the University of California */
  2.  
  3. #ifndef lint
  4. static char SCCSid[] = "@(#)ealloc.c 1.2 2/2/89 LBL";
  5. #endif
  6.  
  7. /*
  8.  *  ealloc.c - memory routines which call exit on error.
  9.  */
  10.  
  11.  
  12. #include  <stdio.h>
  13.  
  14.  
  15. char  *malloc(), *realloc(), *Emalloc(), *Ecalloc(), *Erealloc();
  16.  
  17.  
  18. char *
  19. Emalloc(n)            /* return pointer to n uninitialized bytes */
  20. unsigned  n;
  21. {
  22.     register char  *cp;
  23.     
  24.     if (n == 0)
  25.         return(NULL);
  26.  
  27.     if ((cp = malloc(n)) != NULL)
  28.         return(cp);
  29.  
  30.     fprintf(stderr, "Out of memory in emalloc\n");
  31.     exit(1);
  32. }
  33.  
  34.  
  35. char *
  36. Ecalloc(ne, es)            /* return pointer to initialized memory */
  37. register unsigned  ne;
  38. unsigned  es;
  39. {
  40.     register char  *cp;
  41.     
  42.     ne *= es;
  43.     if (ne == 0)
  44.         return(NULL);
  45.  
  46.     if ((cp = malloc(ne)) == NULL) {
  47.         fprintf(stderr, "Out of memory in ecalloc\n");
  48.         exit(1);
  49.     }
  50.     cp += ne;
  51.     while (ne--) *--cp = 0;
  52.     return(cp);
  53. }
  54.  
  55.  
  56. char *
  57. Erealloc(cp, n)            /* reallocate cp to size n */
  58. register char  *cp;
  59. unsigned  n;
  60. {
  61.     if (n == 0) {
  62.         if (cp != NULL)
  63.             free(cp);
  64.         return(NULL);
  65.     }
  66.  
  67.     if (cp == NULL)
  68.         cp = malloc(n);
  69.     else 
  70.         cp = realloc(cp, n);
  71.  
  72.     if (cp != NULL)
  73.         return(cp);
  74.  
  75.     fprintf(stderr, "Out of memory in erealloc\n");
  76.     exit(1);
  77. }
  78.  
  79.  
  80. Efree(cp)            /* free memory allocated by above */
  81. char  *cp;
  82. {
  83.     free(cp);
  84. }
  85.