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