xref: /freebsd/contrib/llvm-project/libcxx/include/stdlib.h (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
10b57cec5SDimitry Andric // -*- C++ -*-
2349cc55cSDimitry Andric //===----------------------------------------------------------------------===//
30b57cec5SDimitry Andric //
40b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
50b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
60b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
70b57cec5SDimitry Andric //
80b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
90b57cec5SDimitry Andric 
105ffd83dbSDimitry Andric #if defined(__need_malloc_and_calloc)
110b57cec5SDimitry Andric 
120b57cec5SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
130b57cec5SDimitry Andric #  pragma GCC system_header
140b57cec5SDimitry Andric #endif
150b57cec5SDimitry Andric 
160b57cec5SDimitry Andric #include_next <stdlib.h>
170b57cec5SDimitry Andric 
180b57cec5SDimitry Andric #elif !defined(_LIBCPP_STDLIB_H)
190b57cec5SDimitry Andric #define _LIBCPP_STDLIB_H
200b57cec5SDimitry Andric 
210b57cec5SDimitry Andric /*
220b57cec5SDimitry Andric     stdlib.h synopsis
230b57cec5SDimitry Andric 
240b57cec5SDimitry Andric Macros:
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric     EXIT_FAILURE
270b57cec5SDimitry Andric     EXIT_SUCCESS
280b57cec5SDimitry Andric     MB_CUR_MAX
290b57cec5SDimitry Andric     NULL
300b57cec5SDimitry Andric     RAND_MAX
310b57cec5SDimitry Andric 
320b57cec5SDimitry Andric Types:
330b57cec5SDimitry Andric 
340b57cec5SDimitry Andric     size_t
350b57cec5SDimitry Andric     div_t
360b57cec5SDimitry Andric     ldiv_t
370b57cec5SDimitry Andric     lldiv_t                                                               // C99
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric double    atof (const char* nptr);
400b57cec5SDimitry Andric int       atoi (const char* nptr);
410b57cec5SDimitry Andric long      atol (const char* nptr);
420b57cec5SDimitry Andric long long atoll(const char* nptr);                                        // C99
430b57cec5SDimitry Andric double             strtod  (const char* restrict nptr, char** restrict endptr);
440b57cec5SDimitry Andric float              strtof  (const char* restrict nptr, char** restrict endptr); // C99
450b57cec5SDimitry Andric long double        strtold (const char* restrict nptr, char** restrict endptr); // C99
460b57cec5SDimitry Andric long               strtol  (const char* restrict nptr, char** restrict endptr, int base);
470b57cec5SDimitry Andric long long          strtoll (const char* restrict nptr, char** restrict endptr, int base); // C99
480b57cec5SDimitry Andric unsigned long      strtoul (const char* restrict nptr, char** restrict endptr, int base);
490b57cec5SDimitry Andric unsigned long long strtoull(const char* restrict nptr, char** restrict endptr, int base); // C99
500b57cec5SDimitry Andric int rand(void);
510b57cec5SDimitry Andric void srand(unsigned int seed);
520b57cec5SDimitry Andric void* calloc(size_t nmemb, size_t size);
530b57cec5SDimitry Andric void free(void* ptr);
540b57cec5SDimitry Andric void* malloc(size_t size);
550b57cec5SDimitry Andric void* realloc(void* ptr, size_t size);
560b57cec5SDimitry Andric void abort(void);
570b57cec5SDimitry Andric int atexit(void (*func)(void));
580b57cec5SDimitry Andric void exit(int status);
590b57cec5SDimitry Andric void _Exit(int status);
600b57cec5SDimitry Andric char* getenv(const char* name);
610b57cec5SDimitry Andric int system(const char* string);
620b57cec5SDimitry Andric void* bsearch(const void* key, const void* base, size_t nmemb, size_t size,
630b57cec5SDimitry Andric               int (*compar)(const void *, const void *));
640b57cec5SDimitry Andric void qsort(void* base, size_t nmemb, size_t size,
650b57cec5SDimitry Andric            int (*compar)(const void *, const void *));
660b57cec5SDimitry Andric int         abs(      int j);
670b57cec5SDimitry Andric long        abs(     long j);
680b57cec5SDimitry Andric long long   abs(long long j);                                             // C++0X
690b57cec5SDimitry Andric long       labs(     long j);
700b57cec5SDimitry Andric long long llabs(long long j);                                             // C99
710b57cec5SDimitry Andric div_t     div(      int numer,       int denom);
720b57cec5SDimitry Andric ldiv_t    div(     long numer,      long denom);
730b57cec5SDimitry Andric lldiv_t   div(long long numer, long long denom);                          // C++0X
740b57cec5SDimitry Andric ldiv_t   ldiv(     long numer,      long denom);
750b57cec5SDimitry Andric lldiv_t lldiv(long long numer, long long denom);                          // C99
760b57cec5SDimitry Andric int mblen(const char* s, size_t n);
770b57cec5SDimitry Andric int mbtowc(wchar_t* restrict pwc, const char* restrict s, size_t n);
780b57cec5SDimitry Andric int wctomb(char* s, wchar_t wchar);
790b57cec5SDimitry Andric size_t mbstowcs(wchar_t* restrict pwcs, const char* restrict s, size_t n);
800b57cec5SDimitry Andric size_t wcstombs(char* restrict s, const wchar_t* restrict pwcs, size_t n);
810b57cec5SDimitry Andric int at_quick_exit(void (*func)(void))                                     // C++11
820b57cec5SDimitry Andric void quick_exit(int status);                                              // C++11
830b57cec5SDimitry Andric void *aligned_alloc(size_t alignment, size_t size);                       // C11
840b57cec5SDimitry Andric 
850b57cec5SDimitry Andric */
860b57cec5SDimitry Andric 
870b57cec5SDimitry Andric #include <__config>
880b57cec5SDimitry Andric 
890b57cec5SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
900b57cec5SDimitry Andric #  pragma GCC system_header
910b57cec5SDimitry Andric #endif
920b57cec5SDimitry Andric 
93*bdd1243dSDimitry Andric #  if __has_include_next(<stdlib.h>)
940b57cec5SDimitry Andric #    include_next <stdlib.h>
95*bdd1243dSDimitry Andric #  endif
960b57cec5SDimitry Andric 
970b57cec5SDimitry Andric #ifdef __cplusplus
985ffd83dbSDimitry Andric extern "C++" {
995ffd83dbSDimitry Andric // abs
1005ffd83dbSDimitry Andric 
101349cc55cSDimitry Andric #ifdef abs
1025ffd83dbSDimitry Andric # undef abs
103349cc55cSDimitry Andric #endif
104349cc55cSDimitry Andric #ifdef labs
1055ffd83dbSDimitry Andric # undef labs
106349cc55cSDimitry Andric #endif
107349cc55cSDimitry Andric #ifdef llabs
1085ffd83dbSDimitry Andric # undef llabs
1095ffd83dbSDimitry Andric #endif
1105ffd83dbSDimitry Andric 
1115ffd83dbSDimitry Andric // MSVCRT already has the correct prototype in <stdlib.h> if __cplusplus is defined
112fe6060f1SDimitry Andric #if !defined(_LIBCPP_MSVCRT) && !defined(__sun__)
113*bdd1243dSDimitry Andric _LIBCPP_NODISCARD_EXT inline _LIBCPP_INLINE_VISIBILITY long abs(long __x) _NOEXCEPT {
1145ffd83dbSDimitry Andric   return __builtin_labs(__x);
1155ffd83dbSDimitry Andric }
116*bdd1243dSDimitry Andric _LIBCPP_NODISCARD_EXT inline _LIBCPP_INLINE_VISIBILITY long long abs(long long __x) _NOEXCEPT {
1175ffd83dbSDimitry Andric   return __builtin_llabs(__x);
1185ffd83dbSDimitry Andric }
119fe6060f1SDimitry Andric #endif // !defined(_LIBCPP_MSVCRT) && !defined(__sun__)
1205ffd83dbSDimitry Andric 
121fe6060f1SDimitry Andric #if !defined(__sun__)
122*bdd1243dSDimitry Andric _LIBCPP_NODISCARD_EXT inline _LIBCPP_INLINE_VISIBILITY float abs(float __lcpp_x) _NOEXCEPT {
1235ffd83dbSDimitry Andric   return __builtin_fabsf(__lcpp_x); // Use builtins to prevent needing math.h
1245ffd83dbSDimitry Andric }
1255ffd83dbSDimitry Andric 
126*bdd1243dSDimitry Andric _LIBCPP_NODISCARD_EXT inline _LIBCPP_INLINE_VISIBILITY double abs(double __lcpp_x) _NOEXCEPT {
1275ffd83dbSDimitry Andric   return __builtin_fabs(__lcpp_x);
1285ffd83dbSDimitry Andric }
1295ffd83dbSDimitry Andric 
130*bdd1243dSDimitry Andric _LIBCPP_NODISCARD_EXT inline _LIBCPP_INLINE_VISIBILITY long double
1315ffd83dbSDimitry Andric abs(long double __lcpp_x) _NOEXCEPT {
1325ffd83dbSDimitry Andric   return __builtin_fabsl(__lcpp_x);
1335ffd83dbSDimitry Andric }
134fe6060f1SDimitry Andric #endif // !defined(__sun__)
1355ffd83dbSDimitry Andric 
1365ffd83dbSDimitry Andric // div
1375ffd83dbSDimitry Andric 
138349cc55cSDimitry Andric #ifdef div
1395ffd83dbSDimitry Andric # undef div
140349cc55cSDimitry Andric #endif
141349cc55cSDimitry Andric #ifdef ldiv
1425ffd83dbSDimitry Andric # undef ldiv
143349cc55cSDimitry Andric #endif
144349cc55cSDimitry Andric #ifdef lldiv
1455ffd83dbSDimitry Andric # undef lldiv
1465ffd83dbSDimitry Andric #endif
1475ffd83dbSDimitry Andric 
1485ffd83dbSDimitry Andric // MSVCRT already has the correct prototype in <stdlib.h> if __cplusplus is defined
149fe6060f1SDimitry Andric #if !defined(_LIBCPP_MSVCRT) && !defined(__sun__)
1505ffd83dbSDimitry Andric inline _LIBCPP_INLINE_VISIBILITY ldiv_t div(long __x, long __y) _NOEXCEPT {
1515ffd83dbSDimitry Andric   return ::ldiv(__x, __y);
1525ffd83dbSDimitry Andric }
153349cc55cSDimitry Andric #if !(defined(__FreeBSD__) && !defined(__LONG_LONG_SUPPORTED))
1545ffd83dbSDimitry Andric inline _LIBCPP_INLINE_VISIBILITY lldiv_t div(long long __x,
1555ffd83dbSDimitry Andric                                              long long __y) _NOEXCEPT {
1565ffd83dbSDimitry Andric   return ::lldiv(__x, __y);
1575ffd83dbSDimitry Andric }
158349cc55cSDimitry Andric #endif
159fe6060f1SDimitry Andric #endif // _LIBCPP_MSVCRT / __sun__
1605ffd83dbSDimitry Andric } // extern "C++"
1610b57cec5SDimitry Andric #endif // __cplusplus
1620b57cec5SDimitry Andric 
1630b57cec5SDimitry Andric #endif // _LIBCPP_STDLIB_H
164