Back to index

glibc  2.9
ialloc.c
Go to the documentation of this file.
00001 /*
00002 ** This file is in the public domain, so clarified as of
00003 ** 2006-07-17 by Arthur David Olson.
00004 */
00005 
00006 #ifndef lint
00007 #ifndef NOID
00008 static char   elsieid[] = "@(#)ialloc.c   8.30";
00009 #endif /* !defined NOID */
00010 #endif /* !defined lint */
00011 
00012 /*LINTLIBRARY*/
00013 
00014 #include "private.h"
00015 
00016 #define nonzero(n)   (((n) == 0) ? 1 : (n))
00017 
00018 char *
00019 imalloc(n)
00020 const int     n;
00021 {
00022        return malloc((size_t) nonzero(n));
00023 }
00024 
00025 char *
00026 icalloc(nelem, elsize)
00027 int    nelem;
00028 int    elsize;
00029 {
00030        if (nelem == 0 || elsize == 0)
00031               nelem = elsize = 1;
00032        return calloc((size_t) nelem, (size_t) elsize);
00033 }
00034 
00035 void *
00036 irealloc(pointer, size)
00037 void * const  pointer;
00038 const int     size;
00039 {
00040        if (pointer == NULL)
00041               return imalloc(size);
00042        return realloc((void *) pointer, (size_t) nonzero(size));
00043 }
00044 
00045 char *
00046 icatalloc(old, new)
00047 char * const         old;
00048 const char * const   new;
00049 {
00050        register char *      result;
00051        register int  oldsize, newsize;
00052 
00053        newsize = (new == NULL) ? 0 : strlen(new);
00054        if (old == NULL)
00055               oldsize = 0;
00056        else if (newsize == 0)
00057               return old;
00058        else   oldsize = strlen(old);
00059        if ((result = irealloc(old, oldsize + newsize + 1)) != NULL)
00060               if (new != NULL)
00061                      (void) strcpy(result + oldsize, new);
00062        return result;
00063 }
00064 
00065 char *
00066 icpyalloc(string)
00067 const char * const   string;
00068 {
00069        return icatalloc((char *) NULL, string);
00070 }
00071 
00072 void
00073 ifree(p)
00074 char * const  p;
00075 {
00076        if (p != NULL)
00077               (void) free(p);
00078 }
00079 
00080 void
00081 icfree(p)
00082 char * const  p;
00083 {
00084        if (p != NULL)
00085               (void) free(p);
00086 }