1*48425Sbostic /*- 2*48425Sbostic * Copyright (c) 1991 The Regents of the University of California. 3*48425Sbostic * All rights reserved. 4*48425Sbostic * 5*48425Sbostic * This code is derived from software contributed to Berkeley by 6*48425Sbostic * Arthur David Olson of the National Cancer Institute. 7*48425Sbostic * 8*48425Sbostic * %sccs.include.redist.c% 9*48425Sbostic */ 10*48425Sbostic 1139982Sbostic #ifndef lint 12*48425Sbostic static char sccsid[] = "@(#)ialloc.c 5.3 (Berkeley) 04/20/91"; 13*48425Sbostic #endif /* not lint */ 14*48425Sbostic 15*48425Sbostic #ifdef notdef 1639982Sbostic static char elsieid[] = "@(#)ialloc.c 8.18"; 17*48425Sbostic #endif 1839982Sbostic 1939982Sbostic /*LINTLIBRARY*/ 2039982Sbostic 2146793Sbostic #include <string.h> 2246793Sbostic #include <stdlib.h> 2339982Sbostic 2439982Sbostic #ifdef MAL 2539982Sbostic #define NULLMAL(x) ((x) == NULL || (x) == MAL) 2639982Sbostic #else /* !defined MAL */ 2739982Sbostic #define NULLMAL(x) ((x) == NULL) 2839982Sbostic #endif /* !defined MAL */ 2939982Sbostic 3039982Sbostic #define nonzero(n) (((n) == 0) ? 1 : (n)) 3139982Sbostic 3246793Sbostic char * icalloc __P((int nelem, int elsize)); 3346793Sbostic char * icatalloc __P((char * old, const char * new)); 3446793Sbostic char * icpyalloc __P((const char * string)); 3546793Sbostic char * imalloc __P((int n)); 3646793Sbostic char * irealloc __P((char * pointer, int size)); 3746793Sbostic void ifree __P((char * pointer)); 3839982Sbostic 3939982Sbostic char * 4039982Sbostic imalloc(n) 4139982Sbostic const int n; 4239982Sbostic { 4339982Sbostic #ifdef MAL 4439982Sbostic register char * result; 4539982Sbostic 4646793Sbostic result = malloc((size_t) nonzero(n)); 4739982Sbostic return NULLMAL(result) ? NULL : result; 4839982Sbostic #else /* !defined MAL */ 4946793Sbostic return malloc((size_t) nonzero(n)); 5039982Sbostic #endif /* !defined MAL */ 5139982Sbostic } 5239982Sbostic 5339982Sbostic char * 5439982Sbostic icalloc(nelem, elsize) 5539982Sbostic int nelem; 5639982Sbostic int elsize; 5739982Sbostic { 5839982Sbostic if (nelem == 0 || elsize == 0) 5939982Sbostic nelem = elsize = 1; 6046793Sbostic return calloc((size_t) nelem, (size_t) elsize); 6139982Sbostic } 6239982Sbostic 6339982Sbostic char * 6439982Sbostic irealloc(pointer, size) 6539982Sbostic char * const pointer; 6639982Sbostic const int size; 6739982Sbostic { 6839982Sbostic if (NULLMAL(pointer)) 6939982Sbostic return imalloc(size); 7046793Sbostic return realloc((void *) pointer, (size_t) nonzero(size)); 7139982Sbostic } 7239982Sbostic 7339982Sbostic char * 7439982Sbostic icatalloc(old, new) 7539982Sbostic char * const old; 7639982Sbostic const char * const new; 7739982Sbostic { 7839982Sbostic register char * result; 7939982Sbostic register oldsize, newsize; 8039982Sbostic 8139982Sbostic newsize = NULLMAL(new) ? 0 : strlen(new); 8239982Sbostic if (NULLMAL(old)) 8339982Sbostic oldsize = 0; 8439982Sbostic else if (newsize == 0) 8539982Sbostic return old; 8639982Sbostic else oldsize = strlen(old); 8739982Sbostic if ((result = irealloc(old, oldsize + newsize + 1)) != NULL) 8839982Sbostic if (!NULLMAL(new)) 8939982Sbostic (void) strcpy(result + oldsize, new); 9039982Sbostic return result; 9139982Sbostic } 9239982Sbostic 9339982Sbostic char * 9439982Sbostic icpyalloc(string) 9539982Sbostic const char * const string; 9639982Sbostic { 9739982Sbostic return icatalloc((char *) NULL, string); 9839982Sbostic } 9939982Sbostic 10039982Sbostic void 10139982Sbostic ifree(p) 10239982Sbostic char * const p; 10339982Sbostic { 10439982Sbostic if (!NULLMAL(p)) 10539982Sbostic (void) free(p); 10639982Sbostic } 10739982Sbostic 10839982Sbostic void 10939982Sbostic icfree(p) 11039982Sbostic char * const p; 11139982Sbostic { 11239982Sbostic if (!NULLMAL(p)) 11339982Sbostic (void) free(p); 11439982Sbostic } 115