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