home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / communic / pcmail / main / myalloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-05  |  1.1 KB  |  117 lines

  1. #include <stdio.h>
  2.  
  3. #include <varargs.h>
  4.  
  5.  
  6.  
  7. extern char *progname;
  8.  
  9.  
  10.  
  11. #include "defs.h"
  12.  
  13.  
  14.  
  15. /* fatal - another way to terminate */
  16.  
  17.  
  18.  
  19. /* VARARGS */
  20.  
  21.  
  22.  
  23. public  fatal(va_alist) 
  24.  
  25. va_dcl
  26.  
  27. {
  28.  
  29.     va_list ap;
  30.  
  31.     char   *fmt;
  32.  
  33.  
  34.  
  35.     if (progname && *progname)
  36.  
  37.     (void) fprintf(stderr, "%s: ", progname);
  38.  
  39.     va_start(ap);
  40.  
  41.     fmt = va_arg(ap, char *);
  42.  
  43.     (void) vfprintf(stderr, fmt, ap);
  44.  
  45.     va_end(ap);
  46.  
  47.     (void) abort();
  48.  
  49.     exit(1);
  50.  
  51. }
  52.  
  53.  
  54.  
  55. /* myalloc - allocate memory or terminate */
  56.  
  57.  
  58.  
  59. public char *myalloc(size)
  60.  
  61. unsigned size;
  62.  
  63. {
  64.  
  65.     register char *p;
  66.  
  67.     char   *malloc();
  68.  
  69.  
  70.  
  71.     if ((p = malloc(size)) == 0)
  72.  
  73.     fatal("memory allocation error");
  74.  
  75.     return (p);
  76.  
  77. }
  78.  
  79.  
  80.  
  81. /* myrealloc - extend memory or terminate (allows NULL pointer) */
  82.  
  83.  
  84.  
  85. public char *myrealloc(ptr, size)
  86.  
  87. char   *ptr;
  88.  
  89. unsigned size;
  90.  
  91. {
  92.  
  93.     register char *p;
  94.  
  95.     char   *realloc();
  96.  
  97.  
  98.  
  99.     if (ptr == 0) {
  100.  
  101.     return (myalloc(size));
  102.  
  103.     } else if ((p = realloc(ptr, size)) == 0) {
  104.  
  105.     fatal("memory allocation error");
  106.  
  107.     /* NOTREACHED */
  108.  
  109.     } else {
  110.  
  111.     return (p);
  112.  
  113.     }
  114.  
  115. }
  116.  
  117.