xref: /illumos-gate/usr/src/cmd/lp/lib/lp/Sys_malloc.c (revision 2a8bcb4e)
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 /* LINTLIBRARY */
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate #include "unistd.h"
33*7c478bd9Sstevel@tonic-gate #include "sys/types.h"
34*7c478bd9Sstevel@tonic-gate #include "sys/stat.h"
35*7c478bd9Sstevel@tonic-gate #include "errno.h"
36*7c478bd9Sstevel@tonic-gate #include "fcntl.h"
37*7c478bd9Sstevel@tonic-gate #include "stdlib.h"
38*7c478bd9Sstevel@tonic-gate #include "string.h"
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate /**
41*7c478bd9Sstevel@tonic-gate  ** _Malloc()
42*7c478bd9Sstevel@tonic-gate  ** _Realloc()
43*7c478bd9Sstevel@tonic-gate  ** _Calloc()
44*7c478bd9Sstevel@tonic-gate  ** _Strdup()
45*7c478bd9Sstevel@tonic-gate  ** _Free()
46*7c478bd9Sstevel@tonic-gate  **/
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate #if	!defined(TRACE_MALLOC)
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate #if	defined(__STDC__)
51*7c478bd9Sstevel@tonic-gate void			(*lp_alloc_fail_handler)( void ) = 0;
52*7c478bd9Sstevel@tonic-gate #else
53*7c478bd9Sstevel@tonic-gate void			(*lp_alloc_fail_handler)() = 0;
54*7c478bd9Sstevel@tonic-gate #endif
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate #if	defined(__STDC__)
57*7c478bd9Sstevel@tonic-gate typedef void *alloc_type;
58*7c478bd9Sstevel@tonic-gate #else
59*7c478bd9Sstevel@tonic-gate typedef char *alloc_type;
60*7c478bd9Sstevel@tonic-gate #endif
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate alloc_type
63*7c478bd9Sstevel@tonic-gate #if	defined(__STDC__)
_Malloc(size_t size,const char * file,int line)64*7c478bd9Sstevel@tonic-gate _Malloc (
65*7c478bd9Sstevel@tonic-gate 	size_t			size,
66*7c478bd9Sstevel@tonic-gate 	const char *		file,
67*7c478bd9Sstevel@tonic-gate 	int			line
68*7c478bd9Sstevel@tonic-gate )
69*7c478bd9Sstevel@tonic-gate #else
70*7c478bd9Sstevel@tonic-gate _Malloc (size, file, line)
71*7c478bd9Sstevel@tonic-gate 	size_t			size;
72*7c478bd9Sstevel@tonic-gate 	char *			file;
73*7c478bd9Sstevel@tonic-gate 	int			line;
74*7c478bd9Sstevel@tonic-gate #endif
75*7c478bd9Sstevel@tonic-gate {
76*7c478bd9Sstevel@tonic-gate 	alloc_type		ret	= malloc(size);
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate 	if (!ret) {
79*7c478bd9Sstevel@tonic-gate 		if (lp_alloc_fail_handler)
80*7c478bd9Sstevel@tonic-gate 			(*lp_alloc_fail_handler)();
81*7c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
82*7c478bd9Sstevel@tonic-gate 	}
83*7c478bd9Sstevel@tonic-gate 	return (ret);
84*7c478bd9Sstevel@tonic-gate }
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate alloc_type
87*7c478bd9Sstevel@tonic-gate #if	defined(__STDC__)
_Realloc(void * ptr,size_t size,const char * file,int line)88*7c478bd9Sstevel@tonic-gate _Realloc (
89*7c478bd9Sstevel@tonic-gate 	void *			ptr,
90*7c478bd9Sstevel@tonic-gate 	size_t			size,
91*7c478bd9Sstevel@tonic-gate 	const char *		file,
92*7c478bd9Sstevel@tonic-gate 	int			line
93*7c478bd9Sstevel@tonic-gate )
94*7c478bd9Sstevel@tonic-gate #else
95*7c478bd9Sstevel@tonic-gate _Realloc (ptr, size, file, line)
96*7c478bd9Sstevel@tonic-gate 	char *			ptr;
97*7c478bd9Sstevel@tonic-gate 	size_t			size;
98*7c478bd9Sstevel@tonic-gate 	char *			file;
99*7c478bd9Sstevel@tonic-gate 	int			line;
100*7c478bd9Sstevel@tonic-gate #endif
101*7c478bd9Sstevel@tonic-gate {
102*7c478bd9Sstevel@tonic-gate 	alloc_type		ret	= realloc(ptr, size);
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate 	if (!ret) {
105*7c478bd9Sstevel@tonic-gate 		if (lp_alloc_fail_handler)
106*7c478bd9Sstevel@tonic-gate 			(*lp_alloc_fail_handler)();
107*7c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
108*7c478bd9Sstevel@tonic-gate 	}
109*7c478bd9Sstevel@tonic-gate 	return (ret);
110*7c478bd9Sstevel@tonic-gate }
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate alloc_type
113*7c478bd9Sstevel@tonic-gate #if	defined(__STDC__)
_Calloc(size_t nelem,size_t elsize,const char * file,int line)114*7c478bd9Sstevel@tonic-gate _Calloc (
115*7c478bd9Sstevel@tonic-gate 	size_t			nelem,
116*7c478bd9Sstevel@tonic-gate 	size_t			elsize,
117*7c478bd9Sstevel@tonic-gate 	const char *		file,
118*7c478bd9Sstevel@tonic-gate 	int			line
119*7c478bd9Sstevel@tonic-gate )
120*7c478bd9Sstevel@tonic-gate #else
121*7c478bd9Sstevel@tonic-gate _Calloc (nelem, elsize, file, line)
122*7c478bd9Sstevel@tonic-gate 	size_t			nelem;
123*7c478bd9Sstevel@tonic-gate 	size_t			elsize;
124*7c478bd9Sstevel@tonic-gate 	char *			file;
125*7c478bd9Sstevel@tonic-gate 	int			line;
126*7c478bd9Sstevel@tonic-gate #endif
127*7c478bd9Sstevel@tonic-gate {
128*7c478bd9Sstevel@tonic-gate 	alloc_type		ret	= calloc(nelem, elsize);
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate 	if (!ret) {
131*7c478bd9Sstevel@tonic-gate 		if (lp_alloc_fail_handler)
132*7c478bd9Sstevel@tonic-gate 			(*lp_alloc_fail_handler)();
133*7c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
134*7c478bd9Sstevel@tonic-gate 	}
135*7c478bd9Sstevel@tonic-gate 	return (ret);
136*7c478bd9Sstevel@tonic-gate }
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate char *
139*7c478bd9Sstevel@tonic-gate #if	defined(__STDC__)
_Strdup(const char * s,const char * file,int line)140*7c478bd9Sstevel@tonic-gate _Strdup (
141*7c478bd9Sstevel@tonic-gate 	const char *		s,
142*7c478bd9Sstevel@tonic-gate 	const char *		file,
143*7c478bd9Sstevel@tonic-gate 	int			line
144*7c478bd9Sstevel@tonic-gate )
145*7c478bd9Sstevel@tonic-gate #else
146*7c478bd9Sstevel@tonic-gate _Strdup (s, file, line)
147*7c478bd9Sstevel@tonic-gate 	char *			s;
148*7c478bd9Sstevel@tonic-gate 	char *			file;
149*7c478bd9Sstevel@tonic-gate 	int			line;
150*7c478bd9Sstevel@tonic-gate #endif
151*7c478bd9Sstevel@tonic-gate {
152*7c478bd9Sstevel@tonic-gate 	char *			ret;
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate 	if (!s)
155*7c478bd9Sstevel@tonic-gate 		return( (char *) 0);
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate 	ret = strdup(s);
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate 	if (!ret) {
160*7c478bd9Sstevel@tonic-gate 		if (lp_alloc_fail_handler)
161*7c478bd9Sstevel@tonic-gate 			(*lp_alloc_fail_handler)();
162*7c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
163*7c478bd9Sstevel@tonic-gate 	}
164*7c478bd9Sstevel@tonic-gate 	return (ret);
165*7c478bd9Sstevel@tonic-gate }
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate void
168*7c478bd9Sstevel@tonic-gate #if	defined(__STDC__)
_Free(void * ptr,const char * file,int line)169*7c478bd9Sstevel@tonic-gate _Free (
170*7c478bd9Sstevel@tonic-gate 	void *			ptr,
171*7c478bd9Sstevel@tonic-gate 	const char *		file,
172*7c478bd9Sstevel@tonic-gate 	int			line
173*7c478bd9Sstevel@tonic-gate )
174*7c478bd9Sstevel@tonic-gate #else
175*7c478bd9Sstevel@tonic-gate _Free (ptr, file, line)
176*7c478bd9Sstevel@tonic-gate 	char *			ptr;
177*7c478bd9Sstevel@tonic-gate 	char *			file;
178*7c478bd9Sstevel@tonic-gate 	int			line;
179*7c478bd9Sstevel@tonic-gate #endif
180*7c478bd9Sstevel@tonic-gate {
181*7c478bd9Sstevel@tonic-gate 	free (ptr);
182*7c478bd9Sstevel@tonic-gate 	return;
183*7c478bd9Sstevel@tonic-gate }
184*7c478bd9Sstevel@tonic-gate 
185*7c478bd9Sstevel@tonic-gate #else
186*7c478bd9Sstevel@tonic-gate # include "mdl.c"
187*7c478bd9Sstevel@tonic-gate #endif
188