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