Commit | Line | Data |
---|---|---|
6b4c1ff3 KB |
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 | * %sccs.include.redist.c% | |
9 | */ | |
10 | ||
98200606 | 11 | #ifndef lint |
6b4c1ff3 KB |
12 | static char sccsid[] = "@(#)ialloc.c 5.3 (Berkeley) %G%"; |
13 | #endif /* not lint */ | |
14 | ||
15 | #ifdef notdef | |
98200606 | 16 | static char elsieid[] = "@(#)ialloc.c 8.18"; |
6b4c1ff3 | 17 | #endif |
98200606 KB |
18 | |
19 | /*LINTLIBRARY*/ | |
20 | ||
256a180c KB |
21 | #include <string.h> |
22 | #include <stdlib.h> | |
98200606 KB |
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 | ||
256a180c KB |
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)); | |
98200606 KB |
38 | |
39 | char * | |
40 | imalloc(n) | |
41 | const int n; | |
42 | { | |
43 | #ifdef MAL | |
44 | register char * result; | |
45 | ||
256a180c | 46 | result = malloc((size_t) nonzero(n)); |
98200606 KB |
47 | return NULLMAL(result) ? NULL : result; |
48 | #else /* !defined MAL */ | |
256a180c | 49 | return malloc((size_t) nonzero(n)); |
98200606 KB |
50 | #endif /* !defined MAL */ |
51 | } | |
52 | ||
53 | char * | |
54 | icalloc(nelem, elsize) | |
55 | int nelem; | |
56 | int elsize; | |
57 | { | |
58 | if (nelem == 0 || elsize == 0) | |
59 | nelem = elsize = 1; | |
256a180c | 60 | return calloc((size_t) nelem, (size_t) elsize); |
98200606 KB |
61 | } |
62 | ||
63 | char * | |
64 | irealloc(pointer, size) | |
65 | char * const pointer; | |
66 | const int size; | |
67 | { | |
68 | if (NULLMAL(pointer)) | |
69 | return imalloc(size); | |
256a180c | 70 | return realloc((void *) pointer, (size_t) nonzero(size)); |
98200606 KB |
71 | } |
72 | ||
73 | char * | |
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 * | |
94 | icpyalloc(string) | |
95 | const char * const string; | |
96 | { | |
97 | return icatalloc((char *) NULL, string); | |
98 | } | |
99 | ||
100 | void | |
101 | ifree(p) | |
102 | char * const p; | |
103 | { | |
104 | if (!NULLMAL(p)) | |
105 | (void) free(p); | |
106 | } | |
107 | ||
108 | void | |
109 | icfree(p) | |
110 | char * const p; | |
111 | { | |
112 | if (!NULLMAL(p)) | |
113 | (void) free(p); | |
114 | } |