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