Commit | Line | Data |
---|---|---|
15637ed4 RG |
1 | /*- |
2 | * Copyright (c) 1991 The Regents of the University of California. | |
3 | * 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 | * Redistribution and use in source and binary forms, with or without | |
9 | * modification, are permitted provided that the following conditions | |
10 | * are met: | |
11 | * 1. Redistributions of source code must retain the above copyright | |
12 | * notice, this list of conditions and the following disclaimer. | |
13 | * 2. Redistributions in binary form must reproduce the above copyright | |
14 | * notice, this list of conditions and the following disclaimer in the | |
15 | * documentation and/or other materials provided with the distribution. | |
16 | * 3. All advertising materials mentioning features or use of this software | |
17 | * must display the following acknowledgement: | |
18 | * This product includes software developed by the University of | |
19 | * California, Berkeley and its contributors. | |
20 | * 4. Neither the name of the University nor the names of its contributors | |
21 | * may be used to endorse or promote products derived from this software | |
22 | * without specific prior written permission. | |
23 | * | |
24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
34 | * SUCH DAMAGE. | |
35 | */ | |
36 | ||
37 | #ifndef lint | |
38 | static char sccsid[] = "@(#)ialloc.c 5.3 (Berkeley) 4/20/91"; | |
39 | #endif /* not lint */ | |
40 | ||
41 | #ifdef notdef | |
42 | static char elsieid[] = "@(#)ialloc.c 8.18"; | |
43 | #endif | |
44 | ||
45 | /*LINTLIBRARY*/ | |
46 | ||
47 | #include <string.h> | |
48 | #include <stdlib.h> | |
49 | ||
50 | #ifdef MAL | |
51 | #define NULLMAL(x) ((x) == NULL || (x) == MAL) | |
52 | #else /* !defined MAL */ | |
53 | #define NULLMAL(x) ((x) == NULL) | |
54 | #endif /* !defined MAL */ | |
55 | ||
56 | #define nonzero(n) (((n) == 0) ? 1 : (n)) | |
57 | ||
58 | char * icalloc __P((int nelem, int elsize)); | |
59 | char * icatalloc __P((char * old, const char * new)); | |
60 | char * icpyalloc __P((const char * string)); | |
61 | char * imalloc __P((int n)); | |
62 | char * irealloc __P((char * pointer, int size)); | |
63 | void ifree __P((char * pointer)); | |
64 | ||
65 | char * | |
66 | imalloc(n) | |
67 | const int n; | |
68 | { | |
69 | #ifdef MAL | |
70 | register char * result; | |
71 | ||
72 | result = malloc((size_t) nonzero(n)); | |
73 | return NULLMAL(result) ? NULL : result; | |
74 | #else /* !defined MAL */ | |
75 | return malloc((size_t) nonzero(n)); | |
76 | #endif /* !defined MAL */ | |
77 | } | |
78 | ||
79 | char * | |
80 | icalloc(nelem, elsize) | |
81 | int nelem; | |
82 | int elsize; | |
83 | { | |
84 | if (nelem == 0 || elsize == 0) | |
85 | nelem = elsize = 1; | |
86 | return calloc((size_t) nelem, (size_t) elsize); | |
87 | } | |
88 | ||
89 | char * | |
90 | irealloc(pointer, size) | |
91 | char * const pointer; | |
92 | const int size; | |
93 | { | |
94 | if (NULLMAL(pointer)) | |
95 | return imalloc(size); | |
96 | return realloc((void *) pointer, (size_t) nonzero(size)); | |
97 | } | |
98 | ||
99 | char * | |
100 | icatalloc(old, new) | |
101 | char * const old; | |
102 | const char * const new; | |
103 | { | |
104 | register char * result; | |
105 | register oldsize, newsize; | |
106 | ||
107 | newsize = NULLMAL(new) ? 0 : strlen(new); | |
108 | if (NULLMAL(old)) | |
109 | oldsize = 0; | |
110 | else if (newsize == 0) | |
111 | return old; | |
112 | else oldsize = strlen(old); | |
113 | if ((result = irealloc(old, oldsize + newsize + 1)) != NULL) | |
114 | if (!NULLMAL(new)) | |
115 | (void) strcpy(result + oldsize, new); | |
116 | return result; | |
117 | } | |
118 | ||
119 | char * | |
120 | icpyalloc(string) | |
121 | const char * const string; | |
122 | { | |
123 | return icatalloc((char *) NULL, string); | |
124 | } | |
125 | ||
126 | void | |
127 | ifree(p) | |
128 | char * const p; | |
129 | { | |
130 | if (!NULLMAL(p)) | |
131 | (void) free(p); | |
132 | } | |
133 | ||
134 | void | |
135 | icfree(p) | |
136 | char * const p; | |
137 | { | |
138 | if (!NULLMAL(p)) | |
139 | (void) free(p); | |
140 | } |