| 1 |
#ifndef lint |
| 2 |
static const char RCSid[] = "$Id: ealloc.c,v 2.10 2022/01/15 02:00:21 greg Exp $"; |
| 3 |
#endif |
| 4 |
/* |
| 5 |
* ealloc.c - memory routines which call quit on error. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "copyright.h" |
| 9 |
|
| 10 |
|
| 11 |
#include <stdio.h> |
| 12 |
#include <stdlib.h> |
| 13 |
|
| 14 |
#include "rterror.h" |
| 15 |
#include "rtmisc.h" |
| 16 |
|
| 17 |
void * /* return pointer to n uninitialized bytes */ |
| 18 |
emalloc(size_t n) |
| 19 |
{ |
| 20 |
void *cp; |
| 21 |
|
| 22 |
if (n == 0) |
| 23 |
return(NULL); |
| 24 |
|
| 25 |
if ((cp = malloc(n)) != NULL) |
| 26 |
return(cp); |
| 27 |
|
| 28 |
eputs("Out of memory in emalloc\n"); |
| 29 |
quit(1); |
| 30 |
return NULL; /* pro forma return */ |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
void * /* return pointer to initialized memory */ |
| 35 |
ecalloc(size_t ne, size_t es) |
| 36 |
{ |
| 37 |
void *cp; |
| 38 |
|
| 39 |
if (!ne | !es) |
| 40 |
return(NULL); |
| 41 |
|
| 42 |
if ((cp = calloc(ne, es)) != NULL) |
| 43 |
return(cp); |
| 44 |
|
| 45 |
eputs("Out of memory in ecalloc\n"); |
| 46 |
quit(1); |
| 47 |
return(NULL); /* pro forma return */ |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
void * /* reallocate cp to size n */ |
| 52 |
erealloc(void *cp, size_t n) |
| 53 |
{ |
| 54 |
if (n == 0) { |
| 55 |
if (cp != NULL) |
| 56 |
free(cp); |
| 57 |
return(NULL); |
| 58 |
} |
| 59 |
|
| 60 |
if (cp == NULL) |
| 61 |
cp = malloc(n); |
| 62 |
else |
| 63 |
cp = realloc(cp, n); |
| 64 |
|
| 65 |
if (cp != NULL) |
| 66 |
return(cp); |
| 67 |
|
| 68 |
eputs("Out of memory in erealloc\n"); |
| 69 |
quit(1); |
| 70 |
return NULL; /* pro forma return */ |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
void /* free memory allocated by above */ |
| 75 |
efree(void *cp) |
| 76 |
{ |
| 77 |
if (cp) |
| 78 |
free(cp); |
| 79 |
} |