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