xref: /titanic_41/usr/src/lib/lvm/libmeta/common/meta_mem.c (revision 2f172c55ef76964744bc62b4500ece87f3089b4d)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright (c) 1992, 1993, 2000 by Sun Microsystems, Inc.
25  * All rights reserved.
26  */
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 #include <meta.h>
31 
32 /*
33  * free
34  */
35 #ifdef	_DEBUG_MALLOC_INC
36 
37 void
38 _Free(
39 	char	*file,
40 	int	line,
41 	void	*p
42 )
43 {
44 	debug_free(file, line, p);
45 }
46 
47 #else	/* ! _DEBUG_MALLOC_INC */
48 
49 void
50 Free(
51 	void	*p
52 )
53 {
54 	free(p);
55 }
56 
57 #endif	/* ! _DEBUG_MALLOC_INC */
58 
59 /*
60  * malloc
61  */
62 #ifdef	_DEBUG_MALLOC_INC
63 
64 void *
65 _Malloc(
66 	char	*file,
67 	int	line,
68 	size_t	s
69 )
70 {
71 	void *mem;
72 
73 	mem = debug_malloc(file, line, s);
74 	if (mem == NULL) {
75 		md_perror("");
76 		md_exit(NULL, 1);
77 	}
78 	return (mem);
79 }
80 
81 #else	/* ! _DEBUG_MALLOC_INC */
82 
83 void *
84 Malloc(
85 	size_t	s
86 )
87 {
88 	void *mem;
89 
90 	if ((mem = malloc(s)) == NULL) {
91 		md_perror("");
92 		md_exit(NULL, 1);
93 	}
94 	return (mem);
95 }
96 
97 #endif	/* ! _DEBUG_MALLOC_INC */
98 
99 /*
100  * zalloc
101  */
102 #ifdef	_DEBUG_MALLOC_INC
103 
104 void *
105 _Zalloc(
106 	char	*file,
107 	int	line,
108 	size_t	s
109 )
110 {
111 	return (memset(_Malloc(file, line, s), 0, s));
112 }
113 
114 #else	/* ! _DEBUG_MALLOC_INC */
115 
116 void *
117 Zalloc(
118 	size_t	s
119 )
120 {
121 	return (memset(Malloc(s), 0, s));
122 }
123 
124 #endif	/* ! _DEBUG_MALLOC_INC */
125 
126 /*
127  * realloc
128  */
129 #ifdef	_DEBUG_MALLOC_INC
130 
131 void *
132 _Realloc(
133 	char	*file,
134 	int	line,
135 	void	*p,
136 	size_t	s
137 )
138 {
139 	if (p == NULL)
140 		p = debug_malloc(file, line, s);
141 	else
142 		p = debug_realloc(file, line, p, s);
143 	if (p == NULL) {
144 		md_perror("");
145 		md_exit(NULL, 1);
146 	}
147 	return (p);
148 }
149 
150 #else	/* ! _DEBUG_MALLOC_INC */
151 
152 void *
153 Realloc(
154 	void	*p,
155 	size_t	s
156 )
157 {
158 	if ((p = realloc(p, s)) == NULL) {
159 		md_perror("");
160 		md_exit(NULL, 1);
161 	}
162 	return (p);
163 }
164 
165 #endif	/* ! _DEBUG_MALLOC_INC */
166 
167 /*
168  * calloc
169  */
170 #ifdef	_DEBUG_MALLOC_INC
171 
172 void *
173 _Calloc(
174 	char	*file,
175 	int	line,
176 	size_t	n,
177 	size_t	s
178 )
179 {
180 	unsigned long total;
181 
182 	if (n == 0 || s == 0) {
183 		total = 0;
184 	} else {
185 		total = (unsigned long)n * s;
186 		/* check for overflow */
187 		if (total / n != s)
188 			return (NULL);
189 	}
190 	return (_Zalloc(file, line, total));
191 }
192 
193 #else	/* ! _DEBUG_MALLOC_INC */
194 
195 void *
196 Calloc(
197 	size_t	n,
198 	size_t	s
199 )
200 {
201 	unsigned long total;
202 
203 	if (n == 0 || s == 0) {
204 		total = 0;
205 	} else {
206 		total = (unsigned long)n * s;
207 		/* check for overflow */
208 		if (total / n != s)
209 			return (NULL);
210 	}
211 	return (Zalloc(total));
212 }
213 
214 #endif	/* ! _DEBUG_MALLOC_INC */
215 
216 /*
217  * strdup
218  */
219 #ifdef	_DEBUG_MALLOC_INC
220 
221 char *
222 _Strdup(
223 	char	*file,
224 	int	line,
225 	char	*p
226 )
227 {
228 	p = DBstrdup(file, line, p);
229 	if (p == NULL) {
230 		md_perror("");
231 		md_exit(NULL, 1);
232 	}
233 	return (p);
234 }
235 
236 #else	/* ! _DEBUG_MALLOC_INC */
237 
238 char *
239 Strdup(
240 	char	*p
241 )
242 {
243 	if ((p = strdup(p)) == NULL) {
244 		md_perror("");
245 		md_exit(NULL, 1);
246 	}
247 	return (p);
248 }
249 
250 #endif	/* ! _DEBUG_MALLOC_INC */
251