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