xref: /titanic_41/usr/src/cmd/lp/lib/lp/Sys_malloc.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 1993 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.14	*/
32*7c478bd9Sstevel@tonic-gate /* LINTLIBRARY */
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate #include "unistd.h"
35*7c478bd9Sstevel@tonic-gate #include "sys/types.h"
36*7c478bd9Sstevel@tonic-gate #include "sys/stat.h"
37*7c478bd9Sstevel@tonic-gate #include "errno.h"
38*7c478bd9Sstevel@tonic-gate #include "fcntl.h"
39*7c478bd9Sstevel@tonic-gate #include "stdlib.h"
40*7c478bd9Sstevel@tonic-gate #include "string.h"
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate /**
43*7c478bd9Sstevel@tonic-gate  ** _Malloc()
44*7c478bd9Sstevel@tonic-gate  ** _Realloc()
45*7c478bd9Sstevel@tonic-gate  ** _Calloc()
46*7c478bd9Sstevel@tonic-gate  ** _Strdup()
47*7c478bd9Sstevel@tonic-gate  ** _Free()
48*7c478bd9Sstevel@tonic-gate  **/
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate #if	!defined(TRACE_MALLOC)
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate #if	defined(__STDC__)
53*7c478bd9Sstevel@tonic-gate void			(*lp_alloc_fail_handler)( void ) = 0;
54*7c478bd9Sstevel@tonic-gate #else
55*7c478bd9Sstevel@tonic-gate void			(*lp_alloc_fail_handler)() = 0;
56*7c478bd9Sstevel@tonic-gate #endif
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate #if	defined(__STDC__)
59*7c478bd9Sstevel@tonic-gate typedef void *alloc_type;
60*7c478bd9Sstevel@tonic-gate #else
61*7c478bd9Sstevel@tonic-gate typedef char *alloc_type;
62*7c478bd9Sstevel@tonic-gate #endif
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate alloc_type
65*7c478bd9Sstevel@tonic-gate #if	defined(__STDC__)
_Malloc(size_t size,const char * file,int line)66*7c478bd9Sstevel@tonic-gate _Malloc (
67*7c478bd9Sstevel@tonic-gate 	size_t			size,
68*7c478bd9Sstevel@tonic-gate 	const char *		file,
69*7c478bd9Sstevel@tonic-gate 	int			line
70*7c478bd9Sstevel@tonic-gate )
71*7c478bd9Sstevel@tonic-gate #else
72*7c478bd9Sstevel@tonic-gate _Malloc (size, file, line)
73*7c478bd9Sstevel@tonic-gate 	size_t			size;
74*7c478bd9Sstevel@tonic-gate 	char *			file;
75*7c478bd9Sstevel@tonic-gate 	int			line;
76*7c478bd9Sstevel@tonic-gate #endif
77*7c478bd9Sstevel@tonic-gate {
78*7c478bd9Sstevel@tonic-gate 	alloc_type		ret	= malloc(size);
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate 	if (!ret) {
81*7c478bd9Sstevel@tonic-gate 		if (lp_alloc_fail_handler)
82*7c478bd9Sstevel@tonic-gate 			(*lp_alloc_fail_handler)();
83*7c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
84*7c478bd9Sstevel@tonic-gate 	}
85*7c478bd9Sstevel@tonic-gate 	return (ret);
86*7c478bd9Sstevel@tonic-gate }
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate alloc_type
89*7c478bd9Sstevel@tonic-gate #if	defined(__STDC__)
_Realloc(void * ptr,size_t size,const char * file,int line)90*7c478bd9Sstevel@tonic-gate _Realloc (
91*7c478bd9Sstevel@tonic-gate 	void *			ptr,
92*7c478bd9Sstevel@tonic-gate 	size_t			size,
93*7c478bd9Sstevel@tonic-gate 	const char *		file,
94*7c478bd9Sstevel@tonic-gate 	int			line
95*7c478bd9Sstevel@tonic-gate )
96*7c478bd9Sstevel@tonic-gate #else
97*7c478bd9Sstevel@tonic-gate _Realloc (ptr, size, file, line)
98*7c478bd9Sstevel@tonic-gate 	char *			ptr;
99*7c478bd9Sstevel@tonic-gate 	size_t			size;
100*7c478bd9Sstevel@tonic-gate 	char *			file;
101*7c478bd9Sstevel@tonic-gate 	int			line;
102*7c478bd9Sstevel@tonic-gate #endif
103*7c478bd9Sstevel@tonic-gate {
104*7c478bd9Sstevel@tonic-gate 	alloc_type		ret	= realloc(ptr, size);
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate 	if (!ret) {
107*7c478bd9Sstevel@tonic-gate 		if (lp_alloc_fail_handler)
108*7c478bd9Sstevel@tonic-gate 			(*lp_alloc_fail_handler)();
109*7c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
110*7c478bd9Sstevel@tonic-gate 	}
111*7c478bd9Sstevel@tonic-gate 	return (ret);
112*7c478bd9Sstevel@tonic-gate }
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate alloc_type
115*7c478bd9Sstevel@tonic-gate #if	defined(__STDC__)
_Calloc(size_t nelem,size_t elsize,const char * file,int line)116*7c478bd9Sstevel@tonic-gate _Calloc (
117*7c478bd9Sstevel@tonic-gate 	size_t			nelem,
118*7c478bd9Sstevel@tonic-gate 	size_t			elsize,
119*7c478bd9Sstevel@tonic-gate 	const char *		file,
120*7c478bd9Sstevel@tonic-gate 	int			line
121*7c478bd9Sstevel@tonic-gate )
122*7c478bd9Sstevel@tonic-gate #else
123*7c478bd9Sstevel@tonic-gate _Calloc (nelem, elsize, file, line)
124*7c478bd9Sstevel@tonic-gate 	size_t			nelem;
125*7c478bd9Sstevel@tonic-gate 	size_t			elsize;
126*7c478bd9Sstevel@tonic-gate 	char *			file;
127*7c478bd9Sstevel@tonic-gate 	int			line;
128*7c478bd9Sstevel@tonic-gate #endif
129*7c478bd9Sstevel@tonic-gate {
130*7c478bd9Sstevel@tonic-gate 	alloc_type		ret	= calloc(nelem, elsize);
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate 	if (!ret) {
133*7c478bd9Sstevel@tonic-gate 		if (lp_alloc_fail_handler)
134*7c478bd9Sstevel@tonic-gate 			(*lp_alloc_fail_handler)();
135*7c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
136*7c478bd9Sstevel@tonic-gate 	}
137*7c478bd9Sstevel@tonic-gate 	return (ret);
138*7c478bd9Sstevel@tonic-gate }
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate char *
141*7c478bd9Sstevel@tonic-gate #if	defined(__STDC__)
_Strdup(const char * s,const char * file,int line)142*7c478bd9Sstevel@tonic-gate _Strdup (
143*7c478bd9Sstevel@tonic-gate 	const char *		s,
144*7c478bd9Sstevel@tonic-gate 	const char *		file,
145*7c478bd9Sstevel@tonic-gate 	int			line
146*7c478bd9Sstevel@tonic-gate )
147*7c478bd9Sstevel@tonic-gate #else
148*7c478bd9Sstevel@tonic-gate _Strdup (s, file, line)
149*7c478bd9Sstevel@tonic-gate 	char *			s;
150*7c478bd9Sstevel@tonic-gate 	char *			file;
151*7c478bd9Sstevel@tonic-gate 	int			line;
152*7c478bd9Sstevel@tonic-gate #endif
153*7c478bd9Sstevel@tonic-gate {
154*7c478bd9Sstevel@tonic-gate 	char *			ret;
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate 	if (!s)
157*7c478bd9Sstevel@tonic-gate 		return( (char *) 0);
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate 	ret = strdup(s);
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate 	if (!ret) {
162*7c478bd9Sstevel@tonic-gate 		if (lp_alloc_fail_handler)
163*7c478bd9Sstevel@tonic-gate 			(*lp_alloc_fail_handler)();
164*7c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
165*7c478bd9Sstevel@tonic-gate 	}
166*7c478bd9Sstevel@tonic-gate 	return (ret);
167*7c478bd9Sstevel@tonic-gate }
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate void
170*7c478bd9Sstevel@tonic-gate #if	defined(__STDC__)
_Free(void * ptr,const char * file,int line)171*7c478bd9Sstevel@tonic-gate _Free (
172*7c478bd9Sstevel@tonic-gate 	void *			ptr,
173*7c478bd9Sstevel@tonic-gate 	const char *		file,
174*7c478bd9Sstevel@tonic-gate 	int			line
175*7c478bd9Sstevel@tonic-gate )
176*7c478bd9Sstevel@tonic-gate #else
177*7c478bd9Sstevel@tonic-gate _Free (ptr, file, line)
178*7c478bd9Sstevel@tonic-gate 	char *			ptr;
179*7c478bd9Sstevel@tonic-gate 	char *			file;
180*7c478bd9Sstevel@tonic-gate 	int			line;
181*7c478bd9Sstevel@tonic-gate #endif
182*7c478bd9Sstevel@tonic-gate {
183*7c478bd9Sstevel@tonic-gate 	free (ptr);
184*7c478bd9Sstevel@tonic-gate 	return;
185*7c478bd9Sstevel@tonic-gate }
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate #else
188*7c478bd9Sstevel@tonic-gate # include "mdl.c"
189*7c478bd9Sstevel@tonic-gate #endif
190