ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/ealloc.c
Revision: 2.3
Committed: Tue Feb 25 02:47:21 2003 UTC (21 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R5
Changes since 2.2: +1 -56 lines
Log Message:
Replaced inline copyright notice with #include "copyright.h"

File Contents

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