Commit | Line | Data |
---|---|---|
15637ed4 | 1 | #ifndef lint |
1bd76676 | 2 | #ifndef NOID |
6b599c2a | 3 | static char elsieid[] = "@(#)ialloc.c 8.21"; |
1bd76676 GW |
4 | #endif /* !defined NOID */ |
5 | #endif /* !defined lint */ | |
15637ed4 RG |
6 | |
7 | /*LINTLIBRARY*/ | |
8 | ||
1bd76676 | 9 | #include "private.h" |
15637ed4 RG |
10 | |
11 | #ifdef MAL | |
12 | #define NULLMAL(x) ((x) == NULL || (x) == MAL) | |
6b599c2a GW |
13 | #endif /* defined MAL */ |
14 | #ifndef MAL | |
15637ed4 RG |
15 | #define NULLMAL(x) ((x) == NULL) |
16 | #endif /* !defined MAL */ | |
17 | ||
18 | #define nonzero(n) (((n) == 0) ? 1 : (n)) | |
19 | ||
1bd76676 GW |
20 | char * icalloc P((int nelem, int elsize)); |
21 | char * icatalloc P((char * old, const char * new)); | |
22 | char * icpyalloc P((const char * string)); | |
23 | char * imalloc P((int n)); | |
24 | char * irealloc P((char * pointer, int size)); | |
25 | void ifree P((char * pointer)); | |
15637ed4 RG |
26 | |
27 | char * | |
28 | imalloc(n) | |
29 | const int n; | |
30 | { | |
31 | #ifdef MAL | |
32 | register char * result; | |
33 | ||
1bd76676 | 34 | result = malloc((alloc_size_t) nonzero(n)); |
15637ed4 | 35 | return NULLMAL(result) ? NULL : result; |
6b599c2a GW |
36 | #endif /* defined MAL */ |
37 | #ifndef MAL | |
1bd76676 | 38 | return malloc((alloc_size_t) nonzero(n)); |
15637ed4 RG |
39 | #endif /* !defined MAL */ |
40 | } | |
41 | ||
42 | char * | |
43 | icalloc(nelem, elsize) | |
44 | int nelem; | |
45 | int elsize; | |
46 | { | |
47 | if (nelem == 0 || elsize == 0) | |
48 | nelem = elsize = 1; | |
1bd76676 | 49 | return calloc((alloc_size_t) nelem, (alloc_size_t) elsize); |
15637ed4 RG |
50 | } |
51 | ||
52 | char * | |
53 | irealloc(pointer, size) | |
54 | char * const pointer; | |
55 | const int size; | |
56 | { | |
57 | if (NULLMAL(pointer)) | |
58 | return imalloc(size); | |
1bd76676 | 59 | return realloc((genericptr_t) pointer, (alloc_size_t) nonzero(size)); |
15637ed4 RG |
60 | } |
61 | ||
62 | char * | |
63 | icatalloc(old, new) | |
64 | char * const old; | |
65 | const char * const new; | |
66 | { | |
67 | register char * result; | |
1bd76676 | 68 | register int oldsize, newsize; |
15637ed4 RG |
69 | |
70 | newsize = NULLMAL(new) ? 0 : strlen(new); | |
71 | if (NULLMAL(old)) | |
72 | oldsize = 0; | |
73 | else if (newsize == 0) | |
74 | return old; | |
75 | else oldsize = strlen(old); | |
76 | if ((result = irealloc(old, oldsize + newsize + 1)) != NULL) | |
77 | if (!NULLMAL(new)) | |
78 | (void) strcpy(result + oldsize, new); | |
79 | return result; | |
80 | } | |
81 | ||
82 | char * | |
83 | icpyalloc(string) | |
84 | const char * const string; | |
85 | { | |
86 | return icatalloc((char *) NULL, string); | |
87 | } | |
88 | ||
89 | void | |
90 | ifree(p) | |
91 | char * const p; | |
92 | { | |
93 | if (!NULLMAL(p)) | |
94 | (void) free(p); | |
95 | } | |
96 | ||
97 | void | |
98 | icfree(p) | |
99 | char * const p; | |
100 | { | |
101 | if (!NULLMAL(p)) | |
102 | (void) free(p); | |
103 | } |