1 | /** @file
|
---|
2 | * IPRT / No-CRT - Our minimal stdlib.h.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox base platform packages, as
|
---|
9 | * available from https://www.virtualbox.org.
|
---|
10 | *
|
---|
11 | * This program is free software; you can redistribute it and/or
|
---|
12 | * modify it under the terms of the GNU General Public License
|
---|
13 | * as published by the Free Software Foundation, in version 3 of the
|
---|
14 | * License.
|
---|
15 | *
|
---|
16 | * This program is distributed in the hope that it will be useful, but
|
---|
17 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
19 | * General Public License for more details.
|
---|
20 | *
|
---|
21 | * You should have received a copy of the GNU General Public License
|
---|
22 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
23 | *
|
---|
24 | * The contents of this file may alternatively be used under the terms
|
---|
25 | * of the Common Development and Distribution License Version 1.0
|
---|
26 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
27 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
28 | * CDDL are applicable instead of those of the GPL.
|
---|
29 | *
|
---|
30 | * You may elect to license modified versions of this file under the
|
---|
31 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
32 | *
|
---|
33 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
34 | */
|
---|
35 |
|
---|
36 | #ifndef IPRT_INCLUDED_nocrt_stdlib_h
|
---|
37 | #define IPRT_INCLUDED_nocrt_stdlib_h
|
---|
38 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
39 | # pragma once
|
---|
40 | #endif
|
---|
41 |
|
---|
42 | #include <iprt/assert.h>
|
---|
43 | #include <iprt/env.h>
|
---|
44 | #include <iprt/mem.h>
|
---|
45 | #include <iprt/nocrt/limits.h>
|
---|
46 |
|
---|
47 | RT_C_DECLS_BEGIN
|
---|
48 |
|
---|
49 | #define EXIT_SUCCESS RTEXITCODE_SUCCESS
|
---|
50 | #define EXIT_FAILURE RTEXITCODE_FAILURE
|
---|
51 |
|
---|
52 |
|
---|
53 | typedef void FNRTNOCRTATEXITCALLBACK(void) /*RT_NOEXCEPT*/;
|
---|
54 | typedef FNRTNOCRTATEXITCALLBACK *PFNRTNOCRTATEXITCALLBACK;
|
---|
55 | #if defined(_MSC_VER) && defined(RT_WITHOUT_NOCRT_WRAPPERS) /* Clashes with compiler internal prototype or smth. */
|
---|
56 | int nocrt_atexit(PFNRTNOCRTATEXITCALLBACK) RT_NOEXCEPT;
|
---|
57 | # define atexit nocrt_atexit
|
---|
58 | #else
|
---|
59 | int RT_NOCRT(atexit)(PFNRTNOCRTATEXITCALLBACK) RT_NOEXCEPT;
|
---|
60 | #endif
|
---|
61 |
|
---|
62 | #if !defined(RT_WITHOUT_NOCRT_WRAPPERS) && !defined(RT_WITHOUT_NOCRT_WRAPPER_ALIASES)
|
---|
63 | # define atexit RT_NOCRT(atexit)
|
---|
64 | #endif
|
---|
65 |
|
---|
66 |
|
---|
67 | #ifdef IPRT_NO_CRT_FOR_3RD_PARTY
|
---|
68 | /*
|
---|
69 | * Only for external libraries and such.
|
---|
70 | */
|
---|
71 |
|
---|
72 | DECLINLINE(void *) RT_NOCRT(malloc)(size_t cb)
|
---|
73 | {
|
---|
74 | return RTMemAlloc(cb);
|
---|
75 | }
|
---|
76 |
|
---|
77 | DECLINLINE(void *) RT_NOCRT(calloc)(size_t cItems, size_t cbItem)
|
---|
78 | {
|
---|
79 | return RTMemAllocZ(cItems * cbItem); /* caller responsible for overflow issues. */
|
---|
80 | }
|
---|
81 |
|
---|
82 | DECLINLINE(void *) RT_NOCRT(realloc)(void *pvOld, size_t cbNew)
|
---|
83 | {
|
---|
84 | return RTMemRealloc(pvOld, cbNew);
|
---|
85 | }
|
---|
86 |
|
---|
87 | DECLINLINE(void) RT_NOCRT(free)(void *pv)
|
---|
88 | {
|
---|
89 | RTMemFree(pv);
|
---|
90 | }
|
---|
91 |
|
---|
92 | DECLINLINE(const char *) RT_NOCRT(getenv)(const char *pszVar)
|
---|
93 | {
|
---|
94 | return RTEnvGet(pszVar);
|
---|
95 | }
|
---|
96 |
|
---|
97 | int RT_NOCRT(abs)(int) RT_NOEXCEPT;
|
---|
98 | long RT_NOCRT(labs)(long) RT_NOEXCEPT;
|
---|
99 | long long RT_NOCRT(llabs)(long long) RT_NOEXCEPT;
|
---|
100 | int RT_NOCRT(rand)(void) RT_NOEXCEPT;
|
---|
101 | void RT_NOCRT(srand)(unsigned) RT_NOEXCEPT;
|
---|
102 | long RT_NOCRT(strtol)(const char *psz, char **ppszNext, int iBase) RT_NOEXCEPT;
|
---|
103 | long long RT_NOCRT(strtoll)(const char *psz, char **ppszNext, int iBase) RT_NOEXCEPT;
|
---|
104 | unsigned long RT_NOCRT(strtoul)(const char *psz, char **ppszNext, int iBase) RT_NOEXCEPT;
|
---|
105 | unsigned long long RT_NOCRT(strtoull)(const char *psz, char **ppszNext, int iBase) RT_NOEXCEPT;
|
---|
106 | int RT_NOCRT(atoi)(const char *psz) RT_NOEXCEPT;
|
---|
107 | double RT_NOCRT(strtod)(const char *psz, char **ppszNext) RT_NOEXCEPT;
|
---|
108 | double RT_NOCRT(atof)(const char *psz) RT_NOEXCEPT;
|
---|
109 | void *RT_NOCRT(bsearch)(const void *pvKey, const void *pvBase, size_t cEntries, size_t cbEntry,
|
---|
110 | int (*pfnCompare)(const void *pvKey, const void *pvEntry));
|
---|
111 | void RT_NOCRT(qsort)(void *pvBase, size_t cEntries, size_t cbEntry,
|
---|
112 | int (*pfnCompare)(const void *pv1, const void *pv2));
|
---|
113 | void RT_NOCRT(qsort_r)(void *pvBase, size_t cEntries, size_t cbEntry,
|
---|
114 | int (*pfnCompare)(const void *pv1, const void *pv2, void *pvUser), void *pvUser);
|
---|
115 |
|
---|
116 | /* Map exit & abort onto fatal assert. */
|
---|
117 | DECL_NO_RETURN(DECLINLINE(void)) RT_NOCRT(exit)(int iExitCode) { AssertFatalMsgFailed(("exit: iExitCode=%d\n", iExitCode)); }
|
---|
118 | DECL_NO_RETURN(DECLINLINE(void)) RT_NOCRT(abort)(void) { AssertFatalMsgFailed(("abort\n")); }
|
---|
119 |
|
---|
120 | /*
|
---|
121 | * Underscored versions:
|
---|
122 | */
|
---|
123 | DECLINLINE(void *) RT_NOCRT(_malloc)(size_t cb)
|
---|
124 | {
|
---|
125 | return RTMemAlloc(cb);
|
---|
126 | }
|
---|
127 |
|
---|
128 | DECLINLINE(void *) RT_NOCRT(_calloc)(size_t cItems, size_t cbItem)
|
---|
129 | {
|
---|
130 | return RTMemAllocZ(cItems * cbItem); /* caller responsible for overflow issues. */
|
---|
131 | }
|
---|
132 |
|
---|
133 | DECLINLINE(void *) RT_NOCRT(_realloc)(void *pvOld, size_t cbNew)
|
---|
134 | {
|
---|
135 | return RTMemRealloc(pvOld, cbNew);
|
---|
136 | }
|
---|
137 |
|
---|
138 | DECLINLINE(void) RT_NOCRT(_free)(void *pv)
|
---|
139 | {
|
---|
140 | RTMemFree(pv);
|
---|
141 | }
|
---|
142 |
|
---|
143 | DECLINLINE(const char *) RT_NOCRT(_getenv)(const char *pszVar)
|
---|
144 | {
|
---|
145 | return RTEnvGet(pszVar);
|
---|
146 | }
|
---|
147 |
|
---|
148 | int RT_NOCRT(_abs)(int);
|
---|
149 | long RT_NOCRT(_labs)(long);
|
---|
150 | long long RT_NOCRT(_llabs)(long long);
|
---|
151 | int RT_NOCRT(_rand)(void);
|
---|
152 | void RT_NOCRT(_srand)(unsigned);
|
---|
153 | long RT_NOCRT(_strtol)(const char *psz, char **ppszNext, int iBase);
|
---|
154 | long long RT_NOCRT(_strtoll)(const char *psz, char **ppszNext, int iBase);
|
---|
155 | unsigned long RT_NOCRT(_strtoul)(const char *psz, char **ppszNext, int iBase);
|
---|
156 | unsigned long long RT_NOCRT(_strtoull)(const char *psz, char **ppszNext, int iBase);
|
---|
157 | int RT_NOCRT(_atoi)(const char *psz);
|
---|
158 | double RT_NOCRT(_strtod)(const char *psz, char **ppszNext);
|
---|
159 | double RT_NOCRT(_atof)(const char *psz);
|
---|
160 | void *RT_NOCRT(_bsearch)(const void *pvKey, const void *pvBase, size_t cEntries, size_t cbEntry,
|
---|
161 | int (*pfnCompare)(const void *pv1, const void *pv2));
|
---|
162 | void RT_NOCRT(_qsort)(void *pvBase, size_t cEntries, size_t cbEntry,
|
---|
163 | int (*pfnCompare)(const void *pv1, const void *pv2));
|
---|
164 | void RT_NOCRT(_qsort_r)(void *pvBase, size_t cEntries, size_t cbEntry,
|
---|
165 | int (*pfnCompare)(const void *pv1, const void *pv2, void *pvUser), void *pvUser);
|
---|
166 |
|
---|
167 | /* Map exit & abort onto fatal assert. */
|
---|
168 | DECL_NO_RETURN(DECLINLINE(void)) RT_NOCRT(_exit)(int iExitCode) { AssertFatalMsgFailed(("_exit: iExitCode=%d\n", iExitCode)); }
|
---|
169 | DECL_NO_RETURN(DECLINLINE(void)) RT_NOCRT(_abort)(void) { AssertFatalMsgFailed(("_abort\n")); }
|
---|
170 |
|
---|
171 | /* Some windows CRT error control functions we totally ignore (only underscored): */
|
---|
172 | # define _set_error_mode(a_Mode) (0)
|
---|
173 | # define _set_abort_behavior(a_fFlags, a_fMask) (0)
|
---|
174 |
|
---|
175 | /*
|
---|
176 | * No-CRT aliases.
|
---|
177 | */
|
---|
178 | # if !defined(RT_WITHOUT_NOCRT_WRAPPERS) && !defined(RT_WITHOUT_NOCRT_WRAPPER_ALIASES)
|
---|
179 | # define malloc RT_NOCRT(malloc)
|
---|
180 | # define calloc RT_NOCRT(calloc)
|
---|
181 | # define realloc RT_NOCRT(realloc)
|
---|
182 | # define free RT_NOCRT(free)
|
---|
183 | # define getenv RT_NOCRT(getenv)
|
---|
184 | # define bsearch RT_NOCRT(bsearch)
|
---|
185 | # define exit RT_NOCRT(exit)
|
---|
186 | # define abort RT_NOCRT(abort)
|
---|
187 | # define abs RT_NOCRT(abs)
|
---|
188 | # define labs RT_NOCRT(labs)
|
---|
189 | # define llabs RT_NOCRT(llabs)
|
---|
190 | # define rand RT_NOCRT(rand)
|
---|
191 | # define srand RT_NOCRT(srand)
|
---|
192 | # define strtol RT_NOCRT(strtol)
|
---|
193 | # define strtoll RT_NOCRT(strtoll)
|
---|
194 | # define strtoul RT_NOCRT(strtoul)
|
---|
195 | # define strtoull RT_NOCRT(strtoull)
|
---|
196 | # define atoi RT_NOCRT(atoi)
|
---|
197 | # define strtod RT_NOCRT(strtod)
|
---|
198 | # define atof RT_NOCRT(atof)
|
---|
199 |
|
---|
200 | # define _malloc RT_NOCRT(malloc)
|
---|
201 | # define _calloc RT_NOCRT(calloc)
|
---|
202 | # define _realloc RT_NOCRT(realloc)
|
---|
203 | # define _free RT_NOCRT(free)
|
---|
204 | # define _getenv RT_NOCRT(getenv)
|
---|
205 | # define _bsearch RT_NOCRT(bsearch)
|
---|
206 | # define _exit RT_NOCRT(exit)
|
---|
207 | # define _abort RT_NOCRT(abort)
|
---|
208 | # define _abs RT_NOCRT(abs)
|
---|
209 | # define _labs RT_NOCRT(labs)
|
---|
210 | # define _llabs RT_NOCRT(llabs)
|
---|
211 | # define _rand RT_NOCRT(rand)
|
---|
212 | # define _srand RT_NOCRT(srand)
|
---|
213 | # define _strtol RT_NOCRT(strtol)
|
---|
214 | # define _strtoll RT_NOCRT(strtoll)
|
---|
215 | # define _strtoul RT_NOCRT(strtoul)
|
---|
216 | # define _strtoull RT_NOCRT(strtoull)
|
---|
217 | # define _atoi RT_NOCRT(atoi)
|
---|
218 | # define _strtod RT_NOCRT(strtod)
|
---|
219 | # define _atof RT_NOCRT(atof)
|
---|
220 | # endif
|
---|
221 |
|
---|
222 | #endif /* IPRT_NO_CRT_FOR_3RD_PARTY */
|
---|
223 |
|
---|
224 |
|
---|
225 | RT_C_DECLS_END
|
---|
226 |
|
---|
227 | #endif /* !IPRT_INCLUDED_nocrt_stdlib_h */
|
---|