ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/ealloc.c
Revision: 2.11
Committed: Wed Jan 19 00:59:33 2022 UTC (2 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R4, HEAD
Changes since 2.10: +2 -2 lines
Log Message:
fix: bad bug introduced in last change to ecalloc() -- recursive loop!

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.11 static const char RCSid[] = "$Id: ealloc.c,v 2.10 2022/01/15 02:00:21 greg Exp $";
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 schorsch 2.5 #include "rterror.h"
15 schorsch 2.9 #include "rtmisc.h"
16 greg 1.1
17 greg 2.10 void * /* return pointer to n uninitialized bytes */
18 schorsch 2.9 emalloc(size_t n)
19 greg 1.1 {
20 greg 2.10 void *cp;
21 greg 1.1
22     if (n == 0)
23     return(NULL);
24    
25 schorsch 2.9 if ((cp = malloc(n)) != NULL)
26 greg 1.1 return(cp);
27    
28     eputs("Out of memory in emalloc\n");
29     quit(1);
30 schorsch 2.8 return NULL; /* pro forma return */
31 greg 1.1 }
32    
33    
34 greg 2.10 void * /* return pointer to initialized memory */
35     ecalloc(size_t ne, size_t es)
36 greg 1.1 {
37 greg 2.10 void *cp;
38 greg 1.1
39 greg 2.10 if (!ne | !es)
40 greg 1.1 return(NULL);
41    
42 greg 2.11 if ((cp = calloc(ne, es)) != NULL)
43 greg 2.10 return(cp);
44    
45     eputs("Out of memory in ecalloc\n");
46     quit(1);
47     return(NULL); /* pro forma return */
48 greg 1.1 }
49    
50    
51 greg 2.10 void * /* reallocate cp to size n */
52     erealloc(void *cp, size_t n)
53 greg 1.1 {
54     if (n == 0) {
55     if (cp != NULL)
56 schorsch 2.9 free(cp);
57 greg 1.1 return(NULL);
58     }
59    
60     if (cp == NULL)
61 schorsch 2.9 cp = malloc(n);
62 greg 1.1 else
63 schorsch 2.9 cp = realloc(cp, n);
64 greg 1.1
65     if (cp != NULL)
66     return(cp);
67    
68     eputs("Out of memory in erealloc\n");
69     quit(1);
70 schorsch 2.8 return NULL; /* pro forma return */
71 greg 1.1 }
72    
73    
74 greg 2.10 void /* free memory allocated by above */
75     efree(void *cp)
76 greg 1.1 {
77 greg 2.10 if (cp)
78     free(cp);
79 greg 1.1 }