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.virtualbox.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 <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 rtnocrt_atexit(PFNRTNOCRTATEXITCALLBACK) RT_NOEXCEPT;
|
---|
47 | # define atexit rtnocrt_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 | void *RT_NOCRT(bsearch)(const void *pvKey, const void *pvBase, size_t cEntries, size_t cbEntry,
|
---|
88 | int (*pfnCompare)(const void *pv1, const void *pv2));
|
---|
89 | void RT_NOCRT(qsort)(void *pvBase, size_t cEntries, size_t cbEntry,
|
---|
90 | int (*pfnCompare)(const void *pv1, const void *pv2));
|
---|
91 | void RT_NOCRT(qsort_r)(void *pvBase, size_t cEntries, size_t cbEntry,
|
---|
92 | int (*pfnCompare)(const void *pv1, const void *pv2, void *pvUser), void *pvUser);
|
---|
93 |
|
---|
94 | /* Map exit & abort onto fatal assert. */
|
---|
95 | DECL_NO_RETURN(DECLINLINE(void)) RT_NOCRT(exit)(int iExitCode) { AssertMsgFailed(("exit: iExitCode=%d\n", iExitCode)); }
|
---|
96 | DECL_NO_RETURN(DECLINLINE(void)) RT_NOCRT(abort)(void) { AssertMsgFailed(("abort\n")); }
|
---|
97 |
|
---|
98 | /*
|
---|
99 | * Underscored versions:
|
---|
100 | */
|
---|
101 | DECLINLINE(void *) RT_NOCRT(_malloc)(size_t cb)
|
---|
102 | {
|
---|
103 | return RTMemAlloc(cb);
|
---|
104 | }
|
---|
105 |
|
---|
106 | DECLINLINE(void *) RT_NOCRT(_calloc)(size_t cItems, size_t cbItem)
|
---|
107 | {
|
---|
108 | return RTMemAllocZ(cItems * cbItem); /* caller responsible for overflow issues. */
|
---|
109 | }
|
---|
110 |
|
---|
111 | DECLINLINE(void *) RT_NOCRT(_realloc)(void *pvOld, size_t cbNew)
|
---|
112 | {
|
---|
113 | return RTMemRealloc(pvOld, cbNew);
|
---|
114 | }
|
---|
115 |
|
---|
116 | DECLINLINE(void) RT_NOCRT(_free)(void *pv)
|
---|
117 | {
|
---|
118 | RTMemFree(pv);
|
---|
119 | }
|
---|
120 |
|
---|
121 | DECLINLINE(const char *) RT_NOCRT(_getenv)(const char *pszVar)
|
---|
122 | {
|
---|
123 | return RTEnvGet(pszVar);
|
---|
124 | }
|
---|
125 |
|
---|
126 | void *RT_NOCRT(_bsearch)(const void *pvKey, const void *pvBase, size_t cEntries, size_t cbEntry,
|
---|
127 | int (*pfnCompare)(const void *pv1, const void *pv2));
|
---|
128 | void RT_NOCRT(_qsort)(void *pvBase, size_t cEntries, size_t cbEntry,
|
---|
129 | int (*pfnCompare)(const void *pv1, const void *pv2));
|
---|
130 | void RT_NOCRT(_qsort_r)(void *pvBase, size_t cEntries, size_t cbEntry,
|
---|
131 | int (*pfnCompare)(const void *pv1, const void *pv2, void *pvUser), void *pvUser);
|
---|
132 |
|
---|
133 | /* Map exit & abort onto fatal assert. */
|
---|
134 | DECL_NO_RETURN(DECLINLINE(void)) RT_NOCRT(_exit)(int iExitCode) { AssertMsgFailed(("_exit: iExitCode=%d\n", iExitCode)); }
|
---|
135 | DECL_NO_RETURN(DECLINLINE(void)) RT_NOCRT(_abort)(void) { AssertMsgFailed(("_abort\n")); }
|
---|
136 |
|
---|
137 | /* Some windows CRT error control functions we totally ignore (only underscored): */
|
---|
138 | # define _set_error_mode(a_Mode) (0)
|
---|
139 | # define _set_abort_behavior(a_fFlags, a_fMask) (0)
|
---|
140 |
|
---|
141 | /*
|
---|
142 | * No-CRT aliases.
|
---|
143 | */
|
---|
144 | # if !defined(RT_WITHOUT_NOCRT_WRAPPERS) && !defined(RT_WITHOUT_NOCRT_WRAPPER_ALIASES)
|
---|
145 | # define malloc RT_NOCRT(malloc)
|
---|
146 | # define calloc RT_NOCRT(calloc)
|
---|
147 | # define realloc RT_NOCRT(realloc)
|
---|
148 | # define free RT_NOCRT(free)
|
---|
149 | # define getenv RT_NOCRT(getenv)
|
---|
150 | # define bsearch RT_NOCRT(bsearch)
|
---|
151 | # define exit RT_NOCRT(exit)
|
---|
152 | # define abort RT_NOCRT(abort)
|
---|
153 |
|
---|
154 | # define _malloc RT_NOCRT(malloc)
|
---|
155 | # define _calloc RT_NOCRT(calloc)
|
---|
156 | # define _realloc RT_NOCRT(realloc)
|
---|
157 | # define _free RT_NOCRT(free)
|
---|
158 | # define _getenv RT_NOCRT(getenv)
|
---|
159 | # define _bsearch RT_NOCRT(bsearch)
|
---|
160 | # define _exit RT_NOCRT(exit)
|
---|
161 | # define _abort RT_NOCRT(abort)
|
---|
162 | # endif
|
---|
163 |
|
---|
164 | #endif /* IPRT_NO_CRT_FOR_3RD_PARTY */
|
---|
165 |
|
---|
166 |
|
---|
167 | RT_C_DECLS_END
|
---|
168 |
|
---|
169 | #endif /* !IPRT_INCLUDED_nocrt_stdlib_h */
|
---|