| 1 |
greg |
1.1 |
#ifndef lint
|
| 2 |
schorsch |
2.5 |
static const char RCSid[] = "$Id: ealloc.c,v 2.4 2003/04/23 00:52:33 greg 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 |
|
|
char *
|
| 17 |
|
|
emalloc(n) /* return pointer to n uninitialized bytes */
|
| 18 |
greg |
2.2 |
unsigned int n;
|
| 19 |
greg |
1.1 |
{
|
| 20 |
|
|
register char *cp;
|
| 21 |
|
|
|
| 22 |
|
|
if (n == 0)
|
| 23 |
|
|
return(NULL);
|
| 24 |
|
|
|
| 25 |
greg |
2.2 |
if ((cp = (char *)malloc(n)) != NULL)
|
| 26 |
greg |
1.1 |
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 |
greg |
2.2 |
register unsigned int ne;
|
| 36 |
|
|
unsigned int es;
|
| 37 |
greg |
1.1 |
{
|
| 38 |
|
|
register char *cp;
|
| 39 |
|
|
|
| 40 |
|
|
ne *= es;
|
| 41 |
|
|
if (ne == 0)
|
| 42 |
|
|
return(NULL);
|
| 43 |
|
|
|
| 44 |
greg |
2.2 |
if ((cp = (char *)malloc(ne)) == NULL) {
|
| 45 |
greg |
1.1 |
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 |
greg |
2.2 |
unsigned int n;
|
| 59 |
greg |
1.1 |
{
|
| 60 |
|
|
if (n == 0) {
|
| 61 |
|
|
if (cp != NULL)
|
| 62 |
greg |
2.4 |
free((void *)cp);
|
| 63 |
greg |
1.1 |
return(NULL);
|
| 64 |
|
|
}
|
| 65 |
|
|
|
| 66 |
|
|
if (cp == NULL)
|
| 67 |
greg |
2.2 |
cp = (char *)malloc(n);
|
| 68 |
greg |
1.1 |
else
|
| 69 |
greg |
2.2 |
cp = (char *)realloc((void *)cp, n);
|
| 70 |
greg |
1.1 |
|
| 71 |
|
|
if (cp != NULL)
|
| 72 |
|
|
return(cp);
|
| 73 |
|
|
|
| 74 |
|
|
eputs("Out of memory in erealloc\n");
|
| 75 |
|
|
quit(1);
|
| 76 |
|
|
}
|
| 77 |
|
|
|
| 78 |
|
|
|
| 79 |
greg |
2.2 |
void
|
| 80 |
greg |
1.1 |
efree(cp) /* free memory allocated by above */
|
| 81 |
|
|
char *cp;
|
| 82 |
|
|
{
|
| 83 |
greg |
2.4 |
free((void *)cp);
|
| 84 |
greg |
1.1 |
}
|