148425Sbostic /*-
2*62584Sbostic * Copyright (c) 1991, 1993
3*62584Sbostic * The Regents of the University of California. All rights reserved.
448425Sbostic *
548425Sbostic * This code is derived from software contributed to Berkeley by
648425Sbostic * Arthur David Olson of the National Cancer Institute.
748425Sbostic *
848425Sbostic * %sccs.include.redist.c%
948425Sbostic */
1048425Sbostic
1139982Sbostic #ifndef lint
12*62584Sbostic static char sccsid[] = "@(#)ialloc.c 8.1 (Berkeley) 06/08/93";
1348425Sbostic #endif /* not lint */
1448425Sbostic
1548425Sbostic #ifdef notdef
1639982Sbostic static char elsieid[] = "@(#)ialloc.c 8.18";
1748425Sbostic #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 *
imalloc(n)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 *
icalloc(nelem,elsize)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 *
irealloc(pointer,size)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 *
icatalloc(old,new)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 *
icpyalloc(string)9439982Sbostic icpyalloc(string)
9539982Sbostic const char * const string;
9639982Sbostic {
9739982Sbostic return icatalloc((char *) NULL, string);
9839982Sbostic }
9939982Sbostic
10039982Sbostic void
ifree(p)10139982Sbostic ifree(p)
10239982Sbostic char * const p;
10339982Sbostic {
10439982Sbostic if (!NULLMAL(p))
10539982Sbostic (void) free(p);
10639982Sbostic }
10739982Sbostic
10839982Sbostic void
icfree(p)10939982Sbostic icfree(p)
11039982Sbostic char * const p;
11139982Sbostic {
11239982Sbostic if (!NULLMAL(p))
11339982Sbostic (void) free(p);
11439982Sbostic }
115