Back to index

radiance  4R0+20100331
ealloc.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: ealloc.c,v 2.9 2004/03/28 20:33:12 schorsch Exp $";
00003 #endif
00004 /*
00005  *  ealloc.c - memory routines which call quit on error.
00006  */
00007 
00008 #include "copyright.h"
00009 
00010 
00011 #include  <stdio.h>
00012 #include  <stdlib.h>
00013 
00014 #include "rterror.h"
00015 #include "rtmisc.h"
00016 
00017 extern void * /* return pointer to n uninitialized bytes */
00018 emalloc(size_t  n)
00019 {
00020        register void  *cp;
00021        
00022        if (n == 0)
00023               return(NULL);
00024 
00025        if ((cp = malloc(n)) != NULL)
00026               return(cp);
00027 
00028        eputs("Out of memory in emalloc\n");
00029        quit(1);
00030        return NULL; /* pro forma return */
00031 }
00032 
00033 
00034 extern void *               /* return pointer to initialized memory */
00035 ecalloc(register size_t ne, size_t es)
00036 {
00037        register char  *cp;
00038        
00039        ne *= es;
00040        if (ne == 0)
00041               return(NULL);
00042 
00043        if ((cp = malloc(ne)) == NULL) {
00044               eputs("Out of memory in ecalloc\n");
00045               quit(1);
00046        }
00047        cp += ne;
00048        while (ne--)
00049               *--cp = 0;
00050        return(cp);
00051 }
00052 
00053 
00054 extern void *               /* reallocate cp to size n */
00055 erealloc(register void  *cp, size_t  n)
00056 {
00057        if (n == 0) {
00058               if (cp != NULL)
00059                      free(cp);
00060               return(NULL);
00061        }
00062 
00063        if (cp == NULL)
00064               cp = malloc(n);
00065        else 
00066               cp = realloc(cp, n);
00067 
00068        if (cp != NULL)
00069               return(cp);
00070 
00071        eputs("Out of memory in erealloc\n");
00072        quit(1);
00073        return NULL; /* pro forma return */
00074 }
00075 
00076 
00077 extern void                 /* free memory allocated by above */
00078 efree(void  *cp)
00079 {
00080        free(cp);
00081 }