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