michael@0: /* michael@0: ** This file is in the public domain, so clarified as of michael@0: ** 2006-07-17 by Arthur David Olson. michael@0: */ michael@0: michael@0: #ifndef lint michael@0: #ifndef NOID michael@0: static char elsieid[] = "@(#)ialloc.c 8.30"; michael@0: #endif /* !defined NOID */ michael@0: #endif /* !defined lint */ michael@0: michael@0: /*LINTLIBRARY*/ michael@0: michael@0: #include "private.h" michael@0: michael@0: #define nonzero(n) (((n) == 0) ? 1 : (n)) michael@0: michael@0: char * michael@0: imalloc(n) michael@0: const int n; michael@0: { michael@0: return malloc((size_t) nonzero(n)); michael@0: } michael@0: michael@0: char * michael@0: icalloc(nelem, elsize) michael@0: int nelem; michael@0: int elsize; michael@0: { michael@0: if (nelem == 0 || elsize == 0) michael@0: nelem = elsize = 1; michael@0: return calloc((size_t) nelem, (size_t) elsize); michael@0: } michael@0: michael@0: void * michael@0: irealloc(pointer, size) michael@0: void * const pointer; michael@0: const int size; michael@0: { michael@0: if (pointer == NULL) michael@0: return imalloc(size); michael@0: return realloc((void *) pointer, (size_t) nonzero(size)); michael@0: } michael@0: michael@0: char * michael@0: icatalloc(old, new) michael@0: char * const old; michael@0: const char * const new; michael@0: { michael@0: register char * result; michael@0: register int oldsize, newsize; michael@0: michael@0: newsize = (new == NULL) ? 0 : strlen(new); michael@0: if (old == NULL) michael@0: oldsize = 0; michael@0: else if (newsize == 0) michael@0: return old; michael@0: else oldsize = strlen(old); michael@0: if ((result = irealloc(old, oldsize + newsize + 1)) != NULL) michael@0: if (new != NULL) michael@0: (void) strcpy(result + oldsize, new); michael@0: return result; michael@0: } michael@0: michael@0: char * michael@0: icpyalloc(string) michael@0: const char * const string; michael@0: { michael@0: return icatalloc((char *) NULL, string); michael@0: } michael@0: michael@0: void michael@0: ifree(p) michael@0: char * const p; michael@0: { michael@0: if (p != NULL) michael@0: (void) free(p); michael@0: } michael@0: michael@0: void michael@0: icfree(p) michael@0: char * const p; michael@0: { michael@0: if (p != NULL) michael@0: (void) free(p); michael@0: }