ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/ealloc.c
(Generate patch)

Comparing ray/src/common/ealloc.c (file contents):
Revision 2.6 by schorsch, Wed Jul 30 10:11:06 2003 UTC vs.
Revision 2.10 by greg, Sat Jan 15 02:00:21 2022 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines