blob: 64581b67f245fcf946bad023bcc4a1fa99a86d81 [file] [log] [blame]
Richard Smith5ef06d12015-10-09 01:41:45 +00001// -*- C++ -*-
Louis Dionne9bd93882021-11-17 16:25:01 -05002//===----------------------------------------------------------------------===//
Richard Smith5ef06d12015-10-09 01:41:45 +00003//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// 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
Richard Smith5ef06d12015-10-09 01:41:45 +00007//
8//===----------------------------------------------------------------------===//
9
Eric Fiselier6c9e1a72020-02-15 18:55:07 -050010#if defined(__need_malloc_and_calloc)
Richard Smith5ef06d12015-10-09 01:41:45 +000011
12#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Arthur O'Dwyer6eeaa002022-02-01 20:16:40 -050013# pragma GCC system_header
Richard Smith5ef06d12015-10-09 01:41:45 +000014#endif
15
16#include_next <stdlib.h>
17
18#elif !defined(_LIBCPP_STDLIB_H)
19#define _LIBCPP_STDLIB_H
20
21/*
22 stdlib.h synopsis
23
24Macros:
25
26 EXIT_FAILURE
27 EXIT_SUCCESS
28 MB_CUR_MAX
29 NULL
30 RAND_MAX
31
32Types:
33
34 size_t
35 div_t
36 ldiv_t
37 lldiv_t // C99
38
39double atof (const char* nptr);
40int atoi (const char* nptr);
41long atol (const char* nptr);
42long long atoll(const char* nptr); // C99
43double strtod (const char* restrict nptr, char** restrict endptr);
44float strtof (const char* restrict nptr, char** restrict endptr); // C99
45long double strtold (const char* restrict nptr, char** restrict endptr); // C99
46long strtol (const char* restrict nptr, char** restrict endptr, int base);
47long long strtoll (const char* restrict nptr, char** restrict endptr, int base); // C99
48unsigned long strtoul (const char* restrict nptr, char** restrict endptr, int base);
49unsigned long long strtoull(const char* restrict nptr, char** restrict endptr, int base); // C99
50int rand(void);
51void srand(unsigned int seed);
52void* calloc(size_t nmemb, size_t size);
53void free(void* ptr);
54void* malloc(size_t size);
55void* realloc(void* ptr, size_t size);
56void abort(void);
57int atexit(void (*func)(void));
58void exit(int status);
59void _Exit(int status);
60char* getenv(const char* name);
61int system(const char* string);
62void* bsearch(const void* key, const void* base, size_t nmemb, size_t size,
63 int (*compar)(const void *, const void *));
64void qsort(void* base, size_t nmemb, size_t size,
65 int (*compar)(const void *, const void *));
66int abs( int j);
67long abs( long j);
68long long abs(long long j); // C++0X
69long labs( long j);
70long long llabs(long long j); // C99
71div_t div( int numer, int denom);
72ldiv_t div( long numer, long denom);
73lldiv_t div(long long numer, long long denom); // C++0X
74ldiv_t ldiv( long numer, long denom);
75lldiv_t lldiv(long long numer, long long denom); // C99
76int mblen(const char* s, size_t n);
77int mbtowc(wchar_t* restrict pwc, const char* restrict s, size_t n);
78int wctomb(char* s, wchar_t wchar);
79size_t mbstowcs(wchar_t* restrict pwcs, const char* restrict s, size_t n);
80size_t wcstombs(char* restrict s, const wchar_t* restrict pwcs, size_t n);
81int at_quick_exit(void (*func)(void)) // C++11
82void quick_exit(int status); // C++11
83void *aligned_alloc(size_t alignment, size_t size); // C11
84
85*/
86
87#include <__config>
88
89#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Arthur O'Dwyer6eeaa002022-02-01 20:16:40 -050090# pragma GCC system_header
Richard Smith5ef06d12015-10-09 01:41:45 +000091#endif
92
Louis Dionnebe1aa9e2022-11-15 17:08:01 -050093# if __has_include_next(<stdlib.h>)
94# include_next <stdlib.h>
95# endif
Richard Smith5ef06d12015-10-09 01:41:45 +000096
97#ifdef __cplusplus
Eric Fiselier6c9e1a72020-02-15 18:55:07 -050098extern "C++" {
99// abs
100
Louis Dionne787b52c2021-08-24 10:30:39 -0400101#ifdef abs
102# undef abs
103#endif
104#ifdef labs
105# undef labs
106#endif
107#ifdef llabs
108# undef llabs
Eric Fiselier6c9e1a72020-02-15 18:55:07 -0500109#endif
110
111// MSVCRT already has the correct prototype in <stdlib.h> if __cplusplus is defined
jasonliudadd6962021-04-08 21:48:42 +0000112#if !defined(_LIBCPP_MSVCRT) && !defined(__sun__)
Eric Fiselier6c9e1a72020-02-15 18:55:07 -0500113inline _LIBCPP_INLINE_VISIBILITY long abs(long __x) _NOEXCEPT {
114 return __builtin_labs(__x);
115}
Eric Fiselier6c9e1a72020-02-15 18:55:07 -0500116inline _LIBCPP_INLINE_VISIBILITY long long abs(long long __x) _NOEXCEPT {
117 return __builtin_llabs(__x);
118}
jasonliudadd6962021-04-08 21:48:42 +0000119#endif // !defined(_LIBCPP_MSVCRT) && !defined(__sun__)
Eric Fiselier6c9e1a72020-02-15 18:55:07 -0500120
jasonliudadd6962021-04-08 21:48:42 +0000121#if !defined(__sun__)
Eric Fiselier6c9e1a72020-02-15 18:55:07 -0500122inline _LIBCPP_INLINE_VISIBILITY float abs(float __lcpp_x) _NOEXCEPT {
123 return __builtin_fabsf(__lcpp_x); // Use builtins to prevent needing math.h
124}
125
126inline _LIBCPP_INLINE_VISIBILITY double abs(double __lcpp_x) _NOEXCEPT {
127 return __builtin_fabs(__lcpp_x);
128}
129
130inline _LIBCPP_INLINE_VISIBILITY long double
131abs(long double __lcpp_x) _NOEXCEPT {
132 return __builtin_fabsl(__lcpp_x);
133}
jasonliudadd6962021-04-08 21:48:42 +0000134#endif // !defined(__sun__)
Eric Fiselier6c9e1a72020-02-15 18:55:07 -0500135
136// div
137
Louis Dionne787b52c2021-08-24 10:30:39 -0400138#ifdef div
139# undef div
140#endif
141#ifdef ldiv
142# undef ldiv
143#endif
144#ifdef lldiv
145# undef lldiv
Eric Fiselier6c9e1a72020-02-15 18:55:07 -0500146#endif
147
148// MSVCRT already has the correct prototype in <stdlib.h> if __cplusplus is defined
jasonliudadd6962021-04-08 21:48:42 +0000149#if !defined(_LIBCPP_MSVCRT) && !defined(__sun__)
Eric Fiselier6c9e1a72020-02-15 18:55:07 -0500150inline _LIBCPP_INLINE_VISIBILITY ldiv_t div(long __x, long __y) _NOEXCEPT {
151 return ::ldiv(__x, __y);
152}
Louis Dionne787b52c2021-08-24 10:30:39 -0400153#if !(defined(__FreeBSD__) && !defined(__LONG_LONG_SUPPORTED))
Eric Fiselier6c9e1a72020-02-15 18:55:07 -0500154inline _LIBCPP_INLINE_VISIBILITY lldiv_t div(long long __x,
155 long long __y) _NOEXCEPT {
156 return ::lldiv(__x, __y);
157}
Louis Dionne787b52c2021-08-24 10:30:39 -0400158#endif
jasonliudadd6962021-04-08 21:48:42 +0000159#endif // _LIBCPP_MSVCRT / __sun__
Eric Fiselier6c9e1a72020-02-15 18:55:07 -0500160} // extern "C++"
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400161#endif // __cplusplus
Richard Smith5ef06d12015-10-09 01:41:45 +0000162
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400163#endif // _LIBCPP_STDLIB_H