VirtualBox

source: vbox/trunk/include/iprt/nocrt/stdlib.h@ 96074

Last change on this file since 96074 was 96074, checked in by vboxsync, 3 years ago

IPRT/nocrt: abs, labs and llabs. bugref:10261

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 7.5 KB
Line 
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 <iprt/nocrt/limits.h>
36
37RT_C_DECLS_BEGIN
38
39#define EXIT_SUCCESS RTEXITCODE_SUCCESS
40#define EXIT_FAILURE RTEXITCODE_FAILURE
41
42
43typedef void FNRTNOCRTATEXITCALLBACK(void) /*RT_NOEXCEPT*/;
44typedef FNRTNOCRTATEXITCALLBACK *PFNRTNOCRTATEXITCALLBACK;
45#if defined(_MSC_VER) && defined(RT_WITHOUT_NOCRT_WRAPPERS) /* Clashes with compiler internal prototype or smth. */
46int nocrt_atexit(PFNRTNOCRTATEXITCALLBACK) RT_NOEXCEPT;
47# define atexit nocrt_atexit
48#else
49int 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
62DECLINLINE(void *) RT_NOCRT(malloc)(size_t cb)
63{
64 return RTMemAlloc(cb);
65}
66
67DECLINLINE(void *) RT_NOCRT(calloc)(size_t cItems, size_t cbItem)
68{
69 return RTMemAllocZ(cItems * cbItem); /* caller responsible for overflow issues. */
70}
71
72DECLINLINE(void *) RT_NOCRT(realloc)(void *pvOld, size_t cbNew)
73{
74 return RTMemRealloc(pvOld, cbNew);
75}
76
77DECLINLINE(void) RT_NOCRT(free)(void *pv)
78{
79 RTMemFree(pv);
80}
81
82DECLINLINE(const char *) RT_NOCRT(getenv)(const char *pszVar)
83{
84 return RTEnvGet(pszVar);
85}
86
87int RT_NOCRT(abs)(int);
88long RT_NOCRT(labs)(long);
89long long RT_NOCRT(llabs)(long long);
90int RT_NOCRT(rand)(void);
91void RT_NOCRT(srand)(unsigned);
92long RT_NOCRT(strtol)(const char *psz, char **ppszNext, int iBase);
93long long RT_NOCRT(strtoll)(const char *psz, char **ppszNext, int iBase);
94unsigned long RT_NOCRT(strtoul)(const char *psz, char **ppszNext, int iBase);
95unsigned long long RT_NOCRT(strtoull)(const char *psz, char **ppszNext, int iBase);
96int RT_NOCRT(atoi)(const char *psz);
97double RT_NOCRT(strtod)(const char *psz, char **ppszNext);
98double RT_NOCRT(atof)(const char *psz);
99void *RT_NOCRT(bsearch)(const void *pvKey, const void *pvBase, size_t cEntries, size_t cbEntry,
100 int (*pfnCompare)(const void *pv1, const void *pv2));
101void RT_NOCRT(qsort)(void *pvBase, size_t cEntries, size_t cbEntry,
102 int (*pfnCompare)(const void *pv1, const void *pv2));
103void 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. */
107DECL_NO_RETURN(DECLINLINE(void)) RT_NOCRT(exit)(int iExitCode) { AssertMsgFailed(("exit: iExitCode=%d\n", iExitCode)); }
108DECL_NO_RETURN(DECLINLINE(void)) RT_NOCRT(abort)(void) { AssertMsgFailed(("abort\n")); }
109
110/*
111 * Underscored versions:
112 */
113DECLINLINE(void *) RT_NOCRT(_malloc)(size_t cb)
114{
115 return RTMemAlloc(cb);
116}
117
118DECLINLINE(void *) RT_NOCRT(_calloc)(size_t cItems, size_t cbItem)
119{
120 return RTMemAllocZ(cItems * cbItem); /* caller responsible for overflow issues. */
121}
122
123DECLINLINE(void *) RT_NOCRT(_realloc)(void *pvOld, size_t cbNew)
124{
125 return RTMemRealloc(pvOld, cbNew);
126}
127
128DECLINLINE(void) RT_NOCRT(_free)(void *pv)
129{
130 RTMemFree(pv);
131}
132
133DECLINLINE(const char *) RT_NOCRT(_getenv)(const char *pszVar)
134{
135 return RTEnvGet(pszVar);
136}
137
138int RT_NOCRT(_abs)(int);
139long RT_NOCRT(_labs)(long);
140long long RT_NOCRT(_llabs)(long long);
141int RT_NOCRT(_rand)(void);
142void RT_NOCRT(_srand)(unsigned);
143long RT_NOCRT(_strtol)(const char *psz, char **ppszNext, int iBase);
144long long RT_NOCRT(_strtoll)(const char *psz, char **ppszNext, int iBase);
145unsigned long RT_NOCRT(_strtoul)(const char *psz, char **ppszNext, int iBase);
146unsigned long long RT_NOCRT(_strtoull)(const char *psz, char **ppszNext, int iBase);
147int RT_NOCRT(_atoi)(const char *psz);
148double RT_NOCRT(_strtod)(const char *psz, char **ppszNext);
149double RT_NOCRT(_atof)(const char *psz);
150void *RT_NOCRT(_bsearch)(const void *pvKey, const void *pvBase, size_t cEntries, size_t cbEntry,
151 int (*pfnCompare)(const void *pv1, const void *pv2));
152void RT_NOCRT(_qsort)(void *pvBase, size_t cEntries, size_t cbEntry,
153 int (*pfnCompare)(const void *pv1, const void *pv2));
154void 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. */
158DECL_NO_RETURN(DECLINLINE(void)) RT_NOCRT(_exit)(int iExitCode) { AssertMsgFailed(("_exit: iExitCode=%d\n", iExitCode)); }
159DECL_NO_RETURN(DECLINLINE(void)) RT_NOCRT(_abort)(void) { AssertMsgFailed(("_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
207RT_C_DECLS_END
208
209#endif /* !IPRT_INCLUDED_nocrt_stdlib_h */
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette