xref: /llvm-project/libcxx/include/stdlib.h (revision 902e62cf15229a4ef32d12cd4ee083e3a58c447b)
1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
10 /*
11     stdlib.h synopsis
12 
13 Macros:
14 
15     EXIT_FAILURE
16     EXIT_SUCCESS
17     MB_CUR_MAX
18     NULL
19     RAND_MAX
20 
21 Types:
22 
23     size_t
24     div_t
25     ldiv_t
26     lldiv_t                                                               // C99
27 
28 double    atof (const char* nptr);
29 int       atoi (const char* nptr);
30 long      atol (const char* nptr);
31 long long atoll(const char* nptr);                                        // C99
32 double             strtod  (const char* restrict nptr, char** restrict endptr);
33 float              strtof  (const char* restrict nptr, char** restrict endptr); // C99
34 long double        strtold (const char* restrict nptr, char** restrict endptr); // C99
35 long               strtol  (const char* restrict nptr, char** restrict endptr, int base);
36 long long          strtoll (const char* restrict nptr, char** restrict endptr, int base); // C99
37 unsigned long      strtoul (const char* restrict nptr, char** restrict endptr, int base);
38 unsigned long long strtoull(const char* restrict nptr, char** restrict endptr, int base); // C99
39 int rand(void);
40 void srand(unsigned int seed);
41 void* calloc(size_t nmemb, size_t size);
42 void free(void* ptr);
43 void* malloc(size_t size);
44 void* realloc(void* ptr, size_t size);
45 void abort(void);
46 int atexit(void (*func)(void));
47 void exit(int status);
48 void _Exit(int status);
49 char* getenv(const char* name);
50 int system(const char* string);
51 void* bsearch(const void* key, const void* base, size_t nmemb, size_t size,
52               int (*compar)(const void *, const void *));
53 void qsort(void* base, size_t nmemb, size_t size,
54            int (*compar)(const void *, const void *));
55 int         abs(      int j);
56 long        abs(     long j);
57 long long   abs(long long j);                                             // C++0X
58 long       labs(     long j);
59 long long llabs(long long j);                                             // C99
60 div_t     div(      int numer,       int denom);
61 ldiv_t    div(     long numer,      long denom);
62 lldiv_t   div(long long numer, long long denom);                          // C++0X
63 ldiv_t   ldiv(     long numer,      long denom);
64 lldiv_t lldiv(long long numer, long long denom);                          // C99
65 int mblen(const char* s, size_t n);
66 int mbtowc(wchar_t* restrict pwc, const char* restrict s, size_t n);
67 int wctomb(char* s, wchar_t wchar);
68 size_t mbstowcs(wchar_t* restrict pwcs, const char* restrict s, size_t n);
69 size_t wcstombs(char* restrict s, const wchar_t* restrict pwcs, size_t n);
70 int at_quick_exit(void (*func)(void))                                     // C++11
71 void quick_exit(int status);                                              // C++11
72 void *aligned_alloc(size_t alignment, size_t size);                       // C11
73 
74 */
75 
76 #if defined(__cplusplus) && __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
77 #  include <__cxx03/stdlib.h>
78 #else
79 #  include <__config>
80 
81 #  if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
82 #    pragma GCC system_header
83 #  endif
84 
85 // The inclusion of the system's <stdlib.h> is intentionally done once outside of any include
86 // guards because some code expects to be able to include the underlying system header multiple
87 // times to get different definitions based on the macros that are set before inclusion.
88 #  if __has_include_next(<stdlib.h>)
89 #    include_next <stdlib.h>
90 #  endif
91 
92 #  if !defined(_LIBCPP_STDLIB_H)
93 #    define _LIBCPP_STDLIB_H
94 
95 #    ifdef __cplusplus
96 extern "C++" {
97 // abs
98 
99 #      ifdef abs
100 #        undef abs
101 #      endif
102 #      ifdef labs
103 #        undef labs
104 #      endif
105 #      ifdef llabs
106 #        undef llabs
107 #      endif
108 
109 // MSVCRT already has the correct prototype in <stdlib.h> if __cplusplus is defined
110 #      if !defined(_LIBCPP_MSVCRT)
111 [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI long abs(long __x) _NOEXCEPT { return __builtin_labs(__x); }
112 [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI long long abs(long long __x) _NOEXCEPT { return __builtin_llabs(__x); }
113 #      endif // !defined(_LIBCPP_MSVCRT)
114 
115 [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI float abs(float __lcpp_x) _NOEXCEPT {
116   return __builtin_fabsf(__lcpp_x); // Use builtins to prevent needing math.h
117 }
118 
119 [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI double abs(double __lcpp_x) _NOEXCEPT {
120   return __builtin_fabs(__lcpp_x);
121 }
122 
123 [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI long double abs(long double __lcpp_x) _NOEXCEPT {
124   return __builtin_fabsl(__lcpp_x);
125 }
126 
127 // div
128 
129 #      ifdef div
130 #        undef div
131 #      endif
132 #      ifdef ldiv
133 #        undef ldiv
134 #      endif
135 #      ifdef lldiv
136 #        undef lldiv
137 #      endif
138 
139 // MSVCRT already has the correct prototype in <stdlib.h> if __cplusplus is defined
140 #      if !defined(_LIBCPP_MSVCRT)
141 inline _LIBCPP_HIDE_FROM_ABI ldiv_t div(long __x, long __y) _NOEXCEPT { return ::ldiv(__x, __y); }
142 #        if !(defined(__FreeBSD__) && !defined(__LONG_LONG_SUPPORTED))
143 inline _LIBCPP_HIDE_FROM_ABI lldiv_t div(long long __x, long long __y) _NOEXCEPT { return ::lldiv(__x, __y); }
144 #        endif
145 #      endif // _LIBCPP_MSVCRT
146 } // extern "C++"
147 #    endif   // __cplusplus
148 #  endif     // _LIBCPP_STDLIB_H
149 
150 #endif // defined(__cplusplus) && __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
151