| 1 |
/* |
| 2 |
|
| 3 |
#ifndef lint |
| 4 |
static char SCCSid[] = "$SunId$ LBL"; |
| 5 |
#endif |
| 6 |
* ealloc.c - memory routines which call quit on error. |
| 7 |
*/ |
| 8 |
|
| 9 |
|
| 10 |
#include <stdio.h> |
| 11 |
|
| 12 |
|
| 13 |
char *malloc(), *realloc(), *emalloc(), *ecalloc(), *erealloc(); |
| 14 |
|
| 15 |
|
| 16 |
char * |
| 17 |
emalloc(n) /* return pointer to n uninitialized bytes */ |
| 18 |
unsigned n; |
| 19 |
{ |
| 20 |
register char *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 |
} |
| 31 |
|
| 32 |
|
| 33 |
char * |
| 34 |
ecalloc(ne, es) /* return pointer to initialized memory */ |
| 35 |
register unsigned ne; |
| 36 |
unsigned es; |
| 37 |
{ |
| 38 |
register char *cp; |
| 39 |
|
| 40 |
ne *= es; |
| 41 |
if (ne == 0) |
| 42 |
return(NULL); |
| 43 |
|
| 44 |
if ((cp = malloc(ne)) == NULL) { |
| 45 |
eputs("Out of memory in ecalloc\n"); |
| 46 |
quit(1); |
| 47 |
} |
| 48 |
cp += ne; |
| 49 |
while (ne--) |
| 50 |
*--cp = 0; |
| 51 |
return(cp); |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
char * |
| 56 |
erealloc(cp, n) /* reallocate cp to size n */ |
| 57 |
register char *cp; |
| 58 |
unsigned n; |
| 59 |
{ |
| 60 |
if (n == 0) { |
| 61 |
if (cp != NULL) |
| 62 |
free(cp); |
| 63 |
return(NULL); |
| 64 |
} |
| 65 |
|
| 66 |
if (cp == NULL) |
| 67 |
cp = malloc(n); |
| 68 |
else |
| 69 |
cp = realloc(cp, n); |
| 70 |
|
| 71 |
if (cp != NULL) |
| 72 |
return(cp); |
| 73 |
|
| 74 |
eputs("Out of memory in erealloc\n"); |
| 75 |
quit(1); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
efree(cp) /* free memory allocated by above */ |
| 80 |
char *cp; |
| 81 |
{ |
| 82 |
free(cp); |
| 83 |
} |