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