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