1 /*-
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Arthur David Olson of the National Cancer Institute.
7 *
8 * %sccs.include.redist.c%
9 */
10
11 #ifndef lint
12 static char sccsid[] = "@(#)ialloc.c 8.1 (Berkeley) 06/08/93";
13 #endif /* not lint */
14
15 #ifdef notdef
16 static char elsieid[] = "@(#)ialloc.c 8.18";
17 #endif
18
19 /*LINTLIBRARY*/
20
21 #include <string.h>
22 #include <stdlib.h>
23
24 #ifdef MAL
25 #define NULLMAL(x) ((x) == NULL || (x) == MAL)
26 #else /* !defined MAL */
27 #define NULLMAL(x) ((x) == NULL)
28 #endif /* !defined MAL */
29
30 #define nonzero(n) (((n) == 0) ? 1 : (n))
31
32 char * icalloc __P((int nelem, int elsize));
33 char * icatalloc __P((char * old, const char * new));
34 char * icpyalloc __P((const char * string));
35 char * imalloc __P((int n));
36 char * irealloc __P((char * pointer, int size));
37 void ifree __P((char * pointer));
38
39 char *
imalloc(n)40 imalloc(n)
41 const int n;
42 {
43 #ifdef MAL
44 register char * result;
45
46 result = malloc((size_t) nonzero(n));
47 return NULLMAL(result) ? NULL : result;
48 #else /* !defined MAL */
49 return malloc((size_t) nonzero(n));
50 #endif /* !defined MAL */
51 }
52
53 char *
icalloc(nelem,elsize)54 icalloc(nelem, elsize)
55 int nelem;
56 int elsize;
57 {
58 if (nelem == 0 || elsize == 0)
59 nelem = elsize = 1;
60 return calloc((size_t) nelem, (size_t) elsize);
61 }
62
63 char *
irealloc(pointer,size)64 irealloc(pointer, size)
65 char * const pointer;
66 const int size;
67 {
68 if (NULLMAL(pointer))
69 return imalloc(size);
70 return realloc((void *) pointer, (size_t) nonzero(size));
71 }
72
73 char *
icatalloc(old,new)74 icatalloc(old, new)
75 char * const old;
76 const char * const new;
77 {
78 register char * result;
79 register oldsize, newsize;
80
81 newsize = NULLMAL(new) ? 0 : strlen(new);
82 if (NULLMAL(old))
83 oldsize = 0;
84 else if (newsize == 0)
85 return old;
86 else oldsize = strlen(old);
87 if ((result = irealloc(old, oldsize + newsize + 1)) != NULL)
88 if (!NULLMAL(new))
89 (void) strcpy(result + oldsize, new);
90 return result;
91 }
92
93 char *
icpyalloc(string)94 icpyalloc(string)
95 const char * const string;
96 {
97 return icatalloc((char *) NULL, string);
98 }
99
100 void
ifree(p)101 ifree(p)
102 char * const p;
103 {
104 if (!NULLMAL(p))
105 (void) free(p);
106 }
107
108 void
icfree(p)109 icfree(p)
110 char * const p;
111 {
112 if (!NULLMAL(p))
113 (void) free(p);
114 }
115