1 /* 2 ** This file is in the public domain, so clarified as of 3 ** 2006-07-17 by Arthur David Olson. 4 */ 5 6 /* 7 * $FreeBSD: src/usr.sbin/zic/ialloc.c,v 1.5 1999/08/28 01:21:18 peter Exp $ 8 */ 9 /*LINTLIBRARY*/ 10 11 #include "private.h" 12 13 #define nonzero(n) (((n) == 0) ? 1 : (n)) 14 15 char * 16 imalloc(const int n) 17 { 18 return malloc((size_t) nonzero(n)); 19 } 20 21 char * 22 icalloc(int nelem, int elsize) 23 { 24 if (nelem == 0 || elsize == 0) 25 nelem = elsize = 1; 26 return calloc((size_t) nelem, (size_t) elsize); 27 } 28 29 void * 30 irealloc(void *const pointer, const int size) 31 { 32 if (pointer == NULL) 33 return imalloc(size); 34 return realloc((void *) pointer, (size_t) nonzero(size)); 35 } 36 37 char * 38 icatalloc(char *const old, const char *new) 39 { 40 char *result; 41 int oldsize, newsize; 42 43 newsize = (new == NULL) ? 0 : strlen(new); 44 if (old == NULL) 45 oldsize = 0; 46 else if (newsize == 0) 47 return old; 48 else oldsize = strlen(old); 49 if ((result = irealloc(old, oldsize + newsize + 1)) != NULL) 50 if (new != NULL) 51 strcpy(result + oldsize, new); 52 return result; 53 } 54 55 char * 56 icpyalloc(const char *string) 57 { 58 return icatalloc(NULL, string); 59 } 60 61 void 62 ifree(char * const p) 63 { 64 if (p != NULL) 65 free(p); 66 } 67 68 void 69 icfree(char * const p) 70 { 71 if (p != NULL) 72 free(p); 73 } 74