ialloc.c revision 192625
1217309Snwhitehorn/*
2217309Snwhitehorn** This file is in the public domain, so clarified as of
3217309Snwhitehorn** 2006-07-17 by Arthur David Olson.
4217309Snwhitehorn*/
5217309Snwhitehorn
6217309Snwhitehorn#ifndef lint
7217309Snwhitehorn#ifndef NOID
8217309Snwhitehornstatic const char	elsieid[] = "@(#)ialloc.c	8.30";
9217309Snwhitehorn#endif /* !defined NOID */
10217309Snwhitehorn#endif /* !defined lint */
11217309Snwhitehorn
12217309Snwhitehorn#ifndef lint
13217309Snwhitehornstatic const char rcsid[] =
14217309Snwhitehorn  "$FreeBSD: head/usr.sbin/zic/ialloc.c 192625 2009-05-23 06:31:50Z edwin $";
15217309Snwhitehorn#endif /* not lint */
16217309Snwhitehorn
17217309Snwhitehorn/*LINTLIBRARY*/
18217309Snwhitehorn
19217309Snwhitehorn#include "private.h"
20217309Snwhitehorn
21217309Snwhitehorn#define nonzero(n)	(((n) == 0) ? 1 : (n))
22217309Snwhitehorn
23217309Snwhitehornchar *
24imalloc(n)
25const int	n;
26{
27	return malloc((size_t) nonzero(n));
28}
29
30char *
31icalloc(nelem, elsize)
32int	nelem;
33int	elsize;
34{
35	if (nelem == 0 || elsize == 0)
36		nelem = elsize = 1;
37	return calloc((size_t) nelem, (size_t) elsize);
38}
39
40void *
41irealloc(pointer, size)
42void * const	pointer;
43const int	size;
44{
45	if (pointer == NULL)
46		return imalloc(size);
47	return realloc((void *) pointer, (size_t) nonzero(size));
48}
49
50char *
51icatalloc(old, new)
52char * const		old;
53const char * const	new;
54{
55	register char *	result;
56	register int	oldsize, newsize;
57
58	newsize = (new == NULL) ? 0 : strlen(new);
59	if (old == NULL)
60		oldsize = 0;
61	else if (newsize == 0)
62		return old;
63	else	oldsize = strlen(old);
64	if ((result = irealloc(old, oldsize + newsize + 1)) != NULL)
65		if (new != NULL)
66			(void) strcpy(result + oldsize, new);
67	return result;
68}
69
70char *
71icpyalloc(string)
72const char * const	string;
73{
74	return icatalloc((char *) NULL, string);
75}
76
77void
78ifree(p)
79char * const	p;
80{
81	if (p != NULL)
82		(void) free(p);
83}
84
85void
86icfree(p)
87char * const	p;
88{
89	if (p != NULL)
90		(void) free(p);
91}
92